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 err_name(unsigned long err)
492 if ((err < nerrnos) && errnoent[err])
493 return errnoent[err];
498 static long get_regs(struct tcb *);
499 static int get_syscall_args(struct tcb *);
500 static int get_syscall_result(struct tcb *);
501 static int arch_get_scno(struct tcb *tcp);
502 static int arch_set_scno(struct tcb *, kernel_ulong_t);
503 static void get_error(struct tcb *, const bool);
504 static int arch_set_error(struct tcb *);
505 static int arch_set_success(struct tcb *);
507 struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
509 static struct inject_opts *
510 tcb_inject_opts(struct tcb *tcp)
512 return (scno_in_range(tcp->scno) && tcp->inject_vec[current_personality])
513 ? &tcp->inject_vec[current_personality][tcp->scno] : NULL;
518 tamper_with_syscall_entering(struct tcb *tcp, unsigned int *signo)
520 if (!tcp->inject_vec[current_personality]) {
521 tcp->inject_vec[current_personality] =
522 xcalloc(nsyscalls, sizeof(**inject_vec));
523 memcpy(tcp->inject_vec[current_personality],
524 inject_vec[current_personality],
525 nsyscalls * sizeof(**inject_vec));
528 struct inject_opts *opts = tcb_inject_opts(tcp);
530 if (!opts || opts->first == 0)
535 if (opts->first != 0)
538 opts->first = opts->step;
540 if (!recovering(tcp)) {
541 if (opts->data.flags & INJECT_F_SIGNAL)
542 *signo = opts->data.signo;
543 if (opts->data.flags & INJECT_F_RETVAL &&
544 !arch_set_scno(tcp, -1))
545 tcp->flags |= TCB_TAMPERED;
552 tamper_with_syscall_exiting(struct tcb *tcp)
554 struct inject_opts *opts = tcb_inject_opts(tcp);
559 if (opts->data.rval >= 0) {
560 kernel_long_t u_rval = tcp->u_rval;
562 tcp->u_rval = opts->data.rval;
563 if (arch_set_success(tcp)) {
564 tcp->u_rval = u_rval;
569 unsigned long new_error = -opts->data.rval;
571 if (new_error != tcp->u_error && new_error <= MAX_ERRNO_VALUE) {
572 unsigned long u_error = tcp->u_error;
574 tcp->u_error = new_error;
575 if (arch_set_error(tcp)) {
576 tcp->u_error = u_error;
586 * 0: "ignore this ptrace stop", bail out silently.
587 * 1: ok, decoded; call
588 * syscall_entering_finish(tcp, syscall_entering_trace(tcp, ...)).
589 * other: error; call syscall_entering_finish(tcp, res), where res is the value
593 syscall_entering_decode(struct tcb *tcp)
595 int res = get_scno(tcp);
599 if (res != 1 || (res = get_syscall_args(tcp)) != 1) {
601 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
603 * " <unavailable>" will be added later by the code which
604 * detects ptrace errors.
609 #if defined SYS_ipc_subcall \
610 || defined SYS_socket_subcall \
611 || defined SYS_syscall_subcall
613 switch (tcp->s_ent->sen) {
614 # ifdef SYS_ipc_subcall
616 decode_ipc_subcall(tcp);
619 # ifdef SYS_socket_subcall
621 decode_socket_subcall(tcp);
624 # ifdef SYS_syscall_subcall
626 decode_syscall_subcall(tcp);
627 if (tcp->s_ent->sen != SEN_syscall)
640 syscall_entering_trace(struct tcb *tcp, unsigned int *sig)
642 /* Restrain from fault injection while the trace executes strace code. */
644 tcp->qual_flg &= ~QUAL_INJECT;
647 switch (tcp->s_ent->sen) {
650 #if defined SPARC || defined SPARC64
653 tcp->flags &= ~TCB_HIDE_LOG;
657 if (!traced(tcp) || (tracing_paths && !pathtrace_match(tcp))) {
658 tcp->flags |= TCB_FILTERED;
662 tcp->flags &= ~TCB_FILTERED;
669 tamper_with_syscall_entering(tcp, sig);
671 if (cflag == CFLAG_ONLY_STATS) {
676 if (stack_trace_enabled) {
677 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
678 unwind_capture_stacktrace(tcp);
683 tprintf("%s(", tcp->s_ent->sys_name);
684 int res = raw(tcp) ? printargs(tcp) : tcp->s_ent->sys_func(tcp);
690 syscall_entering_finish(struct tcb *tcp, int res)
692 tcp->flags |= TCB_INSYSCALL;
693 tcp->sys_func_rval = res;
694 /* Measure the entrance time as late as possible to avoid errors. */
695 if ((Tflag || cflag) && !filtered(tcp))
696 gettimeofday(&tcp->etime, NULL);
702 * -1: error in one of ptrace ops.
704 * If not 0, call syscall_exiting_trace(tcp, res), where res is the return
705 * value. Anyway, call syscall_exiting_finish(tcp) then.
708 syscall_exiting_decode(struct tcb *tcp, struct timeval *ptv)
710 /* Measure the exit time as early as possible to avoid errors. */
711 if ((Tflag || cflag) && !(filtered(tcp) || hide_log(tcp)))
712 gettimeofday(ptv, NULL);
715 if (stack_trace_enabled) {
716 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
717 unwind_cache_invalidate(tcp);
721 if (filtered(tcp) || hide_log(tcp))
724 #if SUPPORTED_PERSONALITIES > 1
725 update_personality(tcp, tcp->currpers);
728 return get_syscall_result(tcp);
732 syscall_exiting_trace(struct tcb *tcp, struct timeval tv, int res)
734 if (syserror(tcp) && syscall_tampered(tcp))
735 tamper_with_syscall_exiting(tcp);
738 count_syscall(tcp, &tv);
739 if (cflag == CFLAG_ONLY_STATS) {
744 /* If not in -ff mode, and printing_tcp != tcp,
745 * then the log currently does not end with output
746 * of _our syscall entry_, but with something else.
747 * We need to say which syscall's return is this.
749 * Forced reprinting via TCB_REPRINT is used only by
750 * "strace -ff -oLOG test/threaded_execve" corner case.
751 * It's the only case when -ff mode needs reprinting.
753 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
754 tcp->flags &= ~TCB_REPRINT;
756 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
760 tcp->s_prev_ent = NULL;
762 /* There was error in one of prior ptrace ops */
765 tprints("= ? <unavailable>\n");
769 tcp->s_prev_ent = tcp->s_ent;
773 /* sys_res = printargs(tcp); - but it's nop on sysexit */
775 /* FIXME: not_failing_only (IOW, option -z) is broken:
776 * failure of syscall is known only after syscall return.
777 * Thus we end up with something like this on, say, ENOENT:
778 * open("does_not_exist", O_RDONLY <unfinished ...>
779 * {next syscall decode}
780 * whereas the intended result is that open(...) line
781 * is not shown at all.
783 if (not_failing_only && tcp->u_error)
784 return 0; /* ignore failed syscalls */
785 if (tcp->sys_func_rval & RVAL_DECODED)
786 sys_res = tcp->sys_func_rval;
788 sys_res = tcp->s_ent->sys_func(tcp);
793 unsigned long u_error = tcp->u_error;
794 kernel_long_t u_rval;
798 tprintf("= -1 (errno %lu)", u_error);
800 tprintf("= %#" PRI_klx, tcp->u_rval);
802 if (syscall_tampered(tcp))
803 tprints(" (INJECTED)");
804 } else if (!(sys_res & RVAL_NONE) && u_error) {
805 const char *u_error_str;
808 /* Blocked signals do not interrupt any syscalls.
809 * In this case syscalls don't return ERESTARTfoo codes.
811 * Deadly signals set to SIG_DFL interrupt syscalls
812 * and kill the process regardless of which of the codes below
813 * is returned by the interrupted syscall.
814 * In some cases, kernel forces a kernel-generated deadly
815 * signal to be unblocked and set to SIG_DFL (and thus cause
816 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
817 * or SIGILL. (The alternative is to leave process spinning
818 * forever on the faulty instruction - not useful).
820 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
821 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
822 * but kernel will always restart them.
825 /* Most common type of signal-interrupted syscall exit code.
826 * The system call will be restarted with the same arguments
827 * if SA_RESTART is set; otherwise, it will fail with EINTR.
829 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
832 /* Rare. For example, fork() returns this if interrupted.
833 * SA_RESTART is ignored (assumed set): the restart is unconditional.
835 tprints("= ? ERESTARTNOINTR (To be restarted)");
838 /* pause(), rt_sigsuspend() etc use this code.
839 * SA_RESTART is ignored (assumed not set):
840 * syscall won't restart (will return EINTR instead)
841 * even after signal with SA_RESTART set. However,
842 * after SIG_IGN or SIG_DFL signal it will restart
843 * (thus the name "restart only if has no handler").
845 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
847 case ERESTART_RESTARTBLOCK:
848 /* Syscalls like nanosleep(), poll() which can't be
849 * restarted with their original arguments use this
850 * code. Kernel will execute restart_syscall() instead,
851 * which changes arguments before restarting syscall.
852 * SA_RESTART is ignored (assumed not set) similarly
853 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
854 * since restart data is saved in "restart block"
855 * in task struct, and if signal handler uses a syscall
856 * which in turn saves another such restart block,
857 * old data is lost and restart becomes impossible)
859 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
862 u_rval = sys_res & RVAL_PRINT_ERR_VAL ?
864 u_error_str = err_name(u_error);
866 tprintf("= %" PRI_kld " %s (%s)",
867 u_rval, u_error_str, strerror(u_error));
869 tprintf("= %" PRI_kld " %lu (%s)",
870 u_rval, u_error, strerror(u_error));
873 if (syscall_tampered(tcp))
874 tprints(" (INJECTED)");
875 if ((sys_res & RVAL_STR) && tcp->auxstr)
876 tprintf(" (%s)", tcp->auxstr);
878 if (sys_res & RVAL_NONE)
881 switch (sys_res & RVAL_MASK) {
883 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
884 if (current_klongsize < sizeof(tcp->u_rval)) {
886 (unsigned int) tcp->u_rval);
890 tprintf("= %#" PRI_klx, tcp->u_rval);
895 print_numeric_long_umask(tcp->u_rval);
898 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
899 if (current_klongsize < sizeof(tcp->u_rval)) {
901 (unsigned int) tcp->u_rval);
905 tprintf("= %" PRI_klu, tcp->u_rval);
909 tprintf("= %" PRI_kld, tcp->u_rval);
914 printfd(tcp, tcp->u_rval);
916 tprintf("= %" PRI_kld, tcp->u_rval);
919 error_msg("invalid rval format");
923 if ((sys_res & RVAL_STR) && tcp->auxstr)
924 tprintf(" (%s)", tcp->auxstr);
925 if (syscall_tampered(tcp))
926 tprints(" (INJECTED)");
929 tv_sub(&tv, &tv, &tcp->etime);
930 tprintf(" <%ld.%06ld>",
931 (long) tv.tv_sec, (long) tv.tv_usec);
938 if (stack_trace_enabled)
939 unwind_print_stacktrace(tcp);
945 syscall_exiting_finish(struct tcb *tcp)
947 tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED);
948 tcp->sys_func_rval = 0;
949 free_tcb_priv_data(tcp);
953 is_erestart(struct tcb *tcp)
955 switch (tcp->u_error) {
959 case ERESTART_RESTARTBLOCK:
966 static unsigned long saved_u_error;
969 temporarily_clear_syserror(struct tcb *tcp)
971 saved_u_error = tcp->u_error;
976 restore_cleared_syserror(struct tcb *tcp)
978 tcp->u_error = saved_u_error;
981 #include "arch_regs.c"
983 #if HAVE_ARCH_GETRVAL2
984 # include "arch_getrval2.c"
988 print_pc(struct tcb *tcp)
990 #if defined ARCH_PC_REG
991 # define ARCH_GET_PC 0
992 #elif defined ARCH_PC_PEEK_ADDR
994 # define ARCH_PC_REG pc
995 # define ARCH_GET_PC upeek(tcp, ARCH_PC_PEEK_ADDR, &pc)
997 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
999 if (get_regs(tcp) < 0 || ARCH_GET_PC)
1000 tprints(current_wordsize == 4 ? "[????????] "
1001 : "[????????????????] ");
1003 tprintf(current_wordsize == 4
1004 ? "[%08" PRI_klx "] " : "[%016" PRI_klx "] ",
1005 (kernel_ulong_t) ARCH_PC_REG);
1008 #include "getregs_old.h"
1010 #undef ptrace_getregset_or_getregs
1011 #undef ptrace_setregset_or_setregs
1012 #ifdef ARCH_REGS_FOR_GETREGSET
1014 # define ptrace_getregset_or_getregs ptrace_getregset
1016 ptrace_getregset(pid_t pid)
1018 # ifdef ARCH_IOVEC_FOR_GETREGSET
1019 /* variable iovec */
1020 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1021 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1022 &ARCH_IOVEC_FOR_GETREGSET);
1024 /* constant iovec */
1025 static struct iovec io = {
1026 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1027 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1029 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1034 # ifndef HAVE_GETREGS_OLD
1035 # define ptrace_setregset_or_setregs ptrace_setregset
1037 ptrace_setregset(pid_t pid)
1039 # ifdef ARCH_IOVEC_FOR_GETREGSET
1040 /* variable iovec */
1041 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1042 &ARCH_IOVEC_FOR_GETREGSET);
1044 /* constant iovec */
1045 static struct iovec io = {
1046 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1047 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1049 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1052 # endif /* !HAVE_GETREGS_OLD */
1054 #elif defined ARCH_REGS_FOR_GETREGS
1056 # define ptrace_getregset_or_getregs ptrace_getregs
1058 ptrace_getregs(pid_t pid)
1060 # if defined SPARC || defined SPARC64
1061 /* SPARC systems have the meaning of data and addr reversed */
1062 return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1064 return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1068 # ifndef HAVE_GETREGS_OLD
1069 # define ptrace_setregset_or_setregs ptrace_setregs
1071 ptrace_setregs(pid_t pid)
1073 # if defined SPARC || defined SPARC64
1074 /* SPARC systems have the meaning of data and addr reversed */
1075 return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1077 return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1080 # endif /* !HAVE_GETREGS_OLD */
1082 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1084 #ifdef ptrace_getregset_or_getregs
1085 static long get_regs_error;
1089 clear_regs(struct tcb *tcp)
1091 #ifdef ptrace_getregset_or_getregs
1092 get_regs_error = -1;
1097 get_regs(struct tcb *const tcp)
1099 #ifdef ptrace_getregset_or_getregs
1101 if (get_regs_error != -1)
1102 return get_regs_error;
1104 # ifdef HAVE_GETREGS_OLD
1106 * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1107 * fallback to getregs_old.
1109 static int use_getregs_old;
1110 if (use_getregs_old < 0) {
1111 return get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1112 } else if (use_getregs_old == 0) {
1113 get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1114 if (get_regs_error >= 0) {
1115 use_getregs_old = -1;
1116 return get_regs_error;
1118 if (errno == EPERM || errno == ESRCH)
1119 return get_regs_error;
1120 use_getregs_old = 1;
1122 return get_regs_error = getregs_old(tcp);
1123 # else /* !HAVE_GETREGS_OLD */
1124 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1125 get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1127 # if defined ARCH_PERSONALITY_0_IOV_SIZE
1129 return get_regs_error;
1131 switch (ARCH_IOVEC_FOR_GETREGSET.iov_len) {
1132 case ARCH_PERSONALITY_0_IOV_SIZE:
1133 update_personality(tcp, 0);
1135 case ARCH_PERSONALITY_1_IOV_SIZE:
1136 update_personality(tcp, 1);
1139 static bool printed = false;
1142 error_msg("Unsupported regset size returned by "
1143 "PTRACE_GETREGSET: %zu",
1144 ARCH_IOVEC_FOR_GETREGSET.iov_len);
1149 update_personality(tcp, 0);
1152 # endif /* ARCH_PERSONALITY_0_IOV_SIZE */
1154 return get_regs_error;
1156 # endif /* !HAVE_GETREGS_OLD */
1158 #else /* !ptrace_getregset_or_getregs */
1160 # warning get_regs is not implemented for this architecture yet
1163 #endif /* !ptrace_getregset_or_getregs */
1166 #ifdef ptrace_setregset_or_setregs
1170 return ptrace_setregset_or_setregs(pid);
1172 #endif /* ptrace_setregset_or_setregs */
1177 char buf[sizeof("syscall_0x") + sizeof(kernel_ulong_t) * 2];
1181 free_sysent_buf(void *ptr)
1183 struct sysent_buf *s = ptr;
1184 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1190 * 0: "ignore this ptrace stop", syscall_entering_decode() should return a "bail
1191 * out silently" code.
1192 * 1: ok, continue in syscall_entering_decode().
1193 * other: error, syscall_entering_decode() should print error indicator
1194 * ("????" etc) and return an appropriate code.
1197 get_scno(struct tcb *tcp)
1199 if (get_regs(tcp) < 0)
1202 int rc = arch_get_scno(tcp);
1206 tcp->scno = shuffle_scno(tcp->scno);
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_klx, 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_klx,
1227 tcp->pid, shuffle_scno(tcp->scno));
1231 * We refrain from argument decoding during recovering
1232 * as tracee memory mappings has changed and the registers
1233 * are very likely pointing to garbage already.
1235 if (recovering(tcp))
1236 tcp->qual_flg |= QUAL_RAW;
1241 #ifdef ptrace_getregset_or_getregs
1242 # define get_syscall_result_regs get_regs
1244 static int get_syscall_result_regs(struct tcb *);
1248 * 1: ok, continue in syscall_exiting_trace().
1249 * -1: error, syscall_exiting_trace() should print error indicator
1250 * ("????" etc) and bail out.
1253 get_syscall_result(struct tcb *tcp)
1255 if (get_syscall_result_regs(tcp) < 0)
1259 !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS)
1260 || syscall_tampered(tcp));
1265 #include "get_scno.c"
1266 #include "set_scno.c"
1267 #include "get_syscall_args.c"
1268 #ifndef ptrace_getregset_or_getregs
1269 # include "get_syscall_result.c"
1271 #include "get_error.c"
1272 #include "set_error.c"
1273 #ifdef HAVE_GETREGS_OLD
1274 # include "getregs_old.c"
1276 #include "shuffle_scno.c"
1279 syscall_name(kernel_ulong_t scno)
1281 return scno_is_valid(scno) ? sysent[scno].sys_name : NULL;