2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
9 * Copyright (c) 1999-2018 The strace developers.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include "mmap_cache.h"
37 #include "native_defs.h"
40 #include "number_set.h"
43 /* for struct iovec */
46 /* for __X32_SYSCALL_BIT */
47 #include <asm/unistd.h>
52 # undef PTRACE_GETREGS
53 # define PTRACE_GETREGS PTRACE_GETREGS64
54 # undef PTRACE_SETREGS
55 # define PTRACE_SETREGS PTRACE_SETREGS64
59 # define NT_PRSTATUS 1
65 /* Define these shorthand notations to simplify the syscallent files. */
66 #include "sysent_shorthand_defs.h"
68 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
70 const struct_sysent sysent0[] = {
71 #include "syscallent.h"
74 #if SUPPORTED_PERSONALITIES > 1
75 # include PERSONALITY1_INCLUDE_FUNCS
76 static const struct_sysent sysent1[] = {
77 # include "syscallent1.h"
81 #if SUPPORTED_PERSONALITIES > 2
82 # include PERSONALITY2_INCLUDE_FUNCS
83 static const struct_sysent sysent2[] = {
84 # include "syscallent2.h"
88 /* Now undef them since short defines cause wicked namespace pollution. */
89 #include "sysent_shorthand_undefs.h"
92 * `ioctlent[012].h' files are automatically generated by the auxiliary
93 * program `ioctlsort', such that the list is sorted by the `code' field.
94 * This has the side-effect of resolving the _IO.. macros into
95 * plain integers, eliminating the need to include here everything
99 const char *const errnoent0[] = {
100 #include "errnoent.h"
102 const char *const signalent0[] = {
103 #include "signalent.h"
105 const struct_ioctlent ioctlent0[] = {
106 #include "ioctlent0.h"
109 #if SUPPORTED_PERSONALITIES > 1
110 static const char *const errnoent1[] = {
111 # include "errnoent1.h"
113 static const char *const signalent1[] = {
114 # include "signalent1.h"
116 static const struct_ioctlent ioctlent1[] = {
117 # include "ioctlent1.h"
119 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
120 static const struct_printers printers0 = {
121 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
123 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
124 static const struct_printers printers1 = {
125 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
129 #if SUPPORTED_PERSONALITIES > 2
130 static const char *const errnoent2[] = {
131 # include "errnoent2.h"
133 static const char *const signalent2[] = {
134 # include "signalent2.h"
136 static const struct_ioctlent ioctlent2[] = {
137 # include "ioctlent2.h"
139 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
140 static const struct_printers printers2 = {
141 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
146 nsyscalls0 = ARRAY_SIZE(sysent0)
147 #if SUPPORTED_PERSONALITIES > 1
148 , nsyscalls1 = ARRAY_SIZE(sysent1)
149 # if SUPPORTED_PERSONALITIES > 2
150 , nsyscalls2 = ARRAY_SIZE(sysent2)
156 nerrnos0 = ARRAY_SIZE(errnoent0)
157 #if SUPPORTED_PERSONALITIES > 1
158 , nerrnos1 = ARRAY_SIZE(errnoent1)
159 # if SUPPORTED_PERSONALITIES > 2
160 , nerrnos2 = ARRAY_SIZE(errnoent2)
166 nsignals0 = ARRAY_SIZE(signalent0)
167 #if SUPPORTED_PERSONALITIES > 1
168 , nsignals1 = ARRAY_SIZE(signalent1)
169 # if SUPPORTED_PERSONALITIES > 2
170 , nsignals2 = ARRAY_SIZE(signalent2)
176 nioctlents0 = ARRAY_SIZE(ioctlent0)
177 #if SUPPORTED_PERSONALITIES > 1
178 , nioctlents1 = ARRAY_SIZE(ioctlent1)
179 # if SUPPORTED_PERSONALITIES > 2
180 , nioctlents2 = ARRAY_SIZE(ioctlent2)
185 #if SUPPORTED_PERSONALITIES > 1
186 const struct_sysent *sysent = sysent0;
187 const char *const *errnoent = errnoent0;
188 const char *const *signalent = signalent0;
189 const struct_ioctlent *ioctlent = ioctlent0;
190 const struct_printers *printers = &printers0;
193 unsigned nsyscalls = nsyscalls0;
194 unsigned nerrnos = nerrnos0;
195 unsigned nsignals = nsignals0;
196 unsigned nioctlents = nioctlents0;
198 const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES] = {
200 #if SUPPORTED_PERSONALITIES > 1
203 #if SUPPORTED_PERSONALITIES > 2
207 const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
209 #if SUPPORTED_PERSONALITIES > 1
212 #if SUPPORTED_PERSONALITIES > 2
217 const char *const personality_names[] =
219 {"64 bit", "32 bit", "x32"}
222 # elif SUPPORTED_PERSONALITIES == 2
225 {STRINGIFY_VAL(__WORDSIZE) " bit"}
229 #if SUPPORTED_PERSONALITIES > 1
231 unsigned current_personality;
233 # ifndef current_wordsize
234 unsigned current_wordsize = PERSONALITY0_WORDSIZE;
235 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
236 PERSONALITY0_WORDSIZE,
237 PERSONALITY1_WORDSIZE,
238 # if SUPPORTED_PERSONALITIES > 2
239 PERSONALITY2_WORDSIZE,
244 # ifndef current_klongsize
245 unsigned current_klongsize = PERSONALITY0_KLONGSIZE;
246 static const int personality_klongsize[SUPPORTED_PERSONALITIES] = {
247 PERSONALITY0_KLONGSIZE,
248 PERSONALITY1_KLONGSIZE,
249 # if SUPPORTED_PERSONALITIES > 2
250 PERSONALITY2_KLONGSIZE,
256 set_personality(unsigned int personality)
258 if (personality == current_personality)
261 if (personality >= SUPPORTED_PERSONALITIES)
262 error_msg_and_die("Requested switch to unsupported personality "
265 nsyscalls = nsyscall_vec[personality];
266 sysent = sysent_vec[personality];
268 switch (personality) {
270 errnoent = errnoent0;
272 ioctlent = ioctlent0;
273 nioctlents = nioctlents0;
274 signalent = signalent0;
275 nsignals = nsignals0;
276 printers = &printers0;
280 errnoent = errnoent1;
282 ioctlent = ioctlent1;
283 nioctlents = nioctlents1;
284 signalent = signalent1;
285 nsignals = nsignals1;
286 printers = &printers1;
289 # if SUPPORTED_PERSONALITIES > 2
291 errnoent = errnoent2;
293 ioctlent = ioctlent2;
294 nioctlents = nioctlents2;
295 signalent = signalent2;
296 nsignals = nsignals2;
297 printers = &printers2;
302 current_personality = personality;
303 # ifndef current_wordsize
304 current_wordsize = personality_wordsize[personality];
306 # ifndef current_klongsize
307 current_klongsize = personality_klongsize[personality];
312 update_personality(struct tcb *tcp, unsigned int personality)
314 static bool need_mpers_warning[] =
315 { false, !HAVE_PERSONALITY_1_MPERS, !HAVE_PERSONALITY_2_MPERS };
317 set_personality(personality);
319 if (personality == tcp->currpers)
321 tcp->currpers = personality;
324 error_msg("[ Process PID=%d runs in %s mode. ]",
325 tcp->pid, personality_names[personality]);
328 if (need_mpers_warning[personality]) {
329 error_msg("WARNING: Proper structure decoding for this "
330 "personality is not supported, please consider "
331 "building strace with mpers support enabled.");
332 need_mpers_warning[personality] = false;
337 #ifdef SYS_socket_subcall
339 decode_socket_subcall(struct tcb *tcp)
341 const int call = tcp->u_arg[0];
343 if (call < 1 || call >= SYS_socket_nsubcalls)
346 const kernel_ulong_t scno = SYS_socket_subcall + call;
347 const unsigned int nargs = sysent[scno].nargs;
350 if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
354 tcp->qual_flg = qual_flags(scno);
355 tcp->s_ent = &sysent[scno];
358 for (i = 0; i < nargs; ++i)
359 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
360 ? ((uint32_t *) (void *) buf)[i] : buf[i];
362 #endif /* SYS_socket_subcall */
364 #ifdef SYS_ipc_subcall
366 decode_ipc_subcall(struct tcb *tcp)
368 unsigned int call = tcp->u_arg[0];
369 const unsigned int version = call >> 16;
372 # if defined S390 || defined S390X
376 if (current_wordsize == 8)
379 set_tcb_priv_ulong(tcp, version);
385 case 1: case 2: case 3: case 4:
386 case 11: case 12: case 13: case 14:
387 case 21: case 22: case 23: case 24:
393 tcp->scno = SYS_ipc_subcall + call;
394 tcp->qual_flg = qual_flags(tcp->scno);
395 tcp->s_ent = &sysent[tcp->scno];
397 const unsigned int n = tcp->s_ent->nargs;
399 for (i = 0; i < n; i++)
400 tcp->u_arg[i] = tcp->u_arg[i + 1];
402 #endif /* SYS_ipc_subcall */
404 #ifdef SYS_syscall_subcall
406 decode_syscall_subcall(struct tcb *tcp)
408 if (!scno_is_valid(tcp->u_arg[0]))
410 tcp->scno = tcp->u_arg[0];
411 tcp->qual_flg = qual_flags(tcp->scno);
412 tcp->s_ent = &sysent[tcp->scno];
413 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
414 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
415 # ifdef LINUX_MIPSO32
417 * Fetching the last arg of 7-arg syscalls (fadvise64_64
418 * and sync_file_range) requires additional code,
419 * see linux/mips/get_syscall_args.c
421 if (tcp->s_ent->nargs == MAX_ARGS) {
423 mips_REG_SP + MAX_ARGS * sizeof(tcp->u_arg[0]),
424 sizeof(tcp->u_arg[0]),
425 &tcp->u_arg[MAX_ARGS - 1]) < 0)
426 tcp->u_arg[MAX_ARGS - 1] = 0;
428 # endif /* LINUX_MIPSO32 */
430 #endif /* SYS_syscall_subcall */
433 dumpio(struct tcb *tcp)
438 int fd = tcp->u_arg[0];
442 if (is_number_in_set(fd, read_set)) {
443 switch (tcp->s_ent->sen) {
448 case SEN_mq_timedreceive:
449 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
454 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
458 dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
461 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
465 if (is_number_in_set(fd, write_set)) {
466 switch (tcp->s_ent->sen) {
471 case SEN_mq_timedsend:
472 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
478 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1);
481 dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1);
484 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
491 err_name(unsigned long err)
493 if ((err < nerrnos) && errnoent[err])
494 return errnoent[err];
499 static long get_regs(struct tcb *);
500 static int get_syscall_args(struct tcb *);
501 static int get_syscall_result(struct tcb *);
502 static int arch_get_scno(struct tcb *tcp);
503 static int arch_set_scno(struct tcb *, kernel_ulong_t);
504 static void get_error(struct tcb *, const bool);
505 static int arch_set_error(struct tcb *);
506 static int arch_set_success(struct tcb *);
508 struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
510 static struct inject_opts *
511 tcb_inject_opts(struct tcb *tcp)
513 return (scno_in_range(tcp->scno) && tcp->inject_vec[current_personality])
514 ? &tcp->inject_vec[current_personality][tcp->scno] : NULL;
519 tamper_with_syscall_entering(struct tcb *tcp, unsigned int *signo)
521 if (!tcp->inject_vec[current_personality]) {
522 tcp->inject_vec[current_personality] =
523 xcalloc(nsyscalls, sizeof(**inject_vec));
524 memcpy(tcp->inject_vec[current_personality],
525 inject_vec[current_personality],
526 nsyscalls * sizeof(**inject_vec));
529 struct inject_opts *opts = tcb_inject_opts(tcp);
531 if (!opts || opts->first == 0)
536 if (opts->first != 0)
539 opts->first = opts->step;
541 if (!recovering(tcp)) {
542 if (opts->data.flags & INJECT_F_SIGNAL)
543 *signo = opts->data.signo;
544 if (opts->data.flags & INJECT_F_RETVAL &&
545 !arch_set_scno(tcp, -1))
546 tcp->flags |= TCB_TAMPERED;
553 tamper_with_syscall_exiting(struct tcb *tcp)
555 if (!syserror(tcp)) {
556 error_msg("Failed to tamper with process %d: got no error "
557 "(return value %#" PRI_klx ")",
558 tcp->pid, tcp->u_rval);
563 struct inject_opts *opts = tcb_inject_opts(tcp);
564 bool update_tcb = false;
569 if (opts->data.rval >= 0) {
570 kernel_long_t u_rval = tcp->u_rval;
572 tcp->u_rval = opts->data.rval;
573 if (arch_set_success(tcp)) {
574 tcp->u_rval = u_rval;
580 unsigned long new_error = -opts->data.rval;
582 if (new_error != tcp->u_error && new_error <= MAX_ERRNO_VALUE) {
583 unsigned long u_error = tcp->u_error;
585 tcp->u_error = new_error;
586 if (arch_set_error(tcp)) {
587 tcp->u_error = u_error;
596 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
604 * 0: "ignore this ptrace stop", bail out silently.
605 * 1: ok, decoded; call
606 * syscall_entering_finish(tcp, syscall_entering_trace(tcp, ...)).
607 * other: error; call syscall_entering_finish(tcp, res), where res is the value
611 syscall_entering_decode(struct tcb *tcp)
613 int res = get_scno(tcp);
617 if (res != 1 || (res = get_syscall_args(tcp)) != 1) {
619 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
621 * " <unavailable>" will be added later by the code which
622 * detects ptrace errors.
627 #if defined SYS_ipc_subcall \
628 || defined SYS_socket_subcall \
629 || defined SYS_syscall_subcall
631 switch (tcp->s_ent->sen) {
632 # ifdef SYS_ipc_subcall
634 decode_ipc_subcall(tcp);
637 # ifdef SYS_socket_subcall
639 decode_socket_subcall(tcp);
642 # ifdef SYS_syscall_subcall
644 decode_syscall_subcall(tcp);
645 if (tcp->s_ent->sen != SEN_syscall)
658 syscall_entering_trace(struct tcb *tcp, unsigned int *sig)
660 /* Restrain from fault injection while the trace executes strace code. */
662 tcp->qual_flg &= ~QUAL_INJECT;
665 switch (tcp->s_ent->sen) {
668 #if defined SPARC || defined SPARC64
671 tcp->flags &= ~TCB_HIDE_LOG;
675 if (!traced(tcp) || (tracing_paths && !pathtrace_match(tcp))) {
676 tcp->flags |= TCB_FILTERED;
680 tcp->flags &= ~TCB_FILTERED;
687 tamper_with_syscall_entering(tcp, sig);
689 if (cflag == CFLAG_ONLY_STATS) {
694 if (stack_trace_enabled) {
695 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
696 unwind_capture_stacktrace(tcp);
701 tprintf("%s(", tcp->s_ent->sys_name);
702 int res = raw(tcp) ? printargs(tcp) : tcp->s_ent->sys_func(tcp);
708 syscall_entering_finish(struct tcb *tcp, int res)
710 tcp->flags |= TCB_INSYSCALL;
711 tcp->sys_func_rval = res;
712 /* Measure the entrance time as late as possible to avoid errors. */
713 if ((Tflag || cflag) && !filtered(tcp))
714 gettimeofday(&tcp->etime, NULL);
720 * -1: error in one of ptrace ops.
722 * If not 0, call syscall_exiting_trace(tcp, res), where res is the return
723 * value. Anyway, call syscall_exiting_finish(tcp) then.
726 syscall_exiting_decode(struct tcb *tcp, struct timeval *ptv)
728 /* Measure the exit time as early as possible to avoid errors. */
729 if ((Tflag || cflag) && !(filtered(tcp) || hide_log(tcp)))
730 gettimeofday(ptv, NULL);
732 if (mmap_cache_is_enabled()) {
733 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
734 mmap_cache_invalidate(tcp);
737 if (filtered(tcp) || hide_log(tcp))
740 #if SUPPORTED_PERSONALITIES > 1
741 update_personality(tcp, tcp->currpers);
744 return get_syscall_result(tcp);
748 syscall_exiting_trace(struct tcb *tcp, struct timeval tv, int res)
750 if (syscall_tampered(tcp))
751 tamper_with_syscall_exiting(tcp);
754 count_syscall(tcp, &tv);
755 if (cflag == CFLAG_ONLY_STATS) {
760 /* If not in -ff mode, and printing_tcp != tcp,
761 * then the log currently does not end with output
762 * of _our syscall entry_, but with something else.
763 * We need to say which syscall's return is this.
765 * Forced reprinting via TCB_REPRINT is used only by
766 * "strace -ff -oLOG test/threaded_execve" corner case.
767 * It's the only case when -ff mode needs reprinting.
769 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
770 tcp->flags &= ~TCB_REPRINT;
772 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
776 tcp->s_prev_ent = NULL;
778 /* There was error in one of prior ptrace ops */
781 tprints("= ? <unavailable>\n");
785 tcp->s_prev_ent = tcp->s_ent;
789 /* sys_res = printargs(tcp); - but it's nop on sysexit */
791 /* FIXME: not_failing_only (IOW, option -z) is broken:
792 * failure of syscall is known only after syscall return.
793 * Thus we end up with something like this on, say, ENOENT:
794 * open("does_not_exist", O_RDONLY <unfinished ...>
795 * {next syscall decode}
796 * whereas the intended result is that open(...) line
797 * is not shown at all.
799 if (not_failing_only && tcp->u_error)
800 return 0; /* ignore failed syscalls */
801 if (tcp->sys_func_rval & RVAL_DECODED)
802 sys_res = tcp->sys_func_rval;
804 sys_res = tcp->s_ent->sys_func(tcp);
809 unsigned long u_error = tcp->u_error;
810 kernel_long_t u_rval;
814 tprintf("= -1 (errno %lu)", u_error);
816 tprintf("= %#" PRI_klx, tcp->u_rval);
818 if (syscall_tampered(tcp))
819 tprints(" (INJECTED)");
820 } else if (!(sys_res & RVAL_NONE) && u_error) {
821 const char *u_error_str;
824 /* Blocked signals do not interrupt any syscalls.
825 * In this case syscalls don't return ERESTARTfoo codes.
827 * Deadly signals set to SIG_DFL interrupt syscalls
828 * and kill the process regardless of which of the codes below
829 * is returned by the interrupted syscall.
830 * In some cases, kernel forces a kernel-generated deadly
831 * signal to be unblocked and set to SIG_DFL (and thus cause
832 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
833 * or SIGILL. (The alternative is to leave process spinning
834 * forever on the faulty instruction - not useful).
836 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
837 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
838 * but kernel will always restart them.
841 /* Most common type of signal-interrupted syscall exit code.
842 * The system call will be restarted with the same arguments
843 * if SA_RESTART is set; otherwise, it will fail with EINTR.
845 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
848 /* Rare. For example, fork() returns this if interrupted.
849 * SA_RESTART is ignored (assumed set): the restart is unconditional.
851 tprints("= ? ERESTARTNOINTR (To be restarted)");
854 /* pause(), rt_sigsuspend() etc use this code.
855 * SA_RESTART is ignored (assumed not set):
856 * syscall won't restart (will return EINTR instead)
857 * even after signal with SA_RESTART set. However,
858 * after SIG_IGN or SIG_DFL signal it will restart
859 * (thus the name "restart only if has no handler").
861 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
863 case ERESTART_RESTARTBLOCK:
864 /* Syscalls like nanosleep(), poll() which can't be
865 * restarted with their original arguments use this
866 * code. Kernel will execute restart_syscall() instead,
867 * which changes arguments before restarting syscall.
868 * SA_RESTART is ignored (assumed not set) similarly
869 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
870 * since restart data is saved in "restart block"
871 * in task struct, and if signal handler uses a syscall
872 * which in turn saves another such restart block,
873 * old data is lost and restart becomes impossible)
875 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
878 u_rval = sys_res & RVAL_PRINT_ERR_VAL ?
880 u_error_str = err_name(u_error);
882 tprintf("= %" PRI_kld " %s (%s)",
883 u_rval, u_error_str, strerror(u_error));
885 tprintf("= %" PRI_kld " %lu (%s)",
886 u_rval, u_error, strerror(u_error));
889 if (syscall_tampered(tcp))
890 tprints(" (INJECTED)");
891 if ((sys_res & RVAL_STR) && tcp->auxstr)
892 tprintf(" (%s)", tcp->auxstr);
894 if (sys_res & RVAL_NONE)
897 switch (sys_res & RVAL_MASK) {
899 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
900 if (current_klongsize < sizeof(tcp->u_rval)) {
902 (unsigned int) tcp->u_rval);
906 tprintf("= %#" PRI_klx, tcp->u_rval);
911 print_numeric_long_umask(tcp->u_rval);
914 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
915 if (current_klongsize < sizeof(tcp->u_rval)) {
917 (unsigned int) tcp->u_rval);
921 tprintf("= %" PRI_klu, tcp->u_rval);
925 tprintf("= %" PRI_kld, tcp->u_rval);
930 printfd(tcp, tcp->u_rval);
932 tprintf("= %" PRI_kld, tcp->u_rval);
935 error_msg("invalid rval format");
939 if ((sys_res & RVAL_STR) && tcp->auxstr)
940 tprintf(" (%s)", tcp->auxstr);
941 if (syscall_tampered(tcp))
942 tprints(" (INJECTED)");
945 tv_sub(&tv, &tv, &tcp->etime);
946 tprintf(" <%ld.%06ld>",
947 (long) tv.tv_sec, (long) tv.tv_usec);
954 if (stack_trace_enabled)
955 unwind_print_stacktrace(tcp);
961 syscall_exiting_finish(struct tcb *tcp)
963 tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED);
964 tcp->sys_func_rval = 0;
965 free_tcb_priv_data(tcp);
969 is_erestart(struct tcb *tcp)
971 switch (tcp->u_error) {
975 case ERESTART_RESTARTBLOCK:
982 static unsigned long saved_u_error;
985 temporarily_clear_syserror(struct tcb *tcp)
987 saved_u_error = tcp->u_error;
992 restore_cleared_syserror(struct tcb *tcp)
994 tcp->u_error = saved_u_error;
997 #include "arch_regs.c"
999 #if HAVE_ARCH_GETRVAL2
1000 # include "arch_getrval2.c"
1004 print_pc(struct tcb *tcp)
1006 #if defined ARCH_PC_REG
1007 # define ARCH_GET_PC 0
1008 #elif defined ARCH_PC_PEEK_ADDR
1010 # define ARCH_PC_REG pc
1011 # define ARCH_GET_PC upeek(tcp, ARCH_PC_PEEK_ADDR, &pc)
1013 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1015 if (get_regs(tcp) < 0 || ARCH_GET_PC)
1016 tprints(current_wordsize == 4 ? "[????????] "
1017 : "[????????????????] ");
1019 tprintf(current_wordsize == 4
1020 ? "[%08" PRI_klx "] " : "[%016" PRI_klx "] ",
1021 (kernel_ulong_t) ARCH_PC_REG);
1024 #include "getregs_old.h"
1026 #undef ptrace_getregset_or_getregs
1027 #undef ptrace_setregset_or_setregs
1028 #ifdef ARCH_REGS_FOR_GETREGSET
1030 # define ptrace_getregset_or_getregs ptrace_getregset
1032 ptrace_getregset(pid_t pid)
1034 # ifdef ARCH_IOVEC_FOR_GETREGSET
1035 /* variable iovec */
1036 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1037 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1038 &ARCH_IOVEC_FOR_GETREGSET);
1040 /* constant iovec */
1041 static struct iovec io = {
1042 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1043 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1045 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1050 # ifndef HAVE_GETREGS_OLD
1051 # define ptrace_setregset_or_setregs ptrace_setregset
1053 ptrace_setregset(pid_t pid)
1055 # ifdef ARCH_IOVEC_FOR_GETREGSET
1056 /* variable iovec */
1057 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1058 &ARCH_IOVEC_FOR_GETREGSET);
1060 /* constant iovec */
1061 static struct iovec io = {
1062 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1063 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1065 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1068 # endif /* !HAVE_GETREGS_OLD */
1070 #elif defined ARCH_REGS_FOR_GETREGS
1072 # define ptrace_getregset_or_getregs ptrace_getregs
1074 ptrace_getregs(pid_t pid)
1076 # if defined SPARC || defined SPARC64
1077 /* SPARC systems have the meaning of data and addr reversed */
1078 return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1080 return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1084 # ifndef HAVE_GETREGS_OLD
1085 # define ptrace_setregset_or_setregs ptrace_setregs
1087 ptrace_setregs(pid_t pid)
1089 # if defined SPARC || defined SPARC64
1090 /* SPARC systems have the meaning of data and addr reversed */
1091 return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1093 return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1096 # endif /* !HAVE_GETREGS_OLD */
1098 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1100 #ifdef ptrace_getregset_or_getregs
1101 static long get_regs_error;
1105 clear_regs(struct tcb *tcp)
1107 #ifdef ptrace_getregset_or_getregs
1108 get_regs_error = -1;
1113 get_regs(struct tcb *const tcp)
1115 #ifdef ptrace_getregset_or_getregs
1117 if (get_regs_error != -1)
1118 return get_regs_error;
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 return get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1128 } else if (use_getregs_old == 0) {
1129 get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1130 if (get_regs_error >= 0) {
1131 use_getregs_old = -1;
1132 return get_regs_error;
1134 if (errno == EPERM || errno == ESRCH)
1135 return get_regs_error;
1136 use_getregs_old = 1;
1138 return get_regs_error = getregs_old(tcp);
1139 # else /* !HAVE_GETREGS_OLD */
1140 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1141 get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1143 # if defined ARCH_PERSONALITY_0_IOV_SIZE
1145 return get_regs_error;
1147 switch (ARCH_IOVEC_FOR_GETREGSET.iov_len) {
1148 case ARCH_PERSONALITY_0_IOV_SIZE:
1149 update_personality(tcp, 0);
1151 case ARCH_PERSONALITY_1_IOV_SIZE:
1152 update_personality(tcp, 1);
1155 static bool printed = false;
1158 error_msg("Unsupported regset size returned by "
1159 "PTRACE_GETREGSET: %zu",
1160 ARCH_IOVEC_FOR_GETREGSET.iov_len);
1165 update_personality(tcp, 0);
1168 # endif /* ARCH_PERSONALITY_0_IOV_SIZE */
1170 return get_regs_error;
1172 # endif /* !HAVE_GETREGS_OLD */
1174 #else /* !ptrace_getregset_or_getregs */
1176 # warning get_regs is not implemented for this architecture yet
1179 #endif /* !ptrace_getregset_or_getregs */
1182 #ifdef ptrace_setregset_or_setregs
1186 return ptrace_setregset_or_setregs(pid);
1188 #endif /* ptrace_setregset_or_setregs */
1193 char buf[sizeof("syscall_0x") + sizeof(kernel_ulong_t) * 2];
1197 free_sysent_buf(void *ptr)
1199 struct sysent_buf *s = ptr;
1200 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1206 * 0: "ignore this ptrace stop", syscall_entering_decode() should return a "bail
1207 * out silently" code.
1208 * 1: ok, continue in syscall_entering_decode().
1209 * other: error, syscall_entering_decode() should print error indicator
1210 * ("????" etc) and return an appropriate code.
1213 get_scno(struct tcb *tcp)
1215 if (get_regs(tcp) < 0)
1218 int rc = arch_get_scno(tcp);
1222 tcp->scno = shuffle_scno(tcp->scno);
1224 if (scno_is_valid(tcp->scno)) {
1225 tcp->s_ent = &sysent[tcp->scno];
1226 tcp->qual_flg = qual_flags(tcp->scno);
1228 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1231 s->ent.nargs = MAX_ARGS;
1232 s->ent.sen = SEN_printargs;
1233 s->ent.sys_func = printargs;
1234 s->ent.sys_name = s->buf;
1235 xsprintf(s->buf, "syscall_%#" PRI_klx, shuffle_scno(tcp->scno));
1237 tcp->s_ent = &s->ent;
1238 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1240 set_tcb_priv_data(tcp, s, free_sysent_buf);
1242 debug_msg("pid %d invalid syscall %#" PRI_klx,
1243 tcp->pid, shuffle_scno(tcp->scno));
1247 * We refrain from argument decoding during recovering
1248 * as tracee memory mappings has changed and the registers
1249 * are very likely pointing to garbage already.
1251 if (recovering(tcp))
1252 tcp->qual_flg |= QUAL_RAW;
1257 #ifdef ptrace_getregset_or_getregs
1258 # define get_syscall_result_regs get_regs
1260 static int get_syscall_result_regs(struct tcb *);
1264 * 1: ok, continue in syscall_exiting_trace().
1265 * -1: error, syscall_exiting_trace() should print error indicator
1266 * ("????" etc) and bail out.
1269 get_syscall_result(struct tcb *tcp)
1271 if (get_syscall_result_regs(tcp) < 0)
1275 !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS)
1276 || syscall_tampered(tcp));
1281 #include "get_scno.c"
1282 #include "set_scno.c"
1283 #include "get_syscall_args.c"
1284 #ifndef ptrace_getregset_or_getregs
1285 # include "get_syscall_result.c"
1287 #include "get_error.c"
1288 #include "set_error.c"
1289 #ifdef HAVE_GETREGS_OLD
1290 # include "getregs_old.c"
1292 #include "shuffle_scno.c"
1295 syscall_name(kernel_ulong_t scno)
1297 return scno_is_valid(scno) ? sysent[scno].sys_name : NULL;