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"
45 /* for struct iovec */
48 /* for __X32_SYSCALL_BIT */
49 #include <asm/unistd.h>
54 # undef PTRACE_GETREGS
55 # define PTRACE_GETREGS PTRACE_GETREGS64
56 # undef PTRACE_SETREGS
57 # define PTRACE_SETREGS PTRACE_SETREGS64
61 # define NT_PRSTATUS 1
67 /* Define these shorthand notations to simplify the syscallent files. */
68 #include "sysent_shorthand_defs.h"
70 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
72 const struct_sysent sysent0[] = {
73 #include "syscallent.h"
76 #if SUPPORTED_PERSONALITIES > 1
77 # include PERSONALITY1_INCLUDE_FUNCS
78 static const struct_sysent sysent1[] = {
79 # include "syscallent1.h"
83 #if SUPPORTED_PERSONALITIES > 2
84 # include PERSONALITY2_INCLUDE_FUNCS
85 static const struct_sysent sysent2[] = {
86 # include "syscallent2.h"
90 /* Now undef them since short defines cause wicked namespace pollution. */
91 #include "sysent_shorthand_undefs.h"
94 * `ioctlent[012].h' files are automatically generated by the auxiliary
95 * program `ioctlsort', such that the list is sorted by the `code' field.
96 * This has the side-effect of resolving the _IO.. macros into
97 * plain integers, eliminating the need to include here everything
101 const char *const errnoent0[] = {
102 #include "errnoent.h"
104 const char *const signalent0[] = {
105 #include "signalent.h"
107 const struct_ioctlent ioctlent0[] = {
108 #include "ioctlent0.h"
111 #if SUPPORTED_PERSONALITIES > 1
112 static const char *const errnoent1[] = {
113 # include "errnoent1.h"
115 static const char *const signalent1[] = {
116 # include "signalent1.h"
118 static const struct_ioctlent ioctlent1[] = {
119 # include "ioctlent1.h"
121 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
122 static const struct_printers printers0 = {
123 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
125 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
126 static const struct_printers printers1 = {
127 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
131 #if SUPPORTED_PERSONALITIES > 2
132 static const char *const errnoent2[] = {
133 # include "errnoent2.h"
135 static const char *const signalent2[] = {
136 # include "signalent2.h"
138 static const struct_ioctlent ioctlent2[] = {
139 # include "ioctlent2.h"
141 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
142 static const struct_printers printers2 = {
143 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
148 nsyscalls0 = ARRAY_SIZE(sysent0)
149 #if SUPPORTED_PERSONALITIES > 1
150 , nsyscalls1 = ARRAY_SIZE(sysent1)
151 # if SUPPORTED_PERSONALITIES > 2
152 , nsyscalls2 = ARRAY_SIZE(sysent2)
158 nerrnos0 = ARRAY_SIZE(errnoent0)
159 #if SUPPORTED_PERSONALITIES > 1
160 , nerrnos1 = ARRAY_SIZE(errnoent1)
161 # if SUPPORTED_PERSONALITIES > 2
162 , nerrnos2 = ARRAY_SIZE(errnoent2)
168 nsignals0 = ARRAY_SIZE(signalent0)
169 #if SUPPORTED_PERSONALITIES > 1
170 , nsignals1 = ARRAY_SIZE(signalent1)
171 # if SUPPORTED_PERSONALITIES > 2
172 , nsignals2 = ARRAY_SIZE(signalent2)
178 nioctlents0 = ARRAY_SIZE(ioctlent0)
179 #if SUPPORTED_PERSONALITIES > 1
180 , nioctlents1 = ARRAY_SIZE(ioctlent1)
181 # if SUPPORTED_PERSONALITIES > 2
182 , nioctlents2 = ARRAY_SIZE(ioctlent2)
187 #if SUPPORTED_PERSONALITIES > 1
188 const struct_sysent *sysent = sysent0;
189 const char *const *errnoent = errnoent0;
190 const char *const *signalent = signalent0;
191 const struct_ioctlent *ioctlent = ioctlent0;
192 const struct_printers *printers = &printers0;
195 unsigned nsyscalls = nsyscalls0;
196 unsigned nerrnos = nerrnos0;
197 unsigned nsignals = nsignals0;
198 unsigned nioctlents = nioctlents0;
200 const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES] = {
202 #if SUPPORTED_PERSONALITIES > 1
205 #if SUPPORTED_PERSONALITIES > 2
209 const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
211 #if SUPPORTED_PERSONALITIES > 1
214 #if SUPPORTED_PERSONALITIES > 2
219 const char *const personality_names[] =
221 {"64 bit", "32 bit", "x32"}
224 # elif SUPPORTED_PERSONALITIES == 2
227 {STRINGIFY_VAL(__WORDSIZE) " bit"}
231 #if SUPPORTED_PERSONALITIES > 1
233 unsigned current_personality;
235 # ifndef current_wordsize
236 unsigned current_wordsize = PERSONALITY0_WORDSIZE;
237 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
238 PERSONALITY0_WORDSIZE,
239 PERSONALITY1_WORDSIZE,
240 # if SUPPORTED_PERSONALITIES > 2
241 PERSONALITY2_WORDSIZE,
246 # ifndef current_klongsize
247 unsigned current_klongsize = PERSONALITY0_KLONGSIZE;
248 static const int personality_klongsize[SUPPORTED_PERSONALITIES] = {
249 PERSONALITY0_KLONGSIZE,
250 PERSONALITY1_KLONGSIZE,
251 # if SUPPORTED_PERSONALITIES > 2
252 PERSONALITY2_KLONGSIZE,
258 set_personality(unsigned int personality)
260 if (personality == current_personality)
263 if (personality >= SUPPORTED_PERSONALITIES)
264 error_msg_and_die("Requested switch to unsupported personality "
267 nsyscalls = nsyscall_vec[personality];
268 sysent = sysent_vec[personality];
270 switch (personality) {
272 errnoent = errnoent0;
274 ioctlent = ioctlent0;
275 nioctlents = nioctlents0;
276 signalent = signalent0;
277 nsignals = nsignals0;
278 printers = &printers0;
282 errnoent = errnoent1;
284 ioctlent = ioctlent1;
285 nioctlents = nioctlents1;
286 signalent = signalent1;
287 nsignals = nsignals1;
288 printers = &printers1;
291 # if SUPPORTED_PERSONALITIES > 2
293 errnoent = errnoent2;
295 ioctlent = ioctlent2;
296 nioctlents = nioctlents2;
297 signalent = signalent2;
298 nsignals = nsignals2;
299 printers = &printers2;
304 current_personality = personality;
305 # ifndef current_wordsize
306 current_wordsize = personality_wordsize[personality];
308 # ifndef current_klongsize
309 current_klongsize = personality_klongsize[personality];
314 update_personality(struct tcb *tcp, unsigned int personality)
316 static bool need_mpers_warning[] =
317 { false, !HAVE_PERSONALITY_1_MPERS, !HAVE_PERSONALITY_2_MPERS };
319 set_personality(personality);
321 if (personality == tcp->currpers)
323 tcp->currpers = personality;
326 error_msg("[ Process PID=%d runs in %s mode. ]",
327 tcp->pid, personality_names[personality]);
330 if (need_mpers_warning[personality]) {
331 error_msg("WARNING: Proper structure decoding for this "
332 "personality is not supported, please consider "
333 "building strace with mpers support enabled.");
334 need_mpers_warning[personality] = false;
339 #ifdef SYS_socket_subcall
341 decode_socket_subcall(struct tcb *tcp)
343 const int call = tcp->u_arg[0];
345 if (call < 1 || call >= SYS_socket_nsubcalls)
348 const kernel_ulong_t scno = SYS_socket_subcall + call;
349 const unsigned int nargs = sysent[scno].nargs;
352 if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
356 tcp->qual_flg = qual_flags(scno);
357 tcp->s_ent = &sysent[scno];
360 for (i = 0; i < nargs; ++i)
361 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
362 ? ((uint32_t *) (void *) buf)[i] : buf[i];
364 #endif /* SYS_socket_subcall */
366 #ifdef SYS_ipc_subcall
368 decode_ipc_subcall(struct tcb *tcp)
370 unsigned int call = tcp->u_arg[0];
371 const unsigned int version = call >> 16;
374 # if defined S390 || defined S390X
378 if (current_wordsize == 8)
381 set_tcb_priv_ulong(tcp, version);
387 case 1: case 2: case 3: case 4:
388 case 11: case 12: case 13: case 14:
389 case 21: case 22: case 23: case 24:
395 tcp->scno = SYS_ipc_subcall + call;
396 tcp->qual_flg = qual_flags(tcp->scno);
397 tcp->s_ent = &sysent[tcp->scno];
399 const unsigned int n = tcp->s_ent->nargs;
401 for (i = 0; i < n; i++)
402 tcp->u_arg[i] = tcp->u_arg[i + 1];
404 #endif /* SYS_ipc_subcall */
406 #ifdef SYS_syscall_subcall
408 decode_syscall_subcall(struct tcb *tcp)
410 if (!scno_is_valid(tcp->u_arg[0]))
412 tcp->scno = tcp->u_arg[0];
413 tcp->qual_flg = qual_flags(tcp->scno);
414 tcp->s_ent = &sysent[tcp->scno];
415 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
416 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
417 # ifdef LINUX_MIPSO32
419 * Fetching the last arg of 7-arg syscalls (fadvise64_64
420 * and sync_file_range) requires additional code,
421 * see linux/mips/get_syscall_args.c
423 if (tcp->s_ent->nargs == MAX_ARGS) {
425 mips_REG_SP + MAX_ARGS * sizeof(tcp->u_arg[0]),
426 sizeof(tcp->u_arg[0]),
427 &tcp->u_arg[MAX_ARGS - 1]) < 0)
428 tcp->u_arg[MAX_ARGS - 1] = 0;
430 # endif /* LINUX_MIPSO32 */
432 #endif /* SYS_syscall_subcall */
435 dumpio(struct tcb *tcp)
437 int fd = tcp->u_arg[0];
441 if (is_number_in_set(fd, write_set)) {
442 switch (tcp->s_ent->sen) {
447 case SEN_mq_timedsend:
448 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
454 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1);
457 dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1);
460 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
468 if (is_number_in_set(fd, read_set)) {
469 switch (tcp->s_ent->sen) {
474 case SEN_mq_timedreceive:
475 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
480 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
484 dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
487 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
494 err_name(unsigned long err)
496 if ((err < nerrnos) && errnoent[err])
497 return errnoent[err];
502 static long get_regs(struct tcb *);
503 static int get_syscall_args(struct tcb *);
504 static int get_syscall_result(struct tcb *);
505 static int arch_get_scno(struct tcb *tcp);
506 static int arch_set_scno(struct tcb *, kernel_ulong_t);
507 static void get_error(struct tcb *, const bool);
508 static int arch_set_error(struct tcb *);
509 static int arch_set_success(struct tcb *);
511 struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
513 static struct inject_opts *
514 tcb_inject_opts(struct tcb *tcp)
516 return (scno_in_range(tcp->scno) && tcp->inject_vec[current_personality])
517 ? &tcp->inject_vec[current_personality][tcp->scno] : NULL;
522 tamper_with_syscall_entering(struct tcb *tcp, unsigned int *signo)
524 if (!tcp->inject_vec[current_personality]) {
525 tcp->inject_vec[current_personality] =
526 xcalloc(nsyscalls, sizeof(**inject_vec));
527 memcpy(tcp->inject_vec[current_personality],
528 inject_vec[current_personality],
529 nsyscalls * sizeof(**inject_vec));
532 struct inject_opts *opts = tcb_inject_opts(tcp);
534 if (!opts || opts->first == 0)
539 if (opts->first != 0)
542 opts->first = opts->step;
544 if (!recovering(tcp)) {
545 if (opts->data.flags & INJECT_F_SIGNAL)
546 *signo = opts->data.signo;
547 if (opts->data.flags & (INJECT_F_ERROR | INJECT_F_RETVAL) &&
548 !arch_set_scno(tcp, -1))
549 tcp->flags |= TCB_TAMPERED;
550 if (opts->data.flags & INJECT_F_DELAY_ENTER)
551 delay_tcb(tcp, opts->data.delay_idx, true);
552 if (opts->data.flags & INJECT_F_DELAY_EXIT)
553 tcp->flags |= TCB_INJECT_DELAY_EXIT;
560 tamper_with_syscall_exiting(struct tcb *tcp)
562 struct inject_opts *opts = tcb_inject_opts(tcp);
566 if (inject_delay_exit(tcp))
567 delay_tcb(tcp, opts->data.delay_idx, false);
569 if (!syscall_tampered(tcp))
572 if (!syserror(tcp)) {
573 error_msg("Failed to tamper with process %d: got no error "
574 "(return value %#" PRI_klx ")",
575 tcp->pid, tcp->u_rval);
580 bool update_tcb = false;
582 if (opts->data.flags & INJECT_F_RETVAL) {
583 kernel_long_t inject_rval =
584 retval_get(opts->data.rval_idx);
585 kernel_long_t u_rval = tcp->u_rval;
587 tcp->u_rval = inject_rval;
588 if (arch_set_success(tcp)) {
589 tcp->u_rval = u_rval;
595 unsigned long new_error = retval_get(opts->data.rval_idx);
597 if (new_error != tcp->u_error && new_error <= MAX_ERRNO_VALUE) {
598 unsigned long u_error = tcp->u_error;
600 tcp->u_error = new_error;
601 if (arch_set_error(tcp)) {
602 tcp->u_error = u_error;
611 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
619 * 0: "ignore this ptrace stop", bail out silently.
620 * 1: ok, decoded; call
621 * syscall_entering_finish(tcp, syscall_entering_trace(tcp, ...)).
622 * other: error; call syscall_entering_finish(tcp, res), where res is the value
626 syscall_entering_decode(struct tcb *tcp)
628 int res = get_scno(tcp);
632 if (res != 1 || (res = get_syscall_args(tcp)) != 1) {
634 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
636 * " <unavailable>" will be added later by the code which
637 * detects ptrace errors.
642 #if defined SYS_ipc_subcall \
643 || defined SYS_socket_subcall \
644 || defined SYS_syscall_subcall
646 switch (tcp->s_ent->sen) {
647 # ifdef SYS_ipc_subcall
649 decode_ipc_subcall(tcp);
652 # ifdef SYS_socket_subcall
654 decode_socket_subcall(tcp);
657 # ifdef SYS_syscall_subcall
659 decode_syscall_subcall(tcp);
660 if (tcp->s_ent->sen != SEN_syscall)
673 syscall_entering_trace(struct tcb *tcp, unsigned int *sig)
675 /* Restrain from fault injection while the trace executes strace code. */
677 tcp->qual_flg &= ~QUAL_INJECT;
680 switch (tcp->s_ent->sen) {
683 #if defined SPARC || defined SPARC64
686 tcp->flags &= ~TCB_HIDE_LOG;
690 if (!traced(tcp) || (tracing_paths && !pathtrace_match(tcp))) {
691 tcp->flags |= TCB_FILTERED;
695 tcp->flags &= ~TCB_FILTERED;
702 tamper_with_syscall_entering(tcp, sig);
704 if (cflag == CFLAG_ONLY_STATS) {
708 #ifdef ENABLE_STACKTRACE
709 if (stack_trace_enabled) {
710 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
711 unwind_tcb_capture(tcp);
716 tprintf("%s(", tcp->s_ent->sys_name);
717 int res = raw(tcp) ? printargs(tcp) : tcp->s_ent->sys_func(tcp);
723 syscall_entering_finish(struct tcb *tcp, int res)
725 tcp->flags |= TCB_INSYSCALL;
726 tcp->sys_func_rval = res;
727 /* Measure the entrance time as late as possible to avoid errors. */
728 if ((Tflag || cflag) && !filtered(tcp))
729 clock_gettime(CLOCK_MONOTONIC, &tcp->etime);
735 * -1: error in one of ptrace ops.
737 * If not 0, call syscall_exiting_trace(tcp, res), where res is the return
738 * value. Anyway, call syscall_exiting_finish(tcp) then.
741 syscall_exiting_decode(struct tcb *tcp, struct timespec *pts)
743 /* Measure the exit time as early as possible to avoid errors. */
744 if ((Tflag || cflag) && !(filtered(tcp) || hide_log(tcp)))
745 clock_gettime(CLOCK_MONOTONIC, pts);
747 if (mmap_cache_is_enabled()) {
748 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
749 mmap_cache_invalidate(tcp);
752 if (filtered(tcp) || hide_log(tcp))
755 #if SUPPORTED_PERSONALITIES > 1
756 update_personality(tcp, tcp->currpers);
759 return get_syscall_result(tcp);
763 syscall_exiting_trace(struct tcb *tcp, struct timespec *ts, int res)
765 if (syscall_tampered(tcp) || inject_delay_exit(tcp))
766 tamper_with_syscall_exiting(tcp);
769 count_syscall(tcp, ts);
770 if (cflag == CFLAG_ONLY_STATS) {
775 /* If not in -ff mode, and printing_tcp != tcp,
776 * then the log currently does not end with output
777 * of _our syscall entry_, but with something else.
778 * We need to say which syscall's return is this.
780 * Forced reprinting via TCB_REPRINT is used only by
781 * "strace -ff -oLOG test/threaded_execve" corner case.
782 * It's the only case when -ff mode needs reprinting.
784 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
785 tcp->flags &= ~TCB_REPRINT;
787 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
791 tcp->s_prev_ent = NULL;
793 /* There was error in one of prior ptrace ops */
796 tprints("= ? <unavailable>\n");
800 tcp->s_prev_ent = tcp->s_ent;
804 /* sys_res = printargs(tcp); - but it's nop on sysexit */
806 /* FIXME: not_failing_only (IOW, option -z) is broken:
807 * failure of syscall is known only after syscall return.
808 * Thus we end up with something like this on, say, ENOENT:
809 * open("does_not_exist", O_RDONLY <unfinished ...>
810 * {next syscall decode}
811 * whereas the intended result is that open(...) line
812 * is not shown at all.
814 if (not_failing_only && tcp->u_error)
815 return 0; /* ignore failed syscalls */
816 if (tcp->sys_func_rval & RVAL_DECODED)
817 sys_res = tcp->sys_func_rval;
819 sys_res = tcp->s_ent->sys_func(tcp);
824 unsigned long u_error = tcp->u_error;
828 tprintf("= -1 (errno %lu)", u_error);
830 tprintf("= %#" PRI_klx, tcp->u_rval);
832 if (syscall_tampered(tcp))
833 tprints(" (INJECTED)");
834 } else if (!(sys_res & RVAL_NONE) && u_error) {
835 const char *u_error_str;
838 /* Blocked signals do not interrupt any syscalls.
839 * In this case syscalls don't return ERESTARTfoo codes.
841 * Deadly signals set to SIG_DFL interrupt syscalls
842 * and kill the process regardless of which of the codes below
843 * is returned by the interrupted syscall.
844 * In some cases, kernel forces a kernel-generated deadly
845 * signal to be unblocked and set to SIG_DFL (and thus cause
846 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
847 * or SIGILL. (The alternative is to leave process spinning
848 * forever on the faulty instruction - not useful).
850 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
851 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
852 * but kernel will always restart them.
855 /* Most common type of signal-interrupted syscall exit code.
856 * The system call will be restarted with the same arguments
857 * if SA_RESTART is set; otherwise, it will fail with EINTR.
859 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
862 /* Rare. For example, fork() returns this if interrupted.
863 * SA_RESTART is ignored (assumed set): the restart is unconditional.
865 tprints("= ? ERESTARTNOINTR (To be restarted)");
868 /* pause(), rt_sigsuspend() etc use this code.
869 * SA_RESTART is ignored (assumed not set):
870 * syscall won't restart (will return EINTR instead)
871 * even after signal with SA_RESTART set. However,
872 * after SIG_IGN or SIG_DFL signal it will restart
873 * (thus the name "restart only if has no handler").
875 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
877 case ERESTART_RESTARTBLOCK:
878 /* Syscalls like nanosleep(), poll() which can't be
879 * restarted with their original arguments use this
880 * code. Kernel will execute restart_syscall() instead,
881 * which changes arguments before restarting syscall.
882 * SA_RESTART is ignored (assumed not set) similarly
883 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
884 * since restart data is saved in "restart block"
885 * in task struct, and if signal handler uses a syscall
886 * which in turn saves another such restart block,
887 * old data is lost and restart becomes impossible)
889 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
892 u_error_str = err_name(u_error);
894 tprintf("= %" PRI_kld " %s (%s)", tcp->u_rval,
895 u_error_str, strerror(u_error));
897 tprintf("= %" PRI_kld " %lu (%s)", tcp->u_rval,
898 u_error, strerror(u_error));
901 if (syscall_tampered(tcp))
902 tprints(" (INJECTED)");
903 if ((sys_res & RVAL_STR) && tcp->auxstr)
904 tprintf(" (%s)", tcp->auxstr);
906 if (sys_res & RVAL_NONE)
909 switch (sys_res & RVAL_MASK) {
911 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
912 if (current_klongsize < sizeof(tcp->u_rval)) {
914 (unsigned int) tcp->u_rval);
918 tprintf("= %#" PRI_klx, tcp->u_rval);
923 print_numeric_long_umask(tcp->u_rval);
926 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
927 if (current_klongsize < sizeof(tcp->u_rval)) {
929 (unsigned int) tcp->u_rval);
933 tprintf("= %" PRI_klu, tcp->u_rval);
939 printfd(tcp, tcp->u_rval);
941 tprintf("= %" PRI_kld, tcp->u_rval);
944 error_msg("invalid rval format");
948 if ((sys_res & RVAL_STR) && tcp->auxstr)
949 tprintf(" (%s)", tcp->auxstr);
950 if (syscall_tampered(tcp))
951 tprints(" (INJECTED)");
954 ts_sub(ts, ts, &tcp->etime);
955 tprintf(" <%ld.%06ld>",
956 (long) ts->tv_sec, (long) ts->tv_nsec / 1000);
962 #ifdef ENABLE_STACKTRACE
963 if (stack_trace_enabled)
964 unwind_tcb_print(tcp);
970 syscall_exiting_finish(struct tcb *tcp)
972 tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED | TCB_INJECT_DELAY_EXIT);
973 tcp->sys_func_rval = 0;
974 free_tcb_priv_data(tcp);
978 is_erestart(struct tcb *tcp)
980 switch (tcp->u_error) {
984 case ERESTART_RESTARTBLOCK:
991 static unsigned long saved_u_error;
994 temporarily_clear_syserror(struct tcb *tcp)
996 saved_u_error = tcp->u_error;
1001 restore_cleared_syserror(struct tcb *tcp)
1003 tcp->u_error = saved_u_error;
1006 #include "arch_regs.c"
1008 #if HAVE_ARCH_GETRVAL2
1009 # include "arch_getrval2.c"
1013 print_pc(struct tcb *tcp)
1015 #if defined ARCH_PC_REG
1016 # define ARCH_GET_PC 0
1017 #elif defined ARCH_PC_PEEK_ADDR
1019 # define ARCH_PC_REG pc
1020 # define ARCH_GET_PC upeek(tcp, ARCH_PC_PEEK_ADDR, &pc)
1022 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1024 if (get_regs(tcp) < 0 || ARCH_GET_PC)
1025 tprints(current_wordsize == 4 ? "[????????] "
1026 : "[????????????????] ");
1028 tprintf(current_wordsize == 4
1029 ? "[%08" PRI_klx "] " : "[%016" PRI_klx "] ",
1030 (kernel_ulong_t) ARCH_PC_REG);
1033 #include "getregs_old.h"
1035 #undef ptrace_getregset_or_getregs
1036 #undef ptrace_setregset_or_setregs
1037 #ifdef ARCH_REGS_FOR_GETREGSET
1039 # define ptrace_getregset_or_getregs ptrace_getregset
1041 ptrace_getregset(pid_t pid)
1043 # ifdef ARCH_IOVEC_FOR_GETREGSET
1044 /* variable iovec */
1045 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1046 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1047 &ARCH_IOVEC_FOR_GETREGSET);
1049 /* constant iovec */
1050 static struct iovec io = {
1051 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1052 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1054 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1059 # ifndef HAVE_GETREGS_OLD
1060 # define ptrace_setregset_or_setregs ptrace_setregset
1062 ptrace_setregset(pid_t pid)
1064 # ifdef ARCH_IOVEC_FOR_GETREGSET
1065 /* variable iovec */
1066 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1067 &ARCH_IOVEC_FOR_GETREGSET);
1069 /* constant iovec */
1070 static struct iovec io = {
1071 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1072 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1074 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1077 # endif /* !HAVE_GETREGS_OLD */
1079 #elif defined ARCH_REGS_FOR_GETREGS
1081 # define ptrace_getregset_or_getregs ptrace_getregs
1083 ptrace_getregs(pid_t pid)
1085 # if defined SPARC || defined SPARC64
1086 /* SPARC systems have the meaning of data and addr reversed */
1087 return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1089 return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1093 # ifndef HAVE_GETREGS_OLD
1094 # define ptrace_setregset_or_setregs ptrace_setregs
1096 ptrace_setregs(pid_t pid)
1098 # if defined SPARC || defined SPARC64
1099 /* SPARC systems have the meaning of data and addr reversed */
1100 return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1102 return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1105 # endif /* !HAVE_GETREGS_OLD */
1107 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1109 #ifdef ptrace_getregset_or_getregs
1110 static long get_regs_error;
1114 clear_regs(struct tcb *tcp)
1116 #ifdef ptrace_getregset_or_getregs
1117 get_regs_error = -1;
1122 get_regs(struct tcb *const tcp)
1124 #ifdef ptrace_getregset_or_getregs
1126 if (get_regs_error != -1)
1127 return get_regs_error;
1129 # ifdef HAVE_GETREGS_OLD
1131 * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1132 * fallback to getregs_old.
1134 static int use_getregs_old;
1135 if (use_getregs_old < 0) {
1136 return get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1137 } else if (use_getregs_old == 0) {
1138 get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1139 if (get_regs_error >= 0) {
1140 use_getregs_old = -1;
1141 return get_regs_error;
1143 if (errno == EPERM || errno == ESRCH)
1144 return get_regs_error;
1145 use_getregs_old = 1;
1147 return get_regs_error = getregs_old(tcp);
1148 # else /* !HAVE_GETREGS_OLD */
1149 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1150 get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1152 # if defined ARCH_PERSONALITY_0_IOV_SIZE
1154 return get_regs_error;
1156 switch (ARCH_IOVEC_FOR_GETREGSET.iov_len) {
1157 case ARCH_PERSONALITY_0_IOV_SIZE:
1158 update_personality(tcp, 0);
1160 case ARCH_PERSONALITY_1_IOV_SIZE:
1161 update_personality(tcp, 1);
1164 static bool printed = false;
1167 error_msg("Unsupported regset size returned by "
1168 "PTRACE_GETREGSET: %zu",
1169 ARCH_IOVEC_FOR_GETREGSET.iov_len);
1174 update_personality(tcp, 0);
1177 # endif /* ARCH_PERSONALITY_0_IOV_SIZE */
1179 return get_regs_error;
1181 # endif /* !HAVE_GETREGS_OLD */
1183 #else /* !ptrace_getregset_or_getregs */
1185 # warning get_regs is not implemented for this architecture yet
1188 #endif /* !ptrace_getregset_or_getregs */
1191 #ifdef ptrace_setregset_or_setregs
1195 return ptrace_setregset_or_setregs(pid);
1197 #endif /* ptrace_setregset_or_setregs */
1202 char buf[sizeof("syscall_0x") + sizeof(kernel_ulong_t) * 2];
1206 free_sysent_buf(void *ptr)
1208 struct sysent_buf *s = ptr;
1209 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1215 * 0: "ignore this ptrace stop", syscall_entering_decode() should return a "bail
1216 * out silently" code.
1217 * 1: ok, continue in syscall_entering_decode().
1218 * other: error, syscall_entering_decode() should print error indicator
1219 * ("????" etc) and return an appropriate code.
1222 get_scno(struct tcb *tcp)
1224 if (get_regs(tcp) < 0)
1227 int rc = arch_get_scno(tcp);
1231 tcp->scno = shuffle_scno(tcp->scno);
1233 if (scno_is_valid(tcp->scno)) {
1234 tcp->s_ent = &sysent[tcp->scno];
1235 tcp->qual_flg = qual_flags(tcp->scno);
1237 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1240 s->ent.nargs = MAX_ARGS;
1241 s->ent.sen = SEN_printargs;
1242 s->ent.sys_func = printargs;
1243 s->ent.sys_name = s->buf;
1244 xsprintf(s->buf, "syscall_%#" PRI_klx, shuffle_scno(tcp->scno));
1246 tcp->s_ent = &s->ent;
1247 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1249 set_tcb_priv_data(tcp, s, free_sysent_buf);
1251 debug_msg("pid %d invalid syscall %#" PRI_klx,
1252 tcp->pid, shuffle_scno(tcp->scno));
1256 * We refrain from argument decoding during recovering
1257 * as tracee memory mappings has changed and the registers
1258 * are very likely pointing to garbage already.
1260 if (recovering(tcp))
1261 tcp->qual_flg |= QUAL_RAW;
1266 #ifdef ptrace_getregset_or_getregs
1267 # define get_syscall_result_regs get_regs
1269 static int get_syscall_result_regs(struct tcb *);
1273 * 1: ok, continue in syscall_exiting_trace().
1274 * -1: error, syscall_exiting_trace() should print error indicator
1275 * ("????" etc) and bail out.
1278 get_syscall_result(struct tcb *tcp)
1280 if (get_syscall_result_regs(tcp) < 0)
1284 !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS)
1285 || syscall_tampered(tcp));
1290 #include "get_scno.c"
1291 #include "set_scno.c"
1292 #include "get_syscall_args.c"
1293 #ifndef ptrace_getregset_or_getregs
1294 # include "get_syscall_result.c"
1296 #include "get_error.c"
1297 #include "set_error.c"
1298 #ifdef HAVE_GETREGS_OLD
1299 # include "getregs_old.c"
1301 #include "shuffle_scno.c"
1304 syscall_name(kernel_ulong_t scno)
1306 return scno_is_valid(scno) ? sysent[scno].sys_name : NULL;