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"
37 #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
68 /* Define these shorthand notations to simplify the syscallent files. */
72 #define TN TRACE_NETWORK
73 #define TP TRACE_PROCESS
74 #define TS TRACE_SIGNAL
75 #define TM TRACE_MEMORY
76 #define NF SYSCALL_NEVER_FAILS
78 #define SI STACKTRACE_INVALIDATE_CACHE
79 #define SE STACKTRACE_CAPTURE_ON_ENTER
80 #define CST COMPAT_SYSCALL_TYPES
82 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
84 const struct_sysent sysent0[] = {
85 #include "syscallent.h"
88 #if SUPPORTED_PERSONALITIES > 1
89 # include PERSONALITY1_INCLUDE_FUNCS
90 static const struct_sysent sysent1[] = {
91 # include "syscallent1.h"
95 #if SUPPORTED_PERSONALITIES > 2
96 # include PERSONALITY2_INCLUDE_FUNCS
97 static const struct_sysent sysent2[] = {
98 # include "syscallent2.h"
102 /* Now undef them since short defines cause wicked namespace pollution. */
118 * `ioctlent[012].h' files are automatically generated by the auxiliary
119 * program `ioctlsort', such that the list is sorted by the `code' field.
120 * This has the side-effect of resolving the _IO.. macros into
121 * plain integers, eliminating the need to include here everything
125 const char *const errnoent0[] = {
126 #include "errnoent.h"
128 const char *const signalent0[] = {
129 #include "signalent.h"
131 const struct_ioctlent ioctlent0[] = {
132 #include "ioctlent0.h"
135 #if SUPPORTED_PERSONALITIES > 1
136 static const char *const errnoent1[] = {
137 # include "errnoent1.h"
139 static const char *const signalent1[] = {
140 # include "signalent1.h"
142 static const struct_ioctlent ioctlent1[] = {
143 # include "ioctlent1.h"
145 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
146 static const struct_printers printers0 = {
147 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
149 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
150 static const struct_printers printers1 = {
151 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
155 #if SUPPORTED_PERSONALITIES > 2
156 static const char *const errnoent2[] = {
157 # include "errnoent2.h"
159 static const char *const signalent2[] = {
160 # include "signalent2.h"
162 static const struct_ioctlent ioctlent2[] = {
163 # include "ioctlent2.h"
165 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
166 static const struct_printers printers2 = {
167 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
172 nsyscalls0 = ARRAY_SIZE(sysent0)
173 #if SUPPORTED_PERSONALITIES > 1
174 , nsyscalls1 = ARRAY_SIZE(sysent1)
175 # if SUPPORTED_PERSONALITIES > 2
176 , nsyscalls2 = ARRAY_SIZE(sysent2)
182 nerrnos0 = ARRAY_SIZE(errnoent0)
183 #if SUPPORTED_PERSONALITIES > 1
184 , nerrnos1 = ARRAY_SIZE(errnoent1)
185 # if SUPPORTED_PERSONALITIES > 2
186 , nerrnos2 = ARRAY_SIZE(errnoent2)
192 nsignals0 = ARRAY_SIZE(signalent0)
193 #if SUPPORTED_PERSONALITIES > 1
194 , nsignals1 = ARRAY_SIZE(signalent1)
195 # if SUPPORTED_PERSONALITIES > 2
196 , nsignals2 = ARRAY_SIZE(signalent2)
202 nioctlents0 = ARRAY_SIZE(ioctlent0)
203 #if SUPPORTED_PERSONALITIES > 1
204 , nioctlents1 = ARRAY_SIZE(ioctlent1)
205 # if SUPPORTED_PERSONALITIES > 2
206 , nioctlents2 = ARRAY_SIZE(ioctlent2)
211 #if SUPPORTED_PERSONALITIES > 1
212 const struct_sysent *sysent = sysent0;
213 const char *const *errnoent = errnoent0;
214 const char *const *signalent = signalent0;
215 const struct_ioctlent *ioctlent = ioctlent0;
216 const struct_printers *printers = &printers0;
219 unsigned nsyscalls = nsyscalls0;
220 unsigned nerrnos = nerrnos0;
221 unsigned nsignals = nsignals0;
222 unsigned nioctlents = nioctlents0;
224 const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES] = {
226 #if SUPPORTED_PERSONALITIES > 1
229 #if SUPPORTED_PERSONALITIES > 2
233 const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
235 #if SUPPORTED_PERSONALITIES > 1
238 #if SUPPORTED_PERSONALITIES > 2
243 #if SUPPORTED_PERSONALITIES > 1
244 unsigned current_personality;
246 # ifndef current_wordsize
247 unsigned current_wordsize;
248 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
249 PERSONALITY0_WORDSIZE,
250 PERSONALITY1_WORDSIZE,
251 # if SUPPORTED_PERSONALITIES > 2
252 PERSONALITY2_WORDSIZE,
257 # ifndef current_klongsize
258 unsigned current_klongsize;
259 static const int personality_klongsize[SUPPORTED_PERSONALITIES] = {
260 PERSONALITY0_KLONGSIZE,
261 PERSONALITY1_KLONGSIZE,
262 # if SUPPORTED_PERSONALITIES > 2
263 PERSONALITY2_KLONGSIZE,
269 set_personality(int personality)
271 nsyscalls = nsyscall_vec[personality];
272 sysent = sysent_vec[personality];
274 switch (personality) {
276 errnoent = errnoent0;
278 ioctlent = ioctlent0;
279 nioctlents = nioctlents0;
280 signalent = signalent0;
281 nsignals = nsignals0;
282 printers = &printers0;
286 errnoent = errnoent1;
288 ioctlent = ioctlent1;
289 nioctlents = nioctlents1;
290 signalent = signalent1;
291 nsignals = nsignals1;
292 printers = &printers1;
295 # if SUPPORTED_PERSONALITIES > 2
297 errnoent = errnoent2;
299 ioctlent = ioctlent2;
300 nioctlents = nioctlents2;
301 signalent = signalent2;
302 nsignals = nsignals2;
303 printers = &printers2;
308 current_personality = personality;
309 # ifndef current_wordsize
310 current_wordsize = personality_wordsize[personality];
312 # ifndef current_klongsize
313 current_klongsize = personality_klongsize[personality];
318 update_personality(struct tcb *tcp, unsigned int personality)
320 if (personality == current_personality)
322 set_personality(personality);
324 if (personality == tcp->currpers)
326 tcp->currpers = personality;
328 # undef PERSONALITY_NAMES
329 # if defined POWERPC64
330 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
331 # elif defined X86_64
332 # define PERSONALITY_NAMES {"64 bit", "32 bit", "x32"}
334 # define PERSONALITY_NAMES {"x32", "32 bit"}
335 # elif defined AARCH64
336 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
338 # define PERSONALITY_NAMES {"64-bit", "32-bit"}
340 # ifdef PERSONALITY_NAMES
342 static const char *const names[] = PERSONALITY_NAMES;
343 error_msg("[ Process PID=%d runs in %s mode. ]",
344 tcp->pid, names[personality]);
350 #ifdef SYS_socket_subcall
352 decode_socket_subcall(struct tcb *tcp)
354 const int call = tcp->u_arg[0];
356 if (call < 1 || call >= SYS_socket_nsubcalls)
359 const kernel_ulong_t scno = SYS_socket_subcall + call;
360 const unsigned int nargs = sysent[scno].nargs;
363 if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
367 tcp->qual_flg = qual_flags(scno);
368 tcp->s_ent = &sysent[scno];
371 for (i = 0; i < nargs; ++i)
372 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
373 ? ((uint32_t *) (void *) buf)[i] : buf[i];
377 #ifdef SYS_ipc_subcall
379 decode_ipc_subcall(struct tcb *tcp)
381 unsigned int call = tcp->u_arg[0];
382 const unsigned int version = call >> 16;
385 # if defined S390 || defined S390X
389 if (current_wordsize == 8)
392 set_tcb_priv_ulong(tcp, version);
398 case 1: case 2: case 3: case 4:
399 case 11: case 12: case 13: case 14:
400 case 21: case 22: case 23: case 24:
406 tcp->scno = SYS_ipc_subcall + call;
407 tcp->qual_flg = qual_flags(tcp->scno);
408 tcp->s_ent = &sysent[tcp->scno];
410 const unsigned int n = tcp->s_ent->nargs;
412 for (i = 0; i < n; i++)
413 tcp->u_arg[i] = tcp->u_arg[i + 1];
419 decode_mips_subcall(struct tcb *tcp)
421 if (!scno_is_valid(tcp->u_arg[0]))
423 tcp->scno = tcp->u_arg[0];
424 tcp->qual_flg = qual_flags(tcp->scno);
425 tcp->s_ent = &sysent[tcp->scno];
426 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
427 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
429 * Fetching the last arg of 7-arg syscalls (fadvise64_64
430 * and sync_file_range) requires additional code,
431 * see linux/mips/get_syscall_args.c
433 if (tcp->s_ent->nargs == MAX_ARGS) {
435 mips_REG_SP + MAX_ARGS * sizeof(tcp->u_arg[0]),
436 sizeof(tcp->u_arg[0]),
437 &tcp->u_arg[MAX_ARGS - 1]) < 0)
438 tcp->u_arg[MAX_ARGS - 1] = 0;
441 #endif /* LINUX_MIPSO32 */
444 dumpio(struct tcb *tcp)
449 int fd = tcp->u_arg[0];
453 if (is_number_in_set(fd, &read_set)) {
454 switch (tcp->s_ent->sen) {
459 case SEN_mq_timedreceive:
460 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
465 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
469 dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
472 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
476 if (is_number_in_set(fd, &write_set)) {
477 switch (tcp->s_ent->sen) {
482 case SEN_mq_timedsend:
483 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
489 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1);
492 dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1);
495 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
502 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
503 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
505 static kernel_ulong_t
506 shuffle_scno(kernel_ulong_t scno)
508 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
509 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
512 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
513 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
515 if (scno == 0x000ffff0)
516 return ARM_FIRST_SHUFFLED_SYSCALL;
518 # define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
520 * Is it ARM specific syscall?
521 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
522 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
524 if (scno >= 0x000f0000 &&
525 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
526 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
528 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
529 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
531 #endif /* ARM || AARCH64 */
537 err_name(unsigned long err)
539 if ((err < nerrnos) && errnoent[err])
540 return errnoent[err];
545 static long get_regs_error;
553 static int get_syscall_args(struct tcb *);
554 static int get_syscall_result(struct tcb *);
555 static int arch_get_scno(struct tcb *tcp);
556 static int arch_set_scno(struct tcb *, kernel_ulong_t);
557 static void get_error(struct tcb *, const bool);
558 static int arch_set_error(struct tcb *);
560 struct fault_opts *fault_vec[SUPPORTED_PERSONALITIES];
562 static struct fault_opts *
563 tcb_fault_opts(struct tcb *tcp)
565 return (scno_in_range(tcp->scno) && tcp->fault_vec[current_personality])
566 ? &tcp->fault_vec[current_personality][tcp->scno] : NULL;
571 inject_syscall_fault_entering(struct tcb *tcp)
573 if (!tcp->fault_vec[current_personality]) {
574 tcp->fault_vec[current_personality] =
575 xcalloc(nsyscalls, sizeof(**fault_vec));
576 memcpy(tcp->fault_vec[current_personality],
577 fault_vec[current_personality],
578 nsyscalls * sizeof(**fault_vec));
581 struct fault_opts *opts = tcb_fault_opts(tcp);
583 if (!opts || opts->first == 0)
588 if (opts->first != 0)
591 opts->first = opts->step;
593 if (!arch_set_scno(tcp, -1))
594 tcp->flags |= TCB_FAULT_INJ;
600 update_syscall_fault_exiting(struct tcb *tcp)
602 struct fault_opts *opts = tcb_fault_opts(tcp);
604 if (opts && opts->err && tcp->u_error != opts->err) {
605 unsigned long u_error = tcp->u_error;
606 tcp->u_error = opts->err;
607 if (arch_set_error(tcp))
608 tcp->u_error = u_error;
615 trace_syscall_entering(struct tcb *tcp)
619 scno_good = res = get_scno(tcp);
623 res = get_syscall_args(tcp);
627 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
629 * " <unavailable>" will be added later by the code which
630 * detects ptrace errors.
636 if (SEN_syscall == tcp->s_ent->sen)
637 decode_mips_subcall(tcp);
640 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
641 switch (tcp->s_ent->sen) {
642 # ifdef SYS_socket_subcall
644 decode_socket_subcall(tcp);
647 # ifdef SYS_ipc_subcall
649 decode_ipc_subcall(tcp);
655 /* Restrain from fault injection while the trace executes strace code. */
657 tcp->qual_flg &= ~QUAL_FAULT;
660 switch (tcp->s_ent->sen) {
663 #if defined SPARC || defined SPARC64
666 tcp->flags &= ~TCB_HIDE_LOG;
670 if (!(tcp->qual_flg & QUAL_TRACE)
671 || (tracing_paths && !pathtrace_match(tcp))
673 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
674 tcp->sys_func_rval = 0;
678 tcp->flags &= ~TCB_FILTERED;
685 if (tcp->qual_flg & QUAL_FAULT)
686 inject_syscall_fault_entering(tcp);
688 if (cflag == CFLAG_ONLY_STATS) {
694 if (stack_trace_enabled) {
695 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
696 unwind_capture_stacktrace(tcp);
701 tprintf("%s(", tcp->s_ent->sys_name);
702 if (tcp->qual_flg & QUAL_RAW)
703 res = printargs(tcp);
705 res = tcp->s_ent->sys_func(tcp);
709 tcp->flags |= TCB_INSYSCALL;
710 tcp->sys_func_rval = res;
711 /* Measure the entrance time as late as possible to avoid errors. */
713 gettimeofday(&tcp->etime, NULL);
718 syscall_fault_injected(struct tcb *tcp)
720 return tcp->flags & TCB_FAULT_INJ;
724 trace_syscall_exiting(struct tcb *tcp)
729 unsigned long u_error;
730 const char *u_error_str;
732 /* Measure the exit time as early as possible to avoid errors. */
734 gettimeofday(&tv, NULL);
737 if (stack_trace_enabled) {
738 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
739 unwind_cache_invalidate(tcp);
743 #if SUPPORTED_PERSONALITIES > 1
744 update_personality(tcp, tcp->currpers);
746 res = (get_regs_error ? -1 : get_syscall_result(tcp));
747 if (filtered(tcp) || hide_log(tcp))
750 if (syserror(tcp) && syscall_fault_injected(tcp))
751 update_syscall_fault_exiting(tcp);
754 count_syscall(tcp, &tv);
755 if (cflag == CFLAG_ONLY_STATS) {
760 /* If not in -ff mode, and printing_tcp != tcp,
761 * then the log currently does not end with output
762 * of _our syscall entry_, but with something else.
763 * We need to say which syscall's return is this.
765 * Forced reprinting via TCB_REPRINT is used only by
766 * "strace -ff -oLOG test/threaded_execve" corner case.
767 * It's the only case when -ff mode needs reprinting.
769 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
770 tcp->flags &= ~TCB_REPRINT;
772 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
776 tcp->s_prev_ent = NULL;
778 /* There was error in one of prior ptrace ops */
781 tprints("= ? <unavailable>\n");
783 tcp->flags &= ~(TCB_INSYSCALL | TCB_FAULT_INJ);
784 tcp->sys_func_rval = 0;
785 free_tcb_priv_data(tcp);
788 tcp->s_prev_ent = tcp->s_ent;
791 if (tcp->qual_flg & QUAL_RAW) {
792 /* sys_res = printargs(tcp); - but it's nop on sysexit */
794 /* FIXME: not_failing_only (IOW, option -z) is broken:
795 * failure of syscall is known only after syscall return.
796 * Thus we end up with something like this on, say, ENOENT:
797 * open("doesnt_exist", O_RDONLY <unfinished ...>
798 * {next syscall decode}
799 * whereas the intended result is that open(...) line
800 * is not shown at all.
802 if (not_failing_only && tcp->u_error)
803 goto ret; /* ignore failed syscalls */
804 if (tcp->sys_func_rval & RVAL_DECODED)
805 sys_res = tcp->sys_func_rval;
807 sys_res = tcp->s_ent->sys_func(tcp);
812 u_error = tcp->u_error;
814 if (tcp->qual_flg & QUAL_RAW) {
816 tprintf("= -1 (errno %lu)", u_error);
817 if (syscall_fault_injected(tcp))
818 tprints(" (INJECTED)");
820 tprintf("= %#" PRI_klx, tcp->u_rval);
823 else if (!(sys_res & RVAL_NONE) && u_error) {
825 /* Blocked signals do not interrupt any syscalls.
826 * In this case syscalls don't return ERESTARTfoo codes.
828 * Deadly signals set to SIG_DFL interrupt syscalls
829 * and kill the process regardless of which of the codes below
830 * is returned by the interrupted syscall.
831 * In some cases, kernel forces a kernel-generated deadly
832 * signal to be unblocked and set to SIG_DFL (and thus cause
833 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
834 * or SIGILL. (The alternative is to leave process spinning
835 * forever on the faulty instruction - not useful).
837 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
838 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
839 * but kernel will always restart them.
842 /* Most common type of signal-interrupted syscall exit code.
843 * The system call will be restarted with the same arguments
844 * if SA_RESTART is set; otherwise, it will fail with EINTR.
846 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
849 /* Rare. For example, fork() returns this if interrupted.
850 * SA_RESTART is ignored (assumed set): the restart is unconditional.
852 tprints("= ? ERESTARTNOINTR (To be restarted)");
855 /* pause(), rt_sigsuspend() etc use this code.
856 * SA_RESTART is ignored (assumed not set):
857 * syscall won't restart (will return EINTR instead)
858 * even after signal with SA_RESTART set. However,
859 * after SIG_IGN or SIG_DFL signal it will restart
860 * (thus the name "restart only if has no handler").
862 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
864 case ERESTART_RESTARTBLOCK:
865 /* Syscalls like nanosleep(), poll() which can't be
866 * restarted with their original arguments use this
867 * code. Kernel will execute restart_syscall() instead,
868 * which changes arguments before restarting syscall.
869 * SA_RESTART is ignored (assumed not set) similarly
870 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
871 * since restart data is saved in "restart block"
872 * in task struct, and if signal handler uses a syscall
873 * which in turn saves another such restart block,
874 * old data is lost and restart becomes impossible)
876 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
879 u_error_str = err_name(u_error);
881 tprintf("= -1 %s (%s)",
882 u_error_str, strerror(u_error));
884 tprintf("= -1 %lu (%s)",
885 u_error, strerror(u_error));
888 if (syscall_fault_injected(tcp))
889 tprintf(" (INJECTED)");
890 if ((sys_res & RVAL_STR) && tcp->auxstr)
891 tprintf(" (%s)", tcp->auxstr);
894 if (sys_res & RVAL_NONE)
897 switch (sys_res & RVAL_MASK) {
899 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
900 if (current_wordsize < sizeof(tcp->u_rval)) {
902 (unsigned int) tcp->u_rval);
906 tprintf("= %#" PRI_klx, tcp->u_rval);
911 print_numeric_long_umask(tcp->u_rval);
914 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
915 if (current_wordsize < sizeof(tcp->u_rval)) {
917 (unsigned int) tcp->u_rval);
921 tprintf("= %" PRI_klu, tcp->u_rval);
925 tprintf("= %" PRI_kld, tcp->u_rval);
930 printfd(tcp, tcp->u_rval);
933 tprintf("= %" PRI_kld, tcp->u_rval);
936 error_msg("invalid rval format");
940 if ((sys_res & RVAL_STR) && tcp->auxstr)
941 tprintf(" (%s)", tcp->auxstr);
944 tv_sub(&tv, &tv, &tcp->etime);
945 tprintf(" <%ld.%06ld>",
946 (long) tv.tv_sec, (long) tv.tv_usec);
953 if (stack_trace_enabled)
954 unwind_print_stacktrace(tcp);
958 tcp->flags &= ~(TCB_INSYSCALL | TCB_FAULT_INJ);
959 tcp->sys_func_rval = 0;
960 free_tcb_priv_data(tcp);
965 trace_syscall(struct tcb *tcp)
967 return exiting(tcp) ?
968 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
972 is_erestart(struct tcb *tcp)
974 switch (tcp->u_error) {
978 case ERESTART_RESTARTBLOCK:
985 static unsigned long saved_u_error;
988 temporarily_clear_syserror(struct tcb *tcp)
990 saved_u_error = tcp->u_error;
995 restore_cleared_syserror(struct tcb *tcp)
997 tcp->u_error = saved_u_error;
1001 * Check the syscall return value register value for whether it is
1002 * a negated errno code indicating an error, or a success return value.
1005 is_negated_errno(kernel_ulong_t val)
1007 /* Linux kernel defines MAX_ERRNO to 4095. */
1008 kernel_ulong_t max = -(kernel_long_t) 4095;
1010 #ifndef current_klongsize
1011 if (current_klongsize < sizeof(val)) {
1012 val = (uint32_t) val;
1013 max = (uint32_t) max;
1015 #endif /* !current_klongsize */
1020 #include "arch_regs.c"
1022 #ifdef HAVE_GETRVAL2
1023 # include "arch_getrval2.c"
1027 print_pc(struct tcb *tcp)
1029 #if defined ARCH_PC_REG
1030 # define ARCH_GET_PC 0
1031 #elif defined ARCH_PC_PEEK_ADDR
1033 # define ARCH_PC_REG pc
1034 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
1036 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1038 if (get_regs_error || ARCH_GET_PC)
1039 tprints(current_wordsize == 4 ? "[????????] "
1040 : "[????????????????] ");
1042 tprintf(current_wordsize == 4
1043 ? "[%08" PRI_klx "] " : "[%016" PRI_klx "] ",
1044 (kernel_ulong_t) ARCH_PC_REG);
1047 #include "getregs_old.h"
1049 #undef ptrace_getregset_or_getregs
1050 #undef ptrace_setregset_or_setregs
1051 #ifdef ARCH_REGS_FOR_GETREGSET
1053 # define ptrace_getregset_or_getregs ptrace_getregset
1055 ptrace_getregset(pid_t pid)
1057 # ifdef ARCH_IOVEC_FOR_GETREGSET
1058 /* variable iovec */
1059 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1060 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1061 &ARCH_IOVEC_FOR_GETREGSET);
1063 /* constant iovec */
1064 static struct iovec io = {
1065 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1066 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1068 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1073 # ifndef HAVE_GETREGS_OLD
1074 # define ptrace_setregset_or_setregs ptrace_setregset
1076 ptrace_setregset(pid_t pid)
1078 # ifdef ARCH_IOVEC_FOR_GETREGSET
1079 /* variable iovec */
1080 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1081 &ARCH_IOVEC_FOR_GETREGSET);
1083 /* constant iovec */
1084 static struct iovec io = {
1085 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1086 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1088 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1091 # endif /* !HAVE_GETREGS_OLD */
1093 #elif defined ARCH_REGS_FOR_GETREGS
1095 # define ptrace_getregset_or_getregs ptrace_getregs
1097 ptrace_getregs(pid_t pid)
1099 # if defined SPARC || defined SPARC64
1100 /* SPARC systems have the meaning of data and addr reversed */
1101 return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1103 return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1107 # ifndef HAVE_GETREGS_OLD
1108 # define ptrace_setregset_or_setregs ptrace_setregs
1110 ptrace_setregs(pid_t pid)
1112 # if defined SPARC || defined SPARC64
1113 /* SPARC systems have the meaning of data and addr reversed */
1114 return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1116 return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1119 # endif /* !HAVE_GETREGS_OLD */
1121 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1126 #undef USE_GET_SYSCALL_RESULT_REGS
1127 #ifdef ptrace_getregset_or_getregs
1129 # ifdef HAVE_GETREGS_OLD
1131 * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1132 * fallback to getregs_old.
1134 static int use_getregs_old;
1135 if (use_getregs_old < 0) {
1136 get_regs_error = ptrace_getregset_or_getregs(pid);
1138 } else if (use_getregs_old == 0) {
1139 get_regs_error = ptrace_getregset_or_getregs(pid);
1140 if (get_regs_error >= 0) {
1141 use_getregs_old = -1;
1144 if (errno == EPERM || errno == ESRCH)
1146 use_getregs_old = 1;
1148 get_regs_error = getregs_old(pid);
1149 # else /* !HAVE_GETREGS_OLD */
1150 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1151 get_regs_error = ptrace_getregset_or_getregs(pid);
1152 # endif /* !HAVE_GETREGS_OLD */
1154 #else /* !ptrace_getregset_or_getregs */
1156 # define USE_GET_SYSCALL_RESULT_REGS 1
1157 # warning get_regs is not implemented for this architecture yet
1160 #endif /* !ptrace_getregset_or_getregs */
1163 #ifdef ptrace_setregset_or_setregs
1167 return ptrace_setregset_or_setregs(pid);
1169 #endif /* ptrace_setregset_or_setregs */
1174 char buf[sizeof("syscall_%lu") + sizeof(kernel_ulong_t) * 3];
1178 free_sysent_buf(void *ptr)
1180 struct sysent_buf *s = ptr;
1181 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1187 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1188 * 1: ok, continue in trace_syscall_entering().
1189 * other: error, trace_syscall_entering() should print error indicator
1190 * ("????" etc) and bail out.
1193 get_scno(struct tcb *tcp)
1198 int rc = arch_get_scno(tcp);
1202 if (scno_is_valid(tcp->scno)) {
1203 tcp->s_ent = &sysent[tcp->scno];
1204 tcp->qual_flg = qual_flags(tcp->scno);
1206 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1209 s->ent.nargs = MAX_ARGS;
1210 s->ent.sen = SEN_printargs;
1211 s->ent.sys_func = printargs;
1212 s->ent.sys_name = s->buf;
1213 sprintf(s->buf, "syscall_%" PRI_klu, shuffle_scno(tcp->scno));
1215 tcp->s_ent = &s->ent;
1216 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1218 set_tcb_priv_data(tcp, s, free_sysent_buf);
1221 error_msg("pid %d invalid syscall %" PRI_kld,
1222 tcp->pid, tcp->scno);
1227 #ifdef USE_GET_SYSCALL_RESULT_REGS
1228 static int get_syscall_result_regs(struct tcb *);
1232 * 1: ok, continue in trace_syscall_exiting().
1233 * -1: error, trace_syscall_exiting() should print error indicator
1234 * ("????" etc) and bail out.
1237 get_syscall_result(struct tcb *tcp)
1239 #ifdef USE_GET_SYSCALL_RESULT_REGS
1240 if (get_syscall_result_regs(tcp))
1244 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1249 #include "get_scno.c"
1250 #include "set_scno.c"
1251 #include "get_syscall_args.c"
1252 #ifdef USE_GET_SYSCALL_RESULT_REGS
1253 # include "get_syscall_result.c"
1255 #include "get_error.c"
1256 #include "set_error.c"
1257 #ifdef HAVE_GETREGS_OLD
1258 # include "getregs_old.c"
1262 syscall_name(kernel_ulong_t scno)
1264 #if defined X32_PERSONALITY_NUMBER && defined __X32_SYSCALL_BIT
1265 if (current_personality == X32_PERSONALITY_NUMBER)
1266 scno &= ~__X32_SYSCALL_BIT;
1268 return scno_is_valid(scno) ? sysent[scno].sys_name: NULL;