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 # include <asm/psrcompat.h>
68 # define NT_PRSTATUS 1
73 /* Define these shorthand notations to simplify the syscallent files. */
74 #include "sysent_shorthand_defs.h"
76 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
78 const struct_sysent sysent0[] = {
79 #include "syscallent.h"
82 #if SUPPORTED_PERSONALITIES > 1
83 # include PERSONALITY1_INCLUDE_FUNCS
84 static const struct_sysent sysent1[] = {
85 # include "syscallent1.h"
89 #if SUPPORTED_PERSONALITIES > 2
90 # include PERSONALITY2_INCLUDE_FUNCS
91 static const struct_sysent sysent2[] = {
92 # include "syscallent2.h"
96 /* Now undef them since short defines cause wicked namespace pollution. */
97 #include "sysent_shorthand_undefs.h"
100 * `ioctlent[012].h' files are automatically generated by the auxiliary
101 * program `ioctlsort', such that the list is sorted by the `code' field.
102 * This has the side-effect of resolving the _IO.. macros into
103 * plain integers, eliminating the need to include here everything
107 const char *const errnoent0[] = {
108 #include "errnoent.h"
110 const char *const signalent0[] = {
111 #include "signalent.h"
113 const struct_ioctlent ioctlent0[] = {
114 #include "ioctlent0.h"
117 #if SUPPORTED_PERSONALITIES > 1
118 static const char *const errnoent1[] = {
119 # include "errnoent1.h"
121 static const char *const signalent1[] = {
122 # include "signalent1.h"
124 static const struct_ioctlent ioctlent1[] = {
125 # include "ioctlent1.h"
127 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
128 static const struct_printers printers0 = {
129 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
131 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
132 static const struct_printers printers1 = {
133 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
137 #if SUPPORTED_PERSONALITIES > 2
138 static const char *const errnoent2[] = {
139 # include "errnoent2.h"
141 static const char *const signalent2[] = {
142 # include "signalent2.h"
144 static const struct_ioctlent ioctlent2[] = {
145 # include "ioctlent2.h"
147 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
148 static const struct_printers printers2 = {
149 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
154 nsyscalls0 = ARRAY_SIZE(sysent0)
155 #if SUPPORTED_PERSONALITIES > 1
156 , nsyscalls1 = ARRAY_SIZE(sysent1)
157 # if SUPPORTED_PERSONALITIES > 2
158 , nsyscalls2 = ARRAY_SIZE(sysent2)
164 nerrnos0 = ARRAY_SIZE(errnoent0)
165 #if SUPPORTED_PERSONALITIES > 1
166 , nerrnos1 = ARRAY_SIZE(errnoent1)
167 # if SUPPORTED_PERSONALITIES > 2
168 , nerrnos2 = ARRAY_SIZE(errnoent2)
174 nsignals0 = ARRAY_SIZE(signalent0)
175 #if SUPPORTED_PERSONALITIES > 1
176 , nsignals1 = ARRAY_SIZE(signalent1)
177 # if SUPPORTED_PERSONALITIES > 2
178 , nsignals2 = ARRAY_SIZE(signalent2)
184 nioctlents0 = ARRAY_SIZE(ioctlent0)
185 #if SUPPORTED_PERSONALITIES > 1
186 , nioctlents1 = ARRAY_SIZE(ioctlent1)
187 # if SUPPORTED_PERSONALITIES > 2
188 , nioctlents2 = ARRAY_SIZE(ioctlent2)
193 #if SUPPORTED_PERSONALITIES > 1
194 const struct_sysent *sysent = sysent0;
195 const char *const *errnoent = errnoent0;
196 const char *const *signalent = signalent0;
197 const struct_ioctlent *ioctlent = ioctlent0;
198 const struct_printers *printers = &printers0;
201 unsigned nsyscalls = nsyscalls0;
202 unsigned nerrnos = nerrnos0;
203 unsigned nsignals = nsignals0;
204 unsigned nioctlents = nioctlents0;
206 const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES] = {
208 #if SUPPORTED_PERSONALITIES > 1
211 #if SUPPORTED_PERSONALITIES > 2
215 const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
217 #if SUPPORTED_PERSONALITIES > 1
220 #if SUPPORTED_PERSONALITIES > 2
225 #if SUPPORTED_PERSONALITIES > 1
226 unsigned current_personality;
228 # ifndef current_wordsize
229 unsigned current_wordsize;
230 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
231 PERSONALITY0_WORDSIZE,
232 PERSONALITY1_WORDSIZE,
233 # if SUPPORTED_PERSONALITIES > 2
234 PERSONALITY2_WORDSIZE,
239 # ifndef current_klongsize
240 unsigned current_klongsize;
241 static const int personality_klongsize[SUPPORTED_PERSONALITIES] = {
242 PERSONALITY0_KLONGSIZE,
243 PERSONALITY1_KLONGSIZE,
244 # if SUPPORTED_PERSONALITIES > 2
245 PERSONALITY2_KLONGSIZE,
251 set_personality(int personality)
253 nsyscalls = nsyscall_vec[personality];
254 sysent = sysent_vec[personality];
256 switch (personality) {
258 errnoent = errnoent0;
260 ioctlent = ioctlent0;
261 nioctlents = nioctlents0;
262 signalent = signalent0;
263 nsignals = nsignals0;
264 printers = &printers0;
268 errnoent = errnoent1;
270 ioctlent = ioctlent1;
271 nioctlents = nioctlents1;
272 signalent = signalent1;
273 nsignals = nsignals1;
274 printers = &printers1;
277 # if SUPPORTED_PERSONALITIES > 2
279 errnoent = errnoent2;
281 ioctlent = ioctlent2;
282 nioctlents = nioctlents2;
283 signalent = signalent2;
284 nsignals = nsignals2;
285 printers = &printers2;
290 current_personality = personality;
291 # ifndef current_wordsize
292 current_wordsize = personality_wordsize[personality];
294 # ifndef current_klongsize
295 current_klongsize = personality_klongsize[personality];
300 update_personality(struct tcb *tcp, unsigned int personality)
302 if (personality == current_personality)
304 set_personality(personality);
306 if (personality == tcp->currpers)
308 tcp->currpers = personality;
310 # undef PERSONALITY_NAMES
312 # define PERSONALITY_NAMES {"64 bit", "32 bit", "x32"}
314 # define PERSONALITY_NAMES {"x32", "32 bit"}
315 # elif SUPPORTED_PERSONALITIES == 2
316 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
318 # ifdef PERSONALITY_NAMES
320 static const char *const names[] = PERSONALITY_NAMES;
321 error_msg("[ Process PID=%d runs in %s mode. ]",
322 tcp->pid, names[personality]);
328 #ifdef SYS_socket_subcall
330 decode_socket_subcall(struct tcb *tcp)
332 const int call = tcp->u_arg[0];
334 if (call < 1 || call >= SYS_socket_nsubcalls)
337 const kernel_ulong_t scno = SYS_socket_subcall + call;
338 const unsigned int nargs = sysent[scno].nargs;
341 if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
345 tcp->qual_flg = qual_flags(scno);
346 tcp->s_ent = &sysent[scno];
349 for (i = 0; i < nargs; ++i)
350 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
351 ? ((uint32_t *) (void *) buf)[i] : buf[i];
355 #ifdef SYS_ipc_subcall
357 decode_ipc_subcall(struct tcb *tcp)
359 unsigned int call = tcp->u_arg[0];
360 const unsigned int version = call >> 16;
363 # if defined S390 || defined S390X
367 if (current_wordsize == 8)
370 set_tcb_priv_ulong(tcp, version);
376 case 1: case 2: case 3: case 4:
377 case 11: case 12: case 13: case 14:
378 case 21: case 22: case 23: case 24:
384 tcp->scno = SYS_ipc_subcall + call;
385 tcp->qual_flg = qual_flags(tcp->scno);
386 tcp->s_ent = &sysent[tcp->scno];
388 const unsigned int n = tcp->s_ent->nargs;
390 for (i = 0; i < n; i++)
391 tcp->u_arg[i] = tcp->u_arg[i + 1];
397 decode_mips_subcall(struct tcb *tcp)
399 if (!scno_is_valid(tcp->u_arg[0]))
401 tcp->scno = tcp->u_arg[0];
402 tcp->qual_flg = qual_flags(tcp->scno);
403 tcp->s_ent = &sysent[tcp->scno];
404 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
405 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
407 * Fetching the last arg of 7-arg syscalls (fadvise64_64
408 * and sync_file_range) requires additional code,
409 * see linux/mips/get_syscall_args.c
411 if (tcp->s_ent->nargs == MAX_ARGS) {
413 mips_REG_SP + MAX_ARGS * sizeof(tcp->u_arg[0]),
414 sizeof(tcp->u_arg[0]),
415 &tcp->u_arg[MAX_ARGS - 1]) < 0)
416 tcp->u_arg[MAX_ARGS - 1] = 0;
419 #endif /* LINUX_MIPSO32 */
422 dumpio(struct tcb *tcp)
427 int fd = tcp->u_arg[0];
431 if (is_number_in_set(fd, read_set)) {
432 switch (tcp->s_ent->sen) {
437 case SEN_mq_timedreceive:
438 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
443 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
447 dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
450 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
454 if (is_number_in_set(fd, write_set)) {
455 switch (tcp->s_ent->sen) {
460 case SEN_mq_timedsend:
461 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
467 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1);
470 dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1);
473 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
480 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
481 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
483 static kernel_ulong_t
484 shuffle_scno(kernel_ulong_t scno)
486 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
487 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
490 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
491 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
493 if (scno == 0x000ffff0)
494 return ARM_FIRST_SHUFFLED_SYSCALL;
496 # define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
498 * Is it ARM specific syscall?
499 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
500 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
502 if (scno >= 0x000f0000 &&
503 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
504 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
506 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
507 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
509 #endif /* ARM || AARCH64 */
515 err_name(unsigned long err)
517 if ((err < nerrnos) && errnoent[err])
518 return errnoent[err];
523 static long get_regs_error;
531 static void get_regs(pid_t pid);
532 static int get_syscall_args(struct tcb *);
533 static int get_syscall_result(struct tcb *);
534 static int arch_get_scno(struct tcb *tcp);
535 static int arch_set_scno(struct tcb *, kernel_ulong_t);
536 static void get_error(struct tcb *, const bool);
537 static int arch_set_error(struct tcb *);
538 static int arch_set_success(struct tcb *);
540 struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
542 static struct inject_opts *
543 tcb_inject_opts(struct tcb *tcp)
545 return (scno_in_range(tcp->scno) && tcp->inject_vec[current_personality])
546 ? &tcp->inject_vec[current_personality][tcp->scno] : NULL;
551 tamper_with_syscall_entering(struct tcb *tcp, unsigned int *signo)
553 if (!tcp->inject_vec[current_personality]) {
554 tcp->inject_vec[current_personality] =
555 xcalloc(nsyscalls, sizeof(**inject_vec));
556 memcpy(tcp->inject_vec[current_personality],
557 inject_vec[current_personality],
558 nsyscalls * sizeof(**inject_vec));
561 struct inject_opts *opts = tcb_inject_opts(tcp);
563 if (!opts || opts->first == 0)
568 if (opts->first != 0)
571 opts->first = opts->step;
573 if (opts->data.flags & INJECT_F_SIGNAL)
574 *signo = opts->data.signo;
575 if (opts->data.flags & INJECT_F_RETVAL && !arch_set_scno(tcp, -1))
576 tcp->flags |= TCB_TAMPERED;
582 tamper_with_syscall_exiting(struct tcb *tcp)
584 struct inject_opts *opts = tcb_inject_opts(tcp);
589 if (opts->data.rval >= 0) {
590 kernel_long_t u_rval = tcp->u_rval;
592 tcp->u_rval = opts->data.rval;
593 if (arch_set_success(tcp)) {
594 tcp->u_rval = u_rval;
599 unsigned long new_error = -opts->data.rval;
601 if (new_error != tcp->u_error && new_error <= MAX_ERRNO_VALUE) {
602 unsigned long u_error = tcp->u_error;
604 tcp->u_error = new_error;
605 if (arch_set_error(tcp)) {
606 tcp->u_error = u_error;
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.
640 if (SEN_syscall == tcp->s_ent->sen)
641 decode_mips_subcall(tcp);
644 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
645 switch (tcp->s_ent->sen) {
646 # ifdef SYS_socket_subcall
648 decode_socket_subcall(tcp);
651 # ifdef SYS_ipc_subcall
653 decode_ipc_subcall(tcp);
663 syscall_entering_trace(struct tcb *tcp, unsigned int *sig)
665 /* Restrain from fault injection while the trace executes strace code. */
667 tcp->qual_flg &= ~QUAL_INJECT;
670 switch (tcp->s_ent->sen) {
673 #if defined SPARC || defined SPARC64
676 tcp->flags &= ~TCB_HIDE_LOG;
680 if (!traced(tcp) || (tracing_paths && !pathtrace_match(tcp))) {
681 tcp->flags |= TCB_FILTERED;
685 tcp->flags &= ~TCB_FILTERED;
692 tamper_with_syscall_entering(tcp, sig);
694 if (cflag == CFLAG_ONLY_STATS) {
699 if (stack_trace_enabled) {
700 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
701 unwind_capture_stacktrace(tcp);
706 tprintf("%s(", tcp->s_ent->sys_name);
707 int res = raw(tcp) ? printargs(tcp) : tcp->s_ent->sys_func(tcp);
713 syscall_entering_finish(struct tcb *tcp, int res)
715 tcp->flags |= TCB_INSYSCALL;
716 tcp->sys_func_rval = res;
717 /* Measure the entrance time as late as possible to avoid errors. */
718 if ((Tflag || cflag) && !filtered(tcp))
719 gettimeofday(&tcp->etime, NULL);
723 syscall_tampered(struct tcb *tcp)
725 return tcp->flags & TCB_TAMPERED;
731 * -1: error in one of ptrace ops.
733 * If not 0, call syscall_exiting_trace(tcp, res), where res is the return
734 * value. Anyway, call syscall_exiting_finish(tcp) then.
737 syscall_exiting_decode(struct tcb *tcp, struct timeval *ptv)
739 /* Measure the exit time as early as possible to avoid errors. */
740 if ((Tflag || cflag) && !(filtered(tcp) || hide_log(tcp)))
741 gettimeofday(ptv, NULL);
744 if (stack_trace_enabled) {
745 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
746 unwind_cache_invalidate(tcp);
750 if (filtered(tcp) || hide_log(tcp))
754 #if SUPPORTED_PERSONALITIES > 1
755 update_personality(tcp, tcp->currpers);
757 return get_regs_error ? -1 : get_syscall_result(tcp);
761 syscall_exiting_trace(struct tcb *tcp, struct timeval tv, int res)
763 if (syserror(tcp) && syscall_tampered(tcp))
764 tamper_with_syscall_exiting(tcp);
767 count_syscall(tcp, &tv);
768 if (cflag == CFLAG_ONLY_STATS) {
773 /* If not in -ff mode, and printing_tcp != tcp,
774 * then the log currently does not end with output
775 * of _our syscall entry_, but with something else.
776 * We need to say which syscall's return is this.
778 * Forced reprinting via TCB_REPRINT is used only by
779 * "strace -ff -oLOG test/threaded_execve" corner case.
780 * It's the only case when -ff mode needs reprinting.
782 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
783 tcp->flags &= ~TCB_REPRINT;
785 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
789 tcp->s_prev_ent = NULL;
791 /* There was error in one of prior ptrace ops */
794 tprints("= ? <unavailable>\n");
798 tcp->s_prev_ent = tcp->s_ent;
802 /* sys_res = printargs(tcp); - but it's nop on sysexit */
804 /* FIXME: not_failing_only (IOW, option -z) is broken:
805 * failure of syscall is known only after syscall return.
806 * Thus we end up with something like this on, say, ENOENT:
807 * open("does_not_exist", O_RDONLY <unfinished ...>
808 * {next syscall decode}
809 * whereas the intended result is that open(...) line
810 * is not shown at all.
812 if (not_failing_only && tcp->u_error)
813 return 0; /* ignore failed syscalls */
814 if (tcp->sys_func_rval & RVAL_DECODED)
815 sys_res = tcp->sys_func_rval;
817 sys_res = tcp->s_ent->sys_func(tcp);
822 unsigned long u_error = tcp->u_error;
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_error_str = err_name(u_error);
892 tprintf("= -1 %s (%s)",
893 u_error_str, strerror(u_error));
895 tprintf("= -1 %lu (%s)",
896 u_error, strerror(u_error));
899 if (syscall_tampered(tcp))
900 tprints(" (INJECTED)");
901 if ((sys_res & RVAL_STR) && tcp->auxstr)
902 tprintf(" (%s)", tcp->auxstr);
904 if (sys_res & RVAL_NONE)
907 switch (sys_res & RVAL_MASK) {
909 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
910 if (current_wordsize < sizeof(tcp->u_rval)) {
912 (unsigned int) tcp->u_rval);
916 tprintf("= %#" PRI_klx, tcp->u_rval);
921 print_numeric_long_umask(tcp->u_rval);
924 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
925 if (current_wordsize < sizeof(tcp->u_rval)) {
927 (unsigned int) tcp->u_rval);
931 tprintf("= %" PRI_klu, tcp->u_rval);
935 tprintf("= %" PRI_kld, tcp->u_rval);
940 printfd(tcp, tcp->u_rval);
942 tprintf("= %" PRI_kld, tcp->u_rval);
945 error_msg("invalid rval format");
949 if ((sys_res & RVAL_STR) && tcp->auxstr)
950 tprintf(" (%s)", tcp->auxstr);
951 if (syscall_tampered(tcp))
952 tprints(" (INJECTED)");
955 tv_sub(&tv, &tv, &tcp->etime);
956 tprintf(" <%ld.%06ld>",
957 (long) tv.tv_sec, (long) tv.tv_usec);
964 if (stack_trace_enabled)
965 unwind_print_stacktrace(tcp);
971 syscall_exiting_finish(struct tcb *tcp)
973 tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED);
974 tcp->sys_func_rval = 0;
975 free_tcb_priv_data(tcp);
979 is_erestart(struct tcb *tcp)
981 switch (tcp->u_error) {
985 case ERESTART_RESTARTBLOCK:
992 static unsigned long saved_u_error;
995 temporarily_clear_syserror(struct tcb *tcp)
997 saved_u_error = tcp->u_error;
1002 restore_cleared_syserror(struct tcb *tcp)
1004 tcp->u_error = saved_u_error;
1007 #include "arch_regs.c"
1009 #ifdef HAVE_GETRVAL2
1010 # include "arch_getrval2.c"
1014 print_pc(struct tcb *tcp)
1016 #if defined ARCH_PC_REG
1017 # define ARCH_GET_PC 0
1018 #elif defined ARCH_PC_PEEK_ADDR
1020 # define ARCH_PC_REG pc
1021 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
1023 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1026 if (get_regs_error || 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 */
1114 #undef USE_GET_SYSCALL_RESULT_REGS
1115 #ifdef ptrace_getregset_or_getregs
1117 if (get_regs_error != -1)
1120 # ifdef HAVE_GETREGS_OLD
1122 * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1123 * fallback to getregs_old.
1125 static int use_getregs_old;
1126 if (use_getregs_old < 0) {
1127 get_regs_error = ptrace_getregset_or_getregs(pid);
1129 } else if (use_getregs_old == 0) {
1130 get_regs_error = ptrace_getregset_or_getregs(pid);
1131 if (get_regs_error >= 0) {
1132 use_getregs_old = -1;
1135 if (errno == EPERM || errno == ESRCH)
1137 use_getregs_old = 1;
1139 get_regs_error = getregs_old(pid);
1140 # else /* !HAVE_GETREGS_OLD */
1141 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1142 get_regs_error = ptrace_getregset_or_getregs(pid);
1143 # endif /* !HAVE_GETREGS_OLD */
1145 #else /* !ptrace_getregset_or_getregs */
1147 # define USE_GET_SYSCALL_RESULT_REGS 1
1148 # warning get_regs is not implemented for this architecture yet
1151 #endif /* !ptrace_getregset_or_getregs */
1154 #ifdef ptrace_setregset_or_setregs
1158 return ptrace_setregset_or_setregs(pid);
1160 #endif /* ptrace_setregset_or_setregs */
1165 char buf[sizeof("syscall_%lu") + sizeof(kernel_ulong_t) * 3];
1169 free_sysent_buf(void *ptr)
1171 struct sysent_buf *s = ptr;
1172 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1178 * 0: "ignore this ptrace stop", syscall_entering_decode() should return a "bail
1179 * out silently" code.
1180 * 1: ok, continue in syscall_entering_decode().
1181 * other: error, syscall_entering_decode() should print error indicator
1182 * ("????" etc) and return an appropriate code.
1185 get_scno(struct tcb *tcp)
1192 int rc = arch_get_scno(tcp);
1196 if (scno_is_valid(tcp->scno)) {
1197 tcp->s_ent = &sysent[tcp->scno];
1198 tcp->qual_flg = qual_flags(tcp->scno);
1200 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1203 s->ent.nargs = MAX_ARGS;
1204 s->ent.sen = SEN_printargs;
1205 s->ent.sys_func = printargs;
1206 s->ent.sys_name = s->buf;
1207 sprintf(s->buf, "syscall_%" PRI_klu, shuffle_scno(tcp->scno));
1209 tcp->s_ent = &s->ent;
1210 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1212 set_tcb_priv_data(tcp, s, free_sysent_buf);
1215 error_msg("pid %d invalid syscall %" PRI_kld,
1216 tcp->pid, tcp->scno);
1221 #ifdef USE_GET_SYSCALL_RESULT_REGS
1222 static int get_syscall_result_regs(struct tcb *);
1226 * 1: ok, continue in syscall_exiting_trace().
1227 * -1: error, syscall_exiting_trace() should print error indicator
1228 * ("????" etc) and bail out.
1231 get_syscall_result(struct tcb *tcp)
1233 #ifdef USE_GET_SYSCALL_RESULT_REGS
1234 if (get_syscall_result_regs(tcp))
1238 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1243 #include "get_scno.c"
1244 #include "set_scno.c"
1245 #include "get_syscall_args.c"
1246 #ifdef USE_GET_SYSCALL_RESULT_REGS
1247 # include "get_syscall_result.c"
1249 #include "get_error.c"
1250 #include "set_error.c"
1251 #ifdef HAVE_GETREGS_OLD
1252 # include "getregs_old.c"
1256 syscall_name(kernel_ulong_t scno)
1258 #if defined X32_PERSONALITY_NUMBER && defined __X32_SYSCALL_BIT
1259 if (current_personality == X32_PERSONALITY_NUMBER)
1260 scno &= ~__X32_SYSCALL_BIT;
1262 return scno_is_valid(scno) ? sysent[scno].sys_name : NULL;