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
85 #define CST COMPAT_SYSCALL_TYPES
87 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
89 const struct_sysent sysent0[] = {
90 #include "syscallent.h"
93 #if SUPPORTED_PERSONALITIES > 1
94 # include PERSONALITY1_INCLUDE_FUNCS
95 static const struct_sysent sysent1[] = {
96 # include "syscallent1.h"
100 #if SUPPORTED_PERSONALITIES > 2
101 # include PERSONALITY2_INCLUDE_FUNCS
102 static const struct_sysent sysent2[] = {
103 # include "syscallent2.h"
107 /* Now undef them since short defines cause wicked namespace pollution. */
123 * `ioctlent[012].h' files are automatically generated by the auxiliary
124 * program `ioctlsort', such that the list is sorted by the `code' field.
125 * This has the side-effect of resolving the _IO.. macros into
126 * plain integers, eliminating the need to include here everything
130 const char *const errnoent0[] = {
131 #include "errnoent.h"
133 const char *const signalent0[] = {
134 #include "signalent.h"
136 const struct_ioctlent ioctlent0[] = {
137 #include "ioctlent0.h"
140 #if SUPPORTED_PERSONALITIES > 1
141 static const char *const errnoent1[] = {
142 # include "errnoent1.h"
144 static const char *const signalent1[] = {
145 # include "signalent1.h"
147 static const struct_ioctlent ioctlent1[] = {
148 # include "ioctlent1.h"
150 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
151 static const struct_printers printers0 = {
152 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
154 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
155 static const struct_printers printers1 = {
156 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
160 #if SUPPORTED_PERSONALITIES > 2
161 static const char *const errnoent2[] = {
162 # include "errnoent2.h"
164 static const char *const signalent2[] = {
165 # include "signalent2.h"
167 static const struct_ioctlent ioctlent2[] = {
168 # include "ioctlent2.h"
170 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
171 static const struct_printers printers2 = {
172 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
177 nsyscalls0 = ARRAY_SIZE(sysent0)
178 #if SUPPORTED_PERSONALITIES > 1
179 , nsyscalls1 = ARRAY_SIZE(sysent1)
180 # if SUPPORTED_PERSONALITIES > 2
181 , nsyscalls2 = ARRAY_SIZE(sysent2)
187 nerrnos0 = ARRAY_SIZE(errnoent0)
188 #if SUPPORTED_PERSONALITIES > 1
189 , nerrnos1 = ARRAY_SIZE(errnoent1)
190 # if SUPPORTED_PERSONALITIES > 2
191 , nerrnos2 = ARRAY_SIZE(errnoent2)
197 nsignals0 = ARRAY_SIZE(signalent0)
198 #if SUPPORTED_PERSONALITIES > 1
199 , nsignals1 = ARRAY_SIZE(signalent1)
200 # if SUPPORTED_PERSONALITIES > 2
201 , nsignals2 = ARRAY_SIZE(signalent2)
207 nioctlents0 = ARRAY_SIZE(ioctlent0)
208 #if SUPPORTED_PERSONALITIES > 1
209 , nioctlents1 = ARRAY_SIZE(ioctlent1)
210 # if SUPPORTED_PERSONALITIES > 2
211 , nioctlents2 = ARRAY_SIZE(ioctlent2)
216 #if SUPPORTED_PERSONALITIES > 1
217 const struct_sysent *sysent = sysent0;
218 const char *const *errnoent = errnoent0;
219 const char *const *signalent = signalent0;
220 const struct_ioctlent *ioctlent = ioctlent0;
221 const struct_printers *printers = &printers0;
224 unsigned nsyscalls = nsyscalls0;
225 unsigned nerrnos = nerrnos0;
226 unsigned nsignals = nsignals0;
227 unsigned nioctlents = nioctlents0;
229 const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES] = {
231 #if SUPPORTED_PERSONALITIES > 1
234 #if SUPPORTED_PERSONALITIES > 2
238 const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
240 #if SUPPORTED_PERSONALITIES > 1
243 #if SUPPORTED_PERSONALITIES > 2
248 #if SUPPORTED_PERSONALITIES > 1
249 unsigned current_personality;
251 # ifndef current_wordsize
252 unsigned current_wordsize;
253 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
254 PERSONALITY0_WORDSIZE,
255 PERSONALITY1_WORDSIZE,
256 # if SUPPORTED_PERSONALITIES > 2
257 PERSONALITY2_WORDSIZE,
262 # ifndef current_klongsize
263 unsigned current_klongsize;
264 static const int personality_klongsize[SUPPORTED_PERSONALITIES] = {
265 PERSONALITY0_KLONGSIZE,
266 PERSONALITY1_KLONGSIZE,
267 # if SUPPORTED_PERSONALITIES > 2
268 PERSONALITY2_KLONGSIZE,
274 set_personality(int personality)
276 nsyscalls = nsyscall_vec[personality];
277 sysent = sysent_vec[personality];
279 switch (personality) {
281 errnoent = errnoent0;
283 ioctlent = ioctlent0;
284 nioctlents = nioctlents0;
285 signalent = signalent0;
286 nsignals = nsignals0;
287 printers = &printers0;
291 errnoent = errnoent1;
293 ioctlent = ioctlent1;
294 nioctlents = nioctlents1;
295 signalent = signalent1;
296 nsignals = nsignals1;
297 printers = &printers1;
300 # if SUPPORTED_PERSONALITIES > 2
302 errnoent = errnoent2;
304 ioctlent = ioctlent2;
305 nioctlents = nioctlents2;
306 signalent = signalent2;
307 nsignals = nsignals2;
308 printers = &printers2;
313 current_personality = personality;
314 # ifndef current_wordsize
315 current_wordsize = personality_wordsize[personality];
317 # ifndef current_klongsize
318 current_klongsize = personality_klongsize[personality];
323 update_personality(struct tcb *tcp, unsigned int personality)
325 if (personality == current_personality)
327 set_personality(personality);
329 if (personality == tcp->currpers)
331 tcp->currpers = personality;
333 # undef PERSONALITY_NAMES
334 # if defined POWERPC64
335 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
336 # elif defined X86_64
337 # define PERSONALITY_NAMES {"64 bit", "32 bit", "x32"}
339 # define PERSONALITY_NAMES {"x32", "32 bit"}
340 # elif defined AARCH64
341 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
343 # define PERSONALITY_NAMES {"64-bit", "32-bit"}
345 # ifdef PERSONALITY_NAMES
347 static const char *const names[] = PERSONALITY_NAMES;
348 error_msg("[ Process PID=%d runs in %s mode. ]",
349 tcp->pid, names[personality]);
355 #ifdef SYS_socket_subcall
357 decode_socket_subcall(struct tcb *tcp)
359 const int call = tcp->u_arg[0];
361 if (call < 1 || call >= SYS_socket_nsubcalls)
364 const kernel_scno_t scno = SYS_socket_subcall + call;
365 const unsigned int nargs = sysent[scno].nargs;
368 if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
372 tcp->qual_flg = qual_flags(scno);
373 tcp->s_ent = &sysent[scno];
376 for (i = 0; i < nargs; ++i)
377 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
378 ? ((uint32_t *) (void *) buf)[i] : buf[i];
382 #ifdef SYS_ipc_subcall
384 decode_ipc_subcall(struct tcb *tcp)
386 unsigned int call = tcp->u_arg[0];
387 const unsigned int version = call >> 16;
390 # if defined S390 || defined S390X
394 if (current_wordsize == 8)
397 set_tcb_priv_ulong(tcp, version);
403 case 1: case 2: case 3: case 4:
404 case 11: case 12: case 13: case 14:
405 case 21: case 22: case 23: case 24:
411 tcp->scno = SYS_ipc_subcall + call;
412 tcp->qual_flg = qual_flags(tcp->scno);
413 tcp->s_ent = &sysent[tcp->scno];
415 const unsigned int n = tcp->s_ent->nargs;
417 for (i = 0; i < n; i++)
418 tcp->u_arg[i] = tcp->u_arg[i + 1];
424 decode_mips_subcall(struct tcb *tcp)
426 if (!scno_is_valid(tcp->u_arg[0]))
428 tcp->scno = tcp->u_arg[0];
429 tcp->qual_flg = qual_flags(tcp->scno);
430 tcp->s_ent = &sysent[tcp->scno];
431 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
432 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
434 * Fetching the last arg of 7-arg syscalls (fadvise64_64
435 * and sync_file_range) requires additional code,
436 * see linux/mips/get_syscall_args.c
438 if (tcp->s_ent->nargs == MAX_ARGS) {
440 mips_REG_SP + MAX_ARGS * sizeof(tcp->u_arg[0]),
441 sizeof(tcp->u_arg[0]),
442 &tcp->u_arg[MAX_ARGS - 1]) < 0)
443 tcp->u_arg[MAX_ARGS - 1] = 0;
446 #endif /* LINUX_MIPSO32 */
449 dumpio(struct tcb *tcp)
454 int fd = tcp->u_arg[0];
458 if (is_number_in_set(fd, &read_set)) {
459 switch (tcp->s_ent->sen) {
464 case SEN_mq_timedreceive:
465 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
470 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
474 dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
477 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
481 if (is_number_in_set(fd, &write_set)) {
482 switch (tcp->s_ent->sen) {
487 case SEN_mq_timedsend:
488 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
494 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1);
497 dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1);
500 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
507 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
508 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
511 shuffle_scno(kernel_scno_t scno)
513 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
514 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
517 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
518 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
520 if (scno == 0x000ffff0)
521 return ARM_FIRST_SHUFFLED_SYSCALL;
523 # define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
525 * Is it ARM specific syscall?
526 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
527 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
529 if (scno >= 0x000f0000 &&
530 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
531 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
533 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
534 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
536 #endif /* ARM || AARCH64 */
542 err_name(unsigned long err)
544 if ((err < nerrnos) && errnoent[err])
545 return errnoent[err];
550 static long get_regs_error;
558 static int get_syscall_args(struct tcb *);
559 static int get_syscall_result(struct tcb *);
560 static int arch_get_scno(struct tcb *tcp);
561 static int arch_set_scno(struct tcb *, kernel_scno_t);
562 static void get_error(struct tcb *, const bool);
563 static int arch_set_error(struct tcb *);
565 struct fault_opts *fault_vec[SUPPORTED_PERSONALITIES];
567 static struct fault_opts *
568 tcb_fault_opts(struct tcb *tcp)
570 return (scno_in_range(tcp->scno) && tcp->fault_vec[current_personality])
571 ? &tcp->fault_vec[current_personality][tcp->scno] : NULL;
576 inject_syscall_fault_entering(struct tcb *tcp)
578 if (!tcp->fault_vec[current_personality]) {
579 tcp->fault_vec[current_personality] =
580 xcalloc(nsyscalls, sizeof(**fault_vec));
581 memcpy(tcp->fault_vec[current_personality],
582 fault_vec[current_personality],
583 nsyscalls * sizeof(**fault_vec));
586 struct fault_opts *opts = tcb_fault_opts(tcp);
588 if (!opts || opts->first == 0)
593 if (opts->first != 0)
596 opts->first = opts->step;
598 if (!arch_set_scno(tcp, -1))
599 tcp->flags |= TCB_FAULT_INJ;
605 update_syscall_fault_exiting(struct tcb *tcp)
607 struct fault_opts *opts = tcb_fault_opts(tcp);
609 if (opts && opts->err && tcp->u_error != opts->err) {
610 unsigned long u_error = tcp->u_error;
611 tcp->u_error = opts->err;
612 if (arch_set_error(tcp))
613 tcp->u_error = u_error;
620 trace_syscall_entering(struct tcb *tcp)
624 scno_good = res = get_scno(tcp);
628 res = get_syscall_args(tcp);
632 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
634 * " <unavailable>" will be added later by the code which
635 * detects ptrace errors.
641 if (SEN_syscall == tcp->s_ent->sen)
642 decode_mips_subcall(tcp);
645 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
646 switch (tcp->s_ent->sen) {
647 # ifdef SYS_socket_subcall
649 decode_socket_subcall(tcp);
652 # ifdef SYS_ipc_subcall
654 decode_ipc_subcall(tcp);
660 /* Restrain from fault injection while the trace executes strace code. */
662 tcp->qual_flg &= ~QUAL_FAULT;
665 switch (tcp->s_ent->sen) {
668 #if defined SPARC || defined SPARC64
671 tcp->flags &= ~TCB_HIDE_LOG;
675 if (!(tcp->qual_flg & QUAL_TRACE)
676 || (tracing_paths && !pathtrace_match(tcp))
678 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
679 tcp->sys_func_rval = 0;
683 tcp->flags &= ~TCB_FILTERED;
690 if (tcp->qual_flg & QUAL_FAULT)
691 inject_syscall_fault_entering(tcp);
693 if (cflag == CFLAG_ONLY_STATS) {
699 if (stack_trace_enabled) {
700 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
701 unwind_capture_stacktrace(tcp);
706 tprintf("%s(", tcp->s_ent->sys_name);
707 if (tcp->qual_flg & QUAL_RAW)
708 res = printargs(tcp);
710 res = tcp->s_ent->sys_func(tcp);
714 tcp->flags |= TCB_INSYSCALL;
715 tcp->sys_func_rval = res;
716 /* Measure the entrance time as late as possible to avoid errors. */
718 gettimeofday(&tcp->etime, NULL);
723 syscall_fault_injected(struct tcb *tcp)
725 return tcp->flags & TCB_FAULT_INJ;
729 trace_syscall_exiting(struct tcb *tcp)
734 unsigned long u_error;
735 const char *u_error_str;
737 /* Measure the exit time as early as possible to avoid errors. */
739 gettimeofday(&tv, NULL);
742 if (stack_trace_enabled) {
743 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
744 unwind_cache_invalidate(tcp);
748 #if SUPPORTED_PERSONALITIES > 1
749 update_personality(tcp, tcp->currpers);
751 res = (get_regs_error ? -1 : get_syscall_result(tcp));
752 if (filtered(tcp) || hide_log(tcp))
755 if (syserror(tcp) && syscall_fault_injected(tcp))
756 update_syscall_fault_exiting(tcp);
759 count_syscall(tcp, &tv);
760 if (cflag == CFLAG_ONLY_STATS) {
765 /* If not in -ff mode, and printing_tcp != tcp,
766 * then the log currently does not end with output
767 * of _our syscall entry_, but with something else.
768 * We need to say which syscall's return is this.
770 * Forced reprinting via TCB_REPRINT is used only by
771 * "strace -ff -oLOG test/threaded_execve" corner case.
772 * It's the only case when -ff mode needs reprinting.
774 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
775 tcp->flags &= ~TCB_REPRINT;
777 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
781 tcp->s_prev_ent = NULL;
783 /* There was error in one of prior ptrace ops */
786 tprints("= ? <unavailable>\n");
788 tcp->flags &= ~(TCB_INSYSCALL | TCB_FAULT_INJ);
789 tcp->sys_func_rval = 0;
790 free_tcb_priv_data(tcp);
793 tcp->s_prev_ent = tcp->s_ent;
796 if (tcp->qual_flg & QUAL_RAW) {
797 /* sys_res = printargs(tcp); - but it's nop on sysexit */
799 /* FIXME: not_failing_only (IOW, option -z) is broken:
800 * failure of syscall is known only after syscall return.
801 * Thus we end up with something like this on, say, ENOENT:
802 * open("doesnt_exist", O_RDONLY <unfinished ...>
803 * {next syscall decode}
804 * whereas the intended result is that open(...) line
805 * is not shown at all.
807 if (not_failing_only && tcp->u_error)
808 goto ret; /* ignore failed syscalls */
809 if (tcp->sys_func_rval & RVAL_DECODED)
810 sys_res = tcp->sys_func_rval;
812 sys_res = tcp->s_ent->sys_func(tcp);
817 u_error = tcp->u_error;
819 if (tcp->qual_flg & QUAL_RAW) {
821 tprintf("= -1 (errno %lu)", u_error);
822 if (syscall_fault_injected(tcp))
823 tprints(" (INJECTED)");
825 tprintf("= %#lx", tcp->u_rval);
828 else if (!(sys_res & RVAL_NONE) && u_error) {
830 /* Blocked signals do not interrupt any syscalls.
831 * In this case syscalls don't return ERESTARTfoo codes.
833 * Deadly signals set to SIG_DFL interrupt syscalls
834 * and kill the process regardless of which of the codes below
835 * is returned by the interrupted syscall.
836 * In some cases, kernel forces a kernel-generated deadly
837 * signal to be unblocked and set to SIG_DFL (and thus cause
838 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
839 * or SIGILL. (The alternative is to leave process spinning
840 * forever on the faulty instruction - not useful).
842 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
843 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
844 * but kernel will always restart them.
847 /* Most common type of signal-interrupted syscall exit code.
848 * The system call will be restarted with the same arguments
849 * if SA_RESTART is set; otherwise, it will fail with EINTR.
851 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
854 /* Rare. For example, fork() returns this if interrupted.
855 * SA_RESTART is ignored (assumed set): the restart is unconditional.
857 tprints("= ? ERESTARTNOINTR (To be restarted)");
860 /* pause(), rt_sigsuspend() etc use this code.
861 * SA_RESTART is ignored (assumed not set):
862 * syscall won't restart (will return EINTR instead)
863 * even after signal with SA_RESTART set. However,
864 * after SIG_IGN or SIG_DFL signal it will restart
865 * (thus the name "restart only if has no handler").
867 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
869 case ERESTART_RESTARTBLOCK:
870 /* Syscalls like nanosleep(), poll() which can't be
871 * restarted with their original arguments use this
872 * code. Kernel will execute restart_syscall() instead,
873 * which changes arguments before restarting syscall.
874 * SA_RESTART is ignored (assumed not set) similarly
875 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
876 * since restart data is saved in "restart block"
877 * in task struct, and if signal handler uses a syscall
878 * which in turn saves another such restart block,
879 * old data is lost and restart becomes impossible)
881 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
884 u_error_str = err_name(u_error);
886 tprintf("= -1 %s (%s)",
887 u_error_str, strerror(u_error));
889 tprintf("= -1 %lu (%s)",
890 u_error, strerror(u_error));
893 if (syscall_fault_injected(tcp))
894 tprintf(" (INJECTED)");
895 if ((sys_res & RVAL_STR) && tcp->auxstr)
896 tprintf(" (%s)", tcp->auxstr);
899 if (sys_res & RVAL_NONE)
902 switch (sys_res & RVAL_MASK) {
904 #if SUPPORTED_PERSONALITIES > 1
905 if (current_wordsize < sizeof(long))
907 (unsigned int) tcp->u_rval);
910 tprintf("= %#lx", tcp->u_rval);
914 print_numeric_long_umask(tcp->u_rval);
917 #if SUPPORTED_PERSONALITIES > 1
918 if (current_wordsize < sizeof(long))
920 (unsigned int) tcp->u_rval);
923 tprintf("= %lu", tcp->u_rval);
926 tprintf("= %ld", tcp->u_rval);
931 printfd(tcp, tcp->u_rval);
934 tprintf("= %ld", tcp->u_rval);
936 #if HAVE_STRUCT_TCB_EXT_ARG
939 tprintf("= %#llx", tcp->u_lrval);
942 tprintf("= %#llo", tcp->u_lrval);
946 tprintf("= %llu", tcp->u_lrval);
950 tprintf("= %lld", tcp->u_lrval);
953 #endif /* HAVE_STRUCT_TCB_EXT_ARG */
955 error_msg("invalid rval format");
959 if ((sys_res & RVAL_STR) && tcp->auxstr)
960 tprintf(" (%s)", tcp->auxstr);
963 tv_sub(&tv, &tv, &tcp->etime);
964 tprintf(" <%ld.%06ld>",
965 (long) tv.tv_sec, (long) tv.tv_usec);
972 if (stack_trace_enabled)
973 unwind_print_stacktrace(tcp);
977 tcp->flags &= ~(TCB_INSYSCALL | TCB_FAULT_INJ);
978 tcp->sys_func_rval = 0;
979 free_tcb_priv_data(tcp);
984 trace_syscall(struct tcb *tcp)
986 return exiting(tcp) ?
987 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
991 is_erestart(struct tcb *tcp)
993 switch (tcp->u_error) {
997 case ERESTART_RESTARTBLOCK:
1004 static unsigned long saved_u_error;
1007 temporarily_clear_syserror(struct tcb *tcp)
1009 saved_u_error = tcp->u_error;
1014 restore_cleared_syserror(struct tcb *tcp)
1016 tcp->u_error = saved_u_error;
1020 * Check the syscall return value register value for whether it is
1021 * a negated errno code indicating an error, or a success return value.
1024 is_negated_errno(kernel_ulong_t val)
1026 /* Linux kernel defines MAX_ERRNO to 4095. */
1027 kernel_ulong_t max = -(kernel_long_t) 4095;
1029 #ifndef current_klongsize
1030 if (current_klongsize < sizeof(val)) {
1031 val = (uint32_t) val;
1032 max = (uint32_t) max;
1034 #endif /* !current_klongsize */
1039 #include "arch_regs.c"
1041 #ifdef HAVE_GETRVAL2
1042 # include "arch_getrval2.c"
1046 print_pc(struct tcb *tcp)
1048 #if defined ARCH_PC_REG
1049 # define ARCH_GET_PC 0
1050 #elif defined ARCH_PC_PEEK_ADDR
1052 # define ARCH_PC_REG pc
1053 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
1055 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1057 if (get_regs_error || ARCH_GET_PC)
1058 tprints(current_wordsize == 4 ? "[????????] "
1059 : "[????????????????] ");
1061 tprintf(current_wordsize == 4 ? "[%08lx] " : "[%016lx] ",
1062 (unsigned long) ARCH_PC_REG);
1065 #include "getregs_old.h"
1067 #undef ptrace_getregset_or_getregs
1068 #undef ptrace_setregset_or_setregs
1069 #ifdef ARCH_REGS_FOR_GETREGSET
1071 # define ptrace_getregset_or_getregs ptrace_getregset
1073 ptrace_getregset(pid_t pid)
1075 # ifdef ARCH_IOVEC_FOR_GETREGSET
1076 /* variable iovec */
1077 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1078 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1079 &ARCH_IOVEC_FOR_GETREGSET);
1081 /* constant iovec */
1082 static struct iovec io = {
1083 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1084 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1086 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1091 # ifndef HAVE_GETREGS_OLD
1092 # define ptrace_setregset_or_setregs ptrace_setregset
1094 ptrace_setregset(pid_t pid)
1096 # ifdef ARCH_IOVEC_FOR_GETREGSET
1097 /* variable iovec */
1098 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1099 &ARCH_IOVEC_FOR_GETREGSET);
1101 /* constant iovec */
1102 static struct iovec io = {
1103 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1104 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1106 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1109 # endif /* !HAVE_GETREGS_OLD */
1111 #elif defined ARCH_REGS_FOR_GETREGS
1113 # define ptrace_getregset_or_getregs ptrace_getregs
1115 ptrace_getregs(pid_t pid)
1117 # if defined SPARC || defined SPARC64
1118 /* SPARC systems have the meaning of data and addr reversed */
1119 return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1121 return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1125 # ifndef HAVE_GETREGS_OLD
1126 # define ptrace_setregset_or_setregs ptrace_setregs
1128 ptrace_setregs(pid_t pid)
1130 # if defined SPARC || defined SPARC64
1131 /* SPARC systems have the meaning of data and addr reversed */
1132 return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1134 return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1137 # endif /* !HAVE_GETREGS_OLD */
1139 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1144 #undef USE_GET_SYSCALL_RESULT_REGS
1145 #ifdef ptrace_getregset_or_getregs
1147 # ifdef HAVE_GETREGS_OLD
1149 * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1150 * fallback to getregs_old.
1152 static int use_getregs_old;
1153 if (use_getregs_old < 0) {
1154 get_regs_error = ptrace_getregset_or_getregs(pid);
1156 } else if (use_getregs_old == 0) {
1157 get_regs_error = ptrace_getregset_or_getregs(pid);
1158 if (get_regs_error >= 0) {
1159 use_getregs_old = -1;
1162 if (errno == EPERM || errno == ESRCH)
1164 use_getregs_old = 1;
1166 get_regs_error = getregs_old(pid);
1167 # else /* !HAVE_GETREGS_OLD */
1168 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1169 get_regs_error = ptrace_getregset_or_getregs(pid);
1170 # endif /* !HAVE_GETREGS_OLD */
1172 #else /* !ptrace_getregset_or_getregs */
1174 # define USE_GET_SYSCALL_RESULT_REGS 1
1175 # warning get_regs is not implemented for this architecture yet
1178 #endif /* !ptrace_getregset_or_getregs */
1181 #ifdef ptrace_setregset_or_setregs
1185 return ptrace_setregset_or_setregs(pid);
1187 #endif /* ptrace_setregset_or_setregs */
1192 char buf[sizeof("syscall_%lu") + sizeof(long) * 3];
1196 free_sysent_buf(void *ptr)
1198 struct sysent_buf *s = ptr;
1199 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1205 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1206 * 1: ok, continue in trace_syscall_entering().
1207 * other: error, trace_syscall_entering() should print error indicator
1208 * ("????" etc) and bail out.
1211 get_scno(struct tcb *tcp)
1216 int rc = arch_get_scno(tcp);
1220 if (scno_is_valid(tcp->scno)) {
1221 tcp->s_ent = &sysent[tcp->scno];
1222 tcp->qual_flg = qual_flags(tcp->scno);
1224 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1227 s->ent.nargs = MAX_ARGS;
1228 s->ent.sen = SEN_printargs;
1229 s->ent.sys_func = printargs;
1230 s->ent.sys_name = s->buf;
1231 sprintf(s->buf, "syscall_%lu", shuffle_scno(tcp->scno));
1233 tcp->s_ent = &s->ent;
1234 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1236 set_tcb_priv_data(tcp, s, free_sysent_buf);
1239 error_msg("pid %d invalid syscall %ld", tcp->pid, tcp->scno);
1244 #ifdef USE_GET_SYSCALL_RESULT_REGS
1245 static int get_syscall_result_regs(struct tcb *);
1249 * 1: ok, continue in trace_syscall_exiting().
1250 * -1: error, trace_syscall_exiting() should print error indicator
1251 * ("????" etc) and bail out.
1254 get_syscall_result(struct tcb *tcp)
1256 #ifdef USE_GET_SYSCALL_RESULT_REGS
1257 if (get_syscall_result_regs(tcp))
1261 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1266 #include "get_scno.c"
1267 #include "set_scno.c"
1268 #include "get_syscall_args.c"
1269 #ifdef USE_GET_SYSCALL_RESULT_REGS
1270 # include "get_syscall_result.c"
1272 #include "get_error.c"
1273 #include "set_error.c"
1274 #ifdef HAVE_GETREGS_OLD
1275 # include "getregs_old.c"
1279 syscall_name(kernel_scno_t scno)
1281 #if defined X32_PERSONALITY_NUMBER && defined __X32_SYSCALL_BIT
1282 if (current_personality == X32_PERSONALITY_NUMBER)
1283 scno &= ~__X32_SYSCALL_BIT;
1285 return scno_is_valid(scno) ? sysent[scno].sys_name: NULL;