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-2019 The strace developers.
10 * All rights reserved.
12 * SPDX-License-Identifier: LGPL-2.1-or-later
16 #include "get_personality.h"
17 #include "mmap_notify.h"
18 #include "native_defs.h"
20 #include "ptrace_syscall_info.h"
22 #include "number_set.h"
27 /* for struct iovec */
30 /* for __X32_SYSCALL_BIT */
36 # undef PTRACE_GETREGS
37 # define PTRACE_GETREGS PTRACE_GETREGS64
38 # undef PTRACE_SETREGS
39 # define PTRACE_SETREGS PTRACE_SETREGS64
44 #include "syscallent_base_nr.h"
46 /* Define these shorthand notations to simplify the syscallent files. */
47 #include "sysent_shorthand_defs.h"
49 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
51 const struct_sysent sysent0[] = {
52 #include "syscallent.h"
55 #if SUPPORTED_PERSONALITIES > 1
56 # include PERSONALITY1_INCLUDE_FUNCS
57 static const struct_sysent sysent1[] = {
58 # include "syscallent1.h"
62 #if SUPPORTED_PERSONALITIES > 2
63 # include PERSONALITY2_INCLUDE_FUNCS
64 static const struct_sysent sysent2[] = {
65 # include "syscallent2.h"
69 /* Now undef them since short defines cause wicked namespace pollution. */
70 #include "sysent_shorthand_undefs.h"
72 const char *const errnoent[] = {
75 const char *const signalent[] = {
76 #include "signalent.h"
80 * `ioctlent[012].h' files are automatically generated by the auxiliary
81 * program `ioctlsort', such that the list is sorted by the `code' field.
82 * This has the side-effect of resolving the _IO.. macros into
83 * plain integers, eliminating the need to include here everything
87 const struct_ioctlent ioctlent0[] = {
88 #include "ioctlent0.h"
91 #if SUPPORTED_PERSONALITIES > 1
92 static const struct_ioctlent ioctlent1[] = {
93 # include "ioctlent1.h"
95 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
96 static const struct_printers printers0 = {
97 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
99 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
100 static const struct_printers printers1 = {
101 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
105 #if SUPPORTED_PERSONALITIES > 2
106 static const struct_ioctlent ioctlent2[] = {
107 # include "ioctlent2.h"
109 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
110 static const struct_printers printers2 = {
111 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
116 nsyscalls0 = ARRAY_SIZE(sysent0)
117 #if SUPPORTED_PERSONALITIES > 1
118 , nsyscalls1 = ARRAY_SIZE(sysent1)
119 # if SUPPORTED_PERSONALITIES > 2
120 , nsyscalls2 = ARRAY_SIZE(sysent2)
126 nioctlents0 = ARRAY_SIZE(ioctlent0)
127 #if SUPPORTED_PERSONALITIES > 1
128 , nioctlents1 = ARRAY_SIZE(ioctlent1)
129 # if SUPPORTED_PERSONALITIES > 2
130 , nioctlents2 = ARRAY_SIZE(ioctlent2)
135 #if SUPPORTED_PERSONALITIES > 1
136 const struct_sysent *sysent = sysent0;
137 const struct_ioctlent *ioctlent = ioctlent0;
138 const struct_printers *printers = &printers0;
141 const unsigned int nerrnos = ARRAY_SIZE(errnoent);
142 const unsigned int nsignals = ARRAY_SIZE(signalent);
143 unsigned nsyscalls = nsyscalls0;
144 unsigned nioctlents = nioctlents0;
146 const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES] = {
148 #if SUPPORTED_PERSONALITIES > 1
151 #if SUPPORTED_PERSONALITIES > 2
155 const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
157 #if SUPPORTED_PERSONALITIES > 1
160 #if SUPPORTED_PERSONALITIES > 2
165 const char *const personality_names[] =
167 {"64 bit", "32 bit", "x32"}
170 #elif SUPPORTED_PERSONALITIES == 2
173 {STRINGIFY_VAL(__WORDSIZE) " bit"}
177 const char *const personality_designators[] =
179 { "64", "32", "x32" }
182 #elif SUPPORTED_PERSONALITIES == 2
185 { STRINGIFY_VAL(__WORDSIZE) }
189 #if SUPPORTED_PERSONALITIES > 1
191 unsigned current_personality;
193 # ifndef current_wordsize
194 unsigned current_wordsize = PERSONALITY0_WORDSIZE;
195 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
196 PERSONALITY0_WORDSIZE,
197 PERSONALITY1_WORDSIZE,
198 # if SUPPORTED_PERSONALITIES > 2
199 PERSONALITY2_WORDSIZE,
204 # ifndef current_klongsize
205 unsigned current_klongsize = PERSONALITY0_KLONGSIZE;
206 static const int personality_klongsize[SUPPORTED_PERSONALITIES] = {
207 PERSONALITY0_KLONGSIZE,
208 PERSONALITY1_KLONGSIZE,
209 # if SUPPORTED_PERSONALITIES > 2
210 PERSONALITY2_KLONGSIZE,
216 set_personality(unsigned int personality)
218 if (personality == current_personality)
221 if (personality >= SUPPORTED_PERSONALITIES)
222 error_msg_and_die("Requested switch to unsupported personality "
225 nsyscalls = nsyscall_vec[personality];
226 sysent = sysent_vec[personality];
228 switch (personality) {
230 ioctlent = ioctlent0;
231 nioctlents = nioctlents0;
232 printers = &printers0;
236 ioctlent = ioctlent1;
237 nioctlents = nioctlents1;
238 printers = &printers1;
241 # if SUPPORTED_PERSONALITIES > 2
243 ioctlent = ioctlent2;
244 nioctlents = nioctlents2;
245 printers = &printers2;
250 current_personality = personality;
251 # ifndef current_wordsize
252 current_wordsize = personality_wordsize[personality];
254 # ifndef current_klongsize
255 current_klongsize = personality_klongsize[personality];
260 update_personality(struct tcb *tcp, unsigned int personality)
262 static bool need_mpers_warning[] =
263 { false, !HAVE_PERSONALITY_1_MPERS, !HAVE_PERSONALITY_2_MPERS };
265 set_personality(personality);
267 if (personality == tcp->currpers)
269 tcp->currpers = personality;
272 error_msg("[ Process PID=%d runs in %s mode. ]",
273 tcp->pid, personality_names[personality]);
276 if (need_mpers_warning[personality]) {
277 error_msg("WARNING: Proper structure decoding for this "
278 "personality is not supported, please consider "
279 "building strace with mpers support enabled.");
280 need_mpers_warning[personality] = false;
285 #ifdef SYS_socket_subcall
287 decode_socket_subcall(struct tcb *tcp)
289 const int call = tcp->u_arg[0];
291 if (call < 1 || call >= SYS_socket_nsubcalls)
294 const kernel_ulong_t scno = SYS_socket_subcall + call;
295 const unsigned int nargs = sysent[scno].nargs;
298 if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
302 tcp->qual_flg = qual_flags(scno);
303 tcp->s_ent = &sysent[scno];
306 for (i = 0; i < nargs; ++i)
307 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
308 ? ((uint32_t *) (void *) buf)[i] : buf[i];
310 #endif /* SYS_socket_subcall */
312 #ifdef SYS_ipc_subcall
314 decode_ipc_subcall(struct tcb *tcp)
316 unsigned int call = tcp->u_arg[0];
317 const unsigned int version = call >> 16;
320 # if defined S390 || defined S390X
324 if (current_wordsize == 8)
327 set_tcb_priv_ulong(tcp, version);
333 case 1: case 2: case 3: case 4:
334 case 11: case 12: case 13: case 14:
335 case 21: case 22: case 23: case 24:
341 tcp->scno = SYS_ipc_subcall + call;
342 tcp->qual_flg = qual_flags(tcp->scno);
343 tcp->s_ent = &sysent[tcp->scno];
345 const unsigned int n = n_args(tcp);
347 for (i = 0; i < n; i++)
348 tcp->u_arg[i] = tcp->u_arg[i + 1];
350 #endif /* SYS_ipc_subcall */
352 #ifdef SYS_syscall_subcall
353 /* The implementation is architecture specific. */
354 static void decode_syscall_subcall(struct tcb *);
355 #endif /* SYS_syscall_subcall */
358 dumpio(struct tcb *tcp)
360 int fd = tcp->u_arg[0];
364 if (is_number_in_set(fd, write_set)) {
365 switch (tcp_sysent(tcp)->sen) {
370 case SEN_mq_timedsend_time32:
371 case SEN_mq_timedsend_time64:
372 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
378 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1);
381 dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1);
384 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
392 if (is_number_in_set(fd, read_set)) {
393 switch (tcp_sysent(tcp)->sen) {
398 case SEN_mq_timedreceive_time32:
399 case SEN_mq_timedreceive_time64:
400 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
405 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
409 dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
412 case SEN_recvmmsg_time32:
413 case SEN_recvmmsg_time64:
414 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
421 err_name(uint64_t err)
423 return err < nerrnos ? errnoent[err] : NULL;
427 print_err(int64_t err, bool negated)
429 const char *str = err_name(negated ? -err : err);
431 if (!str || xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
432 tprintf(negated ? "%" PRId64 : "%" PRIu64, err);
433 if (!str || xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
435 (xlat_verbose(xlat_verbosity) == XLAT_STYLE_ABBREV
436 ? tprintf : tprintf_comment)("%s%s",
437 negated ? "-" : "", str);
441 print_err_ret(kernel_ulong_t ret, unsigned long u_error)
443 const char *u_error_str = err_name(u_error);
446 tprintf("= %" PRI_kld " %s (%s)",
447 ret, u_error_str, strerror(u_error));
449 tprintf("= %" PRI_kld " (errno %lu)", ret, u_error);
452 static long get_regs(struct tcb *);
453 static int get_syscall_args(struct tcb *);
454 static int get_syscall_result(struct tcb *);
455 static void get_error(struct tcb *, bool);
456 static void set_error(struct tcb *, unsigned long);
457 static void set_success(struct tcb *, kernel_long_t);
458 static int arch_get_scno(struct tcb *);
459 static int arch_check_scno(struct tcb *);
460 static int arch_set_scno(struct tcb *, kernel_ulong_t);
461 static int arch_get_syscall_args(struct tcb *);
462 static void arch_get_error(struct tcb *, bool);
463 static int arch_set_error(struct tcb *);
464 static int arch_set_success(struct tcb *);
466 static void arch_get_syscall_args_extra(struct tcb *, unsigned int);
469 struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
471 static struct inject_opts *
472 tcb_inject_opts(struct tcb *tcp)
474 return (scno_in_range(tcp->scno) && tcp->inject_vec[current_personality])
475 ? &tcp->inject_vec[current_personality][tcp->scno] : NULL;
480 tamper_with_syscall_entering(struct tcb *tcp, unsigned int *signo)
482 if (!tcp->inject_vec[current_personality]) {
483 tcp->inject_vec[current_personality] =
484 xcalloc(nsyscalls, sizeof(**inject_vec));
485 memcpy(tcp->inject_vec[current_personality],
486 inject_vec[current_personality],
487 nsyscalls * sizeof(**inject_vec));
490 struct inject_opts *opts = tcb_inject_opts(tcp);
492 if (!opts || opts->first == 0)
497 if (opts->first != 0)
500 opts->first = opts->step;
502 if (!recovering(tcp)) {
503 if (opts->data.flags & INJECT_F_SIGNAL)
504 *signo = opts->data.signo;
505 if (opts->data.flags & (INJECT_F_ERROR | INJECT_F_RETVAL)) {
507 (opts->data.flags & INJECT_F_SYSCALL)
508 ? (kernel_long_t) shuffle_scno(opts->data.scno)
511 if (!arch_set_scno(tcp, scno)) {
512 tcp->flags |= TCB_TAMPERED;
514 tcp->flags |= TCB_TAMPERED_NO_FAIL;
515 #if ARCH_NEEDS_SET_ERROR_FOR_SCNO_TAMPERING
517 * So far it's just a workaround for hppa,
518 * but let's pretend it could be used elsewhere.
522 (opts->data.flags & INJECT_F_RETVAL) ?
523 ENOSYS : retval_get(opts->data.rval_idx);
525 tcp->u_error = 0; /* force reset */
526 set_error(tcp, rval);
531 if (opts->data.flags & INJECT_F_DELAY_ENTER)
532 delay_tcb(tcp, opts->data.delay_idx, true);
533 if (opts->data.flags & INJECT_F_DELAY_EXIT)
534 tcp->flags |= TCB_INJECT_DELAY_EXIT;
541 tamper_with_syscall_exiting(struct tcb *tcp)
543 struct inject_opts *opts = tcb_inject_opts(tcp);
547 if (inject_delay_exit(tcp))
548 delay_tcb(tcp, opts->data.delay_idx, false);
550 if (!syscall_tampered(tcp))
553 if (!syserror(tcp) ^ !!syscall_tampered_nofail(tcp)) {
554 error_msg("Failed to tamper with process %d: unexpectedly got"
555 " %serror (return value %#" PRI_klx ", error %lu)",
556 tcp->pid, syscall_tampered_nofail(tcp) ? "" : "no ",
557 tcp->u_rval, tcp->u_error);
562 if (opts->data.flags & INJECT_F_RETVAL)
563 set_success(tcp, retval_get(opts->data.rval_idx));
565 set_error(tcp, retval_get(opts->data.rval_idx));
572 * 0: "ignore this ptrace stop", bail out silently.
573 * 1: ok, decoded; call
574 * syscall_entering_finish(tcp, syscall_entering_trace(tcp, ...)).
575 * other: error; call syscall_entering_finish(tcp, res), where res is the value
579 syscall_entering_decode(struct tcb *tcp)
581 int res = get_scno(tcp);
584 if (res != 1 || (res = get_syscall_args(tcp)) != 1) {
586 tprintf("%s(", tcp_sysent(tcp)->sys_name);
588 * " <unavailable>" will be added later by the code which
589 * detects ptrace errors.
594 #ifdef SYS_syscall_subcall
595 if (tcp_sysent(tcp)->sen == SEN_syscall)
596 decode_syscall_subcall(tcp);
598 #if defined SYS_ipc_subcall \
599 || defined SYS_socket_subcall
600 switch (tcp_sysent(tcp)->sen) {
601 # ifdef SYS_ipc_subcall
603 decode_ipc_subcall(tcp);
606 # ifdef SYS_socket_subcall
608 decode_socket_subcall(tcp);
618 syscall_entering_trace(struct tcb *tcp, unsigned int *sig)
622 * Restrain from fault injection
623 * while the trace executes strace code.
625 tcp->qual_flg &= ~QUAL_INJECT;
627 switch (tcp_sysent(tcp)->sen) {
632 * First exec* syscall makes the log visible.
634 tcp->flags &= ~TCB_HIDE_LOG;
636 * Check whether this exec* syscall succeeds.
638 tcp->flags |= TCB_CHECK_EXEC_SYSCALL;
643 if (hide_log(tcp) || !traced(tcp) || (tracing_paths && !pathtrace_match(tcp))) {
644 tcp->flags |= TCB_FILTERED;
648 tcp->flags &= ~TCB_FILTERED;
651 tamper_with_syscall_entering(tcp, sig);
653 if (cflag == CFLAG_ONLY_STATS) {
657 #ifdef ENABLE_STACKTRACE
658 if (stack_trace_enabled) {
659 if (tcp_sysent(tcp)->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
660 unwind_tcb_capture(tcp);
664 if (!is_complete_set(status_set, NUMBER_OF_STATUSES))
665 strace_open_memstream(tcp);
668 tprintf("%s(", tcp_sysent(tcp)->sys_name);
669 int res = raw(tcp) ? printargs(tcp) : tcp_sysent(tcp)->sys_func(tcp);
675 syscall_entering_finish(struct tcb *tcp, int res)
677 tcp->flags |= TCB_INSYSCALL;
678 tcp->sys_func_rval = res;
680 /* Measure the entrance time as late as possible to avoid errors. */
681 if ((Tflag || cflag) && !filtered(tcp))
682 clock_gettime(CLOCK_MONOTONIC, &tcp->etime);
684 /* Start tracking system time */
689 ts_sub(&dt, &tcp->stime, &tcp->ltime);
692 debug_func_msg("pid %d: %.9f seconds of system "
693 "time spent since the last "
695 tcp->pid, ts_float(&dt));
698 tcp->ltime = tcp->stime;
705 * -1: error in one of ptrace ops.
707 * If not 0, call syscall_exiting_trace(tcp, res), where res is the return
708 * value. Anyway, call syscall_exiting_finish(tcp) then.
711 syscall_exiting_decode(struct tcb *tcp, struct timespec *pts)
713 /* Measure the exit time as early as possible to avoid errors. */
714 if ((Tflag || cflag) && !filtered(tcp))
715 clock_gettime(CLOCK_MONOTONIC, pts);
717 if (tcp_sysent(tcp)->sys_flags & MEMORY_MAPPING_CHANGE)
718 mmap_notify_report(tcp);
723 if (check_exec_syscall(tcp)) {
724 /* The check failed, hide the log. */
725 tcp->flags |= TCB_HIDE_LOG;
728 #if SUPPORTED_PERSONALITIES > 1
729 update_personality(tcp, tcp->currpers);
732 return get_syscall_result(tcp);
736 print_syscall_resume(struct tcb *tcp)
738 /* If not in -ff mode, and printing_tcp != tcp,
739 * then the log currently does not end with output
740 * of _our syscall entry_, but with something else.
741 * We need to say which syscall's return is this.
743 * Forced reprinting via TCB_REPRINT is used only by
744 * "strace -ff -oLOG test/threaded_execve" corner case.
745 * It's the only case when -ff mode needs reprinting.
747 if ((followfork < 2 && printing_tcp != tcp && !tcp->staged_output_data)
748 || (tcp->flags & TCB_REPRINT)) {
749 tcp->flags &= ~TCB_REPRINT;
751 tprintf("<... %s resumed>", tcp_sysent(tcp)->sys_name);
756 syscall_exiting_trace(struct tcb *tcp, struct timespec *ts, int res)
758 if (syscall_tampered(tcp) || inject_delay_exit(tcp))
759 tamper_with_syscall_exiting(tcp);
762 count_syscall(tcp, ts);
763 if (cflag == CFLAG_ONLY_STATS) {
768 print_syscall_resume(tcp);
771 tcp->s_prev_ent = NULL;
773 /* There was error in one of prior ptrace ops */
776 tprints("= ? <unavailable>\n");
777 if (!is_complete_set(status_set, NUMBER_OF_STATUSES)) {
778 bool publish = is_number_in_set(STATUS_UNAVAILABLE,
780 strace_close_memstream(tcp, publish);
785 tcp->s_prev_ent = tcp->s_ent;
789 /* sys_res = printargs(tcp); - but it's nop on sysexit */
791 if (tcp->sys_func_rval & RVAL_DECODED)
792 sys_res = tcp->sys_func_rval;
794 sys_res = tcp_sysent(tcp)->sys_func(tcp);
797 if (!is_complete_set(status_set, NUMBER_OF_STATUSES)) {
798 bool publish = syserror(tcp)
799 && is_number_in_set(STATUS_FAILED, status_set);
800 publish |= !syserror(tcp)
801 && is_number_in_set(STATUS_SUCCESSFUL, status_set);
802 strace_close_memstream(tcp, publish);
814 print_err_ret(tcp->u_rval, tcp->u_error);
816 tprintf("= %#" PRI_klx, tcp->u_rval);
818 if (syscall_tampered(tcp))
819 tprints(" (INJECTED)");
820 } else if (!(sys_res & RVAL_NONE) && tcp->u_error) {
821 switch (tcp->u_error) {
822 /* Blocked signals do not interrupt any syscalls.
823 * In this case syscalls don't return ERESTARTfoo codes.
825 * Deadly signals set to SIG_DFL interrupt syscalls
826 * and kill the process regardless of which of the codes below
827 * is returned by the interrupted syscall.
828 * In some cases, kernel forces a kernel-generated deadly
829 * signal to be unblocked and set to SIG_DFL (and thus cause
830 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
831 * or SIGILL. (The alternative is to leave process spinning
832 * forever on the faulty instruction - not useful).
834 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
835 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
836 * but kernel will always restart them.
839 /* Most common type of signal-interrupted syscall exit code.
840 * The system call will be restarted with the same arguments
841 * if SA_RESTART is set; otherwise, it will fail with EINTR.
843 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
846 /* Rare. For example, fork() returns this if interrupted.
847 * SA_RESTART is ignored (assumed set): the restart is unconditional.
849 tprints("= ? ERESTARTNOINTR (To be restarted)");
852 /* pause(), rt_sigsuspend() etc use this code.
853 * SA_RESTART is ignored (assumed not set):
854 * syscall won't restart (will return EINTR instead)
855 * even after signal with SA_RESTART set. However,
856 * after SIG_IGN or SIG_DFL signal it will restart
857 * (thus the name "restart only if has no handler").
859 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
861 case ERESTART_RESTARTBLOCK:
862 /* Syscalls like nanosleep(), poll() which can't be
863 * restarted with their original arguments use this
864 * code. Kernel will execute restart_syscall() instead,
865 * which changes arguments before restarting syscall.
866 * SA_RESTART is ignored (assumed not set) similarly
867 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
868 * since restart data is saved in "restart block"
869 * in task struct, and if signal handler uses a syscall
870 * which in turn saves another such restart block,
871 * old data is lost and restart becomes impossible)
873 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
876 print_err_ret(tcp->u_rval, tcp->u_error);
879 if (syscall_tampered(tcp))
880 tprints(" (INJECTED)");
881 if ((sys_res & RVAL_STR) && tcp->auxstr)
882 tprintf(" (%s)", tcp->auxstr);
884 if (sys_res & RVAL_NONE)
887 switch (sys_res & RVAL_MASK) {
889 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
890 if (current_klongsize < sizeof(tcp->u_rval)) {
892 (unsigned int) tcp->u_rval);
896 tprintf("= %#" PRI_klx, tcp->u_rval);
901 print_numeric_long_umask(tcp->u_rval);
904 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
905 if (current_klongsize < sizeof(tcp->u_rval)) {
907 (unsigned int) tcp->u_rval);
911 tprintf("= %" PRI_klu, tcp->u_rval);
917 printfd(tcp, tcp->u_rval);
919 tprintf("= %" PRI_kld, tcp->u_rval);
922 error_msg("invalid rval format");
926 if ((sys_res & RVAL_STR) && tcp->auxstr)
927 tprintf(" (%s)", tcp->auxstr);
928 if (syscall_tampered(tcp))
929 tprints(" (INJECTED)");
932 ts_sub(ts, ts, &tcp->etime);
933 tprintf(" <%ld.%06ld>",
934 (long) ts->tv_sec, (long) ts->tv_nsec / 1000);
940 #ifdef ENABLE_STACKTRACE
941 if (stack_trace_enabled)
942 unwind_tcb_print(tcp);
948 syscall_exiting_finish(struct tcb *tcp)
950 tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED | TCB_INJECT_DELAY_EXIT);
951 tcp->sys_func_rval = 0;
952 free_tcb_priv_data(tcp);
955 tcp->ltime = tcp->stime;
959 is_erestart(struct tcb *tcp)
961 switch (tcp->u_error) {
965 case ERESTART_RESTARTBLOCK:
972 static unsigned long saved_u_error;
975 temporarily_clear_syserror(struct tcb *tcp)
977 saved_u_error = tcp->u_error;
982 restore_cleared_syserror(struct tcb *tcp)
984 tcp->u_error = saved_u_error;
987 static struct ptrace_syscall_info ptrace_sci;
990 ptrace_syscall_info_is_valid(void)
992 return ptrace_get_syscall_info_supported &&
993 ptrace_sci.op <= PTRACE_SYSCALL_INFO_SECCOMP;
996 #define XLAT_MACROS_ONLY
997 #include "xlat/nt_descriptor_types.h"
998 #undef XLAT_MACROS_ONLY
1000 #define ARCH_MIGHT_USE_SET_REGS 1
1002 #include "arch_regs.c"
1004 #if HAVE_ARCH_GETRVAL2
1005 # include "arch_getrval2.c"
1008 #include "getregs_old.h"
1009 #ifdef HAVE_GETREGS_OLD
1010 /* Either getregs_old() or set_regs() */
1011 # undef ARCH_MIGHT_USE_SET_REGS
1012 # define ARCH_MIGHT_USE_SET_REGS 0
1015 #undef ptrace_getregset_or_getregs
1016 #undef ptrace_setregset_or_setregs
1017 #ifdef ARCH_REGS_FOR_GETREGSET
1019 # define ptrace_getregset_or_getregs ptrace_getregset
1021 ptrace_getregset(pid_t pid)
1023 # ifdef ARCH_IOVEC_FOR_GETREGSET
1024 /* variable iovec */
1025 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1026 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1027 &ARCH_IOVEC_FOR_GETREGSET);
1029 /* constant iovec */
1030 static struct iovec io = {
1031 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1032 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1034 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1039 # if ARCH_MIGHT_USE_SET_REGS
1040 # define ptrace_setregset_or_setregs ptrace_setregset
1042 ptrace_setregset(pid_t pid)
1044 # ifdef ARCH_IOVEC_FOR_GETREGSET
1045 /* variable iovec */
1046 return ptrace(PTRACE_SETREGSET, 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_SETREGSET, pid, NT_PRSTATUS, &io);
1057 # endif /* ARCH_MIGHT_USE_SET_REGS */
1059 #elif defined ARCH_REGS_FOR_GETREGS
1061 # define ptrace_getregset_or_getregs ptrace_getregs
1063 ptrace_getregs(pid_t pid)
1065 # if defined SPARC || defined SPARC64
1066 /* SPARC systems have the meaning of data and addr reversed */
1067 return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1069 return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1073 # if ARCH_MIGHT_USE_SET_REGS
1074 # define ptrace_setregset_or_setregs ptrace_setregs
1076 ptrace_setregs(pid_t pid)
1078 # if defined SPARC || defined SPARC64
1079 /* SPARC systems have the meaning of data and addr reversed */
1080 return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1082 return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1085 # endif /* ARCH_MIGHT_USE_SET_REGS */
1087 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1089 static long get_regs_error = -1;
1092 clear_regs(struct tcb *tcp)
1094 ptrace_sci.op = 0xff;
1095 get_regs_error = -1;
1099 get_regs(struct tcb *const tcp)
1101 #ifdef ptrace_getregset_or_getregs
1103 if (get_regs_error != -1)
1104 return get_regs_error;
1106 # ifdef HAVE_GETREGS_OLD
1108 * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1109 * fallback to getregs_old.
1111 static int use_getregs_old;
1112 if (use_getregs_old < 0) {
1113 return get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1114 } else if (use_getregs_old == 0) {
1115 get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1116 if (get_regs_error >= 0) {
1117 use_getregs_old = -1;
1118 return get_regs_error;
1120 if (errno == EPERM || errno == ESRCH)
1121 return get_regs_error;
1122 use_getregs_old = 1;
1124 return get_regs_error = getregs_old(tcp);
1125 # else /* !HAVE_GETREGS_OLD */
1126 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1127 get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1129 # if defined ARCH_PERSONALITY_0_IOV_SIZE
1131 return get_regs_error;
1133 switch (ARCH_IOVEC_FOR_GETREGSET.iov_len) {
1134 case ARCH_PERSONALITY_0_IOV_SIZE:
1135 update_personality(tcp, 0);
1137 case ARCH_PERSONALITY_1_IOV_SIZE:
1138 update_personality(tcp, 1);
1141 static bool printed = false;
1144 error_msg("Unsupported regset size returned by "
1145 "PTRACE_GETREGSET: %zu",
1146 ARCH_IOVEC_FOR_GETREGSET.iov_len);
1151 update_personality(tcp, 0);
1154 # endif /* ARCH_PERSONALITY_0_IOV_SIZE */
1156 return get_regs_error;
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_0x") + sizeof(kernel_ulong_t) * 2];
1183 free_sysent_buf(void *ptr)
1185 struct sysent_buf *s = ptr;
1186 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1191 strace_get_syscall_info(struct tcb *tcp)
1194 * ptrace_get_syscall_info_supported should have been checked
1197 if (ptrace_sci.op == 0xff) {
1198 const size_t size = sizeof(ptrace_sci);
1199 if (ptrace(PTRACE_GET_SYSCALL_INFO, tcp->pid,
1200 (void *) size, &ptrace_sci) < 0) {
1201 get_regs_error = -2;
1204 #if SUPPORTED_PERSONALITIES > 1
1205 int newpers = get_personality_from_syscall_info(&ptrace_sci);
1207 update_personality(tcp, newpers);
1211 if (entering(tcp)) {
1212 if (ptrace_sci.op == PTRACE_SYSCALL_INFO_EXIT) {
1213 error_msg("pid %d: entering"
1214 ", ptrace_syscall_info.op == %u",
1215 tcp->pid, ptrace_sci.op);
1216 /* TODO: handle this. */
1219 if (ptrace_sci.op == PTRACE_SYSCALL_INFO_ENTRY) {
1220 error_msg("pid %d: exiting"
1221 ", ptrace_syscall_info.op == %u",
1222 tcp->pid, ptrace_sci.op);
1223 /* TODO: handle this. */
1231 get_instruction_pointer(struct tcb *tcp, kernel_ulong_t *ip)
1233 if (get_regs_error < -1)
1236 if (ptrace_get_syscall_info_supported) {
1237 if (!strace_get_syscall_info(tcp))
1239 *ip = (kernel_ulong_t) ptrace_sci.instruction_pointer;
1243 #if defined ARCH_PC_REG
1244 if (get_regs(tcp) < 0)
1246 *ip = (kernel_ulong_t) ARCH_PC_REG;
1248 #elif defined ARCH_PC_PEEK_ADDR
1249 if (upeek(tcp, ARCH_PC_PEEK_ADDR, ip) < 0)
1253 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1258 get_stack_pointer(struct tcb *tcp, kernel_ulong_t *sp)
1260 if (get_regs_error < -1)
1263 if (ptrace_get_syscall_info_supported) {
1264 if (!strace_get_syscall_info(tcp))
1266 *sp = (kernel_ulong_t) ptrace_sci.stack_pointer;
1270 #if defined ARCH_SP_REG
1271 if (get_regs(tcp) < 0)
1273 *sp = (kernel_ulong_t) ARCH_SP_REG;
1275 #elif defined ARCH_SP_PEEK_ADDR
1276 if (upeek(tcp, ARCH_SP_PEEK_ADDR, sp) < 0)
1285 get_syscall_regs(struct tcb *tcp)
1287 if (get_regs_error != -1)
1288 return get_regs_error;
1290 if (ptrace_get_syscall_info_supported)
1291 return strace_get_syscall_info(tcp) ? 0 : get_regs_error;
1293 return get_regs(tcp);
1296 const struct_sysent stub_sysent = {
1298 .sys_flags = MEMORY_MAPPING_CHANGE,
1299 .sen = SEN_printargs,
1300 .sys_func = printargs,
1306 * 0: "ignore this ptrace stop", syscall_entering_decode() should return a "bail
1307 * out silently" code.
1308 * 1: ok, continue in syscall_entering_decode().
1309 * other: error, syscall_entering_decode() should print error indicator
1310 * ("????" etc) and return an appropriate code.
1313 get_scno(struct tcb *tcp)
1317 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1319 if (get_syscall_regs(tcp) < 0)
1322 if (ptrace_syscall_info_is_valid()) {
1324 * So far it's just a workaround for x32,
1325 * but let's pretend it could be used elsewhere.
1327 int rc = arch_check_scno(tcp);
1330 tcp->scno = ptrace_sci.entry.nr;
1332 int rc = arch_get_scno(tcp);
1337 tcp->scno = shuffle_scno(tcp->scno);
1339 if (scno_is_valid(tcp->scno)) {
1340 tcp->s_ent = &sysent[tcp->scno];
1341 tcp->qual_flg = qual_flags(tcp->scno);
1343 struct sysent_buf *s = xzalloc(sizeof(*s));
1346 s->ent = stub_sysent;
1347 s->ent.sys_name = s->buf;
1348 xsprintf(s->buf, "syscall_%#" PRI_klx, shuffle_scno(tcp->scno));
1350 tcp->s_ent = &s->ent;
1352 set_tcb_priv_data(tcp, s, free_sysent_buf);
1354 debug_msg("pid %d invalid syscall %#" PRI_klx,
1355 tcp->pid, shuffle_scno(tcp->scno));
1359 * We refrain from argument decoding during recovering
1360 * as tracee memory mappings has changed and the registers
1361 * are very likely pointing to garbage already.
1363 if (recovering(tcp))
1364 tcp->qual_flg |= QUAL_RAW;
1370 get_syscall_args(struct tcb *tcp)
1372 if (ptrace_syscall_info_is_valid()) {
1373 const unsigned int n =
1374 MIN(ARRAY_SIZE(tcp->u_arg),
1375 ARRAY_SIZE(ptrace_sci.entry.args));
1376 for (unsigned int i = 0; i < n; ++i)
1377 tcp->u_arg[i] = ptrace_sci.entry.args[i];
1378 #if SUPPORTED_PERSONALITIES > 1
1379 if (tcp_sysent(tcp)->sys_flags & COMPAT_SYSCALL_TYPES) {
1380 for (unsigned int i = 0; i < n; ++i)
1381 tcp->u_arg[i] = (uint32_t) tcp->u_arg[i];
1385 * So far it's just a workaround for mips o32,
1386 * but let's pretend it could be used elsewhere.
1389 arch_get_syscall_args_extra(tcp, n);
1393 return arch_get_syscall_args(tcp);
1396 #ifdef ptrace_getregset_or_getregs
1397 # define get_syscall_result_regs get_syscall_regs
1399 static int get_syscall_result_regs(struct tcb *);
1403 * 1: ok, continue in syscall_exiting_trace().
1404 * -1: error, syscall_exiting_trace() should print error indicator
1405 * ("????" etc) and bail out.
1408 get_syscall_result(struct tcb *tcp)
1410 if (get_syscall_result_regs(tcp) < 0)
1413 (!(tcp_sysent(tcp)->sys_flags & SYSCALL_NEVER_FAILS)
1414 || syscall_tampered(tcp))
1415 && !syscall_tampered_nofail(tcp));
1421 get_error(struct tcb *tcp, const bool check_errno)
1423 if (ptrace_syscall_info_is_valid()) {
1424 if (ptrace_sci.exit.is_error) {
1426 tcp->u_error = -ptrace_sci.exit.rval;
1429 tcp->u_rval = ptrace_sci.exit.rval;
1433 arch_get_error(tcp, check_errno);
1438 set_error(struct tcb *tcp, unsigned long new_error)
1440 const unsigned long old_error = tcp->u_error;
1442 if (new_error == old_error || new_error > MAX_ERRNO_VALUE)
1445 #ifdef ptrace_setregset_or_setregs
1446 /* if we are going to invoke set_regs, call get_regs first */
1447 if (get_regs(tcp) < 0)
1451 tcp->u_error = new_error;
1452 if (arch_set_error(tcp)) {
1453 tcp->u_error = old_error;
1454 /* arch_set_error does not update u_rval */
1456 if (ptrace_syscall_info_is_valid())
1459 get_error(tcp, !(tcp_sysent(tcp)->sys_flags &
1460 SYSCALL_NEVER_FAILS));
1465 set_success(struct tcb *tcp, kernel_long_t new_rval)
1467 const kernel_long_t old_rval = tcp->u_rval;
1469 #ifdef ptrace_setregset_or_setregs
1470 /* if we are going to invoke set_regs, call get_regs first */
1471 if (get_regs(tcp) < 0)
1475 tcp->u_rval = new_rval;
1476 if (arch_set_success(tcp)) {
1477 tcp->u_rval = old_rval;
1478 /* arch_set_success does not update u_error */
1480 if (ptrace_syscall_info_is_valid())
1483 get_error(tcp, !(tcp_sysent(tcp)->sys_flags &
1484 SYSCALL_NEVER_FAILS));
1488 #include "get_scno.c"
1489 #include "check_scno.c"
1490 #include "set_scno.c"
1491 #include "get_syscall_args.c"
1492 #ifndef ptrace_getregset_or_getregs
1493 # include "get_syscall_result.c"
1495 #include "get_error.c"
1496 #include "set_error.c"
1497 #ifdef HAVE_GETREGS_OLD
1498 # include "getregs_old.c"
1500 #include "shuffle_scno.c"
1503 syscall_name(kernel_ulong_t scno)
1505 return scno_is_valid(scno) ? sysent[scno].sys_name : NULL;