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)
649 scno_good = 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)
759 unsigned long u_error;
760 const char *u_error_str;
762 /* Measure the exit time as early as possible to avoid errors. */
764 gettimeofday(&tv, NULL);
767 if (stack_trace_enabled) {
768 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
769 unwind_cache_invalidate(tcp);
773 #if SUPPORTED_PERSONALITIES > 1
774 update_personality(tcp, tcp->currpers);
776 res = (get_regs_error ? -1 : get_syscall_result(tcp));
777 if (filtered(tcp) || hide_log(tcp))
780 if (syserror(tcp) && syscall_tampered(tcp))
781 tamper_with_syscall_exiting(tcp);
784 count_syscall(tcp, &tv);
785 if (cflag == CFLAG_ONLY_STATS) {
790 /* If not in -ff mode, and printing_tcp != tcp,
791 * then the log currently does not end with output
792 * of _our syscall entry_, but with something else.
793 * We need to say which syscall's return is this.
795 * Forced reprinting via TCB_REPRINT is used only by
796 * "strace -ff -oLOG test/threaded_execve" corner case.
797 * It's the only case when -ff mode needs reprinting.
799 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
800 tcp->flags &= ~TCB_REPRINT;
802 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
806 tcp->s_prev_ent = NULL;
808 /* There was error in one of prior ptrace ops */
811 tprints("= ? <unavailable>\n");
813 tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED);
814 tcp->sys_func_rval = 0;
815 free_tcb_priv_data(tcp);
818 tcp->s_prev_ent = tcp->s_ent;
821 if (tcp->qual_flg & QUAL_RAW) {
822 /* sys_res = printargs(tcp); - but it's nop on sysexit */
824 /* FIXME: not_failing_only (IOW, option -z) is broken:
825 * failure of syscall is known only after syscall return.
826 * Thus we end up with something like this on, say, ENOENT:
827 * open("doesnt_exist", O_RDONLY <unfinished ...>
828 * {next syscall decode}
829 * whereas the intended result is that open(...) line
830 * is not shown at all.
832 if (not_failing_only && tcp->u_error)
833 goto ret; /* ignore failed syscalls */
834 if (tcp->sys_func_rval & RVAL_DECODED)
835 sys_res = tcp->sys_func_rval;
837 sys_res = tcp->s_ent->sys_func(tcp);
842 u_error = tcp->u_error;
844 if (tcp->qual_flg & QUAL_RAW) {
846 tprintf("= -1 (errno %lu)", u_error);
848 tprintf("= %#" PRI_klx, tcp->u_rval);
850 if (syscall_tampered(tcp))
851 tprints(" (INJECTED)");
853 else if (!(sys_res & RVAL_NONE) && u_error) {
855 /* Blocked signals do not interrupt any syscalls.
856 * In this case syscalls don't return ERESTARTfoo codes.
858 * Deadly signals set to SIG_DFL interrupt syscalls
859 * and kill the process regardless of which of the codes below
860 * is returned by the interrupted syscall.
861 * In some cases, kernel forces a kernel-generated deadly
862 * signal to be unblocked and set to SIG_DFL (and thus cause
863 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
864 * or SIGILL. (The alternative is to leave process spinning
865 * forever on the faulty instruction - not useful).
867 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
868 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
869 * but kernel will always restart them.
872 /* Most common type of signal-interrupted syscall exit code.
873 * The system call will be restarted with the same arguments
874 * if SA_RESTART is set; otherwise, it will fail with EINTR.
876 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
879 /* Rare. For example, fork() returns this if interrupted.
880 * SA_RESTART is ignored (assumed set): the restart is unconditional.
882 tprints("= ? ERESTARTNOINTR (To be restarted)");
885 /* pause(), rt_sigsuspend() etc use this code.
886 * SA_RESTART is ignored (assumed not set):
887 * syscall won't restart (will return EINTR instead)
888 * even after signal with SA_RESTART set. However,
889 * after SIG_IGN or SIG_DFL signal it will restart
890 * (thus the name "restart only if has no handler").
892 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
894 case ERESTART_RESTARTBLOCK:
895 /* Syscalls like nanosleep(), poll() which can't be
896 * restarted with their original arguments use this
897 * code. Kernel will execute restart_syscall() instead,
898 * which changes arguments before restarting syscall.
899 * SA_RESTART is ignored (assumed not set) similarly
900 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
901 * since restart data is saved in "restart block"
902 * in task struct, and if signal handler uses a syscall
903 * which in turn saves another such restart block,
904 * old data is lost and restart becomes impossible)
906 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
909 u_error_str = err_name(u_error);
911 tprintf("= -1 %s (%s)",
912 u_error_str, strerror(u_error));
914 tprintf("= -1 %lu (%s)",
915 u_error, strerror(u_error));
918 if (syscall_tampered(tcp))
919 tprints(" (INJECTED)");
920 if ((sys_res & RVAL_STR) && tcp->auxstr)
921 tprintf(" (%s)", tcp->auxstr);
924 if (sys_res & RVAL_NONE)
927 switch (sys_res & RVAL_MASK) {
929 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
930 if (current_wordsize < sizeof(tcp->u_rval)) {
932 (unsigned int) tcp->u_rval);
936 tprintf("= %#" PRI_klx, tcp->u_rval);
941 print_numeric_long_umask(tcp->u_rval);
944 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
945 if (current_wordsize < sizeof(tcp->u_rval)) {
947 (unsigned int) tcp->u_rval);
951 tprintf("= %" PRI_klu, tcp->u_rval);
955 tprintf("= %" PRI_kld, tcp->u_rval);
960 printfd(tcp, tcp->u_rval);
963 tprintf("= %" PRI_kld, tcp->u_rval);
966 error_msg("invalid rval format");
970 if ((sys_res & RVAL_STR) && tcp->auxstr)
971 tprintf(" (%s)", tcp->auxstr);
972 if (syscall_tampered(tcp))
973 tprints(" (INJECTED)");
976 tv_sub(&tv, &tv, &tcp->etime);
977 tprintf(" <%ld.%06ld>",
978 (long) tv.tv_sec, (long) tv.tv_usec);
985 if (stack_trace_enabled)
986 unwind_print_stacktrace(tcp);
990 tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED);
991 tcp->sys_func_rval = 0;
992 free_tcb_priv_data(tcp);
997 trace_syscall(struct tcb *tcp, unsigned int *signo)
999 return exiting(tcp) ?
1000 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp, signo);
1004 is_erestart(struct tcb *tcp)
1006 switch (tcp->u_error) {
1008 case ERESTARTNOINTR:
1009 case ERESTARTNOHAND:
1010 case ERESTART_RESTARTBLOCK:
1017 static unsigned long saved_u_error;
1020 temporarily_clear_syserror(struct tcb *tcp)
1022 saved_u_error = tcp->u_error;
1027 restore_cleared_syserror(struct tcb *tcp)
1029 tcp->u_error = saved_u_error;
1033 * Check the syscall return value register value for whether it is
1034 * a negated errno code indicating an error, or a success return value.
1037 is_negated_errno(kernel_ulong_t val)
1039 /* Linux kernel defines MAX_ERRNO to 4095. */
1040 kernel_ulong_t max = -(kernel_long_t) 4095;
1042 #ifndef current_klongsize
1043 if (current_klongsize < sizeof(val)) {
1044 val = (uint32_t) val;
1045 max = (uint32_t) max;
1047 #endif /* !current_klongsize */
1052 #include "arch_regs.c"
1054 #ifdef HAVE_GETRVAL2
1055 # include "arch_getrval2.c"
1059 print_pc(struct tcb *tcp)
1061 #if defined ARCH_PC_REG
1062 # define ARCH_GET_PC 0
1063 #elif defined ARCH_PC_PEEK_ADDR
1065 # define ARCH_PC_REG pc
1066 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
1068 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1070 if (get_regs_error || ARCH_GET_PC)
1071 tprints(current_wordsize == 4 ? "[????????] "
1072 : "[????????????????] ");
1074 tprintf(current_wordsize == 4
1075 ? "[%08" PRI_klx "] " : "[%016" PRI_klx "] ",
1076 (kernel_ulong_t) ARCH_PC_REG);
1079 #include "getregs_old.h"
1081 #undef ptrace_getregset_or_getregs
1082 #undef ptrace_setregset_or_setregs
1083 #ifdef ARCH_REGS_FOR_GETREGSET
1085 # define ptrace_getregset_or_getregs ptrace_getregset
1087 ptrace_getregset(pid_t pid)
1089 # ifdef ARCH_IOVEC_FOR_GETREGSET
1090 /* variable iovec */
1091 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1092 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1093 &ARCH_IOVEC_FOR_GETREGSET);
1095 /* constant iovec */
1096 static struct iovec io = {
1097 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1098 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1100 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1105 # ifndef HAVE_GETREGS_OLD
1106 # define ptrace_setregset_or_setregs ptrace_setregset
1108 ptrace_setregset(pid_t pid)
1110 # ifdef ARCH_IOVEC_FOR_GETREGSET
1111 /* variable iovec */
1112 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1113 &ARCH_IOVEC_FOR_GETREGSET);
1115 /* constant iovec */
1116 static struct iovec io = {
1117 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1118 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1120 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1123 # endif /* !HAVE_GETREGS_OLD */
1125 #elif defined ARCH_REGS_FOR_GETREGS
1127 # define ptrace_getregset_or_getregs ptrace_getregs
1129 ptrace_getregs(pid_t pid)
1131 # if defined SPARC || defined SPARC64
1132 /* SPARC systems have the meaning of data and addr reversed */
1133 return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1135 return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1139 # ifndef HAVE_GETREGS_OLD
1140 # define ptrace_setregset_or_setregs ptrace_setregs
1142 ptrace_setregs(pid_t pid)
1144 # if defined SPARC || defined SPARC64
1145 /* SPARC systems have the meaning of data and addr reversed */
1146 return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1148 return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1151 # endif /* !HAVE_GETREGS_OLD */
1153 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1158 #undef USE_GET_SYSCALL_RESULT_REGS
1159 #ifdef ptrace_getregset_or_getregs
1161 # ifdef HAVE_GETREGS_OLD
1163 * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1164 * fallback to getregs_old.
1166 static int use_getregs_old;
1167 if (use_getregs_old < 0) {
1168 get_regs_error = ptrace_getregset_or_getregs(pid);
1170 } else if (use_getregs_old == 0) {
1171 get_regs_error = ptrace_getregset_or_getregs(pid);
1172 if (get_regs_error >= 0) {
1173 use_getregs_old = -1;
1176 if (errno == EPERM || errno == ESRCH)
1178 use_getregs_old = 1;
1180 get_regs_error = getregs_old(pid);
1181 # else /* !HAVE_GETREGS_OLD */
1182 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1183 get_regs_error = ptrace_getregset_or_getregs(pid);
1184 # endif /* !HAVE_GETREGS_OLD */
1186 #else /* !ptrace_getregset_or_getregs */
1188 # define USE_GET_SYSCALL_RESULT_REGS 1
1189 # warning get_regs is not implemented for this architecture yet
1192 #endif /* !ptrace_getregset_or_getregs */
1195 #ifdef ptrace_setregset_or_setregs
1199 return ptrace_setregset_or_setregs(pid);
1201 #endif /* ptrace_setregset_or_setregs */
1206 char buf[sizeof("syscall_%lu") + sizeof(kernel_ulong_t) * 3];
1210 free_sysent_buf(void *ptr)
1212 struct sysent_buf *s = ptr;
1213 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1219 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1220 * 1: ok, continue in trace_syscall_entering().
1221 * other: error, trace_syscall_entering() should print error indicator
1222 * ("????" etc) and bail out.
1225 get_scno(struct tcb *tcp)
1230 int rc = arch_get_scno(tcp);
1234 if (scno_is_valid(tcp->scno)) {
1235 tcp->s_ent = &sysent[tcp->scno];
1236 tcp->qual_flg = qual_flags(tcp->scno);
1238 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1241 s->ent.nargs = MAX_ARGS;
1242 s->ent.sen = SEN_printargs;
1243 s->ent.sys_func = printargs;
1244 s->ent.sys_name = s->buf;
1245 sprintf(s->buf, "syscall_%" PRI_klu, shuffle_scno(tcp->scno));
1247 tcp->s_ent = &s->ent;
1248 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1250 set_tcb_priv_data(tcp, s, free_sysent_buf);
1253 error_msg("pid %d invalid syscall %" PRI_kld,
1254 tcp->pid, tcp->scno);
1259 #ifdef USE_GET_SYSCALL_RESULT_REGS
1260 static int get_syscall_result_regs(struct tcb *);
1264 * 1: ok, continue in trace_syscall_exiting().
1265 * -1: error, trace_syscall_exiting() should print error indicator
1266 * ("????" etc) and bail out.
1269 get_syscall_result(struct tcb *tcp)
1271 #ifdef USE_GET_SYSCALL_RESULT_REGS
1272 if (get_syscall_result_regs(tcp))
1276 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1281 #include "get_scno.c"
1282 #include "set_scno.c"
1283 #include "get_syscall_args.c"
1284 #ifdef USE_GET_SYSCALL_RESULT_REGS
1285 # include "get_syscall_result.c"
1287 #include "get_error.c"
1288 #include "set_error.c"
1289 #ifdef HAVE_GETREGS_OLD
1290 # include "getregs_old.c"
1294 syscall_name(kernel_ulong_t scno)
1296 #if defined X32_PERSONALITY_NUMBER && defined __X32_SYSCALL_BIT
1297 if (current_personality == X32_PERSONALITY_NUMBER)
1298 scno &= ~__X32_SYSCALL_BIT;
1300 return scno_is_valid(scno) ? sysent[scno].sys_name: NULL;