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>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "native_defs.h"
36 #include <sys/param.h>
39 /* for struct iovec */
46 # undef PTRACE_GETREGS
47 # define PTRACE_GETREGS PTRACE_GETREGS64
48 # undef PTRACE_SETREGS
49 # define PTRACE_SETREGS PTRACE_SETREGS64
53 # include <asm/psrcompat.h>
63 # define NT_PRSTATUS 1
67 # warning: NSIG is not defined, using 32
73 /* Define these shorthand notations to simplify the syscallent files. */
77 #define TN TRACE_NETWORK
78 #define TP TRACE_PROCESS
79 #define TS TRACE_SIGNAL
80 #define TM TRACE_MEMORY
81 #define NF SYSCALL_NEVER_FAILS
83 #define SI STACKTRACE_INVALIDATE_CACHE
84 #define SE STACKTRACE_CAPTURE_ON_ENTER
86 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
88 const struct_sysent sysent0[] = {
89 #include "syscallent.h"
92 #if SUPPORTED_PERSONALITIES > 1
93 # include PERSONALITY1_INCLUDE_FUNCS
94 static const struct_sysent sysent1[] = {
95 # include "syscallent1.h"
99 #if SUPPORTED_PERSONALITIES > 2
100 # include PERSONALITY2_INCLUDE_FUNCS
101 static const struct_sysent sysent2[] = {
102 # include "syscallent2.h"
106 /* Now undef them since short defines cause wicked namespace pollution. */
121 * `ioctlent[012].h' files are automatically generated by the auxiliary
122 * program `ioctlsort', such that the list is sorted by the `code' field.
123 * This has the side-effect of resolving the _IO.. macros into
124 * plain integers, eliminating the need to include here everything
128 const char *const errnoent0[] = {
129 #include "errnoent.h"
131 const char *const signalent0[] = {
132 #include "signalent.h"
134 const struct_ioctlent ioctlent0[] = {
135 #include "ioctlent0.h"
138 #if SUPPORTED_PERSONALITIES > 1
139 static const char *const errnoent1[] = {
140 # include "errnoent1.h"
142 static const char *const signalent1[] = {
143 # include "signalent1.h"
145 static const struct_ioctlent ioctlent1[] = {
146 # include "ioctlent1.h"
148 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
149 static const struct_printers printers0 = {
150 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
152 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
153 static const struct_printers printers1 = {
154 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
158 #if SUPPORTED_PERSONALITIES > 2
159 static const char *const errnoent2[] = {
160 # include "errnoent2.h"
162 static const char *const signalent2[] = {
163 # include "signalent2.h"
165 static const struct_ioctlent ioctlent2[] = {
166 # include "ioctlent2.h"
168 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
169 static const struct_printers printers2 = {
170 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
175 nsyscalls0 = ARRAY_SIZE(sysent0)
176 #if SUPPORTED_PERSONALITIES > 1
177 , nsyscalls1 = ARRAY_SIZE(sysent1)
178 # if SUPPORTED_PERSONALITIES > 2
179 , nsyscalls2 = ARRAY_SIZE(sysent2)
185 nerrnos0 = ARRAY_SIZE(errnoent0)
186 #if SUPPORTED_PERSONALITIES > 1
187 , nerrnos1 = ARRAY_SIZE(errnoent1)
188 # if SUPPORTED_PERSONALITIES > 2
189 , nerrnos2 = ARRAY_SIZE(errnoent2)
195 nsignals0 = ARRAY_SIZE(signalent0)
196 #if SUPPORTED_PERSONALITIES > 1
197 , nsignals1 = ARRAY_SIZE(signalent1)
198 # if SUPPORTED_PERSONALITIES > 2
199 , nsignals2 = ARRAY_SIZE(signalent2)
205 nioctlents0 = ARRAY_SIZE(ioctlent0)
206 #if SUPPORTED_PERSONALITIES > 1
207 , nioctlents1 = ARRAY_SIZE(ioctlent1)
208 # if SUPPORTED_PERSONALITIES > 2
209 , nioctlents2 = ARRAY_SIZE(ioctlent2)
214 #if SUPPORTED_PERSONALITIES > 1
215 const struct_sysent *sysent = sysent0;
216 const char *const *errnoent = errnoent0;
217 const char *const *signalent = signalent0;
218 const struct_ioctlent *ioctlent = ioctlent0;
219 const struct_printers *printers = &printers0;
222 unsigned nsyscalls = nsyscalls0;
223 unsigned nerrnos = nerrnos0;
224 unsigned nsignals = nsignals0;
225 unsigned nioctlents = nioctlents0;
227 const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES] = {
229 #if SUPPORTED_PERSONALITIES > 1
232 #if SUPPORTED_PERSONALITIES > 2
236 const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
238 #if SUPPORTED_PERSONALITIES > 1
241 #if SUPPORTED_PERSONALITIES > 2
246 #if SUPPORTED_PERSONALITIES > 1
247 unsigned current_personality;
249 # ifndef current_wordsize
250 unsigned current_wordsize;
251 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
252 PERSONALITY0_WORDSIZE,
253 PERSONALITY1_WORDSIZE,
254 # if SUPPORTED_PERSONALITIES > 2
255 PERSONALITY2_WORDSIZE,
260 # ifndef current_klongsize
261 unsigned current_klongsize;
262 static const int personality_klongsize[SUPPORTED_PERSONALITIES] = {
263 PERSONALITY0_KLONGSIZE,
264 PERSONALITY1_KLONGSIZE,
265 # if SUPPORTED_PERSONALITIES > 2
266 PERSONALITY2_KLONGSIZE,
272 set_personality(int personality)
274 nsyscalls = nsyscall_vec[personality];
275 sysent = sysent_vec[personality];
277 switch (personality) {
279 errnoent = errnoent0;
281 ioctlent = ioctlent0;
282 nioctlents = nioctlents0;
283 signalent = signalent0;
284 nsignals = nsignals0;
285 printers = &printers0;
289 errnoent = errnoent1;
291 ioctlent = ioctlent1;
292 nioctlents = nioctlents1;
293 signalent = signalent1;
294 nsignals = nsignals1;
295 printers = &printers1;
298 # if SUPPORTED_PERSONALITIES > 2
300 errnoent = errnoent2;
302 ioctlent = ioctlent2;
303 nioctlents = nioctlents2;
304 signalent = signalent2;
305 nsignals = nsignals2;
306 printers = &printers2;
311 current_personality = personality;
312 # ifndef current_wordsize
313 current_wordsize = personality_wordsize[personality];
315 # ifndef current_klongsize
316 current_klongsize = personality_klongsize[personality];
321 update_personality(struct tcb *tcp, unsigned int personality)
323 if (personality == current_personality)
325 set_personality(personality);
327 if (personality == tcp->currpers)
329 tcp->currpers = personality;
331 # undef PERSONALITY_NAMES
332 # if defined POWERPC64
333 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
334 # elif defined X86_64
335 # define PERSONALITY_NAMES {"64 bit", "32 bit", "x32"}
337 # define PERSONALITY_NAMES {"x32", "32 bit"}
338 # elif defined AARCH64
339 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
341 # define PERSONALITY_NAMES {"64-bit", "32-bit"}
343 # ifdef PERSONALITY_NAMES
345 static const char *const names[] = PERSONALITY_NAMES;
346 error_msg("[ Process PID=%d runs in %s mode. ]",
347 tcp->pid, names[personality]);
353 #ifdef SYS_socket_subcall
355 decode_socket_subcall(struct tcb *tcp)
357 const int call = tcp->u_arg[0];
359 if (call < 1 || call >= SYS_socket_nsubcalls)
362 const kernel_scno_t scno = SYS_socket_subcall + call;
363 const unsigned int nargs = sysent[scno].nargs;
366 if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
370 tcp->qual_flg = qual_flags(scno);
371 tcp->s_ent = &sysent[scno];
374 for (i = 0; i < nargs; ++i)
375 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
376 ? ((uint32_t *) (void *) buf)[i] : buf[i];
380 #ifdef SYS_ipc_subcall
382 decode_ipc_subcall(struct tcb *tcp)
384 unsigned int call = tcp->u_arg[0];
385 const unsigned int version = call >> 16;
388 # if defined S390 || defined S390X
392 if (current_wordsize == 8)
395 set_tcb_priv_ulong(tcp, version);
401 case 1: case 2: case 3: case 4:
402 case 11: case 12: case 13: case 14:
403 case 21: case 22: case 23: case 24:
409 tcp->scno = SYS_ipc_subcall + call;
410 tcp->qual_flg = qual_flags(tcp->scno);
411 tcp->s_ent = &sysent[tcp->scno];
413 const unsigned int n = tcp->s_ent->nargs;
415 for (i = 0; i < n; i++)
416 tcp->u_arg[i] = tcp->u_arg[i + 1];
422 decode_mips_subcall(struct tcb *tcp)
424 if (!scno_is_valid(tcp->u_arg[0]))
426 tcp->scno = tcp->u_arg[0];
427 tcp->qual_flg = qual_flags(tcp->scno);
428 tcp->s_ent = &sysent[tcp->scno];
429 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
430 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
432 * Fetching the last arg of 7-arg syscalls (fadvise64_64
433 * and sync_file_range) requires additional code,
434 * see linux/mips/get_syscall_args.c
436 if (tcp->s_ent->nargs == MAX_ARGS) {
438 mips_REG_SP + MAX_ARGS * sizeof(tcp->u_arg[0]),
439 sizeof(tcp->u_arg[0]),
440 &tcp->u_arg[MAX_ARGS - 1]) < 0)
441 tcp->u_arg[MAX_ARGS - 1] = 0;
444 #endif /* LINUX_MIPSO32 */
447 dumpio(struct tcb *tcp)
452 int fd = tcp->u_arg[0];
456 if (is_number_in_set(fd, &read_set)) {
457 switch (tcp->s_ent->sen) {
462 case SEN_mq_timedreceive:
463 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
468 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
472 dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
475 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
479 if (is_number_in_set(fd, &write_set)) {
480 switch (tcp->s_ent->sen) {
485 case SEN_mq_timedsend:
486 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
492 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
495 dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1UL);
498 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
505 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
506 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
509 shuffle_scno(kernel_scno_t scno)
511 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
512 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
515 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
516 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
518 if (scno == 0x000ffff0)
519 return ARM_FIRST_SHUFFLED_SYSCALL;
521 # define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
523 * Is it ARM specific syscall?
524 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
525 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
527 if (scno >= 0x000f0000 &&
528 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
529 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
531 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
532 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
534 #endif /* ARM || AARCH64 */
540 err_name(unsigned long err)
542 if ((err < nerrnos) && errnoent[err])
543 return errnoent[err];
548 static long get_regs_error;
556 static int get_syscall_args(struct tcb *);
557 static int get_syscall_result(struct tcb *);
558 static int arch_get_scno(struct tcb *tcp);
559 static int arch_set_scno(struct tcb *, kernel_scno_t);
560 static void get_error(struct tcb *, const bool);
561 static int arch_set_error(struct tcb *);
563 struct fault_opts *fault_vec[SUPPORTED_PERSONALITIES];
565 static struct fault_opts *
566 tcb_fault_opts(struct tcb *tcp)
568 return (scno_in_range(tcp->scno) && tcp->fault_vec[current_personality])
569 ? &tcp->fault_vec[current_personality][tcp->scno] : NULL;
574 inject_syscall_fault_entering(struct tcb *tcp)
576 if (!tcp->fault_vec[current_personality]) {
577 tcp->fault_vec[current_personality] =
578 xcalloc(nsyscalls, sizeof(**fault_vec));
579 memcpy(tcp->fault_vec[current_personality],
580 fault_vec[current_personality],
581 nsyscalls * sizeof(**fault_vec));
584 struct fault_opts *opts = tcb_fault_opts(tcp);
586 if (!opts || opts->first == 0)
591 if (opts->first != 0)
594 opts->first = opts->step;
596 if (!arch_set_scno(tcp, -1))
597 tcp->flags |= TCB_FAULT_INJ;
603 update_syscall_fault_exiting(struct tcb *tcp)
605 struct fault_opts *opts = tcb_fault_opts(tcp);
607 if (opts && opts->err && tcp->u_error != opts->err) {
608 unsigned long u_error = tcp->u_error;
609 tcp->u_error = opts->err;
610 if (arch_set_error(tcp))
611 tcp->u_error = u_error;
618 trace_syscall_entering(struct tcb *tcp)
622 scno_good = res = get_scno(tcp);
626 res = get_syscall_args(tcp);
630 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
632 * " <unavailable>" will be added later by the code which
633 * detects ptrace errors.
639 if (SEN_syscall == tcp->s_ent->sen)
640 decode_mips_subcall(tcp);
643 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
644 switch (tcp->s_ent->sen) {
645 # ifdef SYS_socket_subcall
647 decode_socket_subcall(tcp);
650 # ifdef SYS_ipc_subcall
652 decode_ipc_subcall(tcp);
658 /* Restrain from fault injection while the trace executes strace code. */
660 tcp->qual_flg &= ~QUAL_FAULT;
663 switch (tcp->s_ent->sen) {
666 #if defined SPARC || defined SPARC64
669 tcp->flags &= ~TCB_HIDE_LOG;
673 if (!(tcp->qual_flg & QUAL_TRACE)
674 || (tracing_paths && !pathtrace_match(tcp))
676 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
677 tcp->sys_func_rval = 0;
681 tcp->flags &= ~TCB_FILTERED;
688 if (tcp->qual_flg & QUAL_FAULT)
689 inject_syscall_fault_entering(tcp);
691 if (cflag == CFLAG_ONLY_STATS) {
697 if (stack_trace_enabled) {
698 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
699 unwind_capture_stacktrace(tcp);
704 tprintf("%s(", tcp->s_ent->sys_name);
705 if (tcp->qual_flg & QUAL_RAW)
706 res = printargs(tcp);
708 res = tcp->s_ent->sys_func(tcp);
712 tcp->flags |= TCB_INSYSCALL;
713 tcp->sys_func_rval = res;
714 /* Measure the entrance time as late as possible to avoid errors. */
716 gettimeofday(&tcp->etime, NULL);
721 syscall_fault_injected(struct tcb *tcp)
723 return tcp->flags & TCB_FAULT_INJ;
727 trace_syscall_exiting(struct tcb *tcp)
732 unsigned long u_error;
733 const char *u_error_str;
735 /* Measure the exit time as early as possible to avoid errors. */
737 gettimeofday(&tv, NULL);
740 if (stack_trace_enabled) {
741 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
742 unwind_cache_invalidate(tcp);
746 #if SUPPORTED_PERSONALITIES > 1
747 update_personality(tcp, tcp->currpers);
749 res = (get_regs_error ? -1 : get_syscall_result(tcp));
750 if (filtered(tcp) || hide_log(tcp))
753 if (syserror(tcp) && syscall_fault_injected(tcp))
754 update_syscall_fault_exiting(tcp);
757 count_syscall(tcp, &tv);
758 if (cflag == CFLAG_ONLY_STATS) {
763 /* If not in -ff mode, and printing_tcp != tcp,
764 * then the log currently does not end with output
765 * of _our syscall entry_, but with something else.
766 * We need to say which syscall's return is this.
768 * Forced reprinting via TCB_REPRINT is used only by
769 * "strace -ff -oLOG test/threaded_execve" corner case.
770 * It's the only case when -ff mode needs reprinting.
772 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
773 tcp->flags &= ~TCB_REPRINT;
775 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
779 tcp->s_prev_ent = NULL;
781 /* There was error in one of prior ptrace ops */
784 tprints("= ? <unavailable>\n");
786 tcp->flags &= ~(TCB_INSYSCALL | TCB_FAULT_INJ);
787 tcp->sys_func_rval = 0;
788 free_tcb_priv_data(tcp);
791 tcp->s_prev_ent = tcp->s_ent;
794 if (tcp->qual_flg & QUAL_RAW) {
795 /* sys_res = printargs(tcp); - but it's nop on sysexit */
797 /* FIXME: not_failing_only (IOW, option -z) is broken:
798 * failure of syscall is known only after syscall return.
799 * Thus we end up with something like this on, say, ENOENT:
800 * open("doesnt_exist", O_RDONLY <unfinished ...>
801 * {next syscall decode}
802 * whereas the intended result is that open(...) line
803 * is not shown at all.
805 if (not_failing_only && tcp->u_error)
806 goto ret; /* ignore failed syscalls */
807 if (tcp->sys_func_rval & RVAL_DECODED)
808 sys_res = tcp->sys_func_rval;
810 sys_res = tcp->s_ent->sys_func(tcp);
815 u_error = tcp->u_error;
817 if (tcp->qual_flg & QUAL_RAW) {
819 tprintf("= -1 (errno %lu)", u_error);
820 if (syscall_fault_injected(tcp))
821 tprints(" (INJECTED)");
823 tprintf("= %#lx", tcp->u_rval);
826 else if (!(sys_res & RVAL_NONE) && u_error) {
828 /* Blocked signals do not interrupt any syscalls.
829 * In this case syscalls don't return ERESTARTfoo codes.
831 * Deadly signals set to SIG_DFL interrupt syscalls
832 * and kill the process regardless of which of the codes below
833 * is returned by the interrupted syscall.
834 * In some cases, kernel forces a kernel-generated deadly
835 * signal to be unblocked and set to SIG_DFL (and thus cause
836 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
837 * or SIGILL. (The alternative is to leave process spinning
838 * forever on the faulty instruction - not useful).
840 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
841 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
842 * but kernel will always restart them.
845 /* Most common type of signal-interrupted syscall exit code.
846 * The system call will be restarted with the same arguments
847 * if SA_RESTART is set; otherwise, it will fail with EINTR.
849 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
852 /* Rare. For example, fork() returns this if interrupted.
853 * SA_RESTART is ignored (assumed set): the restart is unconditional.
855 tprints("= ? ERESTARTNOINTR (To be restarted)");
858 /* pause(), rt_sigsuspend() etc use this code.
859 * SA_RESTART is ignored (assumed not set):
860 * syscall won't restart (will return EINTR instead)
861 * even after signal with SA_RESTART set. However,
862 * after SIG_IGN or SIG_DFL signal it will restart
863 * (thus the name "restart only if has no handler").
865 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
867 case ERESTART_RESTARTBLOCK:
868 /* Syscalls like nanosleep(), poll() which can't be
869 * restarted with their original arguments use this
870 * code. Kernel will execute restart_syscall() instead,
871 * which changes arguments before restarting syscall.
872 * SA_RESTART is ignored (assumed not set) similarly
873 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
874 * since restart data is saved in "restart block"
875 * in task struct, and if signal handler uses a syscall
876 * which in turn saves another such restart block,
877 * old data is lost and restart becomes impossible)
879 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
882 u_error_str = err_name(u_error);
884 tprintf("= -1 %s (%s)",
885 u_error_str, strerror(u_error));
887 tprintf("= -1 %lu (%s)",
888 u_error, strerror(u_error));
891 if (syscall_fault_injected(tcp))
892 tprintf(" (INJECTED)");
893 if ((sys_res & RVAL_STR) && tcp->auxstr)
894 tprintf(" (%s)", tcp->auxstr);
897 if (sys_res & RVAL_NONE)
900 switch (sys_res & RVAL_MASK) {
902 #if SUPPORTED_PERSONALITIES > 1
903 if (current_wordsize < sizeof(long))
905 (unsigned int) tcp->u_rval);
908 tprintf("= %#lx", tcp->u_rval);
912 print_numeric_long_umask(tcp->u_rval);
915 #if SUPPORTED_PERSONALITIES > 1
916 if (current_wordsize < sizeof(long))
918 (unsigned int) tcp->u_rval);
921 tprintf("= %lu", tcp->u_rval);
924 tprintf("= %ld", tcp->u_rval);
929 printfd(tcp, tcp->u_rval);
932 tprintf("= %ld", tcp->u_rval);
934 #if HAVE_STRUCT_TCB_EXT_ARG
937 tprintf("= %#llx", tcp->u_lrval);
940 tprintf("= %#llo", tcp->u_lrval);
944 tprintf("= %llu", tcp->u_lrval);
948 tprintf("= %lld", tcp->u_lrval);
951 #endif /* HAVE_STRUCT_TCB_EXT_ARG */
953 error_msg("invalid rval format");
957 if ((sys_res & RVAL_STR) && tcp->auxstr)
958 tprintf(" (%s)", tcp->auxstr);
961 tv_sub(&tv, &tv, &tcp->etime);
962 tprintf(" <%ld.%06ld>",
963 (long) tv.tv_sec, (long) tv.tv_usec);
970 if (stack_trace_enabled)
971 unwind_print_stacktrace(tcp);
975 tcp->flags &= ~(TCB_INSYSCALL | TCB_FAULT_INJ);
976 tcp->sys_func_rval = 0;
977 free_tcb_priv_data(tcp);
982 trace_syscall(struct tcb *tcp)
984 return exiting(tcp) ?
985 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
989 is_erestart(struct tcb *tcp)
991 switch (tcp->u_error) {
995 case ERESTART_RESTARTBLOCK:
1002 static unsigned long saved_u_error;
1005 temporarily_clear_syserror(struct tcb *tcp)
1007 saved_u_error = tcp->u_error;
1012 restore_cleared_syserror(struct tcb *tcp)
1014 tcp->u_error = saved_u_error;
1018 * Check the syscall return value register value for whether it is
1019 * a negated errno code indicating an error, or a success return value.
1022 is_negated_errno(kernel_ulong_t val)
1024 /* Linux kernel defines MAX_ERRNO to 4095. */
1025 kernel_ulong_t max = -(kernel_long_t) 4095;
1027 #ifndef current_klongsize
1028 if (current_klongsize < sizeof(val)) {
1029 val = (uint32_t) val;
1030 max = (uint32_t) max;
1032 #endif /* !current_klongsize */
1037 #include "arch_regs.c"
1039 #ifdef HAVE_GETRVAL2
1040 # include "arch_getrval2.c"
1044 print_pc(struct tcb *tcp)
1046 #if defined ARCH_PC_REG
1047 # define ARCH_GET_PC 0
1048 #elif defined ARCH_PC_PEEK_ADDR
1050 # define ARCH_PC_REG pc
1051 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
1053 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1055 if (get_regs_error || ARCH_GET_PC)
1056 tprints(current_wordsize == 4 ? "[????????] "
1057 : "[????????????????] ");
1059 tprintf(current_wordsize == 4 ? "[%08lx] " : "[%016lx] ",
1060 (unsigned long) ARCH_PC_REG);
1063 #include "getregs_old.h"
1065 #undef ptrace_getregset_or_getregs
1066 #undef ptrace_setregset_or_setregs
1067 #ifdef ARCH_REGS_FOR_GETREGSET
1069 # define ptrace_getregset_or_getregs ptrace_getregset
1071 ptrace_getregset(pid_t pid)
1073 # ifdef ARCH_IOVEC_FOR_GETREGSET
1074 /* variable iovec */
1075 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1076 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1077 &ARCH_IOVEC_FOR_GETREGSET);
1079 /* constant iovec */
1080 static struct iovec io = {
1081 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1082 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1084 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1089 # ifndef HAVE_GETREGS_OLD
1090 # define ptrace_setregset_or_setregs ptrace_setregset
1092 ptrace_setregset(pid_t pid)
1094 # ifdef ARCH_IOVEC_FOR_GETREGSET
1095 /* variable iovec */
1096 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1097 &ARCH_IOVEC_FOR_GETREGSET);
1099 /* constant iovec */
1100 static struct iovec io = {
1101 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1102 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1104 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1107 # endif /* !HAVE_GETREGS_OLD */
1109 #elif defined ARCH_REGS_FOR_GETREGS
1111 # define ptrace_getregset_or_getregs ptrace_getregs
1113 ptrace_getregs(pid_t pid)
1115 # if defined SPARC || defined SPARC64
1116 /* SPARC systems have the meaning of data and addr reversed */
1117 return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1119 return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1123 # ifndef HAVE_GETREGS_OLD
1124 # define ptrace_setregset_or_setregs ptrace_setregs
1126 ptrace_setregs(pid_t pid)
1128 # if defined SPARC || defined SPARC64
1129 /* SPARC systems have the meaning of data and addr reversed */
1130 return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1132 return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1135 # endif /* !HAVE_GETREGS_OLD */
1137 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1142 #undef USE_GET_SYSCALL_RESULT_REGS
1143 #ifdef ptrace_getregset_or_getregs
1145 # ifdef HAVE_GETREGS_OLD
1147 * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1148 * fallback to getregs_old.
1150 static int use_getregs_old;
1151 if (use_getregs_old < 0) {
1152 get_regs_error = ptrace_getregset_or_getregs(pid);
1154 } else if (use_getregs_old == 0) {
1155 get_regs_error = ptrace_getregset_or_getregs(pid);
1156 if (get_regs_error >= 0) {
1157 use_getregs_old = -1;
1160 if (errno == EPERM || errno == ESRCH)
1162 use_getregs_old = 1;
1164 get_regs_error = getregs_old(pid);
1165 # else /* !HAVE_GETREGS_OLD */
1166 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1167 get_regs_error = ptrace_getregset_or_getregs(pid);
1168 # endif /* !HAVE_GETREGS_OLD */
1170 #else /* !ptrace_getregset_or_getregs */
1172 # define USE_GET_SYSCALL_RESULT_REGS 1
1173 # warning get_regs is not implemented for this architecture yet
1176 #endif /* !ptrace_getregset_or_getregs */
1179 #ifdef ptrace_setregset_or_setregs
1183 return ptrace_setregset_or_setregs(pid);
1185 #endif /* ptrace_setregset_or_setregs */
1190 char buf[sizeof("syscall_%lu") + sizeof(long) * 3];
1194 free_sysent_buf(void *ptr)
1196 struct sysent_buf *s = ptr;
1197 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1203 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1204 * 1: ok, continue in trace_syscall_entering().
1205 * other: error, trace_syscall_entering() should print error indicator
1206 * ("????" etc) and bail out.
1209 get_scno(struct tcb *tcp)
1214 int rc = arch_get_scno(tcp);
1218 if (scno_is_valid(tcp->scno)) {
1219 tcp->s_ent = &sysent[tcp->scno];
1220 tcp->qual_flg = qual_flags(tcp->scno);
1222 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1225 s->ent.nargs = MAX_ARGS;
1226 s->ent.sen = SEN_printargs;
1227 s->ent.sys_func = printargs;
1228 s->ent.sys_name = s->buf;
1229 sprintf(s->buf, "syscall_%lu", shuffle_scno(tcp->scno));
1231 tcp->s_ent = &s->ent;
1232 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1234 set_tcb_priv_data(tcp, s, free_sysent_buf);
1237 error_msg("pid %d invalid syscall %ld", tcp->pid, tcp->scno);
1242 #ifdef USE_GET_SYSCALL_RESULT_REGS
1243 static int get_syscall_result_regs(struct tcb *);
1247 * 1: ok, continue in trace_syscall_exiting().
1248 * -1: error, trace_syscall_exiting() should print error indicator
1249 * ("????" etc) and bail out.
1252 get_syscall_result(struct tcb *tcp)
1254 #ifdef USE_GET_SYSCALL_RESULT_REGS
1255 if (get_syscall_result_regs(tcp))
1259 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1264 #include "get_scno.c"
1265 #include "set_scno.c"
1266 #include "get_syscall_args.c"
1267 #ifdef USE_GET_SYSCALL_RESULT_REGS
1268 # include "get_syscall_result.c"
1270 #include "get_error.c"
1271 #include "set_error.c"
1272 #ifdef HAVE_GETREGS_OLD
1273 # include "getregs_old.c"
1277 syscall_name(kernel_scno_t scno)
1279 #if defined X32_PERSONALITY_NUMBER && defined __X32_SYSCALL_BIT
1280 if (current_personality == X32_PERSONALITY_NUMBER)
1281 scno &= ~__X32_SYSCALL_BIT;
1283 return scno_is_valid(scno) ? sysent[scno].sys_name: NULL;