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 = PERSONALITY0_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 = PERSONALITY0_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(unsigned int personality)
257 if (personality == current_personality)
260 if (personality >= SUPPORTED_PERSONALITIES)
261 error_msg_and_die("Requested switch to unsupported personality "
264 nsyscalls = nsyscall_vec[personality];
265 sysent = sysent_vec[personality];
267 switch (personality) {
269 errnoent = errnoent0;
271 ioctlent = ioctlent0;
272 nioctlents = nioctlents0;
273 signalent = signalent0;
274 nsignals = nsignals0;
275 printers = &printers0;
279 errnoent = errnoent1;
281 ioctlent = ioctlent1;
282 nioctlents = nioctlents1;
283 signalent = signalent1;
284 nsignals = nsignals1;
285 printers = &printers1;
288 # if SUPPORTED_PERSONALITIES > 2
290 errnoent = errnoent2;
292 ioctlent = ioctlent2;
293 nioctlents = nioctlents2;
294 signalent = signalent2;
295 nsignals = nsignals2;
296 printers = &printers2;
301 current_personality = personality;
302 # ifndef current_wordsize
303 current_wordsize = personality_wordsize[personality];
305 # ifndef current_klongsize
306 current_klongsize = personality_klongsize[personality];
311 update_personality(struct tcb *tcp, unsigned int personality)
313 static bool need_mpers_warning[] =
314 { false, !HAVE_PERSONALITY_1_MPERS, !HAVE_PERSONALITY_2_MPERS };
316 set_personality(personality);
318 if (personality == tcp->currpers)
320 tcp->currpers = personality;
323 error_msg("[ Process PID=%d runs in %s mode. ]",
324 tcp->pid, personality_names[personality]);
327 if (need_mpers_warning[personality]) {
328 error_msg("WARNING: Proper structure decoding for this "
329 "personality is not supported, please consider "
330 "building strace with mpers support enabled.");
331 need_mpers_warning[personality] = false;
336 #ifdef SYS_socket_subcall
338 decode_socket_subcall(struct tcb *tcp)
340 const int call = tcp->u_arg[0];
342 if (call < 1 || call >= SYS_socket_nsubcalls)
345 const kernel_ulong_t scno = SYS_socket_subcall + call;
346 const unsigned int nargs = sysent[scno].nargs;
349 if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
353 tcp->qual_flg = qual_flags(scno);
354 tcp->s_ent = &sysent[scno];
357 for (i = 0; i < nargs; ++i)
358 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
359 ? ((uint32_t *) (void *) buf)[i] : buf[i];
361 #endif /* SYS_socket_subcall */
363 #ifdef SYS_ipc_subcall
365 decode_ipc_subcall(struct tcb *tcp)
367 unsigned int call = tcp->u_arg[0];
368 const unsigned int version = call >> 16;
371 # if defined S390 || defined S390X
375 if (current_wordsize == 8)
378 set_tcb_priv_ulong(tcp, version);
384 case 1: case 2: case 3: case 4:
385 case 11: case 12: case 13: case 14:
386 case 21: case 22: case 23: case 24:
392 tcp->scno = SYS_ipc_subcall + call;
393 tcp->qual_flg = qual_flags(tcp->scno);
394 tcp->s_ent = &sysent[tcp->scno];
396 const unsigned int n = tcp->s_ent->nargs;
398 for (i = 0; i < n; i++)
399 tcp->u_arg[i] = tcp->u_arg[i + 1];
401 #endif /* SYS_ipc_subcall */
403 #ifdef SYS_syscall_subcall
405 decode_syscall_subcall(struct tcb *tcp)
407 if (!scno_is_valid(tcp->u_arg[0]))
409 tcp->scno = tcp->u_arg[0];
410 tcp->qual_flg = qual_flags(tcp->scno);
411 tcp->s_ent = &sysent[tcp->scno];
412 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
413 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
414 # ifdef LINUX_MIPSO32
416 * Fetching the last arg of 7-arg syscalls (fadvise64_64
417 * and sync_file_range) requires additional code,
418 * see linux/mips/get_syscall_args.c
420 if (tcp->s_ent->nargs == MAX_ARGS) {
422 mips_REG_SP + MAX_ARGS * sizeof(tcp->u_arg[0]),
423 sizeof(tcp->u_arg[0]),
424 &tcp->u_arg[MAX_ARGS - 1]) < 0)
425 tcp->u_arg[MAX_ARGS - 1] = 0;
427 # endif /* LINUX_MIPSO32 */
429 #endif /* SYS_syscall_subcall */
432 dumpio(struct tcb *tcp)
437 int fd = tcp->u_arg[0];
441 if (is_number_in_set(fd, read_set)) {
442 switch (tcp->s_ent->sen) {
447 case SEN_mq_timedreceive:
448 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
453 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
457 dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
460 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
464 if (is_number_in_set(fd, write_set)) {
465 switch (tcp->s_ent->sen) {
470 case SEN_mq_timedsend:
471 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
477 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1);
480 dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1);
483 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
490 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
491 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
493 static kernel_ulong_t
494 shuffle_scno(kernel_ulong_t scno)
496 #ifdef ARM_FIRST_SHUFFLED_SYSCALL /* So far only 32-bit ARM needs this */
497 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
500 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
501 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
503 if (scno == 0x000ffff0)
504 return ARM_FIRST_SHUFFLED_SYSCALL;
506 # define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
508 * Is it ARM specific syscall?
509 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
510 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
512 if (scno >= 0x000f0000 &&
513 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
514 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
516 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
517 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
519 #endif /* ARM_FIRST_SHUFFLED_SYSCALL */
525 err_name(unsigned long err)
527 if ((err < nerrnos) && errnoent[err])
528 return errnoent[err];
533 static long get_regs(struct tcb *);
534 static int get_syscall_args(struct tcb *);
535 static int get_syscall_result(struct tcb *);
536 static int arch_get_scno(struct tcb *tcp);
537 static int arch_set_scno(struct tcb *, kernel_ulong_t);
538 static void get_error(struct tcb *, const bool);
539 static int arch_set_error(struct tcb *);
540 static int arch_set_success(struct tcb *);
542 struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
544 static struct inject_opts *
545 tcb_inject_opts(struct tcb *tcp)
547 return (scno_in_range(tcp->scno) && tcp->inject_vec[current_personality])
548 ? &tcp->inject_vec[current_personality][tcp->scno] : NULL;
553 tamper_with_syscall_entering(struct tcb *tcp, unsigned int *signo)
555 if (!tcp->inject_vec[current_personality]) {
556 tcp->inject_vec[current_personality] =
557 xcalloc(nsyscalls, sizeof(**inject_vec));
558 memcpy(tcp->inject_vec[current_personality],
559 inject_vec[current_personality],
560 nsyscalls * sizeof(**inject_vec));
563 struct inject_opts *opts = tcb_inject_opts(tcp);
565 if (!opts || opts->first == 0)
570 if (opts->first != 0)
573 opts->first = opts->step;
575 if (opts->data.flags & INJECT_F_SIGNAL)
576 *signo = opts->data.signo;
577 if (opts->data.flags & INJECT_F_RETVAL && !arch_set_scno(tcp, -1))
578 tcp->flags |= TCB_TAMPERED;
584 tamper_with_syscall_exiting(struct tcb *tcp)
586 struct inject_opts *opts = tcb_inject_opts(tcp);
591 if (opts->data.rval >= 0) {
592 kernel_long_t u_rval = tcp->u_rval;
594 tcp->u_rval = opts->data.rval;
595 if (arch_set_success(tcp)) {
596 tcp->u_rval = u_rval;
601 unsigned long new_error = -opts->data.rval;
603 if (new_error != tcp->u_error && new_error <= MAX_ERRNO_VALUE) {
604 unsigned long u_error = tcp->u_error;
606 tcp->u_error = new_error;
607 if (arch_set_error(tcp)) {
608 tcp->u_error = u_error;
618 * 0: "ignore this ptrace stop", bail out silently.
619 * 1: ok, decoded; call
620 * syscall_entering_finish(tcp, syscall_entering_trace(tcp, ...)).
621 * other: error; call syscall_entering_finish(tcp, res), where res is the value
625 syscall_entering_decode(struct tcb *tcp)
627 int res = get_scno(tcp);
631 if (res != 1 || (res = get_syscall_args(tcp)) != 1) {
633 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
635 * " <unavailable>" will be added later by the code which
636 * detects ptrace errors.
641 #if defined SYS_ipc_subcall \
642 || defined SYS_socket_subcall \
643 || defined SYS_syscall_subcall
645 switch (tcp->s_ent->sen) {
646 # ifdef SYS_ipc_subcall
648 decode_ipc_subcall(tcp);
651 # ifdef SYS_socket_subcall
653 decode_socket_subcall(tcp);
656 # ifdef SYS_syscall_subcall
658 decode_syscall_subcall(tcp);
659 if (tcp->s_ent->sen != SEN_syscall)
672 syscall_entering_trace(struct tcb *tcp, unsigned int *sig)
674 /* Restrain from fault injection while the trace executes strace code. */
676 tcp->qual_flg &= ~QUAL_INJECT;
679 switch (tcp->s_ent->sen) {
682 #if defined SPARC || defined SPARC64
685 tcp->flags &= ~TCB_HIDE_LOG;
689 if (!traced(tcp) || (tracing_paths && !pathtrace_match(tcp))) {
690 tcp->flags |= TCB_FILTERED;
694 tcp->flags &= ~TCB_FILTERED;
701 tamper_with_syscall_entering(tcp, sig);
703 if (cflag == CFLAG_ONLY_STATS) {
708 if (stack_trace_enabled) {
709 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
710 unwind_capture_stacktrace(tcp);
715 tprintf("%s(", tcp->s_ent->sys_name);
716 int res = raw(tcp) ? printargs(tcp) : tcp->s_ent->sys_func(tcp);
722 syscall_entering_finish(struct tcb *tcp, int res)
724 tcp->flags |= TCB_INSYSCALL;
725 tcp->sys_func_rval = res;
726 /* Measure the entrance time as late as possible to avoid errors. */
727 if ((Tflag || cflag) && !filtered(tcp))
728 gettimeofday(&tcp->etime, NULL);
734 * -1: error in one of ptrace ops.
736 * If not 0, call syscall_exiting_trace(tcp, res), where res is the return
737 * value. Anyway, call syscall_exiting_finish(tcp) then.
740 syscall_exiting_decode(struct tcb *tcp, struct timeval *ptv)
742 /* Measure the exit time as early as possible to avoid errors. */
743 if ((Tflag || cflag) && !(filtered(tcp) || hide_log(tcp)))
744 gettimeofday(ptv, NULL);
747 if (stack_trace_enabled) {
748 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
749 unwind_cache_invalidate(tcp);
753 if (filtered(tcp) || hide_log(tcp))
756 #if SUPPORTED_PERSONALITIES > 1
757 update_personality(tcp, tcp->currpers);
760 return get_syscall_result(tcp);
764 syscall_exiting_trace(struct tcb *tcp, struct timeval tv, int res)
766 if (syserror(tcp) && syscall_tampered(tcp))
767 tamper_with_syscall_exiting(tcp);
770 count_syscall(tcp, &tv);
771 if (cflag == CFLAG_ONLY_STATS) {
776 /* If not in -ff mode, and printing_tcp != tcp,
777 * then the log currently does not end with output
778 * of _our syscall entry_, but with something else.
779 * We need to say which syscall's return is this.
781 * Forced reprinting via TCB_REPRINT is used only by
782 * "strace -ff -oLOG test/threaded_execve" corner case.
783 * It's the only case when -ff mode needs reprinting.
785 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
786 tcp->flags &= ~TCB_REPRINT;
788 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
792 tcp->s_prev_ent = NULL;
794 /* There was error in one of prior ptrace ops */
797 tprints("= ? <unavailable>\n");
801 tcp->s_prev_ent = tcp->s_ent;
805 /* sys_res = printargs(tcp); - but it's nop on sysexit */
807 /* FIXME: not_failing_only (IOW, option -z) is broken:
808 * failure of syscall is known only after syscall return.
809 * Thus we end up with something like this on, say, ENOENT:
810 * open("does_not_exist", O_RDONLY <unfinished ...>
811 * {next syscall decode}
812 * whereas the intended result is that open(...) line
813 * is not shown at all.
815 if (not_failing_only && tcp->u_error)
816 return 0; /* ignore failed syscalls */
817 if (tcp->sys_func_rval & RVAL_DECODED)
818 sys_res = tcp->sys_func_rval;
820 sys_res = tcp->s_ent->sys_func(tcp);
825 unsigned long u_error = tcp->u_error;
826 kernel_long_t u_rval;
830 tprintf("= -1 (errno %lu)", u_error);
832 tprintf("= %#" PRI_klx, tcp->u_rval);
834 if (syscall_tampered(tcp))
835 tprints(" (INJECTED)");
836 } else if (!(sys_res & RVAL_NONE) && u_error) {
837 const char *u_error_str;
840 /* Blocked signals do not interrupt any syscalls.
841 * In this case syscalls don't return ERESTARTfoo codes.
843 * Deadly signals set to SIG_DFL interrupt syscalls
844 * and kill the process regardless of which of the codes below
845 * is returned by the interrupted syscall.
846 * In some cases, kernel forces a kernel-generated deadly
847 * signal to be unblocked and set to SIG_DFL (and thus cause
848 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
849 * or SIGILL. (The alternative is to leave process spinning
850 * forever on the faulty instruction - not useful).
852 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
853 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
854 * but kernel will always restart them.
857 /* Most common type of signal-interrupted syscall exit code.
858 * The system call will be restarted with the same arguments
859 * if SA_RESTART is set; otherwise, it will fail with EINTR.
861 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
864 /* Rare. For example, fork() returns this if interrupted.
865 * SA_RESTART is ignored (assumed set): the restart is unconditional.
867 tprints("= ? ERESTARTNOINTR (To be restarted)");
870 /* pause(), rt_sigsuspend() etc use this code.
871 * SA_RESTART is ignored (assumed not set):
872 * syscall won't restart (will return EINTR instead)
873 * even after signal with SA_RESTART set. However,
874 * after SIG_IGN or SIG_DFL signal it will restart
875 * (thus the name "restart only if has no handler").
877 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
879 case ERESTART_RESTARTBLOCK:
880 /* Syscalls like nanosleep(), poll() which can't be
881 * restarted with their original arguments use this
882 * code. Kernel will execute restart_syscall() instead,
883 * which changes arguments before restarting syscall.
884 * SA_RESTART is ignored (assumed not set) similarly
885 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
886 * since restart data is saved in "restart block"
887 * in task struct, and if signal handler uses a syscall
888 * which in turn saves another such restart block,
889 * old data is lost and restart becomes impossible)
891 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
894 u_rval = sys_res & RVAL_PRINT_ERR_VAL ?
896 u_error_str = err_name(u_error);
898 tprintf("= %" PRI_kld " %s (%s)",
899 u_rval, u_error_str, strerror(u_error));
901 tprintf("= %" PRI_kld " %lu (%s)",
902 u_rval, u_error, strerror(u_error));
905 if (syscall_tampered(tcp))
906 tprints(" (INJECTED)");
907 if ((sys_res & RVAL_STR) && tcp->auxstr)
908 tprintf(" (%s)", tcp->auxstr);
910 if (sys_res & RVAL_NONE)
913 switch (sys_res & RVAL_MASK) {
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_klx, tcp->u_rval);
927 print_numeric_long_umask(tcp->u_rval);
930 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
931 if (current_wordsize < sizeof(tcp->u_rval)) {
933 (unsigned int) tcp->u_rval);
937 tprintf("= %" PRI_klu, tcp->u_rval);
941 tprintf("= %" PRI_kld, tcp->u_rval);
946 printfd(tcp, tcp->u_rval);
948 tprintf("= %" PRI_kld, tcp->u_rval);
951 error_msg("invalid rval format");
955 if ((sys_res & RVAL_STR) && tcp->auxstr)
956 tprintf(" (%s)", tcp->auxstr);
957 if (syscall_tampered(tcp))
958 tprints(" (INJECTED)");
961 tv_sub(&tv, &tv, &tcp->etime);
962 tprintf(" <%ld.%06ld>",
963 (long) tv.tv_sec, (long) tv.tv_usec);
970 if (stack_trace_enabled)
971 unwind_print_stacktrace(tcp);
977 syscall_exiting_finish(struct tcb *tcp)
979 tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED);
980 tcp->sys_func_rval = 0;
981 free_tcb_priv_data(tcp);
985 is_erestart(struct tcb *tcp)
987 switch (tcp->u_error) {
991 case ERESTART_RESTARTBLOCK:
998 static unsigned long saved_u_error;
1001 temporarily_clear_syserror(struct tcb *tcp)
1003 saved_u_error = tcp->u_error;
1008 restore_cleared_syserror(struct tcb *tcp)
1010 tcp->u_error = saved_u_error;
1013 #include "arch_regs.c"
1015 #ifdef HAVE_GETRVAL2
1016 # include "arch_getrval2.c"
1020 print_pc(struct tcb *tcp)
1022 #if defined ARCH_PC_REG
1023 # define ARCH_GET_PC 0
1024 #elif defined ARCH_PC_PEEK_ADDR
1026 # define ARCH_PC_REG pc
1027 # define ARCH_GET_PC upeek(tcp, ARCH_PC_PEEK_ADDR, &pc)
1029 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1031 if (get_regs(tcp) < 0 || ARCH_GET_PC)
1032 tprints(current_wordsize == 4 ? "[????????] "
1033 : "[????????????????] ");
1035 tprintf(current_wordsize == 4
1036 ? "[%08" PRI_klx "] " : "[%016" PRI_klx "] ",
1037 (kernel_ulong_t) ARCH_PC_REG);
1040 #include "getregs_old.h"
1042 #undef ptrace_getregset_or_getregs
1043 #undef ptrace_setregset_or_setregs
1044 #ifdef ARCH_REGS_FOR_GETREGSET
1046 # define ptrace_getregset_or_getregs ptrace_getregset
1048 ptrace_getregset(pid_t pid)
1050 # ifdef ARCH_IOVEC_FOR_GETREGSET
1051 /* variable iovec */
1052 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1053 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1054 &ARCH_IOVEC_FOR_GETREGSET);
1056 /* constant iovec */
1057 static struct iovec io = {
1058 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1059 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1061 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1066 # ifndef HAVE_GETREGS_OLD
1067 # define ptrace_setregset_or_setregs ptrace_setregset
1069 ptrace_setregset(pid_t pid)
1071 # ifdef ARCH_IOVEC_FOR_GETREGSET
1072 /* variable iovec */
1073 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1074 &ARCH_IOVEC_FOR_GETREGSET);
1076 /* constant iovec */
1077 static struct iovec io = {
1078 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1079 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1081 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1084 # endif /* !HAVE_GETREGS_OLD */
1086 #elif defined ARCH_REGS_FOR_GETREGS
1088 # define ptrace_getregset_or_getregs ptrace_getregs
1090 ptrace_getregs(pid_t pid)
1092 # if defined SPARC || defined SPARC64
1093 /* SPARC systems have the meaning of data and addr reversed */
1094 return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1096 return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1100 # ifndef HAVE_GETREGS_OLD
1101 # define ptrace_setregset_or_setregs ptrace_setregs
1103 ptrace_setregs(pid_t pid)
1105 # if defined SPARC || defined SPARC64
1106 /* SPARC systems have the meaning of data and addr reversed */
1107 return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1109 return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1112 # endif /* !HAVE_GETREGS_OLD */
1114 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1116 #ifdef ptrace_getregset_or_getregs
1117 static long get_regs_error;
1121 clear_regs(struct tcb *tcp)
1123 #ifdef ptrace_getregset_or_getregs
1124 get_regs_error = -1;
1129 get_regs(struct tcb *const tcp)
1131 #ifdef ptrace_getregset_or_getregs
1133 if (get_regs_error != -1)
1134 return get_regs_error;
1136 # ifdef HAVE_GETREGS_OLD
1138 * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1139 * fallback to getregs_old.
1141 static int use_getregs_old;
1142 if (use_getregs_old < 0) {
1143 return get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1144 } else if (use_getregs_old == 0) {
1145 get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1146 if (get_regs_error >= 0) {
1147 use_getregs_old = -1;
1148 return get_regs_error;
1150 if (errno == EPERM || errno == ESRCH)
1151 return get_regs_error;
1152 use_getregs_old = 1;
1154 return get_regs_error = getregs_old(tcp);
1155 # else /* !HAVE_GETREGS_OLD */
1156 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1157 return get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1158 # endif /* !HAVE_GETREGS_OLD */
1160 #else /* !ptrace_getregset_or_getregs */
1162 # warning get_regs is not implemented for this architecture yet
1165 #endif /* !ptrace_getregset_or_getregs */
1168 #ifdef ptrace_setregset_or_setregs
1172 return ptrace_setregset_or_setregs(pid);
1174 #endif /* ptrace_setregset_or_setregs */
1179 char buf[sizeof("syscall_%lu") + sizeof(kernel_ulong_t) * 3];
1183 free_sysent_buf(void *ptr)
1185 struct sysent_buf *s = ptr;
1186 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1192 * 0: "ignore this ptrace stop", syscall_entering_decode() should return a "bail
1193 * out silently" code.
1194 * 1: ok, continue in syscall_entering_decode().
1195 * other: error, syscall_entering_decode() should print error indicator
1196 * ("????" etc) and return an appropriate code.
1199 get_scno(struct tcb *tcp)
1201 if (get_regs(tcp) < 0)
1204 int rc = arch_get_scno(tcp);
1208 if (scno_is_valid(tcp->scno)) {
1209 tcp->s_ent = &sysent[tcp->scno];
1210 tcp->qual_flg = qual_flags(tcp->scno);
1212 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1215 s->ent.nargs = MAX_ARGS;
1216 s->ent.sen = SEN_printargs;
1217 s->ent.sys_func = printargs;
1218 s->ent.sys_name = s->buf;
1219 xsprintf(s->buf, "syscall_%" PRI_klu, shuffle_scno(tcp->scno));
1221 tcp->s_ent = &s->ent;
1222 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1224 set_tcb_priv_data(tcp, s, free_sysent_buf);
1226 debug_msg("pid %d invalid syscall %" PRI_kld,
1227 tcp->pid, tcp->scno);
1232 #ifdef ptrace_getregset_or_getregs
1233 # define get_syscall_result_regs get_regs
1235 static int get_syscall_result_regs(struct tcb *);
1239 * 1: ok, continue in syscall_exiting_trace().
1240 * -1: error, syscall_exiting_trace() should print error indicator
1241 * ("????" etc) and bail out.
1244 get_syscall_result(struct tcb *tcp)
1246 if (get_syscall_result_regs(tcp) < 0)
1249 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1254 #include "get_scno.c"
1255 #include "set_scno.c"
1256 #include "get_syscall_args.c"
1257 #ifndef ptrace_getregset_or_getregs
1258 # include "get_syscall_result.c"
1260 #include "get_error.c"
1261 #include "set_error.c"
1262 #ifdef HAVE_GETREGS_OLD
1263 # include "getregs_old.c"
1267 syscall_name(kernel_ulong_t scno)
1269 #if defined X32_PERSONALITY_NUMBER && defined __X32_SYSCALL_BIT
1270 if (current_personality == X32_PERSONALITY_NUMBER)
1271 scno &= ~__X32_SYSCALL_BIT;
1273 return scno_is_valid(scno) ? sysent[scno].sys_name : NULL;