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"
37 #include <sys/param.h>
39 /* for struct iovec */
42 /* for __X32_SYSCALL_BIT */
43 #include <asm/unistd.h>
49 # undef PTRACE_GETREGS
50 # define PTRACE_GETREGS PTRACE_GETREGS64
51 # undef PTRACE_SETREGS
52 # define PTRACE_SETREGS PTRACE_SETREGS64
56 # include <asm/psrcompat.h>
66 # define NT_PRSTATUS 1
71 /* Define these shorthand notations to simplify the syscallent files. */
75 #define TN TRACE_NETWORK
76 #define TP TRACE_PROCESS
77 #define TS TRACE_SIGNAL
78 #define TM TRACE_MEMORY
79 #define TSC TRACE_SCHED
80 #define TCL TRACE_CLOCK
81 #define TSF TRACE_STATFS
82 #define NF SYSCALL_NEVER_FAILS
84 #define SI STACKTRACE_INVALIDATE_CACHE
85 #define SE STACKTRACE_CAPTURE_ON_ENTER
86 #define CST COMPAT_SYSCALL_TYPES
88 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
90 const struct_sysent sysent0[] = {
91 #include "syscallent.h"
94 #if SUPPORTED_PERSONALITIES > 1
95 # include PERSONALITY1_INCLUDE_FUNCS
96 static const struct_sysent sysent1[] = {
97 # include "syscallent1.h"
101 #if SUPPORTED_PERSONALITIES > 2
102 # include PERSONALITY2_INCLUDE_FUNCS
103 static const struct_sysent sysent2[] = {
104 # include "syscallent2.h"
108 /* Now undef them since short defines cause wicked namespace pollution. */
127 * `ioctlent[012].h' files are automatically generated by the auxiliary
128 * program `ioctlsort', such that the list is sorted by the `code' field.
129 * This has the side-effect of resolving the _IO.. macros into
130 * plain integers, eliminating the need to include here everything
134 const char *const errnoent0[] = {
135 #include "errnoent.h"
137 const char *const signalent0[] = {
138 #include "signalent.h"
140 const struct_ioctlent ioctlent0[] = {
141 #include "ioctlent0.h"
144 #if SUPPORTED_PERSONALITIES > 1
145 static const char *const errnoent1[] = {
146 # include "errnoent1.h"
148 static const char *const signalent1[] = {
149 # include "signalent1.h"
151 static const struct_ioctlent ioctlent1[] = {
152 # include "ioctlent1.h"
154 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
155 static const struct_printers printers0 = {
156 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
158 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
159 static const struct_printers printers1 = {
160 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
164 #if SUPPORTED_PERSONALITIES > 2
165 static const char *const errnoent2[] = {
166 # include "errnoent2.h"
168 static const char *const signalent2[] = {
169 # include "signalent2.h"
171 static const struct_ioctlent ioctlent2[] = {
172 # include "ioctlent2.h"
174 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
175 static const struct_printers printers2 = {
176 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
181 nsyscalls0 = ARRAY_SIZE(sysent0)
182 #if SUPPORTED_PERSONALITIES > 1
183 , nsyscalls1 = ARRAY_SIZE(sysent1)
184 # if SUPPORTED_PERSONALITIES > 2
185 , nsyscalls2 = ARRAY_SIZE(sysent2)
191 nerrnos0 = ARRAY_SIZE(errnoent0)
192 #if SUPPORTED_PERSONALITIES > 1
193 , nerrnos1 = ARRAY_SIZE(errnoent1)
194 # if SUPPORTED_PERSONALITIES > 2
195 , nerrnos2 = ARRAY_SIZE(errnoent2)
201 nsignals0 = ARRAY_SIZE(signalent0)
202 #if SUPPORTED_PERSONALITIES > 1
203 , nsignals1 = ARRAY_SIZE(signalent1)
204 # if SUPPORTED_PERSONALITIES > 2
205 , nsignals2 = ARRAY_SIZE(signalent2)
211 nioctlents0 = ARRAY_SIZE(ioctlent0)
212 #if SUPPORTED_PERSONALITIES > 1
213 , nioctlents1 = ARRAY_SIZE(ioctlent1)
214 # if SUPPORTED_PERSONALITIES > 2
215 , nioctlents2 = ARRAY_SIZE(ioctlent2)
220 #if SUPPORTED_PERSONALITIES > 1
221 const struct_sysent *sysent = sysent0;
222 const char *const *errnoent = errnoent0;
223 const char *const *signalent = signalent0;
224 const struct_ioctlent *ioctlent = ioctlent0;
225 const struct_printers *printers = &printers0;
228 unsigned nsyscalls = nsyscalls0;
229 unsigned nerrnos = nerrnos0;
230 unsigned nsignals = nsignals0;
231 unsigned nioctlents = nioctlents0;
233 const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES] = {
235 #if SUPPORTED_PERSONALITIES > 1
238 #if SUPPORTED_PERSONALITIES > 2
242 const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
244 #if SUPPORTED_PERSONALITIES > 1
247 #if SUPPORTED_PERSONALITIES > 2
252 #if SUPPORTED_PERSONALITIES > 1
253 unsigned current_personality;
255 # ifndef current_wordsize
256 unsigned current_wordsize;
257 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
258 PERSONALITY0_WORDSIZE,
259 PERSONALITY1_WORDSIZE,
260 # if SUPPORTED_PERSONALITIES > 2
261 PERSONALITY2_WORDSIZE,
266 # ifndef current_klongsize
267 unsigned current_klongsize;
268 static const int personality_klongsize[SUPPORTED_PERSONALITIES] = {
269 PERSONALITY0_KLONGSIZE,
270 PERSONALITY1_KLONGSIZE,
271 # if SUPPORTED_PERSONALITIES > 2
272 PERSONALITY2_KLONGSIZE,
278 set_personality(int personality)
280 nsyscalls = nsyscall_vec[personality];
281 sysent = sysent_vec[personality];
283 switch (personality) {
285 errnoent = errnoent0;
287 ioctlent = ioctlent0;
288 nioctlents = nioctlents0;
289 signalent = signalent0;
290 nsignals = nsignals0;
291 printers = &printers0;
295 errnoent = errnoent1;
297 ioctlent = ioctlent1;
298 nioctlents = nioctlents1;
299 signalent = signalent1;
300 nsignals = nsignals1;
301 printers = &printers1;
304 # if SUPPORTED_PERSONALITIES > 2
306 errnoent = errnoent2;
308 ioctlent = ioctlent2;
309 nioctlents = nioctlents2;
310 signalent = signalent2;
311 nsignals = nsignals2;
312 printers = &printers2;
317 current_personality = personality;
318 # ifndef current_wordsize
319 current_wordsize = personality_wordsize[personality];
321 # ifndef current_klongsize
322 current_klongsize = personality_klongsize[personality];
327 update_personality(struct tcb *tcp, unsigned int personality)
329 if (personality == current_personality)
331 set_personality(personality);
333 if (personality == tcp->currpers)
335 tcp->currpers = personality;
337 # undef PERSONALITY_NAMES
338 # if defined POWERPC64
339 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
340 # elif defined X86_64
341 # define PERSONALITY_NAMES {"64 bit", "32 bit", "x32"}
343 # define PERSONALITY_NAMES {"x32", "32 bit"}
344 # elif defined AARCH64
345 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
347 # define PERSONALITY_NAMES {"64-bit", "32-bit"}
349 # ifdef PERSONALITY_NAMES
351 static const char *const names[] = PERSONALITY_NAMES;
352 error_msg("[ Process PID=%d runs in %s mode. ]",
353 tcp->pid, names[personality]);
359 #ifdef SYS_socket_subcall
361 decode_socket_subcall(struct tcb *tcp)
363 const int call = tcp->u_arg[0];
365 if (call < 1 || call >= SYS_socket_nsubcalls)
368 const kernel_ulong_t scno = SYS_socket_subcall + call;
369 const unsigned int nargs = sysent[scno].nargs;
372 if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
376 tcp->qual_flg = qual_flags(scno);
377 tcp->s_ent = &sysent[scno];
380 for (i = 0; i < nargs; ++i)
381 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
382 ? ((uint32_t *) (void *) buf)[i] : buf[i];
386 #ifdef SYS_ipc_subcall
388 decode_ipc_subcall(struct tcb *tcp)
390 unsigned int call = tcp->u_arg[0];
391 const unsigned int version = call >> 16;
394 # if defined S390 || defined S390X
398 if (current_wordsize == 8)
401 set_tcb_priv_ulong(tcp, version);
407 case 1: case 2: case 3: case 4:
408 case 11: case 12: case 13: case 14:
409 case 21: case 22: case 23: case 24:
415 tcp->scno = SYS_ipc_subcall + call;
416 tcp->qual_flg = qual_flags(tcp->scno);
417 tcp->s_ent = &sysent[tcp->scno];
419 const unsigned int n = tcp->s_ent->nargs;
421 for (i = 0; i < n; i++)
422 tcp->u_arg[i] = tcp->u_arg[i + 1];
428 decode_mips_subcall(struct tcb *tcp)
430 if (!scno_is_valid(tcp->u_arg[0]))
432 tcp->scno = tcp->u_arg[0];
433 tcp->qual_flg = qual_flags(tcp->scno);
434 tcp->s_ent = &sysent[tcp->scno];
435 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
436 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
438 * Fetching the last arg of 7-arg syscalls (fadvise64_64
439 * and sync_file_range) requires additional code,
440 * see linux/mips/get_syscall_args.c
442 if (tcp->s_ent->nargs == MAX_ARGS) {
444 mips_REG_SP + MAX_ARGS * sizeof(tcp->u_arg[0]),
445 sizeof(tcp->u_arg[0]),
446 &tcp->u_arg[MAX_ARGS - 1]) < 0)
447 tcp->u_arg[MAX_ARGS - 1] = 0;
450 #endif /* LINUX_MIPSO32 */
453 dumpio(struct tcb *tcp)
458 int fd = tcp->u_arg[0];
462 if (is_number_in_set(fd, &read_set)) {
463 switch (tcp->s_ent->sen) {
468 case SEN_mq_timedreceive:
469 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
474 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
478 dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
481 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
485 if (is_number_in_set(fd, &write_set)) {
486 switch (tcp->s_ent->sen) {
491 case SEN_mq_timedsend:
492 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
498 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1);
501 dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1);
504 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
511 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
512 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
514 static kernel_ulong_t
515 shuffle_scno(kernel_ulong_t scno)
517 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
518 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
521 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
522 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
524 if (scno == 0x000ffff0)
525 return ARM_FIRST_SHUFFLED_SYSCALL;
527 # define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
529 * Is it ARM specific syscall?
530 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
531 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
533 if (scno >= 0x000f0000 &&
534 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
535 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
537 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
538 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
540 #endif /* ARM || AARCH64 */
546 err_name(unsigned long err)
548 if ((err < nerrnos) && errnoent[err])
549 return errnoent[err];
554 static long get_regs_error;
562 static int get_syscall_args(struct tcb *);
563 static int get_syscall_result(struct tcb *);
564 static int arch_get_scno(struct tcb *tcp);
565 static int arch_set_scno(struct tcb *, kernel_ulong_t);
566 static void get_error(struct tcb *, const bool);
567 static int arch_set_error(struct tcb *);
568 static int arch_set_success(struct tcb *);
570 struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
572 static struct inject_opts *
573 tcb_inject_opts(struct tcb *tcp)
575 return (scno_in_range(tcp->scno) && tcp->inject_vec[current_personality])
576 ? &tcp->inject_vec[current_personality][tcp->scno] : NULL;
581 tamper_with_syscall_entering(struct tcb *tcp, unsigned int *signo)
583 if (!tcp->inject_vec[current_personality]) {
584 tcp->inject_vec[current_personality] =
585 xcalloc(nsyscalls, sizeof(**inject_vec));
586 memcpy(tcp->inject_vec[current_personality],
587 inject_vec[current_personality],
588 nsyscalls * sizeof(**inject_vec));
591 struct inject_opts *opts = tcb_inject_opts(tcp);
593 if (!opts || opts->first == 0)
598 if (opts->first != 0)
601 opts->first = opts->step;
604 *signo = opts->signo;
605 if (opts->rval != INJECT_OPTS_RVAL_DEFAULT && !arch_set_scno(tcp, -1))
606 tcp->flags |= TCB_TAMPERED;
612 tamper_with_syscall_exiting(struct tcb *tcp)
614 struct inject_opts *opts = tcb_inject_opts(tcp);
619 if (opts->rval >= 0) {
620 kernel_long_t u_rval = tcp->u_rval;
622 tcp->u_rval = opts->rval;
623 if (arch_set_success(tcp)) {
624 tcp->u_rval = u_rval;
629 unsigned long new_error = -opts->rval;
631 if (new_error != tcp->u_error && new_error <= MAX_ERRNO_VALUE) {
632 unsigned long u_error = tcp->u_error;
634 tcp->u_error = new_error;
635 if (arch_set_error(tcp)) {
636 tcp->u_error = u_error;
645 trace_syscall_entering(struct tcb *tcp, unsigned int *sig)
647 int res = get_scno(tcp);
653 res = get_syscall_args(tcp);
657 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
659 * " <unavailable>" will be added later by the code which
660 * detects ptrace errors.
666 if (SEN_syscall == tcp->s_ent->sen)
667 decode_mips_subcall(tcp);
670 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
671 switch (tcp->s_ent->sen) {
672 # ifdef SYS_socket_subcall
674 decode_socket_subcall(tcp);
677 # ifdef SYS_ipc_subcall
679 decode_ipc_subcall(tcp);
685 /* Restrain from fault injection while the trace executes strace code. */
687 tcp->qual_flg &= ~QUAL_INJECT;
690 switch (tcp->s_ent->sen) {
693 #if defined SPARC || defined SPARC64
696 tcp->flags &= ~TCB_HIDE_LOG;
700 if (!(tcp->qual_flg & QUAL_TRACE)
701 || (tracing_paths && !pathtrace_match(tcp))
703 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
704 tcp->sys_func_rval = 0;
708 tcp->flags &= ~TCB_FILTERED;
715 if (tcp->qual_flg & QUAL_INJECT)
716 tamper_with_syscall_entering(tcp, sig);
718 if (cflag == CFLAG_ONLY_STATS) {
724 if (stack_trace_enabled) {
725 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
726 unwind_capture_stacktrace(tcp);
731 tprintf("%s(", tcp->s_ent->sys_name);
732 if (tcp->qual_flg & QUAL_RAW)
733 res = printargs(tcp);
735 res = tcp->s_ent->sys_func(tcp);
739 tcp->flags |= TCB_INSYSCALL;
740 tcp->sys_func_rval = res;
741 /* Measure the entrance time as late as possible to avoid errors. */
743 gettimeofday(&tcp->etime, NULL);
748 syscall_tampered(struct tcb *tcp)
750 return tcp->flags & TCB_TAMPERED;
754 trace_syscall_exiting(struct tcb *tcp)
758 /* Measure the exit time as early as possible to avoid errors. */
760 gettimeofday(&tv, NULL);
763 if (stack_trace_enabled) {
764 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
765 unwind_cache_invalidate(tcp);
769 #if SUPPORTED_PERSONALITIES > 1
770 update_personality(tcp, tcp->currpers);
772 int res = (get_regs_error ? -1 : get_syscall_result(tcp));
773 if (filtered(tcp) || hide_log(tcp))
776 if (syserror(tcp) && syscall_tampered(tcp))
777 tamper_with_syscall_exiting(tcp);
780 count_syscall(tcp, &tv);
781 if (cflag == CFLAG_ONLY_STATS) {
786 /* If not in -ff mode, and printing_tcp != tcp,
787 * then the log currently does not end with output
788 * of _our syscall entry_, but with something else.
789 * We need to say which syscall's return is this.
791 * Forced reprinting via TCB_REPRINT is used only by
792 * "strace -ff -oLOG test/threaded_execve" corner case.
793 * It's the only case when -ff mode needs reprinting.
795 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
796 tcp->flags &= ~TCB_REPRINT;
798 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
802 tcp->s_prev_ent = NULL;
804 /* There was error in one of prior ptrace ops */
807 tprints("= ? <unavailable>\n");
809 tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED);
810 tcp->sys_func_rval = 0;
811 free_tcb_priv_data(tcp);
814 tcp->s_prev_ent = tcp->s_ent;
817 if (tcp->qual_flg & QUAL_RAW) {
818 /* sys_res = printargs(tcp); - but it's nop on sysexit */
820 /* FIXME: not_failing_only (IOW, option -z) is broken:
821 * failure of syscall is known only after syscall return.
822 * Thus we end up with something like this on, say, ENOENT:
823 * open("doesnt_exist", O_RDONLY <unfinished ...>
824 * {next syscall decode}
825 * whereas the intended result is that open(...) line
826 * is not shown at all.
828 if (not_failing_only && tcp->u_error)
829 goto ret; /* ignore failed syscalls */
830 if (tcp->sys_func_rval & RVAL_DECODED)
831 sys_res = tcp->sys_func_rval;
833 sys_res = tcp->s_ent->sys_func(tcp);
838 unsigned long u_error = tcp->u_error;
840 if (tcp->qual_flg & QUAL_RAW) {
842 tprintf("= -1 (errno %lu)", u_error);
844 tprintf("= %#" PRI_klx, tcp->u_rval);
846 if (syscall_tampered(tcp))
847 tprints(" (INJECTED)");
849 else if (!(sys_res & RVAL_NONE) && u_error) {
850 const char *u_error_str;
853 /* Blocked signals do not interrupt any syscalls.
854 * In this case syscalls don't return ERESTARTfoo codes.
856 * Deadly signals set to SIG_DFL interrupt syscalls
857 * and kill the process regardless of which of the codes below
858 * is returned by the interrupted syscall.
859 * In some cases, kernel forces a kernel-generated deadly
860 * signal to be unblocked and set to SIG_DFL (and thus cause
861 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
862 * or SIGILL. (The alternative is to leave process spinning
863 * forever on the faulty instruction - not useful).
865 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
866 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
867 * but kernel will always restart them.
870 /* Most common type of signal-interrupted syscall exit code.
871 * The system call will be restarted with the same arguments
872 * if SA_RESTART is set; otherwise, it will fail with EINTR.
874 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
877 /* Rare. For example, fork() returns this if interrupted.
878 * SA_RESTART is ignored (assumed set): the restart is unconditional.
880 tprints("= ? ERESTARTNOINTR (To be restarted)");
883 /* pause(), rt_sigsuspend() etc use this code.
884 * SA_RESTART is ignored (assumed not set):
885 * syscall won't restart (will return EINTR instead)
886 * even after signal with SA_RESTART set. However,
887 * after SIG_IGN or SIG_DFL signal it will restart
888 * (thus the name "restart only if has no handler").
890 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
892 case ERESTART_RESTARTBLOCK:
893 /* Syscalls like nanosleep(), poll() which can't be
894 * restarted with their original arguments use this
895 * code. Kernel will execute restart_syscall() instead,
896 * which changes arguments before restarting syscall.
897 * SA_RESTART is ignored (assumed not set) similarly
898 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
899 * since restart data is saved in "restart block"
900 * in task struct, and if signal handler uses a syscall
901 * which in turn saves another such restart block,
902 * old data is lost and restart becomes impossible)
904 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
907 u_error_str = err_name(u_error);
909 tprintf("= -1 %s (%s)",
910 u_error_str, strerror(u_error));
912 tprintf("= -1 %lu (%s)",
913 u_error, strerror(u_error));
916 if (syscall_tampered(tcp))
917 tprints(" (INJECTED)");
918 if ((sys_res & RVAL_STR) && tcp->auxstr)
919 tprintf(" (%s)", tcp->auxstr);
922 if (sys_res & RVAL_NONE)
925 switch (sys_res & RVAL_MASK) {
927 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
928 if (current_wordsize < sizeof(tcp->u_rval)) {
930 (unsigned int) tcp->u_rval);
934 tprintf("= %#" PRI_klx, tcp->u_rval);
939 print_numeric_long_umask(tcp->u_rval);
942 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
943 if (current_wordsize < sizeof(tcp->u_rval)) {
945 (unsigned int) tcp->u_rval);
949 tprintf("= %" PRI_klu, tcp->u_rval);
953 tprintf("= %" PRI_kld, tcp->u_rval);
958 printfd(tcp, tcp->u_rval);
961 tprintf("= %" PRI_kld, tcp->u_rval);
964 error_msg("invalid rval format");
968 if ((sys_res & RVAL_STR) && tcp->auxstr)
969 tprintf(" (%s)", tcp->auxstr);
970 if (syscall_tampered(tcp))
971 tprints(" (INJECTED)");
974 tv_sub(&tv, &tv, &tcp->etime);
975 tprintf(" <%ld.%06ld>",
976 (long) tv.tv_sec, (long) tv.tv_usec);
983 if (stack_trace_enabled)
984 unwind_print_stacktrace(tcp);
988 tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED);
989 tcp->sys_func_rval = 0;
990 free_tcb_priv_data(tcp);
995 trace_syscall(struct tcb *tcp, unsigned int *signo)
997 return exiting(tcp) ?
998 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp, signo);
1002 is_erestart(struct tcb *tcp)
1004 switch (tcp->u_error) {
1006 case ERESTARTNOINTR:
1007 case ERESTARTNOHAND:
1008 case ERESTART_RESTARTBLOCK:
1015 static unsigned long saved_u_error;
1018 temporarily_clear_syserror(struct tcb *tcp)
1020 saved_u_error = tcp->u_error;
1025 restore_cleared_syserror(struct tcb *tcp)
1027 tcp->u_error = saved_u_error;
1031 * Check the syscall return value register value for whether it is
1032 * a negated errno code indicating an error, or a success return value.
1035 is_negated_errno(kernel_ulong_t val)
1037 /* Linux kernel defines MAX_ERRNO to 4095. */
1038 kernel_ulong_t max = -(kernel_long_t) 4095;
1040 #ifndef current_klongsize
1041 if (current_klongsize < sizeof(val)) {
1042 val = (uint32_t) val;
1043 max = (uint32_t) max;
1045 #endif /* !current_klongsize */
1050 #include "arch_regs.c"
1052 #ifdef HAVE_GETRVAL2
1053 # include "arch_getrval2.c"
1057 print_pc(struct tcb *tcp)
1059 #if defined ARCH_PC_REG
1060 # define ARCH_GET_PC 0
1061 #elif defined ARCH_PC_PEEK_ADDR
1063 # define ARCH_PC_REG pc
1064 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
1066 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1068 if (get_regs_error || ARCH_GET_PC)
1069 tprints(current_wordsize == 4 ? "[????????] "
1070 : "[????????????????] ");
1072 tprintf(current_wordsize == 4
1073 ? "[%08" PRI_klx "] " : "[%016" PRI_klx "] ",
1074 (kernel_ulong_t) ARCH_PC_REG);
1077 #include "getregs_old.h"
1079 #undef ptrace_getregset_or_getregs
1080 #undef ptrace_setregset_or_setregs
1081 #ifdef ARCH_REGS_FOR_GETREGSET
1083 # define ptrace_getregset_or_getregs ptrace_getregset
1085 ptrace_getregset(pid_t pid)
1087 # ifdef ARCH_IOVEC_FOR_GETREGSET
1088 /* variable iovec */
1089 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1090 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1091 &ARCH_IOVEC_FOR_GETREGSET);
1093 /* constant iovec */
1094 static struct iovec io = {
1095 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1096 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1098 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1103 # ifndef HAVE_GETREGS_OLD
1104 # define ptrace_setregset_or_setregs ptrace_setregset
1106 ptrace_setregset(pid_t pid)
1108 # ifdef ARCH_IOVEC_FOR_GETREGSET
1109 /* variable iovec */
1110 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1111 &ARCH_IOVEC_FOR_GETREGSET);
1113 /* constant iovec */
1114 static struct iovec io = {
1115 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1116 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1118 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1121 # endif /* !HAVE_GETREGS_OLD */
1123 #elif defined ARCH_REGS_FOR_GETREGS
1125 # define ptrace_getregset_or_getregs ptrace_getregs
1127 ptrace_getregs(pid_t pid)
1129 # if defined SPARC || defined SPARC64
1130 /* SPARC systems have the meaning of data and addr reversed */
1131 return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1133 return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1137 # ifndef HAVE_GETREGS_OLD
1138 # define ptrace_setregset_or_setregs ptrace_setregs
1140 ptrace_setregs(pid_t pid)
1142 # if defined SPARC || defined SPARC64
1143 /* SPARC systems have the meaning of data and addr reversed */
1144 return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1146 return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1149 # endif /* !HAVE_GETREGS_OLD */
1151 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1156 #undef USE_GET_SYSCALL_RESULT_REGS
1157 #ifdef ptrace_getregset_or_getregs
1159 if (get_regs_error != -1)
1162 # ifdef HAVE_GETREGS_OLD
1164 * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1165 * fallback to getregs_old.
1167 static int use_getregs_old;
1168 if (use_getregs_old < 0) {
1169 get_regs_error = ptrace_getregset_or_getregs(pid);
1171 } else if (use_getregs_old == 0) {
1172 get_regs_error = ptrace_getregset_or_getregs(pid);
1173 if (get_regs_error >= 0) {
1174 use_getregs_old = -1;
1177 if (errno == EPERM || errno == ESRCH)
1179 use_getregs_old = 1;
1181 get_regs_error = getregs_old(pid);
1182 # else /* !HAVE_GETREGS_OLD */
1183 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1184 get_regs_error = ptrace_getregset_or_getregs(pid);
1185 # endif /* !HAVE_GETREGS_OLD */
1187 #else /* !ptrace_getregset_or_getregs */
1189 # define USE_GET_SYSCALL_RESULT_REGS 1
1190 # warning get_regs is not implemented for this architecture yet
1193 #endif /* !ptrace_getregset_or_getregs */
1196 #ifdef ptrace_setregset_or_setregs
1200 return ptrace_setregset_or_setregs(pid);
1202 #endif /* ptrace_setregset_or_setregs */
1207 char buf[sizeof("syscall_%lu") + sizeof(kernel_ulong_t) * 3];
1211 free_sysent_buf(void *ptr)
1213 struct sysent_buf *s = ptr;
1214 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1220 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1221 * 1: ok, continue in trace_syscall_entering().
1222 * other: error, trace_syscall_entering() should print error indicator
1223 * ("????" etc) and bail out.
1226 get_scno(struct tcb *tcp)
1231 int rc = arch_get_scno(tcp);
1235 if (scno_is_valid(tcp->scno)) {
1236 tcp->s_ent = &sysent[tcp->scno];
1237 tcp->qual_flg = qual_flags(tcp->scno);
1239 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1242 s->ent.nargs = MAX_ARGS;
1243 s->ent.sen = SEN_printargs;
1244 s->ent.sys_func = printargs;
1245 s->ent.sys_name = s->buf;
1246 sprintf(s->buf, "syscall_%" PRI_klu, shuffle_scno(tcp->scno));
1248 tcp->s_ent = &s->ent;
1249 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1251 set_tcb_priv_data(tcp, s, free_sysent_buf);
1254 error_msg("pid %d invalid syscall %" PRI_kld,
1255 tcp->pid, tcp->scno);
1260 #ifdef USE_GET_SYSCALL_RESULT_REGS
1261 static int get_syscall_result_regs(struct tcb *);
1265 * 1: ok, continue in trace_syscall_exiting().
1266 * -1: error, trace_syscall_exiting() should print error indicator
1267 * ("????" etc) and bail out.
1270 get_syscall_result(struct tcb *tcp)
1272 #ifdef USE_GET_SYSCALL_RESULT_REGS
1273 if (get_syscall_result_regs(tcp))
1277 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1282 #include "get_scno.c"
1283 #include "set_scno.c"
1284 #include "get_syscall_args.c"
1285 #ifdef USE_GET_SYSCALL_RESULT_REGS
1286 # include "get_syscall_result.c"
1288 #include "get_error.c"
1289 #include "set_error.c"
1290 #ifdef HAVE_GETREGS_OLD
1291 # include "getregs_old.c"
1295 syscall_name(kernel_ulong_t scno)
1297 #if defined X32_PERSONALITY_NUMBER && defined __X32_SYSCALL_BIT
1298 if (current_personality == X32_PERSONALITY_NUMBER)
1299 scno &= ~__X32_SYSCALL_BIT;
1301 return scno_is_valid(scno) ? sysent[scno].sys_name: NULL;