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 "number_set.h"
39 #include <sys/param.h>
41 /* for struct iovec */
44 /* for __X32_SYSCALL_BIT */
45 #include <asm/unistd.h>
51 # undef PTRACE_GETREGS
52 # define PTRACE_GETREGS PTRACE_GETREGS64
53 # undef PTRACE_SETREGS
54 # define PTRACE_SETREGS PTRACE_SETREGS64
58 # define NT_PRSTATUS 1
64 /* Define these shorthand notations to simplify the syscallent files. */
65 #include "sysent_shorthand_defs.h"
67 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
69 const struct_sysent sysent0[] = {
70 #include "syscallent.h"
73 #if SUPPORTED_PERSONALITIES > 1
74 # include PERSONALITY1_INCLUDE_FUNCS
75 static const struct_sysent sysent1[] = {
76 # include "syscallent1.h"
80 #if SUPPORTED_PERSONALITIES > 2
81 # include PERSONALITY2_INCLUDE_FUNCS
82 static const struct_sysent sysent2[] = {
83 # include "syscallent2.h"
87 /* Now undef them since short defines cause wicked namespace pollution. */
88 #include "sysent_shorthand_undefs.h"
91 * `ioctlent[012].h' files are automatically generated by the auxiliary
92 * program `ioctlsort', such that the list is sorted by the `code' field.
93 * This has the side-effect of resolving the _IO.. macros into
94 * plain integers, eliminating the need to include here everything
98 const char *const errnoent0[] = {
101 const char *const signalent0[] = {
102 #include "signalent.h"
104 const struct_ioctlent ioctlent0[] = {
105 #include "ioctlent0.h"
108 #if SUPPORTED_PERSONALITIES > 1
109 static const char *const errnoent1[] = {
110 # include "errnoent1.h"
112 static const char *const signalent1[] = {
113 # include "signalent1.h"
115 static const struct_ioctlent ioctlent1[] = {
116 # include "ioctlent1.h"
118 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
119 static const struct_printers printers0 = {
120 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
122 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
123 static const struct_printers printers1 = {
124 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
128 #if SUPPORTED_PERSONALITIES > 2
129 static const char *const errnoent2[] = {
130 # include "errnoent2.h"
132 static const char *const signalent2[] = {
133 # include "signalent2.h"
135 static const struct_ioctlent ioctlent2[] = {
136 # include "ioctlent2.h"
138 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
139 static const struct_printers printers2 = {
140 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
145 nsyscalls0 = ARRAY_SIZE(sysent0)
146 #if SUPPORTED_PERSONALITIES > 1
147 , nsyscalls1 = ARRAY_SIZE(sysent1)
148 # if SUPPORTED_PERSONALITIES > 2
149 , nsyscalls2 = ARRAY_SIZE(sysent2)
155 nerrnos0 = ARRAY_SIZE(errnoent0)
156 #if SUPPORTED_PERSONALITIES > 1
157 , nerrnos1 = ARRAY_SIZE(errnoent1)
158 # if SUPPORTED_PERSONALITIES > 2
159 , nerrnos2 = ARRAY_SIZE(errnoent2)
165 nsignals0 = ARRAY_SIZE(signalent0)
166 #if SUPPORTED_PERSONALITIES > 1
167 , nsignals1 = ARRAY_SIZE(signalent1)
168 # if SUPPORTED_PERSONALITIES > 2
169 , nsignals2 = ARRAY_SIZE(signalent2)
175 nioctlents0 = ARRAY_SIZE(ioctlent0)
176 #if SUPPORTED_PERSONALITIES > 1
177 , nioctlents1 = ARRAY_SIZE(ioctlent1)
178 # if SUPPORTED_PERSONALITIES > 2
179 , nioctlents2 = ARRAY_SIZE(ioctlent2)
184 #if SUPPORTED_PERSONALITIES > 1
185 const struct_sysent *sysent = sysent0;
186 const char *const *errnoent = errnoent0;
187 const char *const *signalent = signalent0;
188 const struct_ioctlent *ioctlent = ioctlent0;
189 const struct_printers *printers = &printers0;
192 unsigned nsyscalls = nsyscalls0;
193 unsigned nerrnos = nerrnos0;
194 unsigned nsignals = nsignals0;
195 unsigned nioctlents = nioctlents0;
197 const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES] = {
199 #if SUPPORTED_PERSONALITIES > 1
202 #if SUPPORTED_PERSONALITIES > 2
206 const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
208 #if SUPPORTED_PERSONALITIES > 1
211 #if SUPPORTED_PERSONALITIES > 2
216 const char *const personality_names[] =
218 {"64 bit", "32 bit", "x32"}
221 # elif SUPPORTED_PERSONALITIES == 2
224 {STRINGIFY_VAL(__WORDSIZE) " bit"}
228 #if SUPPORTED_PERSONALITIES > 1
230 unsigned current_personality;
232 # ifndef current_wordsize
233 unsigned current_wordsize;
234 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
235 PERSONALITY0_WORDSIZE,
236 PERSONALITY1_WORDSIZE,
237 # if SUPPORTED_PERSONALITIES > 2
238 PERSONALITY2_WORDSIZE,
243 # ifndef current_klongsize
244 unsigned current_klongsize;
245 static const int personality_klongsize[SUPPORTED_PERSONALITIES] = {
246 PERSONALITY0_KLONGSIZE,
247 PERSONALITY1_KLONGSIZE,
248 # if SUPPORTED_PERSONALITIES > 2
249 PERSONALITY2_KLONGSIZE,
255 set_personality(int personality)
257 nsyscalls = nsyscall_vec[personality];
258 sysent = sysent_vec[personality];
260 switch (personality) {
262 errnoent = errnoent0;
264 ioctlent = ioctlent0;
265 nioctlents = nioctlents0;
266 signalent = signalent0;
267 nsignals = nsignals0;
268 printers = &printers0;
272 errnoent = errnoent1;
274 ioctlent = ioctlent1;
275 nioctlents = nioctlents1;
276 signalent = signalent1;
277 nsignals = nsignals1;
278 printers = &printers1;
281 # if SUPPORTED_PERSONALITIES > 2
283 errnoent = errnoent2;
285 ioctlent = ioctlent2;
286 nioctlents = nioctlents2;
287 signalent = signalent2;
288 nsignals = nsignals2;
289 printers = &printers2;
294 current_personality = personality;
295 # ifndef current_wordsize
296 current_wordsize = personality_wordsize[personality];
298 # ifndef current_klongsize
299 current_klongsize = personality_klongsize[personality];
304 update_personality(struct tcb *tcp, unsigned int personality)
306 static bool need_mpers_warning[] =
307 { false, !HAVE_PERSONALITY_1_MPERS, !HAVE_PERSONALITY_2_MPERS };
309 if (personality == current_personality)
311 set_personality(personality);
313 if (personality == tcp->currpers)
315 tcp->currpers = personality;
318 error_msg("[ Process PID=%d runs in %s mode. ]",
319 tcp->pid, personality_names[personality]);
322 if (need_mpers_warning[personality]) {
323 error_msg("WARNING: Proper structure decoding for this "
324 "personality is not supported, please consider "
325 "building strace with mpers support enabled.");
326 need_mpers_warning[personality] = false;
331 #ifdef SYS_socket_subcall
333 decode_socket_subcall(struct tcb *tcp)
335 const int call = tcp->u_arg[0];
337 if (call < 1 || call >= SYS_socket_nsubcalls)
340 const kernel_ulong_t scno = SYS_socket_subcall + call;
341 const unsigned int nargs = sysent[scno].nargs;
344 if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
348 tcp->qual_flg = qual_flags(scno);
349 tcp->s_ent = &sysent[scno];
352 for (i = 0; i < nargs; ++i)
353 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
354 ? ((uint32_t *) (void *) buf)[i] : buf[i];
356 #endif /* SYS_socket_subcall */
358 #ifdef SYS_ipc_subcall
360 decode_ipc_subcall(struct tcb *tcp)
362 unsigned int call = tcp->u_arg[0];
363 const unsigned int version = call >> 16;
366 # if defined S390 || defined S390X
370 if (current_wordsize == 8)
373 set_tcb_priv_ulong(tcp, version);
379 case 1: case 2: case 3: case 4:
380 case 11: case 12: case 13: case 14:
381 case 21: case 22: case 23: case 24:
387 tcp->scno = SYS_ipc_subcall + call;
388 tcp->qual_flg = qual_flags(tcp->scno);
389 tcp->s_ent = &sysent[tcp->scno];
391 const unsigned int n = tcp->s_ent->nargs;
393 for (i = 0; i < n; i++)
394 tcp->u_arg[i] = tcp->u_arg[i + 1];
396 #endif /* SYS_ipc_subcall */
398 #ifdef SYS_syscall_subcall
400 decode_syscall_subcall(struct tcb *tcp)
402 if (!scno_is_valid(tcp->u_arg[0]))
404 tcp->scno = tcp->u_arg[0];
405 tcp->qual_flg = qual_flags(tcp->scno);
406 tcp->s_ent = &sysent[tcp->scno];
407 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
408 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
409 # ifdef LINUX_MIPSO32
411 * Fetching the last arg of 7-arg syscalls (fadvise64_64
412 * and sync_file_range) requires additional code,
413 * see linux/mips/get_syscall_args.c
415 if (tcp->s_ent->nargs == MAX_ARGS) {
417 mips_REG_SP + MAX_ARGS * sizeof(tcp->u_arg[0]),
418 sizeof(tcp->u_arg[0]),
419 &tcp->u_arg[MAX_ARGS - 1]) < 0)
420 tcp->u_arg[MAX_ARGS - 1] = 0;
422 # endif /* LINUX_MIPSO32 */
424 #endif /* SYS_syscall_subcall */
427 dumpio(struct tcb *tcp)
432 int fd = tcp->u_arg[0];
436 if (is_number_in_set(fd, read_set)) {
437 switch (tcp->s_ent->sen) {
442 case SEN_mq_timedreceive:
443 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
448 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
452 dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
455 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
459 if (is_number_in_set(fd, write_set)) {
460 switch (tcp->s_ent->sen) {
465 case SEN_mq_timedsend:
466 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
472 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1);
475 dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1);
478 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
485 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
486 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
488 static kernel_ulong_t
489 shuffle_scno(kernel_ulong_t scno)
491 #ifdef ARM_FIRST_SHUFFLED_SYSCALL /* So far only 32-bit ARM needs this */
492 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
495 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
496 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
498 if (scno == 0x000ffff0)
499 return ARM_FIRST_SHUFFLED_SYSCALL;
501 # define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
503 * Is it ARM specific syscall?
504 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
505 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
507 if (scno >= 0x000f0000 &&
508 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
509 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
511 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
512 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
514 #endif /* ARM_FIRST_SHUFFLED_SYSCALL */
520 err_name(unsigned long err)
522 if ((err < nerrnos) && errnoent[err])
523 return errnoent[err];
528 static long get_regs(struct tcb *);
529 static int get_syscall_args(struct tcb *);
530 static int get_syscall_result(struct tcb *);
531 static int arch_get_scno(struct tcb *tcp);
532 static int arch_set_scno(struct tcb *, kernel_ulong_t);
533 static void get_error(struct tcb *, const bool);
534 static int arch_set_error(struct tcb *);
535 static int arch_set_success(struct tcb *);
537 struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
539 static struct inject_opts *
540 tcb_inject_opts(struct tcb *tcp)
542 return (scno_in_range(tcp->scno) && tcp->inject_vec[current_personality])
543 ? &tcp->inject_vec[current_personality][tcp->scno] : NULL;
548 tamper_with_syscall_entering(struct tcb *tcp, unsigned int *signo)
550 if (!tcp->inject_vec[current_personality]) {
551 tcp->inject_vec[current_personality] =
552 xcalloc(nsyscalls, sizeof(**inject_vec));
553 memcpy(tcp->inject_vec[current_personality],
554 inject_vec[current_personality],
555 nsyscalls * sizeof(**inject_vec));
558 struct inject_opts *opts = tcb_inject_opts(tcp);
560 if (!opts || opts->first == 0)
565 if (opts->first != 0)
568 opts->first = opts->step;
570 if (opts->data.flags & INJECT_F_SIGNAL)
571 *signo = opts->data.signo;
572 if (opts->data.flags & INJECT_F_RETVAL && !arch_set_scno(tcp, -1))
573 tcp->flags |= TCB_TAMPERED;
579 tamper_with_syscall_exiting(struct tcb *tcp)
581 struct inject_opts *opts = tcb_inject_opts(tcp);
586 if (opts->data.rval >= 0) {
587 kernel_long_t u_rval = tcp->u_rval;
589 tcp->u_rval = opts->data.rval;
590 if (arch_set_success(tcp)) {
591 tcp->u_rval = u_rval;
596 unsigned long new_error = -opts->data.rval;
598 if (new_error != tcp->u_error && new_error <= MAX_ERRNO_VALUE) {
599 unsigned long u_error = tcp->u_error;
601 tcp->u_error = new_error;
602 if (arch_set_error(tcp)) {
603 tcp->u_error = u_error;
613 * 0: "ignore this ptrace stop", bail out silently.
614 * 1: ok, decoded; call
615 * syscall_entering_finish(tcp, syscall_entering_trace(tcp, ...)).
616 * other: error; call syscall_entering_finish(tcp, res), where res is the value
620 syscall_entering_decode(struct tcb *tcp)
622 int res = get_scno(tcp);
626 if (res != 1 || (res = get_syscall_args(tcp)) != 1) {
628 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
630 * " <unavailable>" will be added later by the code which
631 * detects ptrace errors.
636 #if defined SYS_ipc_subcall \
637 || defined SYS_socket_subcall \
638 || defined SYS_syscall_subcall
640 switch (tcp->s_ent->sen) {
641 # ifdef SYS_ipc_subcall
643 decode_ipc_subcall(tcp);
646 # ifdef SYS_socket_subcall
648 decode_socket_subcall(tcp);
651 # ifdef SYS_syscall_subcall
653 decode_syscall_subcall(tcp);
654 if (tcp->s_ent->sen != SEN_syscall)
667 syscall_entering_trace(struct tcb *tcp, unsigned int *sig)
669 /* Restrain from fault injection while the trace executes strace code. */
671 tcp->qual_flg &= ~QUAL_INJECT;
674 switch (tcp->s_ent->sen) {
677 #if defined SPARC || defined SPARC64
680 tcp->flags &= ~TCB_HIDE_LOG;
684 if (!traced(tcp) || (tracing_paths && !pathtrace_match(tcp))) {
685 tcp->flags |= TCB_FILTERED;
689 tcp->flags &= ~TCB_FILTERED;
696 tamper_with_syscall_entering(tcp, sig);
698 if (cflag == CFLAG_ONLY_STATS) {
703 if (stack_trace_enabled) {
704 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
705 unwind_capture_stacktrace(tcp);
710 tprintf("%s(", tcp->s_ent->sys_name);
711 int res = raw(tcp) ? printargs(tcp) : tcp->s_ent->sys_func(tcp);
717 syscall_entering_finish(struct tcb *tcp, int res)
719 tcp->flags |= TCB_INSYSCALL;
720 tcp->sys_func_rval = res;
721 /* Measure the entrance time as late as possible to avoid errors. */
722 if ((Tflag || cflag) && !filtered(tcp))
723 gettimeofday(&tcp->etime, NULL);
729 * -1: error in one of ptrace ops.
731 * If not 0, call syscall_exiting_trace(tcp, res), where res is the return
732 * value. Anyway, call syscall_exiting_finish(tcp) then.
735 syscall_exiting_decode(struct tcb *tcp, struct timeval *ptv)
737 /* Measure the exit time as early as possible to avoid errors. */
738 if ((Tflag || cflag) && !(filtered(tcp) || hide_log(tcp)))
739 gettimeofday(ptv, NULL);
742 if (stack_trace_enabled) {
743 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
744 unwind_cache_invalidate(tcp);
748 if (filtered(tcp) || hide_log(tcp))
751 #if SUPPORTED_PERSONALITIES > 1
752 update_personality(tcp, tcp->currpers);
755 return get_syscall_result(tcp);
759 syscall_exiting_trace(struct tcb *tcp, struct timeval tv, int res)
761 if (syserror(tcp) && syscall_tampered(tcp))
762 tamper_with_syscall_exiting(tcp);
765 count_syscall(tcp, &tv);
766 if (cflag == CFLAG_ONLY_STATS) {
771 /* If not in -ff mode, and printing_tcp != tcp,
772 * then the log currently does not end with output
773 * of _our syscall entry_, but with something else.
774 * We need to say which syscall's return is this.
776 * Forced reprinting via TCB_REPRINT is used only by
777 * "strace -ff -oLOG test/threaded_execve" corner case.
778 * It's the only case when -ff mode needs reprinting.
780 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
781 tcp->flags &= ~TCB_REPRINT;
783 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
787 tcp->s_prev_ent = NULL;
789 /* There was error in one of prior ptrace ops */
792 tprints("= ? <unavailable>\n");
796 tcp->s_prev_ent = tcp->s_ent;
800 /* sys_res = printargs(tcp); - but it's nop on sysexit */
802 /* FIXME: not_failing_only (IOW, option -z) is broken:
803 * failure of syscall is known only after syscall return.
804 * Thus we end up with something like this on, say, ENOENT:
805 * open("does_not_exist", O_RDONLY <unfinished ...>
806 * {next syscall decode}
807 * whereas the intended result is that open(...) line
808 * is not shown at all.
810 if (not_failing_only && tcp->u_error)
811 return 0; /* ignore failed syscalls */
812 if (tcp->sys_func_rval & RVAL_DECODED)
813 sys_res = tcp->sys_func_rval;
815 sys_res = tcp->s_ent->sys_func(tcp);
820 unsigned long u_error = tcp->u_error;
821 kernel_long_t u_rval;
825 tprintf("= -1 (errno %lu)", u_error);
827 tprintf("= %#" PRI_klx, tcp->u_rval);
829 if (syscall_tampered(tcp))
830 tprints(" (INJECTED)");
831 } else if (!(sys_res & RVAL_NONE) && u_error) {
832 const char *u_error_str;
835 /* Blocked signals do not interrupt any syscalls.
836 * In this case syscalls don't return ERESTARTfoo codes.
838 * Deadly signals set to SIG_DFL interrupt syscalls
839 * and kill the process regardless of which of the codes below
840 * is returned by the interrupted syscall.
841 * In some cases, kernel forces a kernel-generated deadly
842 * signal to be unblocked and set to SIG_DFL (and thus cause
843 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
844 * or SIGILL. (The alternative is to leave process spinning
845 * forever on the faulty instruction - not useful).
847 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
848 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
849 * but kernel will always restart them.
852 /* Most common type of signal-interrupted syscall exit code.
853 * The system call will be restarted with the same arguments
854 * if SA_RESTART is set; otherwise, it will fail with EINTR.
856 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
859 /* Rare. For example, fork() returns this if interrupted.
860 * SA_RESTART is ignored (assumed set): the restart is unconditional.
862 tprints("= ? ERESTARTNOINTR (To be restarted)");
865 /* pause(), rt_sigsuspend() etc use this code.
866 * SA_RESTART is ignored (assumed not set):
867 * syscall won't restart (will return EINTR instead)
868 * even after signal with SA_RESTART set. However,
869 * after SIG_IGN or SIG_DFL signal it will restart
870 * (thus the name "restart only if has no handler").
872 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
874 case ERESTART_RESTARTBLOCK:
875 /* Syscalls like nanosleep(), poll() which can't be
876 * restarted with their original arguments use this
877 * code. Kernel will execute restart_syscall() instead,
878 * which changes arguments before restarting syscall.
879 * SA_RESTART is ignored (assumed not set) similarly
880 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
881 * since restart data is saved in "restart block"
882 * in task struct, and if signal handler uses a syscall
883 * which in turn saves another such restart block,
884 * old data is lost and restart becomes impossible)
886 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
889 u_rval = sys_res & RVAL_PRINT_ERR_VAL ?
891 u_error_str = err_name(u_error);
893 tprintf("= %" PRI_kld " %s (%s)",
894 u_rval, u_error_str, strerror(u_error));
896 tprintf("= %" PRI_kld " %lu (%s)",
897 u_rval, u_error, strerror(u_error));
900 if (syscall_tampered(tcp))
901 tprints(" (INJECTED)");
902 if ((sys_res & RVAL_STR) && tcp->auxstr)
903 tprintf(" (%s)", tcp->auxstr);
905 if (sys_res & RVAL_NONE)
908 switch (sys_res & RVAL_MASK) {
910 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
911 if (current_wordsize < sizeof(tcp->u_rval)) {
913 (unsigned int) tcp->u_rval);
917 tprintf("= %#" PRI_klx, tcp->u_rval);
922 print_numeric_long_umask(tcp->u_rval);
925 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
926 if (current_wordsize < sizeof(tcp->u_rval)) {
928 (unsigned int) tcp->u_rval);
932 tprintf("= %" PRI_klu, tcp->u_rval);
936 tprintf("= %" PRI_kld, tcp->u_rval);
941 printfd(tcp, tcp->u_rval);
943 tprintf("= %" PRI_kld, tcp->u_rval);
946 error_msg("invalid rval format");
950 if ((sys_res & RVAL_STR) && tcp->auxstr)
951 tprintf(" (%s)", tcp->auxstr);
952 if (syscall_tampered(tcp))
953 tprints(" (INJECTED)");
956 tv_sub(&tv, &tv, &tcp->etime);
957 tprintf(" <%ld.%06ld>",
958 (long) tv.tv_sec, (long) tv.tv_usec);
965 if (stack_trace_enabled)
966 unwind_print_stacktrace(tcp);
972 syscall_exiting_finish(struct tcb *tcp)
974 tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED);
975 tcp->sys_func_rval = 0;
976 free_tcb_priv_data(tcp);
980 is_erestart(struct tcb *tcp)
982 switch (tcp->u_error) {
986 case ERESTART_RESTARTBLOCK:
993 static unsigned long saved_u_error;
996 temporarily_clear_syserror(struct tcb *tcp)
998 saved_u_error = tcp->u_error;
1003 restore_cleared_syserror(struct tcb *tcp)
1005 tcp->u_error = saved_u_error;
1008 #include "arch_regs.c"
1010 #ifdef HAVE_GETRVAL2
1011 # include "arch_getrval2.c"
1015 print_pc(struct tcb *tcp)
1017 #if defined ARCH_PC_REG
1018 # define ARCH_GET_PC 0
1019 #elif defined ARCH_PC_PEEK_ADDR
1021 # define ARCH_PC_REG pc
1022 # define ARCH_GET_PC upeek(tcp, ARCH_PC_PEEK_ADDR, &pc)
1024 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1026 if (get_regs(tcp) < 0 || ARCH_GET_PC)
1027 tprints(current_wordsize == 4 ? "[????????] "
1028 : "[????????????????] ");
1030 tprintf(current_wordsize == 4
1031 ? "[%08" PRI_klx "] " : "[%016" PRI_klx "] ",
1032 (kernel_ulong_t) ARCH_PC_REG);
1035 #include "getregs_old.h"
1037 #undef ptrace_getregset_or_getregs
1038 #undef ptrace_setregset_or_setregs
1039 #ifdef ARCH_REGS_FOR_GETREGSET
1041 # define ptrace_getregset_or_getregs ptrace_getregset
1043 ptrace_getregset(pid_t pid)
1045 # ifdef ARCH_IOVEC_FOR_GETREGSET
1046 /* variable iovec */
1047 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1048 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1049 &ARCH_IOVEC_FOR_GETREGSET);
1051 /* constant iovec */
1052 static struct iovec io = {
1053 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1054 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1056 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1061 # ifndef HAVE_GETREGS_OLD
1062 # define ptrace_setregset_or_setregs ptrace_setregset
1064 ptrace_setregset(pid_t pid)
1066 # ifdef ARCH_IOVEC_FOR_GETREGSET
1067 /* variable iovec */
1068 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1069 &ARCH_IOVEC_FOR_GETREGSET);
1071 /* constant iovec */
1072 static struct iovec io = {
1073 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1074 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1076 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1079 # endif /* !HAVE_GETREGS_OLD */
1081 #elif defined ARCH_REGS_FOR_GETREGS
1083 # define ptrace_getregset_or_getregs ptrace_getregs
1085 ptrace_getregs(pid_t pid)
1087 # if defined SPARC || defined SPARC64
1088 /* SPARC systems have the meaning of data and addr reversed */
1089 return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1091 return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1095 # ifndef HAVE_GETREGS_OLD
1096 # define ptrace_setregset_or_setregs ptrace_setregs
1098 ptrace_setregs(pid_t pid)
1100 # if defined SPARC || defined SPARC64
1101 /* SPARC systems have the meaning of data and addr reversed */
1102 return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1104 return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1107 # endif /* !HAVE_GETREGS_OLD */
1109 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1111 #ifdef ptrace_getregset_or_getregs
1112 static long get_regs_error;
1116 clear_regs(struct tcb *tcp)
1118 #ifdef ptrace_getregset_or_getregs
1119 get_regs_error = -1;
1124 get_regs(struct tcb *const tcp)
1126 #ifdef ptrace_getregset_or_getregs
1128 if (get_regs_error != -1)
1129 return get_regs_error;
1131 # ifdef HAVE_GETREGS_OLD
1133 * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1134 * fallback to getregs_old.
1136 static int use_getregs_old;
1137 if (use_getregs_old < 0) {
1138 return get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1139 } else if (use_getregs_old == 0) {
1140 get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1141 if (get_regs_error >= 0) {
1142 use_getregs_old = -1;
1143 return get_regs_error;
1145 if (errno == EPERM || errno == ESRCH)
1146 return get_regs_error;
1147 use_getregs_old = 1;
1149 return get_regs_error = getregs_old(tcp);
1150 # else /* !HAVE_GETREGS_OLD */
1151 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1152 return get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1153 # endif /* !HAVE_GETREGS_OLD */
1155 #else /* !ptrace_getregset_or_getregs */
1157 # warning get_regs is not implemented for this architecture yet
1160 #endif /* !ptrace_getregset_or_getregs */
1163 #ifdef ptrace_setregset_or_setregs
1167 return ptrace_setregset_or_setregs(pid);
1169 #endif /* ptrace_setregset_or_setregs */
1174 char buf[sizeof("syscall_%lu") + sizeof(kernel_ulong_t) * 3];
1178 free_sysent_buf(void *ptr)
1180 struct sysent_buf *s = ptr;
1181 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1187 * 0: "ignore this ptrace stop", syscall_entering_decode() should return a "bail
1188 * out silently" code.
1189 * 1: ok, continue in syscall_entering_decode().
1190 * other: error, syscall_entering_decode() should print error indicator
1191 * ("????" etc) and return an appropriate code.
1194 get_scno(struct tcb *tcp)
1196 if (get_regs(tcp) < 0)
1199 int rc = arch_get_scno(tcp);
1203 if (scno_is_valid(tcp->scno)) {
1204 tcp->s_ent = &sysent[tcp->scno];
1205 tcp->qual_flg = qual_flags(tcp->scno);
1207 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1210 s->ent.nargs = MAX_ARGS;
1211 s->ent.sen = SEN_printargs;
1212 s->ent.sys_func = printargs;
1213 s->ent.sys_name = s->buf;
1214 xsprintf(s->buf, "syscall_%" PRI_klu, shuffle_scno(tcp->scno));
1216 tcp->s_ent = &s->ent;
1217 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1219 set_tcb_priv_data(tcp, s, free_sysent_buf);
1221 debug_msg("pid %d invalid syscall %" PRI_kld,
1222 tcp->pid, tcp->scno);
1227 #ifdef ptrace_getregset_or_getregs
1228 # define get_syscall_result_regs get_regs
1230 static int get_syscall_result_regs(struct tcb *);
1234 * 1: ok, continue in syscall_exiting_trace().
1235 * -1: error, syscall_exiting_trace() should print error indicator
1236 * ("????" etc) and bail out.
1239 get_syscall_result(struct tcb *tcp)
1241 if (get_syscall_result_regs(tcp) < 0)
1244 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1249 #include "get_scno.c"
1250 #include "set_scno.c"
1251 #include "get_syscall_args.c"
1252 #ifndef ptrace_getregset_or_getregs
1253 # include "get_syscall_result.c"
1255 #include "get_error.c"
1256 #include "set_error.c"
1257 #ifdef HAVE_GETREGS_OLD
1258 # include "getregs_old.c"
1262 syscall_name(kernel_ulong_t scno)
1264 #if defined X32_PERSONALITY_NUMBER && defined __X32_SYSCALL_BIT
1265 if (current_personality == X32_PERSONALITY_NUMBER)
1266 scno &= ~__X32_SYSCALL_BIT;
1268 return scno_is_valid(scno) ? sysent[scno].sys_name : NULL;