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-2018 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 "mmap_cache.h"
37 #include "native_defs.h"
40 #include "number_set.h"
44 /* for struct iovec */
47 /* for __X32_SYSCALL_BIT */
48 #include <asm/unistd.h>
53 # undef PTRACE_GETREGS
54 # define PTRACE_GETREGS PTRACE_GETREGS64
55 # undef PTRACE_SETREGS
56 # define PTRACE_SETREGS PTRACE_SETREGS64
60 # define NT_PRSTATUS 1
66 /* Define these shorthand notations to simplify the syscallent files. */
67 #include "sysent_shorthand_defs.h"
69 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
71 const struct_sysent sysent0[] = {
72 #include "syscallent.h"
75 #if SUPPORTED_PERSONALITIES > 1
76 # include PERSONALITY1_INCLUDE_FUNCS
77 static const struct_sysent sysent1[] = {
78 # include "syscallent1.h"
82 #if SUPPORTED_PERSONALITIES > 2
83 # include PERSONALITY2_INCLUDE_FUNCS
84 static const struct_sysent sysent2[] = {
85 # include "syscallent2.h"
89 /* Now undef them since short defines cause wicked namespace pollution. */
90 #include "sysent_shorthand_undefs.h"
93 * `ioctlent[012].h' files are automatically generated by the auxiliary
94 * program `ioctlsort', such that the list is sorted by the `code' field.
95 * This has the side-effect of resolving the _IO.. macros into
96 * plain integers, eliminating the need to include here everything
100 const char *const errnoent0[] = {
101 #include "errnoent.h"
103 const char *const signalent0[] = {
104 #include "signalent.h"
106 const struct_ioctlent ioctlent0[] = {
107 #include "ioctlent0.h"
110 #if SUPPORTED_PERSONALITIES > 1
111 static const char *const errnoent1[] = {
112 # include "errnoent1.h"
114 static const char *const signalent1[] = {
115 # include "signalent1.h"
117 static const struct_ioctlent ioctlent1[] = {
118 # include "ioctlent1.h"
120 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
121 static const struct_printers printers0 = {
122 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
124 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
125 static const struct_printers printers1 = {
126 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
130 #if SUPPORTED_PERSONALITIES > 2
131 static const char *const errnoent2[] = {
132 # include "errnoent2.h"
134 static const char *const signalent2[] = {
135 # include "signalent2.h"
137 static const struct_ioctlent ioctlent2[] = {
138 # include "ioctlent2.h"
140 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
141 static const struct_printers printers2 = {
142 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
147 nsyscalls0 = ARRAY_SIZE(sysent0)
148 #if SUPPORTED_PERSONALITIES > 1
149 , nsyscalls1 = ARRAY_SIZE(sysent1)
150 # if SUPPORTED_PERSONALITIES > 2
151 , nsyscalls2 = ARRAY_SIZE(sysent2)
157 nerrnos0 = ARRAY_SIZE(errnoent0)
158 #if SUPPORTED_PERSONALITIES > 1
159 , nerrnos1 = ARRAY_SIZE(errnoent1)
160 # if SUPPORTED_PERSONALITIES > 2
161 , nerrnos2 = ARRAY_SIZE(errnoent2)
167 nsignals0 = ARRAY_SIZE(signalent0)
168 #if SUPPORTED_PERSONALITIES > 1
169 , nsignals1 = ARRAY_SIZE(signalent1)
170 # if SUPPORTED_PERSONALITIES > 2
171 , nsignals2 = ARRAY_SIZE(signalent2)
177 nioctlents0 = ARRAY_SIZE(ioctlent0)
178 #if SUPPORTED_PERSONALITIES > 1
179 , nioctlents1 = ARRAY_SIZE(ioctlent1)
180 # if SUPPORTED_PERSONALITIES > 2
181 , nioctlents2 = ARRAY_SIZE(ioctlent2)
186 #if SUPPORTED_PERSONALITIES > 1
187 const struct_sysent *sysent = sysent0;
188 const char *const *errnoent = errnoent0;
189 const char *const *signalent = signalent0;
190 const struct_ioctlent *ioctlent = ioctlent0;
191 const struct_printers *printers = &printers0;
194 unsigned nsyscalls = nsyscalls0;
195 unsigned nerrnos = nerrnos0;
196 unsigned nsignals = nsignals0;
197 unsigned nioctlents = nioctlents0;
199 const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES] = {
201 #if SUPPORTED_PERSONALITIES > 1
204 #if SUPPORTED_PERSONALITIES > 2
208 const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
210 #if SUPPORTED_PERSONALITIES > 1
213 #if SUPPORTED_PERSONALITIES > 2
218 const char *const personality_names[] =
220 {"64 bit", "32 bit", "x32"}
223 # elif SUPPORTED_PERSONALITIES == 2
226 {STRINGIFY_VAL(__WORDSIZE) " bit"}
230 #if SUPPORTED_PERSONALITIES > 1
232 unsigned current_personality;
234 # ifndef current_wordsize
235 unsigned current_wordsize = PERSONALITY0_WORDSIZE;
236 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
237 PERSONALITY0_WORDSIZE,
238 PERSONALITY1_WORDSIZE,
239 # if SUPPORTED_PERSONALITIES > 2
240 PERSONALITY2_WORDSIZE,
245 # ifndef current_klongsize
246 unsigned current_klongsize = PERSONALITY0_KLONGSIZE;
247 static const int personality_klongsize[SUPPORTED_PERSONALITIES] = {
248 PERSONALITY0_KLONGSIZE,
249 PERSONALITY1_KLONGSIZE,
250 # if SUPPORTED_PERSONALITIES > 2
251 PERSONALITY2_KLONGSIZE,
257 set_personality(unsigned int personality)
259 if (personality == current_personality)
262 if (personality >= SUPPORTED_PERSONALITIES)
263 error_msg_and_die("Requested switch to unsupported personality "
266 nsyscalls = nsyscall_vec[personality];
267 sysent = sysent_vec[personality];
269 switch (personality) {
271 errnoent = errnoent0;
273 ioctlent = ioctlent0;
274 nioctlents = nioctlents0;
275 signalent = signalent0;
276 nsignals = nsignals0;
277 printers = &printers0;
281 errnoent = errnoent1;
283 ioctlent = ioctlent1;
284 nioctlents = nioctlents1;
285 signalent = signalent1;
286 nsignals = nsignals1;
287 printers = &printers1;
290 # if SUPPORTED_PERSONALITIES > 2
292 errnoent = errnoent2;
294 ioctlent = ioctlent2;
295 nioctlents = nioctlents2;
296 signalent = signalent2;
297 nsignals = nsignals2;
298 printers = &printers2;
303 current_personality = personality;
304 # ifndef current_wordsize
305 current_wordsize = personality_wordsize[personality];
307 # ifndef current_klongsize
308 current_klongsize = personality_klongsize[personality];
313 update_personality(struct tcb *tcp, unsigned int personality)
315 static bool need_mpers_warning[] =
316 { false, !HAVE_PERSONALITY_1_MPERS, !HAVE_PERSONALITY_2_MPERS };
318 set_personality(personality);
320 if (personality == tcp->currpers)
322 tcp->currpers = personality;
325 error_msg("[ Process PID=%d runs in %s mode. ]",
326 tcp->pid, personality_names[personality]);
329 if (need_mpers_warning[personality]) {
330 error_msg("WARNING: Proper structure decoding for this "
331 "personality is not supported, please consider "
332 "building strace with mpers support enabled.");
333 need_mpers_warning[personality] = false;
338 #ifdef SYS_socket_subcall
340 decode_socket_subcall(struct tcb *tcp)
342 const int call = tcp->u_arg[0];
344 if (call < 1 || call >= SYS_socket_nsubcalls)
347 const kernel_ulong_t scno = SYS_socket_subcall + call;
348 const unsigned int nargs = sysent[scno].nargs;
351 if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
355 tcp->qual_flg = qual_flags(scno);
356 tcp->s_ent = &sysent[scno];
359 for (i = 0; i < nargs; ++i)
360 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
361 ? ((uint32_t *) (void *) buf)[i] : buf[i];
363 #endif /* SYS_socket_subcall */
365 #ifdef SYS_ipc_subcall
367 decode_ipc_subcall(struct tcb *tcp)
369 unsigned int call = tcp->u_arg[0];
370 const unsigned int version = call >> 16;
373 # if defined S390 || defined S390X
377 if (current_wordsize == 8)
380 set_tcb_priv_ulong(tcp, version);
386 case 1: case 2: case 3: case 4:
387 case 11: case 12: case 13: case 14:
388 case 21: case 22: case 23: case 24:
394 tcp->scno = SYS_ipc_subcall + call;
395 tcp->qual_flg = qual_flags(tcp->scno);
396 tcp->s_ent = &sysent[tcp->scno];
398 const unsigned int n = tcp->s_ent->nargs;
400 for (i = 0; i < n; i++)
401 tcp->u_arg[i] = tcp->u_arg[i + 1];
403 #endif /* SYS_ipc_subcall */
405 #ifdef SYS_syscall_subcall
407 decode_syscall_subcall(struct tcb *tcp)
409 if (!scno_is_valid(tcp->u_arg[0]))
411 tcp->scno = tcp->u_arg[0];
412 tcp->qual_flg = qual_flags(tcp->scno);
413 tcp->s_ent = &sysent[tcp->scno];
414 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
415 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
416 # ifdef LINUX_MIPSO32
418 * Fetching the last arg of 7-arg syscalls (fadvise64_64
419 * and sync_file_range) requires additional code,
420 * see linux/mips/get_syscall_args.c
422 if (tcp->s_ent->nargs == MAX_ARGS) {
424 mips_REG_SP + MAX_ARGS * sizeof(tcp->u_arg[0]),
425 sizeof(tcp->u_arg[0]),
426 &tcp->u_arg[MAX_ARGS - 1]) < 0)
427 tcp->u_arg[MAX_ARGS - 1] = 0;
429 # endif /* LINUX_MIPSO32 */
431 #endif /* SYS_syscall_subcall */
434 dumpio(struct tcb *tcp)
439 int fd = tcp->u_arg[0];
443 if (is_number_in_set(fd, read_set)) {
444 switch (tcp->s_ent->sen) {
449 case SEN_mq_timedreceive:
450 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
455 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
459 dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
462 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
466 if (is_number_in_set(fd, write_set)) {
467 switch (tcp->s_ent->sen) {
472 case SEN_mq_timedsend:
473 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
479 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1);
482 dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1);
485 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
492 err_name(unsigned long err)
494 if ((err < nerrnos) && errnoent[err])
495 return errnoent[err];
500 static long get_regs(struct tcb *);
501 static int get_syscall_args(struct tcb *);
502 static int get_syscall_result(struct tcb *);
503 static int arch_get_scno(struct tcb *tcp);
504 static int arch_set_scno(struct tcb *, kernel_ulong_t);
505 static void get_error(struct tcb *, const bool);
506 static int arch_set_error(struct tcb *);
507 static int arch_set_success(struct tcb *);
509 struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
511 static struct inject_opts *
512 tcb_inject_opts(struct tcb *tcp)
514 return (scno_in_range(tcp->scno) && tcp->inject_vec[current_personality])
515 ? &tcp->inject_vec[current_personality][tcp->scno] : NULL;
520 tamper_with_syscall_entering(struct tcb *tcp, unsigned int *signo)
522 if (!tcp->inject_vec[current_personality]) {
523 tcp->inject_vec[current_personality] =
524 xcalloc(nsyscalls, sizeof(**inject_vec));
525 memcpy(tcp->inject_vec[current_personality],
526 inject_vec[current_personality],
527 nsyscalls * sizeof(**inject_vec));
530 struct inject_opts *opts = tcb_inject_opts(tcp);
532 if (!opts || opts->first == 0)
537 if (opts->first != 0)
540 opts->first = opts->step;
542 if (!recovering(tcp)) {
543 if (opts->data.flags & INJECT_F_SIGNAL)
544 *signo = opts->data.signo;
545 if (opts->data.flags & INJECT_F_RETVAL &&
546 !arch_set_scno(tcp, -1))
547 tcp->flags |= TCB_TAMPERED;
548 if (opts->data.flags & INJECT_F_DELAY_ENTER)
549 delay_tcb(tcp, opts->data.delay_idx, true);
550 if (opts->data.flags & INJECT_F_DELAY_EXIT)
551 tcp->flags |= TCB_INJECT_DELAY_EXIT;
558 tamper_with_syscall_exiting(struct tcb *tcp)
560 struct inject_opts *opts = tcb_inject_opts(tcp);
564 if (inject_delay_exit(tcp))
565 delay_tcb(tcp, opts->data.delay_idx, false);
567 if (!syscall_tampered(tcp))
570 if (!syserror(tcp)) {
571 error_msg("Failed to tamper with process %d: got no error "
572 "(return value %#" PRI_klx ")",
573 tcp->pid, tcp->u_rval);
578 bool update_tcb = false;
580 const kernel_long_t inject_rval = retval_get(opts->data.rval_idx);
581 if (inject_rval >= 0) {
582 kernel_long_t u_rval = tcp->u_rval;
584 tcp->u_rval = inject_rval;
585 if (arch_set_success(tcp)) {
586 tcp->u_rval = u_rval;
592 unsigned long new_error = -inject_rval;
594 if (new_error != tcp->u_error && new_error <= MAX_ERRNO_VALUE) {
595 unsigned long u_error = tcp->u_error;
597 tcp->u_error = new_error;
598 if (arch_set_error(tcp)) {
599 tcp->u_error = u_error;
608 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
616 * 0: "ignore this ptrace stop", bail out silently.
617 * 1: ok, decoded; call
618 * syscall_entering_finish(tcp, syscall_entering_trace(tcp, ...)).
619 * other: error; call syscall_entering_finish(tcp, res), where res is the value
623 syscall_entering_decode(struct tcb *tcp)
625 int res = get_scno(tcp);
629 if (res != 1 || (res = get_syscall_args(tcp)) != 1) {
631 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
633 * " <unavailable>" will be added later by the code which
634 * detects ptrace errors.
639 #if defined SYS_ipc_subcall \
640 || defined SYS_socket_subcall \
641 || defined SYS_syscall_subcall
643 switch (tcp->s_ent->sen) {
644 # ifdef SYS_ipc_subcall
646 decode_ipc_subcall(tcp);
649 # ifdef SYS_socket_subcall
651 decode_socket_subcall(tcp);
654 # ifdef SYS_syscall_subcall
656 decode_syscall_subcall(tcp);
657 if (tcp->s_ent->sen != SEN_syscall)
670 syscall_entering_trace(struct tcb *tcp, unsigned int *sig)
672 /* Restrain from fault injection while the trace executes strace code. */
674 tcp->qual_flg &= ~QUAL_INJECT;
677 switch (tcp->s_ent->sen) {
680 #if defined SPARC || defined SPARC64
683 tcp->flags &= ~TCB_HIDE_LOG;
687 if (!traced(tcp) || (tracing_paths && !pathtrace_match(tcp))) {
688 tcp->flags |= TCB_FILTERED;
692 tcp->flags &= ~TCB_FILTERED;
699 tamper_with_syscall_entering(tcp, sig);
701 if (cflag == CFLAG_ONLY_STATS) {
706 if (stack_trace_enabled) {
707 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
708 unwind_capture_stacktrace(tcp);
713 tprintf("%s(", tcp->s_ent->sys_name);
714 int res = raw(tcp) ? printargs(tcp) : tcp->s_ent->sys_func(tcp);
720 syscall_entering_finish(struct tcb *tcp, int res)
722 tcp->flags |= TCB_INSYSCALL;
723 tcp->sys_func_rval = res;
724 /* Measure the entrance time as late as possible to avoid errors. */
725 if ((Tflag || cflag) && !filtered(tcp))
726 clock_gettime(CLOCK_MONOTONIC, &tcp->etime);
732 * -1: error in one of ptrace ops.
734 * If not 0, call syscall_exiting_trace(tcp, res), where res is the return
735 * value. Anyway, call syscall_exiting_finish(tcp) then.
738 syscall_exiting_decode(struct tcb *tcp, struct timespec *pts)
740 /* Measure the exit time as early as possible to avoid errors. */
741 if ((Tflag || cflag) && !(filtered(tcp) || hide_log(tcp)))
742 clock_gettime(CLOCK_MONOTONIC, pts);
744 if (mmap_cache_is_enabled()) {
745 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
746 mmap_cache_invalidate(tcp);
749 if (filtered(tcp) || hide_log(tcp))
752 #if SUPPORTED_PERSONALITIES > 1
753 update_personality(tcp, tcp->currpers);
756 return get_syscall_result(tcp);
760 syscall_exiting_trace(struct tcb *tcp, struct timespec *ts, int res)
762 if (syscall_tampered(tcp) || inject_delay_exit(tcp))
763 tamper_with_syscall_exiting(tcp);
766 count_syscall(tcp, ts);
767 if (cflag == CFLAG_ONLY_STATS) {
772 /* If not in -ff mode, and printing_tcp != tcp,
773 * then the log currently does not end with output
774 * of _our syscall entry_, but with something else.
775 * We need to say which syscall's return is this.
777 * Forced reprinting via TCB_REPRINT is used only by
778 * "strace -ff -oLOG test/threaded_execve" corner case.
779 * It's the only case when -ff mode needs reprinting.
781 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
782 tcp->flags &= ~TCB_REPRINT;
784 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
788 tcp->s_prev_ent = NULL;
790 /* There was error in one of prior ptrace ops */
793 tprints("= ? <unavailable>\n");
797 tcp->s_prev_ent = tcp->s_ent;
801 /* sys_res = printargs(tcp); - but it's nop on sysexit */
803 /* FIXME: not_failing_only (IOW, option -z) is broken:
804 * failure of syscall is known only after syscall return.
805 * Thus we end up with something like this on, say, ENOENT:
806 * open("does_not_exist", O_RDONLY <unfinished ...>
807 * {next syscall decode}
808 * whereas the intended result is that open(...) line
809 * is not shown at all.
811 if (not_failing_only && tcp->u_error)
812 return 0; /* ignore failed syscalls */
813 if (tcp->sys_func_rval & RVAL_DECODED)
814 sys_res = tcp->sys_func_rval;
816 sys_res = tcp->s_ent->sys_func(tcp);
821 unsigned long u_error = tcp->u_error;
822 kernel_long_t u_rval;
826 tprintf("= -1 (errno %lu)", u_error);
828 tprintf("= %#" PRI_klx, tcp->u_rval);
830 if (syscall_tampered(tcp))
831 tprints(" (INJECTED)");
832 } else if (!(sys_res & RVAL_NONE) && u_error) {
833 const char *u_error_str;
836 /* Blocked signals do not interrupt any syscalls.
837 * In this case syscalls don't return ERESTARTfoo codes.
839 * Deadly signals set to SIG_DFL interrupt syscalls
840 * and kill the process regardless of which of the codes below
841 * is returned by the interrupted syscall.
842 * In some cases, kernel forces a kernel-generated deadly
843 * signal to be unblocked and set to SIG_DFL (and thus cause
844 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
845 * or SIGILL. (The alternative is to leave process spinning
846 * forever on the faulty instruction - not useful).
848 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
849 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
850 * but kernel will always restart them.
853 /* Most common type of signal-interrupted syscall exit code.
854 * The system call will be restarted with the same arguments
855 * if SA_RESTART is set; otherwise, it will fail with EINTR.
857 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
860 /* Rare. For example, fork() returns this if interrupted.
861 * SA_RESTART is ignored (assumed set): the restart is unconditional.
863 tprints("= ? ERESTARTNOINTR (To be restarted)");
866 /* pause(), rt_sigsuspend() etc use this code.
867 * SA_RESTART is ignored (assumed not set):
868 * syscall won't restart (will return EINTR instead)
869 * even after signal with SA_RESTART set. However,
870 * after SIG_IGN or SIG_DFL signal it will restart
871 * (thus the name "restart only if has no handler").
873 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
875 case ERESTART_RESTARTBLOCK:
876 /* Syscalls like nanosleep(), poll() which can't be
877 * restarted with their original arguments use this
878 * code. Kernel will execute restart_syscall() instead,
879 * which changes arguments before restarting syscall.
880 * SA_RESTART is ignored (assumed not set) similarly
881 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
882 * since restart data is saved in "restart block"
883 * in task struct, and if signal handler uses a syscall
884 * which in turn saves another such restart block,
885 * old data is lost and restart becomes impossible)
887 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
890 u_rval = sys_res & RVAL_PRINT_ERR_VAL ?
892 u_error_str = err_name(u_error);
894 tprintf("= %" PRI_kld " %s (%s)",
895 u_rval, u_error_str, strerror(u_error));
897 tprintf("= %" PRI_kld " %lu (%s)",
898 u_rval, u_error, strerror(u_error));
901 if (syscall_tampered(tcp))
902 tprints(" (INJECTED)");
903 if ((sys_res & RVAL_STR) && tcp->auxstr)
904 tprintf(" (%s)", tcp->auxstr);
906 if (sys_res & RVAL_NONE)
909 switch (sys_res & RVAL_MASK) {
911 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
912 if (current_klongsize < sizeof(tcp->u_rval)) {
914 (unsigned int) tcp->u_rval);
918 tprintf("= %#" PRI_klx, tcp->u_rval);
923 print_numeric_long_umask(tcp->u_rval);
926 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
927 if (current_klongsize < sizeof(tcp->u_rval)) {
929 (unsigned int) tcp->u_rval);
933 tprintf("= %" PRI_klu, tcp->u_rval);
937 tprintf("= %" PRI_kld, tcp->u_rval);
942 printfd(tcp, tcp->u_rval);
944 tprintf("= %" PRI_kld, tcp->u_rval);
947 error_msg("invalid rval format");
951 if ((sys_res & RVAL_STR) && tcp->auxstr)
952 tprintf(" (%s)", tcp->auxstr);
953 if (syscall_tampered(tcp))
954 tprints(" (INJECTED)");
957 ts_sub(ts, ts, &tcp->etime);
958 tprintf(" <%ld.%06ld>",
959 (long) ts->tv_sec, (long) ts->tv_nsec / 1000);
966 if (stack_trace_enabled)
967 unwind_print_stacktrace(tcp);
973 syscall_exiting_finish(struct tcb *tcp)
975 tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED | TCB_INJECT_DELAY_EXIT);
976 tcp->sys_func_rval = 0;
977 free_tcb_priv_data(tcp);
981 is_erestart(struct tcb *tcp)
983 switch (tcp->u_error) {
987 case ERESTART_RESTARTBLOCK:
994 static unsigned long saved_u_error;
997 temporarily_clear_syserror(struct tcb *tcp)
999 saved_u_error = tcp->u_error;
1004 restore_cleared_syserror(struct tcb *tcp)
1006 tcp->u_error = saved_u_error;
1009 #include "arch_regs.c"
1011 #if HAVE_ARCH_GETRVAL2
1012 # include "arch_getrval2.c"
1016 print_pc(struct tcb *tcp)
1018 #if defined ARCH_PC_REG
1019 # define ARCH_GET_PC 0
1020 #elif defined ARCH_PC_PEEK_ADDR
1022 # define ARCH_PC_REG pc
1023 # define ARCH_GET_PC upeek(tcp, ARCH_PC_PEEK_ADDR, &pc)
1025 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1027 if (get_regs(tcp) < 0 || ARCH_GET_PC)
1028 tprints(current_wordsize == 4 ? "[????????] "
1029 : "[????????????????] ");
1031 tprintf(current_wordsize == 4
1032 ? "[%08" PRI_klx "] " : "[%016" PRI_klx "] ",
1033 (kernel_ulong_t) ARCH_PC_REG);
1036 #include "getregs_old.h"
1038 #undef ptrace_getregset_or_getregs
1039 #undef ptrace_setregset_or_setregs
1040 #ifdef ARCH_REGS_FOR_GETREGSET
1042 # define ptrace_getregset_or_getregs ptrace_getregset
1044 ptrace_getregset(pid_t pid)
1046 # ifdef ARCH_IOVEC_FOR_GETREGSET
1047 /* variable iovec */
1048 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1049 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1050 &ARCH_IOVEC_FOR_GETREGSET);
1052 /* constant iovec */
1053 static struct iovec io = {
1054 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1055 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1057 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1062 # ifndef HAVE_GETREGS_OLD
1063 # define ptrace_setregset_or_setregs ptrace_setregset
1065 ptrace_setregset(pid_t pid)
1067 # ifdef ARCH_IOVEC_FOR_GETREGSET
1068 /* variable iovec */
1069 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1070 &ARCH_IOVEC_FOR_GETREGSET);
1072 /* constant iovec */
1073 static struct iovec io = {
1074 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1075 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1077 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1080 # endif /* !HAVE_GETREGS_OLD */
1082 #elif defined ARCH_REGS_FOR_GETREGS
1084 # define ptrace_getregset_or_getregs ptrace_getregs
1086 ptrace_getregs(pid_t pid)
1088 # if defined SPARC || defined SPARC64
1089 /* SPARC systems have the meaning of data and addr reversed */
1090 return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1092 return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1096 # ifndef HAVE_GETREGS_OLD
1097 # define ptrace_setregset_or_setregs ptrace_setregs
1099 ptrace_setregs(pid_t pid)
1101 # if defined SPARC || defined SPARC64
1102 /* SPARC systems have the meaning of data and addr reversed */
1103 return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1105 return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1108 # endif /* !HAVE_GETREGS_OLD */
1110 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1112 #ifdef ptrace_getregset_or_getregs
1113 static long get_regs_error;
1117 clear_regs(struct tcb *tcp)
1119 #ifdef ptrace_getregset_or_getregs
1120 get_regs_error = -1;
1125 get_regs(struct tcb *const tcp)
1127 #ifdef ptrace_getregset_or_getregs
1129 if (get_regs_error != -1)
1130 return get_regs_error;
1132 # ifdef HAVE_GETREGS_OLD
1134 * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1135 * fallback to getregs_old.
1137 static int use_getregs_old;
1138 if (use_getregs_old < 0) {
1139 return get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1140 } else if (use_getregs_old == 0) {
1141 get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1142 if (get_regs_error >= 0) {
1143 use_getregs_old = -1;
1144 return get_regs_error;
1146 if (errno == EPERM || errno == ESRCH)
1147 return get_regs_error;
1148 use_getregs_old = 1;
1150 return get_regs_error = getregs_old(tcp);
1151 # else /* !HAVE_GETREGS_OLD */
1152 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1153 get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1155 # if defined ARCH_PERSONALITY_0_IOV_SIZE
1157 return get_regs_error;
1159 switch (ARCH_IOVEC_FOR_GETREGSET.iov_len) {
1160 case ARCH_PERSONALITY_0_IOV_SIZE:
1161 update_personality(tcp, 0);
1163 case ARCH_PERSONALITY_1_IOV_SIZE:
1164 update_personality(tcp, 1);
1167 static bool printed = false;
1170 error_msg("Unsupported regset size returned by "
1171 "PTRACE_GETREGSET: %zu",
1172 ARCH_IOVEC_FOR_GETREGSET.iov_len);
1177 update_personality(tcp, 0);
1180 # endif /* ARCH_PERSONALITY_0_IOV_SIZE */
1182 return get_regs_error;
1184 # endif /* !HAVE_GETREGS_OLD */
1186 #else /* !ptrace_getregset_or_getregs */
1188 # warning get_regs is not implemented for this architecture yet
1191 #endif /* !ptrace_getregset_or_getregs */
1194 #ifdef ptrace_setregset_or_setregs
1198 return ptrace_setregset_or_setregs(pid);
1200 #endif /* ptrace_setregset_or_setregs */
1205 char buf[sizeof("syscall_0x") + sizeof(kernel_ulong_t) * 2];
1209 free_sysent_buf(void *ptr)
1211 struct sysent_buf *s = ptr;
1212 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1218 * 0: "ignore this ptrace stop", syscall_entering_decode() should return a "bail
1219 * out silently" code.
1220 * 1: ok, continue in syscall_entering_decode().
1221 * other: error, syscall_entering_decode() should print error indicator
1222 * ("????" etc) and return an appropriate code.
1225 get_scno(struct tcb *tcp)
1227 if (get_regs(tcp) < 0)
1230 int rc = arch_get_scno(tcp);
1234 tcp->scno = shuffle_scno(tcp->scno);
1236 if (scno_is_valid(tcp->scno)) {
1237 tcp->s_ent = &sysent[tcp->scno];
1238 tcp->qual_flg = qual_flags(tcp->scno);
1240 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1243 s->ent.nargs = MAX_ARGS;
1244 s->ent.sen = SEN_printargs;
1245 s->ent.sys_func = printargs;
1246 s->ent.sys_name = s->buf;
1247 xsprintf(s->buf, "syscall_%#" PRI_klx, shuffle_scno(tcp->scno));
1249 tcp->s_ent = &s->ent;
1250 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1252 set_tcb_priv_data(tcp, s, free_sysent_buf);
1254 debug_msg("pid %d invalid syscall %#" PRI_klx,
1255 tcp->pid, shuffle_scno(tcp->scno));
1259 * We refrain from argument decoding during recovering
1260 * as tracee memory mappings has changed and the registers
1261 * are very likely pointing to garbage already.
1263 if (recovering(tcp))
1264 tcp->qual_flg |= QUAL_RAW;
1269 #ifdef ptrace_getregset_or_getregs
1270 # define get_syscall_result_regs get_regs
1272 static int get_syscall_result_regs(struct tcb *);
1276 * 1: ok, continue in syscall_exiting_trace().
1277 * -1: error, syscall_exiting_trace() should print error indicator
1278 * ("????" etc) and bail out.
1281 get_syscall_result(struct tcb *tcp)
1283 if (get_syscall_result_regs(tcp) < 0)
1287 !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS)
1288 || syscall_tampered(tcp));
1293 #include "get_scno.c"
1294 #include "set_scno.c"
1295 #include "get_syscall_args.c"
1296 #ifndef ptrace_getregset_or_getregs
1297 # include "get_syscall_result.c"
1299 #include "get_error.c"
1300 #include "set_error.c"
1301 #ifdef HAVE_GETREGS_OLD
1302 # include "getregs_old.c"
1304 #include "shuffle_scno.c"
1307 syscall_name(kernel_ulong_t scno)
1309 return scno_is_valid(scno) ? sysent[scno].sys_name : NULL;