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,
261 set_personality(int personality)
263 nsyscalls = nsyscall_vec[personality];
264 sysent = sysent_vec[personality];
266 switch (personality) {
268 errnoent = errnoent0;
270 ioctlent = ioctlent0;
271 nioctlents = nioctlents0;
272 signalent = signalent0;
273 nsignals = nsignals0;
274 printers = &printers0;
278 errnoent = errnoent1;
280 ioctlent = ioctlent1;
281 nioctlents = nioctlents1;
282 signalent = signalent1;
283 nsignals = nsignals1;
284 printers = &printers1;
287 # if SUPPORTED_PERSONALITIES > 2
289 errnoent = errnoent2;
291 ioctlent = ioctlent2;
292 nioctlents = nioctlents2;
293 signalent = signalent2;
294 nsignals = nsignals2;
295 printers = &printers2;
300 current_personality = personality;
301 # ifndef current_wordsize
302 current_wordsize = personality_wordsize[personality];
307 update_personality(struct tcb *tcp, unsigned int personality)
309 if (personality == current_personality)
311 set_personality(personality);
313 if (personality == tcp->currpers)
315 tcp->currpers = personality;
317 # undef PERSONALITY_NAMES
318 # if defined POWERPC64
319 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
320 # elif defined X86_64
321 # define PERSONALITY_NAMES {"64 bit", "32 bit", "x32"}
323 # define PERSONALITY_NAMES {"x32", "32 bit"}
324 # elif defined AARCH64
325 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
327 # define PERSONALITY_NAMES {"64-bit", "32-bit"}
329 # ifdef PERSONALITY_NAMES
331 static const char *const names[] = PERSONALITY_NAMES;
332 error_msg("[ Process PID=%d runs in %s mode. ]",
333 tcp->pid, names[personality]);
339 #ifdef SYS_socket_subcall
341 decode_socket_subcall(struct tcb *tcp)
343 const int call = tcp->u_arg[0];
345 if (call < 1 || call >= SYS_socket_nsubcalls)
348 const unsigned long scno = SYS_socket_subcall + call;
349 const unsigned int nargs = sysent[scno].nargs;
352 if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
356 tcp->qual_flg = qual_flags(scno);
357 tcp->s_ent = &sysent[scno];
360 for (i = 0; i < nargs; ++i)
361 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
362 ? ((uint32_t *) (void *) buf)[i] : buf[i];
366 #ifdef SYS_ipc_subcall
368 decode_ipc_subcall(struct tcb *tcp)
370 unsigned int call = tcp->u_arg[0];
371 const unsigned int version = call >> 16;
374 # if defined S390 || defined S390X
378 if (current_wordsize == 8)
381 set_tcb_priv_ulong(tcp, version);
387 case 1: case 2: case 3: case 4:
388 case 11: case 12: case 13: case 14:
389 case 21: case 22: case 23: case 24:
395 tcp->scno = SYS_ipc_subcall + call;
396 tcp->qual_flg = qual_flags(tcp->scno);
397 tcp->s_ent = &sysent[tcp->scno];
399 const unsigned int n = tcp->s_ent->nargs;
401 for (i = 0; i < n; i++)
402 tcp->u_arg[i] = tcp->u_arg[i + 1];
408 decode_mips_subcall(struct tcb *tcp)
410 if (!SCNO_IS_VALID(tcp->u_arg[0]))
412 tcp->scno = tcp->u_arg[0];
413 tcp->qual_flg = qual_flags(tcp->scno);
414 tcp->s_ent = &sysent[tcp->scno];
415 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
416 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
418 * Fetching the last arg of 7-arg syscalls (fadvise64_64
419 * and sync_file_range) requires additional code,
420 * see linux/mips/get_syscall_args.c
422 if (tcp->s_ent->nargs == MAX_ARGS) {
424 mips_REG_SP + MAX_ARGS * sizeof(tcp->u_arg[0]),
425 sizeof(tcp->u_arg[0]),
426 &tcp->u_arg[MAX_ARGS - 1]) < 0)
427 tcp->u_arg[MAX_ARGS - 1] = 0;
430 #endif /* LINUX_MIPSO32 */
433 dumpio(struct tcb *tcp)
438 int fd = tcp->u_arg[0];
442 if (is_number_in_set(fd, &read_set)) {
443 switch (tcp->s_ent->sen) {
448 case SEN_mq_timedreceive:
449 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
454 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
458 dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
461 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
465 if (is_number_in_set(fd, &write_set)) {
466 switch (tcp->s_ent->sen) {
471 case SEN_mq_timedsend:
472 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
478 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
481 dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1UL);
484 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
491 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
492 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
494 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
496 shuffle_scno(unsigned long scno)
498 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
501 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
502 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
504 if (scno == 0x000ffff0)
505 return ARM_FIRST_SHUFFLED_SYSCALL;
507 #define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
509 * Is it ARM specific syscall?
510 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
511 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
513 if (scno >= 0x000f0000 &&
514 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
515 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
517 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
518 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
524 # define shuffle_scno(scno) ((long)(scno))
528 err_name(unsigned long err)
530 if ((err < nerrnos) && errnoent[err])
531 return errnoent[err];
536 static long get_regs_error;
544 static int get_syscall_args(struct tcb *);
545 static int get_syscall_result(struct tcb *);
546 static int arch_get_scno(struct tcb *tcp);
547 static int arch_set_scno(struct tcb *, long);
548 static void get_error(struct tcb *, const bool);
549 static int arch_set_error(struct tcb *);
551 struct fault_opts *fault_vec[SUPPORTED_PERSONALITIES];
553 static struct fault_opts *
554 tcb_fault_opts(struct tcb *tcp)
556 return (SCNO_IN_RANGE(tcp->scno) && tcp->fault_vec[current_personality])
557 ? &tcp->fault_vec[current_personality][tcp->scno] : NULL;
562 inject_syscall_fault_entering(struct tcb *tcp)
564 if (!tcp->fault_vec[current_personality]) {
565 tcp->fault_vec[current_personality] =
566 xcalloc(nsyscalls, sizeof(**fault_vec));
567 memcpy(tcp->fault_vec[current_personality],
568 fault_vec[current_personality],
569 nsyscalls * sizeof(**fault_vec));
572 struct fault_opts *opts = tcb_fault_opts(tcp);
574 if (!opts || opts->first == 0)
579 if (opts->first != 0)
582 opts->first = opts->step;
584 if (!arch_set_scno(tcp, -1))
585 tcp->flags |= TCB_FAULT_INJ;
591 update_syscall_fault_exiting(struct tcb *tcp)
593 struct fault_opts *opts = tcb_fault_opts(tcp);
595 if (opts && opts->err && tcp->u_error != opts->err) {
596 unsigned long u_error = tcp->u_error;
597 tcp->u_error = opts->err;
598 if (arch_set_error(tcp))
599 tcp->u_error = u_error;
606 trace_syscall_entering(struct tcb *tcp)
610 scno_good = res = get_scno(tcp);
614 res = get_syscall_args(tcp);
618 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
620 * " <unavailable>" will be added later by the code which
621 * detects ptrace errors.
627 if (SEN_syscall == tcp->s_ent->sen)
628 decode_mips_subcall(tcp);
631 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
632 switch (tcp->s_ent->sen) {
633 # ifdef SYS_socket_subcall
635 decode_socket_subcall(tcp);
638 # ifdef SYS_ipc_subcall
640 decode_ipc_subcall(tcp);
646 /* Restrain from fault injection while the trace executes strace code. */
648 tcp->qual_flg &= ~QUAL_FAULT;
651 switch (tcp->s_ent->sen) {
654 #if defined SPARC || defined SPARC64
657 tcp->flags &= ~TCB_HIDE_LOG;
661 if (!(tcp->qual_flg & QUAL_TRACE)
662 || (tracing_paths && !pathtrace_match(tcp))
664 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
665 tcp->sys_func_rval = 0;
669 tcp->flags &= ~TCB_FILTERED;
676 if (tcp->qual_flg & QUAL_FAULT)
677 inject_syscall_fault_entering(tcp);
679 if (cflag == CFLAG_ONLY_STATS) {
685 if (stack_trace_enabled) {
686 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
687 unwind_capture_stacktrace(tcp);
692 tprintf("%s(", tcp->s_ent->sys_name);
693 if (tcp->qual_flg & QUAL_RAW)
694 res = printargs(tcp);
696 res = tcp->s_ent->sys_func(tcp);
700 tcp->flags |= TCB_INSYSCALL;
701 tcp->sys_func_rval = res;
702 /* Measure the entrance time as late as possible to avoid errors. */
704 gettimeofday(&tcp->etime, NULL);
709 syscall_fault_injected(struct tcb *tcp)
711 return tcp->flags & TCB_FAULT_INJ;
715 trace_syscall_exiting(struct tcb *tcp)
720 unsigned long u_error;
721 const char *u_error_str;
723 /* Measure the exit time as early as possible to avoid errors. */
725 gettimeofday(&tv, NULL);
728 if (stack_trace_enabled) {
729 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
730 unwind_cache_invalidate(tcp);
734 #if SUPPORTED_PERSONALITIES > 1
735 update_personality(tcp, tcp->currpers);
737 res = (get_regs_error ? -1 : get_syscall_result(tcp));
738 if (filtered(tcp) || hide_log(tcp))
741 if (syserror(tcp) && syscall_fault_injected(tcp))
742 update_syscall_fault_exiting(tcp);
745 count_syscall(tcp, &tv);
746 if (cflag == CFLAG_ONLY_STATS) {
751 /* If not in -ff mode, and printing_tcp != tcp,
752 * then the log currently does not end with output
753 * of _our syscall entry_, but with something else.
754 * We need to say which syscall's return is this.
756 * Forced reprinting via TCB_REPRINT is used only by
757 * "strace -ff -oLOG test/threaded_execve" corner case.
758 * It's the only case when -ff mode needs reprinting.
760 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
761 tcp->flags &= ~TCB_REPRINT;
763 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
767 tcp->s_prev_ent = NULL;
769 /* There was error in one of prior ptrace ops */
772 tprints("= ? <unavailable>\n");
774 tcp->flags &= ~(TCB_INSYSCALL | TCB_FAULT_INJ);
775 tcp->sys_func_rval = 0;
776 free_tcb_priv_data(tcp);
779 tcp->s_prev_ent = tcp->s_ent;
782 if (tcp->qual_flg & QUAL_RAW) {
783 /* sys_res = printargs(tcp); - but it's nop on sysexit */
785 /* FIXME: not_failing_only (IOW, option -z) is broken:
786 * failure of syscall is known only after syscall return.
787 * Thus we end up with something like this on, say, ENOENT:
788 * open("doesnt_exist", O_RDONLY <unfinished ...>
789 * {next syscall decode}
790 * whereas the intended result is that open(...) line
791 * is not shown at all.
793 if (not_failing_only && tcp->u_error)
794 goto ret; /* ignore failed syscalls */
795 if (tcp->sys_func_rval & RVAL_DECODED)
796 sys_res = tcp->sys_func_rval;
798 sys_res = tcp->s_ent->sys_func(tcp);
803 u_error = tcp->u_error;
805 if (tcp->qual_flg & QUAL_RAW) {
807 tprintf("= -1 (errno %lu)", u_error);
808 if (syscall_fault_injected(tcp))
809 tprints(" (INJECTED)");
811 tprintf("= %#lx", tcp->u_rval);
814 else if (!(sys_res & RVAL_NONE) && u_error) {
816 /* Blocked signals do not interrupt any syscalls.
817 * In this case syscalls don't return ERESTARTfoo codes.
819 * Deadly signals set to SIG_DFL interrupt syscalls
820 * and kill the process regardless of which of the codes below
821 * is returned by the interrupted syscall.
822 * In some cases, kernel forces a kernel-generated deadly
823 * signal to be unblocked and set to SIG_DFL (and thus cause
824 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
825 * or SIGILL. (The alternative is to leave process spinning
826 * forever on the faulty instruction - not useful).
828 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
829 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
830 * but kernel will always restart them.
833 /* Most common type of signal-interrupted syscall exit code.
834 * The system call will be restarted with the same arguments
835 * if SA_RESTART is set; otherwise, it will fail with EINTR.
837 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
840 /* Rare. For example, fork() returns this if interrupted.
841 * SA_RESTART is ignored (assumed set): the restart is unconditional.
843 tprints("= ? ERESTARTNOINTR (To be restarted)");
846 /* pause(), rt_sigsuspend() etc use this code.
847 * SA_RESTART is ignored (assumed not set):
848 * syscall won't restart (will return EINTR instead)
849 * even after signal with SA_RESTART set. However,
850 * after SIG_IGN or SIG_DFL signal it will restart
851 * (thus the name "restart only if has no handler").
853 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
855 case ERESTART_RESTARTBLOCK:
856 /* Syscalls like nanosleep(), poll() which can't be
857 * restarted with their original arguments use this
858 * code. Kernel will execute restart_syscall() instead,
859 * which changes arguments before restarting syscall.
860 * SA_RESTART is ignored (assumed not set) similarly
861 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
862 * since restart data is saved in "restart block"
863 * in task struct, and if signal handler uses a syscall
864 * which in turn saves another such restart block,
865 * old data is lost and restart becomes impossible)
867 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
870 u_error_str = err_name(u_error);
872 tprintf("= -1 %s (%s)",
873 u_error_str, strerror(u_error));
875 tprintf("= -1 %lu (%s)",
876 u_error, strerror(u_error));
879 if (syscall_fault_injected(tcp))
880 tprintf(" (INJECTED)");
881 if ((sys_res & RVAL_STR) && tcp->auxstr)
882 tprintf(" (%s)", tcp->auxstr);
885 if (sys_res & RVAL_NONE)
888 switch (sys_res & RVAL_MASK) {
890 #if SUPPORTED_PERSONALITIES > 1
891 if (current_wordsize < sizeof(long))
893 (unsigned int) tcp->u_rval);
896 tprintf("= %#lx", tcp->u_rval);
900 print_numeric_long_umask(tcp->u_rval);
903 #if SUPPORTED_PERSONALITIES > 1
904 if (current_wordsize < sizeof(long))
906 (unsigned int) tcp->u_rval);
909 tprintf("= %lu", tcp->u_rval);
912 tprintf("= %ld", tcp->u_rval);
917 printfd(tcp, tcp->u_rval);
920 tprintf("= %ld", tcp->u_rval);
922 #if HAVE_STRUCT_TCB_EXT_ARG
925 tprintf("= %#llx", tcp->u_lrval);
928 tprintf("= %#llo", tcp->u_lrval);
932 tprintf("= %llu", tcp->u_lrval);
936 tprintf("= %lld", tcp->u_lrval);
939 #endif /* HAVE_STRUCT_TCB_EXT_ARG */
941 error_msg("invalid rval format");
945 if ((sys_res & RVAL_STR) && tcp->auxstr)
946 tprintf(" (%s)", tcp->auxstr);
949 tv_sub(&tv, &tv, &tcp->etime);
950 tprintf(" <%ld.%06ld>",
951 (long) tv.tv_sec, (long) tv.tv_usec);
958 if (stack_trace_enabled)
959 unwind_print_stacktrace(tcp);
963 tcp->flags &= ~(TCB_INSYSCALL | TCB_FAULT_INJ);
964 tcp->sys_func_rval = 0;
965 free_tcb_priv_data(tcp);
970 trace_syscall(struct tcb *tcp)
972 return exiting(tcp) ?
973 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
977 is_erestart(struct tcb *tcp)
979 switch (tcp->u_error) {
983 case ERESTART_RESTARTBLOCK:
990 static unsigned long saved_u_error;
993 temporarily_clear_syserror(struct tcb *tcp)
995 saved_u_error = tcp->u_error;
1000 restore_cleared_syserror(struct tcb *tcp)
1002 tcp->u_error = saved_u_error;
1006 * Check the syscall return value register value for whether it is
1007 * a negated errno code indicating an error, or a success return value.
1010 is_negated_errno(kernel_ulong_t val)
1012 /* Linux kernel defines MAX_ERRNO to 4095. */
1013 kernel_ulong_t max = -(kernel_long_t) 4095;
1015 #if defined X86_64 || defined X32
1017 * current_wordsize is 4 for x32 personality
1018 * but truncation _must not_ be done in it, so
1019 * check current_personality instead.
1021 if (current_personality == 1) {
1022 val = (uint32_t) val;
1023 max = (uint32_t) max;
1025 #elif SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1026 if (current_wordsize < sizeof(val)) {
1027 val = (uint32_t) val;
1028 max = (uint32_t) max;
1035 #include "arch_regs.c"
1037 #ifdef HAVE_GETRVAL2
1038 # include "arch_getrval2.c"
1042 print_pc(struct tcb *tcp)
1044 #if defined ARCH_PC_REG
1045 # define ARCH_GET_PC 0
1046 #elif defined ARCH_PC_PEEK_ADDR
1048 # define ARCH_PC_REG pc
1049 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
1051 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1053 if (get_regs_error || ARCH_GET_PC)
1054 tprints(current_wordsize == 4 ? "[????????] "
1055 : "[????????????????] ");
1057 tprintf(current_wordsize == 4 ? "[%08lx] " : "[%016lx] ",
1058 (unsigned long) ARCH_PC_REG);
1061 #include "getregs_old.h"
1063 #undef ptrace_getregset_or_getregs
1064 #undef ptrace_setregset_or_setregs
1065 #ifdef ARCH_REGS_FOR_GETREGSET
1067 # define ptrace_getregset_or_getregs ptrace_getregset
1069 ptrace_getregset(pid_t pid)
1071 # ifdef ARCH_IOVEC_FOR_GETREGSET
1072 /* variable iovec */
1073 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1074 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1075 &ARCH_IOVEC_FOR_GETREGSET);
1077 /* constant iovec */
1078 static struct iovec io = {
1079 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1080 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1082 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1087 # ifndef HAVE_GETREGS_OLD
1088 # define ptrace_setregset_or_setregs ptrace_setregset
1090 ptrace_setregset(pid_t pid)
1092 # ifdef ARCH_IOVEC_FOR_GETREGSET
1093 /* variable iovec */
1094 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1095 &ARCH_IOVEC_FOR_GETREGSET);
1097 /* constant iovec */
1098 static struct iovec io = {
1099 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1100 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1102 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1105 # endif /* !HAVE_GETREGS_OLD */
1107 #elif defined ARCH_REGS_FOR_GETREGS
1109 # define ptrace_getregset_or_getregs ptrace_getregs
1111 ptrace_getregs(pid_t pid)
1113 # if defined SPARC || defined SPARC64
1114 /* SPARC systems have the meaning of data and addr reversed */
1115 return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1117 return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1121 # ifndef HAVE_GETREGS_OLD
1122 # define ptrace_setregset_or_setregs ptrace_setregs
1124 ptrace_setregs(pid_t pid)
1126 # if defined SPARC || defined SPARC64
1127 /* SPARC systems have the meaning of data and addr reversed */
1128 return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1130 return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1133 # endif /* !HAVE_GETREGS_OLD */
1135 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1140 #undef USE_GET_SYSCALL_RESULT_REGS
1141 #ifdef ptrace_getregset_or_getregs
1143 # ifdef HAVE_GETREGS_OLD
1145 * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1146 * fallback to getregs_old.
1148 static int use_getregs_old;
1149 if (use_getregs_old < 0) {
1150 get_regs_error = ptrace_getregset_or_getregs(pid);
1152 } else if (use_getregs_old == 0) {
1153 get_regs_error = ptrace_getregset_or_getregs(pid);
1154 if (get_regs_error >= 0) {
1155 use_getregs_old = -1;
1158 if (errno == EPERM || errno == ESRCH)
1160 use_getregs_old = 1;
1162 get_regs_error = getregs_old(pid);
1163 # else /* !HAVE_GETREGS_OLD */
1164 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1165 get_regs_error = ptrace_getregset_or_getregs(pid);
1166 # endif /* !HAVE_GETREGS_OLD */
1168 #else /* !ptrace_getregset_or_getregs */
1170 # define USE_GET_SYSCALL_RESULT_REGS 1
1171 # warning get_regs is not implemented for this architecture yet
1174 #endif /* !ptrace_getregset_or_getregs */
1177 #ifdef ptrace_setregset_or_setregs
1181 return ptrace_setregset_or_setregs(pid);
1183 #endif /* ptrace_setregset_or_setregs */
1188 char buf[sizeof("syscall_%lu") + sizeof(long) * 3];
1192 free_sysent_buf(void *ptr)
1194 struct sysent_buf *s = ptr;
1195 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1201 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1202 * 1: ok, continue in trace_syscall_entering().
1203 * other: error, trace_syscall_entering() should print error indicator
1204 * ("????" etc) and bail out.
1207 get_scno(struct tcb *tcp)
1212 int rc = arch_get_scno(tcp);
1216 if (SCNO_IS_VALID(tcp->scno)) {
1217 tcp->s_ent = &sysent[tcp->scno];
1218 tcp->qual_flg = qual_flags(tcp->scno);
1220 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1223 s->ent.nargs = MAX_ARGS;
1224 s->ent.sen = SEN_printargs;
1225 s->ent.sys_func = printargs;
1226 s->ent.sys_name = s->buf;
1227 sprintf(s->buf, "syscall_%lu", shuffle_scno(tcp->scno));
1229 tcp->s_ent = &s->ent;
1230 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1232 set_tcb_priv_data(tcp, s, free_sysent_buf);
1235 error_msg("pid %d invalid syscall %ld", tcp->pid, tcp->scno);
1240 #ifdef USE_GET_SYSCALL_RESULT_REGS
1241 static int get_syscall_result_regs(struct tcb *);
1245 * 1: ok, continue in trace_syscall_exiting().
1246 * -1: error, trace_syscall_exiting() should print error indicator
1247 * ("????" etc) and bail out.
1250 get_syscall_result(struct tcb *tcp)
1252 #ifdef USE_GET_SYSCALL_RESULT_REGS
1253 if (get_syscall_result_regs(tcp))
1257 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1262 #include "get_scno.c"
1263 #include "set_scno.c"
1264 #include "get_syscall_args.c"
1265 #ifdef USE_GET_SYSCALL_RESULT_REGS
1266 # include "get_syscall_result.c"
1268 #include "get_error.c"
1269 #include "set_error.c"
1270 #ifdef HAVE_GETREGS_OLD
1271 # include "getregs_old.c"
1275 syscall_name(long scno)
1277 #if defined X32_PERSONALITY_NUMBER && defined __X32_SYSCALL_BIT
1278 if (current_personality == X32_PERSONALITY_NUMBER)
1279 scno &= ~__X32_SYSCALL_BIT;
1281 return SCNO_IS_VALID(scno) ? sysent[scno].sys_name: NULL;