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 */
42 /* for __X32_SYSCALL_BIT */
43 #include <asm/unistd.h>
49 # undef PTRACE_GETREGS
50 # define PTRACE_GETREGS PTRACE_GETREGS64
51 # undef PTRACE_SETREGS
52 # define PTRACE_SETREGS PTRACE_SETREGS64
56 # include <asm/psrcompat.h>
66 # define NT_PRSTATUS 1
71 /* Define these shorthand notations to simplify the syscallent files. */
75 #define TN TRACE_NETWORK
76 #define TP TRACE_PROCESS
77 #define TS TRACE_SIGNAL
78 #define TM TRACE_MEMORY
79 #define TSF TRACE_STATFS
80 #define NF SYSCALL_NEVER_FAILS
82 #define SI STACKTRACE_INVALIDATE_CACHE
83 #define SE STACKTRACE_CAPTURE_ON_ENTER
84 #define CST COMPAT_SYSCALL_TYPES
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. */
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_ulong_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.
510 static kernel_ulong_t
511 shuffle_scno(kernel_ulong_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 void get_regs(pid_t pid);
559 static int get_syscall_args(struct tcb *);
560 static int get_syscall_result(struct tcb *);
561 static int arch_get_scno(struct tcb *tcp);
562 static int arch_set_scno(struct tcb *, kernel_ulong_t);
563 static void get_error(struct tcb *, const bool);
564 static int arch_set_error(struct tcb *);
565 static int arch_set_success(struct tcb *);
567 struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
569 static struct inject_opts *
570 tcb_inject_opts(struct tcb *tcp)
572 return (scno_in_range(tcp->scno) && tcp->inject_vec[current_personality])
573 ? &tcp->inject_vec[current_personality][tcp->scno] : NULL;
578 tamper_with_syscall_entering(struct tcb *tcp, unsigned int *signo)
580 if (!tcp->inject_vec[current_personality]) {
581 tcp->inject_vec[current_personality] =
582 xcalloc(nsyscalls, sizeof(**inject_vec));
583 memcpy(tcp->inject_vec[current_personality],
584 inject_vec[current_personality],
585 nsyscalls * sizeof(**inject_vec));
588 struct inject_opts *opts = tcb_inject_opts(tcp);
590 if (!opts || opts->first == 0)
595 if (opts->first != 0)
598 opts->first = opts->step;
601 *signo = opts->signo;
602 if (opts->rval != INJECT_OPTS_RVAL_DEFAULT && !arch_set_scno(tcp, -1))
603 tcp->flags |= TCB_TAMPERED;
609 tamper_with_syscall_exiting(struct tcb *tcp)
611 struct inject_opts *opts = tcb_inject_opts(tcp);
616 if (opts->rval >= 0) {
617 kernel_long_t u_rval = tcp->u_rval;
619 tcp->u_rval = opts->rval;
620 if (arch_set_success(tcp)) {
621 tcp->u_rval = u_rval;
626 unsigned long new_error = -opts->rval;
628 if (new_error != tcp->u_error && new_error <= MAX_ERRNO_VALUE) {
629 unsigned long u_error = tcp->u_error;
631 tcp->u_error = new_error;
632 if (arch_set_error(tcp)) {
633 tcp->u_error = u_error;
642 trace_syscall_entering(struct tcb *tcp, unsigned int *sig)
644 int res = get_scno(tcp);
650 res = get_syscall_args(tcp);
654 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
656 * " <unavailable>" will be added later by the code which
657 * detects ptrace errors.
663 if (SEN_syscall == tcp->s_ent->sen)
664 decode_mips_subcall(tcp);
667 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
668 switch (tcp->s_ent->sen) {
669 # ifdef SYS_socket_subcall
671 decode_socket_subcall(tcp);
674 # ifdef SYS_ipc_subcall
676 decode_ipc_subcall(tcp);
682 /* Restrain from fault injection while the trace executes strace code. */
684 tcp->qual_flg &= ~QUAL_INJECT;
687 switch (tcp->s_ent->sen) {
690 #if defined SPARC || defined SPARC64
693 tcp->flags &= ~TCB_HIDE_LOG;
697 if (!(tcp->qual_flg & QUAL_TRACE)
698 || (tracing_paths && !pathtrace_match(tcp))
700 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
701 tcp->sys_func_rval = 0;
705 tcp->flags &= ~TCB_FILTERED;
712 if (tcp->qual_flg & QUAL_INJECT)
713 tamper_with_syscall_entering(tcp, sig);
715 if (cflag == CFLAG_ONLY_STATS) {
721 if (stack_trace_enabled) {
722 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
723 unwind_capture_stacktrace(tcp);
728 tprintf("%s(", tcp->s_ent->sys_name);
729 if (tcp->qual_flg & QUAL_RAW)
730 res = printargs(tcp);
732 res = tcp->s_ent->sys_func(tcp);
736 tcp->flags |= TCB_INSYSCALL;
737 tcp->sys_func_rval = res;
738 /* Measure the entrance time as late as possible to avoid errors. */
740 gettimeofday(&tcp->etime, NULL);
745 syscall_tampered(struct tcb *tcp)
747 return tcp->flags & TCB_TAMPERED;
751 trace_syscall_exiting(struct tcb *tcp)
755 /* Measure the exit time as early as possible to avoid errors. */
756 if ((Tflag || cflag) && !(filtered(tcp) || hide_log(tcp)))
757 gettimeofday(&tv, NULL);
760 if (stack_trace_enabled) {
761 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
762 unwind_cache_invalidate(tcp);
766 if (filtered(tcp) || hide_log(tcp))
770 #if SUPPORTED_PERSONALITIES > 1
771 update_personality(tcp, tcp->currpers);
773 int res = (get_regs_error ? -1 : get_syscall_result(tcp));
775 if (syserror(tcp) && syscall_tampered(tcp))
776 tamper_with_syscall_exiting(tcp);
779 count_syscall(tcp, &tv);
780 if (cflag == CFLAG_ONLY_STATS) {
785 /* If not in -ff mode, and printing_tcp != tcp,
786 * then the log currently does not end with output
787 * of _our syscall entry_, but with something else.
788 * We need to say which syscall's return is this.
790 * Forced reprinting via TCB_REPRINT is used only by
791 * "strace -ff -oLOG test/threaded_execve" corner case.
792 * It's the only case when -ff mode needs reprinting.
794 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
795 tcp->flags &= ~TCB_REPRINT;
797 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
801 tcp->s_prev_ent = NULL;
803 /* There was error in one of prior ptrace ops */
806 tprints("= ? <unavailable>\n");
808 tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED);
809 tcp->sys_func_rval = 0;
810 free_tcb_priv_data(tcp);
813 tcp->s_prev_ent = tcp->s_ent;
816 if (tcp->qual_flg & QUAL_RAW) {
817 /* sys_res = printargs(tcp); - but it's nop on sysexit */
819 /* FIXME: not_failing_only (IOW, option -z) is broken:
820 * failure of syscall is known only after syscall return.
821 * Thus we end up with something like this on, say, ENOENT:
822 * open("doesnt_exist", O_RDONLY <unfinished ...>
823 * {next syscall decode}
824 * whereas the intended result is that open(...) line
825 * is not shown at all.
827 if (not_failing_only && tcp->u_error)
828 goto ret; /* ignore failed syscalls */
829 if (tcp->sys_func_rval & RVAL_DECODED)
830 sys_res = tcp->sys_func_rval;
832 sys_res = tcp->s_ent->sys_func(tcp);
837 unsigned long u_error = tcp->u_error;
839 if (tcp->qual_flg & QUAL_RAW) {
841 tprintf("= -1 (errno %lu)", u_error);
843 tprintf("= %#" PRI_klx, tcp->u_rval);
845 if (syscall_tampered(tcp))
846 tprints(" (INJECTED)");
848 else if (!(sys_res & RVAL_NONE) && u_error) {
849 const char *u_error_str;
852 /* Blocked signals do not interrupt any syscalls.
853 * In this case syscalls don't return ERESTARTfoo codes.
855 * Deadly signals set to SIG_DFL interrupt syscalls
856 * and kill the process regardless of which of the codes below
857 * is returned by the interrupted syscall.
858 * In some cases, kernel forces a kernel-generated deadly
859 * signal to be unblocked and set to SIG_DFL (and thus cause
860 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
861 * or SIGILL. (The alternative is to leave process spinning
862 * forever on the faulty instruction - not useful).
864 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
865 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
866 * but kernel will always restart them.
869 /* Most common type of signal-interrupted syscall exit code.
870 * The system call will be restarted with the same arguments
871 * if SA_RESTART is set; otherwise, it will fail with EINTR.
873 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
876 /* Rare. For example, fork() returns this if interrupted.
877 * SA_RESTART is ignored (assumed set): the restart is unconditional.
879 tprints("= ? ERESTARTNOINTR (To be restarted)");
882 /* pause(), rt_sigsuspend() etc use this code.
883 * SA_RESTART is ignored (assumed not set):
884 * syscall won't restart (will return EINTR instead)
885 * even after signal with SA_RESTART set. However,
886 * after SIG_IGN or SIG_DFL signal it will restart
887 * (thus the name "restart only if has no handler").
889 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
891 case ERESTART_RESTARTBLOCK:
892 /* Syscalls like nanosleep(), poll() which can't be
893 * restarted with their original arguments use this
894 * code. Kernel will execute restart_syscall() instead,
895 * which changes arguments before restarting syscall.
896 * SA_RESTART is ignored (assumed not set) similarly
897 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
898 * since restart data is saved in "restart block"
899 * in task struct, and if signal handler uses a syscall
900 * which in turn saves another such restart block,
901 * old data is lost and restart becomes impossible)
903 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
906 u_error_str = err_name(u_error);
908 tprintf("= -1 %s (%s)",
909 u_error_str, strerror(u_error));
911 tprintf("= -1 %lu (%s)",
912 u_error, strerror(u_error));
915 if (syscall_tampered(tcp))
916 tprints(" (INJECTED)");
917 if ((sys_res & RVAL_STR) && tcp->auxstr)
918 tprintf(" (%s)", tcp->auxstr);
921 if (sys_res & RVAL_NONE)
924 switch (sys_res & RVAL_MASK) {
926 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
927 if (current_wordsize < sizeof(tcp->u_rval)) {
929 (unsigned int) tcp->u_rval);
933 tprintf("= %#" PRI_klx, tcp->u_rval);
938 print_numeric_long_umask(tcp->u_rval);
941 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
942 if (current_wordsize < sizeof(tcp->u_rval)) {
944 (unsigned int) tcp->u_rval);
948 tprintf("= %" PRI_klu, tcp->u_rval);
952 tprintf("= %" PRI_kld, tcp->u_rval);
957 printfd(tcp, tcp->u_rval);
960 tprintf("= %" PRI_kld, tcp->u_rval);
963 error_msg("invalid rval format");
967 if ((sys_res & RVAL_STR) && tcp->auxstr)
968 tprintf(" (%s)", tcp->auxstr);
969 if (syscall_tampered(tcp))
970 tprints(" (INJECTED)");
973 tv_sub(&tv, &tv, &tcp->etime);
974 tprintf(" <%ld.%06ld>",
975 (long) tv.tv_sec, (long) tv.tv_usec);
982 if (stack_trace_enabled)
983 unwind_print_stacktrace(tcp);
987 tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED);
988 tcp->sys_func_rval = 0;
989 free_tcb_priv_data(tcp);
994 trace_syscall(struct tcb *tcp, unsigned int *signo)
996 return exiting(tcp) ?
997 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp, signo);
1001 is_erestart(struct tcb *tcp)
1003 switch (tcp->u_error) {
1005 case ERESTARTNOINTR:
1006 case ERESTARTNOHAND:
1007 case ERESTART_RESTARTBLOCK:
1014 static unsigned long saved_u_error;
1017 temporarily_clear_syserror(struct tcb *tcp)
1019 saved_u_error = tcp->u_error;
1024 restore_cleared_syserror(struct tcb *tcp)
1026 tcp->u_error = saved_u_error;
1030 * Check the syscall return value register value for whether it is
1031 * a negated errno code indicating an error, or a success return value.
1034 is_negated_errno(kernel_ulong_t val)
1036 /* Linux kernel defines MAX_ERRNO to 4095. */
1037 kernel_ulong_t max = -(kernel_long_t) 4095;
1039 #ifndef current_klongsize
1040 if (current_klongsize < sizeof(val)) {
1041 val = (uint32_t) val;
1042 max = (uint32_t) max;
1044 #endif /* !current_klongsize */
1049 #include "arch_regs.c"
1051 #ifdef HAVE_GETRVAL2
1052 # include "arch_getrval2.c"
1056 print_pc(struct tcb *tcp)
1058 #if defined ARCH_PC_REG
1059 # define ARCH_GET_PC 0
1060 #elif defined ARCH_PC_PEEK_ADDR
1062 # define ARCH_PC_REG pc
1063 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
1065 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1068 if (get_regs_error || ARCH_GET_PC)
1069 tprints(current_wordsize == 4 ? "[????????] "
1070 : "[????????????????] ");
1072 tprintf(current_wordsize == 4
1073 ? "[%08" PRI_klx "] " : "[%016" PRI_klx "] ",
1074 (kernel_ulong_t) ARCH_PC_REG);
1077 #include "getregs_old.h"
1079 #undef ptrace_getregset_or_getregs
1080 #undef ptrace_setregset_or_setregs
1081 #ifdef ARCH_REGS_FOR_GETREGSET
1083 # define ptrace_getregset_or_getregs ptrace_getregset
1085 ptrace_getregset(pid_t pid)
1087 # ifdef ARCH_IOVEC_FOR_GETREGSET
1088 /* variable iovec */
1089 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1090 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1091 &ARCH_IOVEC_FOR_GETREGSET);
1093 /* constant iovec */
1094 static struct iovec io = {
1095 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1096 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1098 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1103 # ifndef HAVE_GETREGS_OLD
1104 # define ptrace_setregset_or_setregs ptrace_setregset
1106 ptrace_setregset(pid_t pid)
1108 # ifdef ARCH_IOVEC_FOR_GETREGSET
1109 /* variable iovec */
1110 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1111 &ARCH_IOVEC_FOR_GETREGSET);
1113 /* constant iovec */
1114 static struct iovec io = {
1115 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1116 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1118 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1121 # endif /* !HAVE_GETREGS_OLD */
1123 #elif defined ARCH_REGS_FOR_GETREGS
1125 # define ptrace_getregset_or_getregs ptrace_getregs
1127 ptrace_getregs(pid_t pid)
1129 # if defined SPARC || defined SPARC64
1130 /* SPARC systems have the meaning of data and addr reversed */
1131 return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1133 return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1137 # ifndef HAVE_GETREGS_OLD
1138 # define ptrace_setregset_or_setregs ptrace_setregs
1140 ptrace_setregs(pid_t pid)
1142 # if defined SPARC || defined SPARC64
1143 /* SPARC systems have the meaning of data and addr reversed */
1144 return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1146 return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1149 # endif /* !HAVE_GETREGS_OLD */
1151 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1156 #undef USE_GET_SYSCALL_RESULT_REGS
1157 #ifdef ptrace_getregset_or_getregs
1159 if (get_regs_error != -1)
1162 # ifdef HAVE_GETREGS_OLD
1164 * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1165 * fallback to getregs_old.
1167 static int use_getregs_old;
1168 if (use_getregs_old < 0) {
1169 get_regs_error = ptrace_getregset_or_getregs(pid);
1171 } else if (use_getregs_old == 0) {
1172 get_regs_error = ptrace_getregset_or_getregs(pid);
1173 if (get_regs_error >= 0) {
1174 use_getregs_old = -1;
1177 if (errno == EPERM || errno == ESRCH)
1179 use_getregs_old = 1;
1181 get_regs_error = getregs_old(pid);
1182 # else /* !HAVE_GETREGS_OLD */
1183 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1184 get_regs_error = ptrace_getregset_or_getregs(pid);
1185 # endif /* !HAVE_GETREGS_OLD */
1187 #else /* !ptrace_getregset_or_getregs */
1189 # define USE_GET_SYSCALL_RESULT_REGS 1
1190 # warning get_regs is not implemented for this architecture yet
1193 #endif /* !ptrace_getregset_or_getregs */
1196 #ifdef ptrace_setregset_or_setregs
1200 return ptrace_setregset_or_setregs(pid);
1202 #endif /* ptrace_setregset_or_setregs */
1207 char buf[sizeof("syscall_%lu") + sizeof(kernel_ulong_t) * 3];
1211 free_sysent_buf(void *ptr)
1213 struct sysent_buf *s = ptr;
1214 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1220 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1221 * 1: ok, continue in trace_syscall_entering().
1222 * other: error, trace_syscall_entering() should print error indicator
1223 * ("????" etc) and bail out.
1226 get_scno(struct tcb *tcp)
1233 int rc = arch_get_scno(tcp);
1237 if (scno_is_valid(tcp->scno)) {
1238 tcp->s_ent = &sysent[tcp->scno];
1239 tcp->qual_flg = qual_flags(tcp->scno);
1241 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1244 s->ent.nargs = MAX_ARGS;
1245 s->ent.sen = SEN_printargs;
1246 s->ent.sys_func = printargs;
1247 s->ent.sys_name = s->buf;
1248 sprintf(s->buf, "syscall_%" PRI_klu, shuffle_scno(tcp->scno));
1250 tcp->s_ent = &s->ent;
1251 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1253 set_tcb_priv_data(tcp, s, free_sysent_buf);
1256 error_msg("pid %d invalid syscall %" PRI_kld,
1257 tcp->pid, tcp->scno);
1262 #ifdef USE_GET_SYSCALL_RESULT_REGS
1263 static int get_syscall_result_regs(struct tcb *);
1267 * 1: ok, continue in trace_syscall_exiting().
1268 * -1: error, trace_syscall_exiting() should print error indicator
1269 * ("????" etc) and bail out.
1272 get_syscall_result(struct tcb *tcp)
1274 #ifdef USE_GET_SYSCALL_RESULT_REGS
1275 if (get_syscall_result_regs(tcp))
1279 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1284 #include "get_scno.c"
1285 #include "set_scno.c"
1286 #include "get_syscall_args.c"
1287 #ifdef USE_GET_SYSCALL_RESULT_REGS
1288 # include "get_syscall_result.c"
1290 #include "get_error.c"
1291 #include "set_error.c"
1292 #ifdef HAVE_GETREGS_OLD
1293 # include "getregs_old.c"
1297 syscall_name(kernel_ulong_t scno)
1299 #if defined X32_PERSONALITY_NUMBER && defined __X32_SYSCALL_BIT
1300 if (current_personality == X32_PERSONALITY_NUMBER)
1301 scno &= ~__X32_SYSCALL_BIT;
1303 return scno_is_valid(scno) ? sysent[scno].sys_name: NULL;