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 if (personality == current_personality)
308 set_personality(personality);
310 if (personality == tcp->currpers)
312 tcp->currpers = personality;
315 error_msg("[ Process PID=%d runs in %s mode. ]",
316 tcp->pid, personality_names[personality]);
321 #ifdef SYS_socket_subcall
323 decode_socket_subcall(struct tcb *tcp)
325 const int call = tcp->u_arg[0];
327 if (call < 1 || call >= SYS_socket_nsubcalls)
330 const kernel_ulong_t scno = SYS_socket_subcall + call;
331 const unsigned int nargs = sysent[scno].nargs;
334 if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
338 tcp->qual_flg = qual_flags(scno);
339 tcp->s_ent = &sysent[scno];
342 for (i = 0; i < nargs; ++i)
343 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
344 ? ((uint32_t *) (void *) buf)[i] : buf[i];
346 #endif /* SYS_socket_subcall */
348 #ifdef SYS_ipc_subcall
350 decode_ipc_subcall(struct tcb *tcp)
352 unsigned int call = tcp->u_arg[0];
353 const unsigned int version = call >> 16;
356 # if defined S390 || defined S390X
360 if (current_wordsize == 8)
363 set_tcb_priv_ulong(tcp, version);
369 case 1: case 2: case 3: case 4:
370 case 11: case 12: case 13: case 14:
371 case 21: case 22: case 23: case 24:
377 tcp->scno = SYS_ipc_subcall + call;
378 tcp->qual_flg = qual_flags(tcp->scno);
379 tcp->s_ent = &sysent[tcp->scno];
381 const unsigned int n = tcp->s_ent->nargs;
383 for (i = 0; i < n; i++)
384 tcp->u_arg[i] = tcp->u_arg[i + 1];
386 #endif /* SYS_ipc_subcall */
388 #ifdef SYS_syscall_subcall
390 decode_syscall_subcall(struct tcb *tcp)
392 if (!scno_is_valid(tcp->u_arg[0]))
394 tcp->scno = tcp->u_arg[0];
395 tcp->qual_flg = qual_flags(tcp->scno);
396 tcp->s_ent = &sysent[tcp->scno];
397 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
398 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
399 # ifdef LINUX_MIPSO32
401 * Fetching the last arg of 7-arg syscalls (fadvise64_64
402 * and sync_file_range) requires additional code,
403 * see linux/mips/get_syscall_args.c
405 if (tcp->s_ent->nargs == MAX_ARGS) {
407 mips_REG_SP + MAX_ARGS * sizeof(tcp->u_arg[0]),
408 sizeof(tcp->u_arg[0]),
409 &tcp->u_arg[MAX_ARGS - 1]) < 0)
410 tcp->u_arg[MAX_ARGS - 1] = 0;
412 # endif /* LINUX_MIPSO32 */
414 #endif /* SYS_syscall_subcall */
417 dumpio(struct tcb *tcp)
422 int fd = tcp->u_arg[0];
426 if (is_number_in_set(fd, read_set)) {
427 switch (tcp->s_ent->sen) {
432 case SEN_mq_timedreceive:
433 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
438 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
442 dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
445 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
449 if (is_number_in_set(fd, write_set)) {
450 switch (tcp->s_ent->sen) {
455 case SEN_mq_timedsend:
456 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
462 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1);
465 dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1);
468 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
475 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
476 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
478 static kernel_ulong_t
479 shuffle_scno(kernel_ulong_t scno)
481 #ifdef ARM_FIRST_SHUFFLED_SYSCALL /* So far only 32-bit ARM needs this */
482 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
485 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
486 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
488 if (scno == 0x000ffff0)
489 return ARM_FIRST_SHUFFLED_SYSCALL;
491 # define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
493 * Is it ARM specific syscall?
494 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
495 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
497 if (scno >= 0x000f0000 &&
498 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
499 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
501 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
502 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
504 #endif /* ARM_FIRST_SHUFFLED_SYSCALL */
510 err_name(unsigned long err)
512 if ((err < nerrnos) && errnoent[err])
513 return errnoent[err];
518 static long get_regs(struct tcb *);
519 static int get_syscall_args(struct tcb *);
520 static int get_syscall_result(struct tcb *);
521 static int arch_get_scno(struct tcb *tcp);
522 static int arch_set_scno(struct tcb *, kernel_ulong_t);
523 static void get_error(struct tcb *, const bool);
524 static int arch_set_error(struct tcb *);
525 static int arch_set_success(struct tcb *);
527 struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
529 static struct inject_opts *
530 tcb_inject_opts(struct tcb *tcp)
532 return (scno_in_range(tcp->scno) && tcp->inject_vec[current_personality])
533 ? &tcp->inject_vec[current_personality][tcp->scno] : NULL;
538 tamper_with_syscall_entering(struct tcb *tcp, unsigned int *signo)
540 if (!tcp->inject_vec[current_personality]) {
541 tcp->inject_vec[current_personality] =
542 xcalloc(nsyscalls, sizeof(**inject_vec));
543 memcpy(tcp->inject_vec[current_personality],
544 inject_vec[current_personality],
545 nsyscalls * sizeof(**inject_vec));
548 struct inject_opts *opts = tcb_inject_opts(tcp);
550 if (!opts || opts->first == 0)
555 if (opts->first != 0)
558 opts->first = opts->step;
560 if (opts->data.flags & INJECT_F_SIGNAL)
561 *signo = opts->data.signo;
562 if (opts->data.flags & INJECT_F_RETVAL && !arch_set_scno(tcp, -1))
563 tcp->flags |= TCB_TAMPERED;
569 tamper_with_syscall_exiting(struct tcb *tcp)
571 struct inject_opts *opts = tcb_inject_opts(tcp);
576 if (opts->data.rval >= 0) {
577 kernel_long_t u_rval = tcp->u_rval;
579 tcp->u_rval = opts->data.rval;
580 if (arch_set_success(tcp)) {
581 tcp->u_rval = u_rval;
586 unsigned long new_error = -opts->data.rval;
588 if (new_error != tcp->u_error && new_error <= MAX_ERRNO_VALUE) {
589 unsigned long u_error = tcp->u_error;
591 tcp->u_error = new_error;
592 if (arch_set_error(tcp)) {
593 tcp->u_error = u_error;
603 * 0: "ignore this ptrace stop", bail out silently.
604 * 1: ok, decoded; call
605 * syscall_entering_finish(tcp, syscall_entering_trace(tcp, ...)).
606 * other: error; call syscall_entering_finish(tcp, res), where res is the value
610 syscall_entering_decode(struct tcb *tcp)
612 int res = get_scno(tcp);
616 if (res != 1 || (res = get_syscall_args(tcp)) != 1) {
618 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
620 * " <unavailable>" will be added later by the code which
621 * detects ptrace errors.
626 #if defined SYS_ipc_subcall \
627 || defined SYS_socket_subcall \
628 || defined SYS_syscall_subcall
630 switch (tcp->s_ent->sen) {
631 # ifdef SYS_ipc_subcall
633 decode_ipc_subcall(tcp);
636 # ifdef SYS_socket_subcall
638 decode_socket_subcall(tcp);
641 # ifdef SYS_syscall_subcall
643 decode_syscall_subcall(tcp);
644 if (tcp->s_ent->sen != SEN_syscall)
657 syscall_entering_trace(struct tcb *tcp, unsigned int *sig)
659 /* Restrain from fault injection while the trace executes strace code. */
661 tcp->qual_flg &= ~QUAL_INJECT;
664 switch (tcp->s_ent->sen) {
667 #if defined SPARC || defined SPARC64
670 tcp->flags &= ~TCB_HIDE_LOG;
674 if (!traced(tcp) || (tracing_paths && !pathtrace_match(tcp))) {
675 tcp->flags |= TCB_FILTERED;
679 tcp->flags &= ~TCB_FILTERED;
686 tamper_with_syscall_entering(tcp, sig);
688 if (cflag == CFLAG_ONLY_STATS) {
693 if (stack_trace_enabled) {
694 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
695 unwind_capture_stacktrace(tcp);
700 tprintf("%s(", tcp->s_ent->sys_name);
701 int res = raw(tcp) ? printargs(tcp) : tcp->s_ent->sys_func(tcp);
707 syscall_entering_finish(struct tcb *tcp, int res)
709 tcp->flags |= TCB_INSYSCALL;
710 tcp->sys_func_rval = res;
711 /* Measure the entrance time as late as possible to avoid errors. */
712 if ((Tflag || cflag) && !filtered(tcp))
713 gettimeofday(&tcp->etime, NULL);
719 * -1: error in one of ptrace ops.
721 * If not 0, call syscall_exiting_trace(tcp, res), where res is the return
722 * value. Anyway, call syscall_exiting_finish(tcp) then.
725 syscall_exiting_decode(struct tcb *tcp, struct timeval *ptv)
727 /* Measure the exit time as early as possible to avoid errors. */
728 if ((Tflag || cflag) && !(filtered(tcp) || hide_log(tcp)))
729 gettimeofday(ptv, NULL);
732 if (stack_trace_enabled) {
733 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
734 unwind_cache_invalidate(tcp);
738 if (filtered(tcp) || hide_log(tcp))
741 #if SUPPORTED_PERSONALITIES > 1
742 update_personality(tcp, tcp->currpers);
745 return get_syscall_result(tcp);
749 syscall_exiting_trace(struct tcb *tcp, struct timeval tv, int res)
751 if (syserror(tcp) && syscall_tampered(tcp))
752 tamper_with_syscall_exiting(tcp);
755 count_syscall(tcp, &tv);
756 if (cflag == CFLAG_ONLY_STATS) {
761 /* If not in -ff mode, and printing_tcp != tcp,
762 * then the log currently does not end with output
763 * of _our syscall entry_, but with something else.
764 * We need to say which syscall's return is this.
766 * Forced reprinting via TCB_REPRINT is used only by
767 * "strace -ff -oLOG test/threaded_execve" corner case.
768 * It's the only case when -ff mode needs reprinting.
770 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
771 tcp->flags &= ~TCB_REPRINT;
773 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
777 tcp->s_prev_ent = NULL;
779 /* There was error in one of prior ptrace ops */
782 tprints("= ? <unavailable>\n");
786 tcp->s_prev_ent = tcp->s_ent;
790 /* sys_res = printargs(tcp); - but it's nop on sysexit */
792 /* FIXME: not_failing_only (IOW, option -z) is broken:
793 * failure of syscall is known only after syscall return.
794 * Thus we end up with something like this on, say, ENOENT:
795 * open("does_not_exist", O_RDONLY <unfinished ...>
796 * {next syscall decode}
797 * whereas the intended result is that open(...) line
798 * is not shown at all.
800 if (not_failing_only && tcp->u_error)
801 return 0; /* ignore failed syscalls */
802 if (tcp->sys_func_rval & RVAL_DECODED)
803 sys_res = tcp->sys_func_rval;
805 sys_res = tcp->s_ent->sys_func(tcp);
810 unsigned long u_error = tcp->u_error;
811 kernel_long_t u_rval;
815 tprintf("= -1 (errno %lu)", u_error);
817 tprintf("= %#" PRI_klx, tcp->u_rval);
819 if (syscall_tampered(tcp))
820 tprints(" (INJECTED)");
821 } else if (!(sys_res & RVAL_NONE) && u_error) {
822 const char *u_error_str;
825 /* Blocked signals do not interrupt any syscalls.
826 * In this case syscalls don't return ERESTARTfoo codes.
828 * Deadly signals set to SIG_DFL interrupt syscalls
829 * and kill the process regardless of which of the codes below
830 * is returned by the interrupted syscall.
831 * In some cases, kernel forces a kernel-generated deadly
832 * signal to be unblocked and set to SIG_DFL (and thus cause
833 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
834 * or SIGILL. (The alternative is to leave process spinning
835 * forever on the faulty instruction - not useful).
837 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
838 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
839 * but kernel will always restart them.
842 /* Most common type of signal-interrupted syscall exit code.
843 * The system call will be restarted with the same arguments
844 * if SA_RESTART is set; otherwise, it will fail with EINTR.
846 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
849 /* Rare. For example, fork() returns this if interrupted.
850 * SA_RESTART is ignored (assumed set): the restart is unconditional.
852 tprints("= ? ERESTARTNOINTR (To be restarted)");
855 /* pause(), rt_sigsuspend() etc use this code.
856 * SA_RESTART is ignored (assumed not set):
857 * syscall won't restart (will return EINTR instead)
858 * even after signal with SA_RESTART set. However,
859 * after SIG_IGN or SIG_DFL signal it will restart
860 * (thus the name "restart only if has no handler").
862 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
864 case ERESTART_RESTARTBLOCK:
865 /* Syscalls like nanosleep(), poll() which can't be
866 * restarted with their original arguments use this
867 * code. Kernel will execute restart_syscall() instead,
868 * which changes arguments before restarting syscall.
869 * SA_RESTART is ignored (assumed not set) similarly
870 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
871 * since restart data is saved in "restart block"
872 * in task struct, and if signal handler uses a syscall
873 * which in turn saves another such restart block,
874 * old data is lost and restart becomes impossible)
876 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
879 u_rval = sys_res & RVAL_PRINT_ERR_VAL ?
881 u_error_str = err_name(u_error);
883 tprintf("= %" PRI_kld " %s (%s)",
884 u_rval, u_error_str, strerror(u_error));
886 tprintf("= %" PRI_kld " %lu (%s)",
887 u_rval, u_error, strerror(u_error));
890 if (syscall_tampered(tcp))
891 tprints(" (INJECTED)");
892 if ((sys_res & RVAL_STR) && tcp->auxstr)
893 tprintf(" (%s)", tcp->auxstr);
895 if (sys_res & RVAL_NONE)
898 switch (sys_res & RVAL_MASK) {
900 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
901 if (current_wordsize < sizeof(tcp->u_rval)) {
903 (unsigned int) tcp->u_rval);
907 tprintf("= %#" PRI_klx, tcp->u_rval);
912 print_numeric_long_umask(tcp->u_rval);
915 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
916 if (current_wordsize < sizeof(tcp->u_rval)) {
918 (unsigned int) tcp->u_rval);
922 tprintf("= %" PRI_klu, tcp->u_rval);
926 tprintf("= %" PRI_kld, tcp->u_rval);
931 printfd(tcp, tcp->u_rval);
933 tprintf("= %" PRI_kld, tcp->u_rval);
936 error_msg("invalid rval format");
940 if ((sys_res & RVAL_STR) && tcp->auxstr)
941 tprintf(" (%s)", tcp->auxstr);
942 if (syscall_tampered(tcp))
943 tprints(" (INJECTED)");
946 tv_sub(&tv, &tv, &tcp->etime);
947 tprintf(" <%ld.%06ld>",
948 (long) tv.tv_sec, (long) tv.tv_usec);
955 if (stack_trace_enabled)
956 unwind_print_stacktrace(tcp);
962 syscall_exiting_finish(struct tcb *tcp)
964 tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED);
965 tcp->sys_func_rval = 0;
966 free_tcb_priv_data(tcp);
970 is_erestart(struct tcb *tcp)
972 switch (tcp->u_error) {
976 case ERESTART_RESTARTBLOCK:
983 static unsigned long saved_u_error;
986 temporarily_clear_syserror(struct tcb *tcp)
988 saved_u_error = tcp->u_error;
993 restore_cleared_syserror(struct tcb *tcp)
995 tcp->u_error = saved_u_error;
998 #include "arch_regs.c"
1000 #ifdef HAVE_GETRVAL2
1001 # include "arch_getrval2.c"
1005 print_pc(struct tcb *tcp)
1007 #if defined ARCH_PC_REG
1008 # define ARCH_GET_PC 0
1009 #elif defined ARCH_PC_PEEK_ADDR
1011 # define ARCH_PC_REG pc
1012 # define ARCH_GET_PC upeek(tcp, ARCH_PC_PEEK_ADDR, &pc)
1014 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1016 if (get_regs(tcp) < 0 || ARCH_GET_PC)
1017 tprints(current_wordsize == 4 ? "[????????] "
1018 : "[????????????????] ");
1020 tprintf(current_wordsize == 4
1021 ? "[%08" PRI_klx "] " : "[%016" PRI_klx "] ",
1022 (kernel_ulong_t) ARCH_PC_REG);
1025 #include "getregs_old.h"
1027 #undef ptrace_getregset_or_getregs
1028 #undef ptrace_setregset_or_setregs
1029 #ifdef ARCH_REGS_FOR_GETREGSET
1031 # define ptrace_getregset_or_getregs ptrace_getregset
1033 ptrace_getregset(pid_t pid)
1035 # ifdef ARCH_IOVEC_FOR_GETREGSET
1036 /* variable iovec */
1037 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1038 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1039 &ARCH_IOVEC_FOR_GETREGSET);
1041 /* constant iovec */
1042 static struct iovec io = {
1043 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1044 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1046 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1051 # ifndef HAVE_GETREGS_OLD
1052 # define ptrace_setregset_or_setregs ptrace_setregset
1054 ptrace_setregset(pid_t pid)
1056 # ifdef ARCH_IOVEC_FOR_GETREGSET
1057 /* variable iovec */
1058 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1059 &ARCH_IOVEC_FOR_GETREGSET);
1061 /* constant iovec */
1062 static struct iovec io = {
1063 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1064 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1066 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1069 # endif /* !HAVE_GETREGS_OLD */
1071 #elif defined ARCH_REGS_FOR_GETREGS
1073 # define ptrace_getregset_or_getregs ptrace_getregs
1075 ptrace_getregs(pid_t pid)
1077 # if defined SPARC || defined SPARC64
1078 /* SPARC systems have the meaning of data and addr reversed */
1079 return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1081 return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1085 # ifndef HAVE_GETREGS_OLD
1086 # define ptrace_setregset_or_setregs ptrace_setregs
1088 ptrace_setregs(pid_t pid)
1090 # if defined SPARC || defined SPARC64
1091 /* SPARC systems have the meaning of data and addr reversed */
1092 return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1094 return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1097 # endif /* !HAVE_GETREGS_OLD */
1099 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1101 #ifdef ptrace_getregset_or_getregs
1102 static long get_regs_error;
1106 clear_regs(struct tcb *tcp)
1108 #ifdef ptrace_getregset_or_getregs
1109 get_regs_error = -1;
1114 get_regs(struct tcb *const tcp)
1116 #ifdef ptrace_getregset_or_getregs
1118 if (get_regs_error != -1)
1119 return get_regs_error;
1121 # ifdef HAVE_GETREGS_OLD
1123 * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1124 * fallback to getregs_old.
1126 static int use_getregs_old;
1127 if (use_getregs_old < 0) {
1128 return get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1129 } else if (use_getregs_old == 0) {
1130 get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1131 if (get_regs_error >= 0) {
1132 use_getregs_old = -1;
1133 return get_regs_error;
1135 if (errno == EPERM || errno == ESRCH)
1136 return get_regs_error;
1137 use_getregs_old = 1;
1139 return get_regs_error = getregs_old(tcp);
1140 # else /* !HAVE_GETREGS_OLD */
1141 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1142 return get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1143 # endif /* !HAVE_GETREGS_OLD */
1145 #else /* !ptrace_getregset_or_getregs */
1147 # warning get_regs is not implemented for this architecture yet
1150 #endif /* !ptrace_getregset_or_getregs */
1153 #ifdef ptrace_setregset_or_setregs
1157 return ptrace_setregset_or_setregs(pid);
1159 #endif /* ptrace_setregset_or_setregs */
1164 char buf[sizeof("syscall_%lu") + sizeof(kernel_ulong_t) * 3];
1168 free_sysent_buf(void *ptr)
1170 struct sysent_buf *s = ptr;
1171 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1177 * 0: "ignore this ptrace stop", syscall_entering_decode() should return a "bail
1178 * out silently" code.
1179 * 1: ok, continue in syscall_entering_decode().
1180 * other: error, syscall_entering_decode() should print error indicator
1181 * ("????" etc) and return an appropriate code.
1184 get_scno(struct tcb *tcp)
1186 if (get_regs(tcp) < 0)
1189 int rc = arch_get_scno(tcp);
1193 if (scno_is_valid(tcp->scno)) {
1194 tcp->s_ent = &sysent[tcp->scno];
1195 tcp->qual_flg = qual_flags(tcp->scno);
1197 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1200 s->ent.nargs = MAX_ARGS;
1201 s->ent.sen = SEN_printargs;
1202 s->ent.sys_func = printargs;
1203 s->ent.sys_name = s->buf;
1204 xsprintf(s->buf, "syscall_%" PRI_klu, shuffle_scno(tcp->scno));
1206 tcp->s_ent = &s->ent;
1207 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1209 set_tcb_priv_data(tcp, s, free_sysent_buf);
1211 debug_msg("pid %d invalid syscall %" PRI_kld,
1212 tcp->pid, tcp->scno);
1217 #ifdef ptrace_getregset_or_getregs
1218 # define get_syscall_result_regs get_regs
1220 static int get_syscall_result_regs(struct tcb *);
1224 * 1: ok, continue in syscall_exiting_trace().
1225 * -1: error, syscall_exiting_trace() should print error indicator
1226 * ("????" etc) and bail out.
1229 get_syscall_result(struct tcb *tcp)
1231 if (get_syscall_result_regs(tcp) < 0)
1234 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1239 #include "get_scno.c"
1240 #include "set_scno.c"
1241 #include "get_syscall_args.c"
1242 #ifndef ptrace_getregset_or_getregs
1243 # include "get_syscall_result.c"
1245 #include "get_error.c"
1246 #include "set_error.c"
1247 #ifdef HAVE_GETREGS_OLD
1248 # include "getregs_old.c"
1252 syscall_name(kernel_ulong_t scno)
1254 #if defined X32_PERSONALITY_NUMBER && defined __X32_SYSCALL_BIT
1255 if (current_personality == X32_PERSONALITY_NUMBER)
1256 scno &= ~__X32_SYSCALL_BIT;
1258 return scno_is_valid(scno) ? sysent[scno].sys_name : NULL;