* Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
* Linux for s390 port by D.J. Barrow
* <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
+ * Copyright (c) 1999-2017 The strace developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
#include "defs.h"
#include "native_defs.h"
+#include "nsig.h"
#include <sys/param.h>
-#include <signal.h>
/* for struct iovec */
#include <sys/uio.h>
+/* for __X32_SYSCALL_BIT */
+#include <asm/unistd.h>
+
#include "regs.h"
#include "ptrace.h"
# define NT_PRSTATUS 1
#endif
-#ifndef NSIG
-# warning: NSIG is not defined, using 32
-# define NSIG 32
-#endif
-
#include "syscall.h"
/* Define these shorthand notations to simplify the syscallent files. */
#define TP TRACE_PROCESS
#define TS TRACE_SIGNAL
#define TM TRACE_MEMORY
+#define TST TRACE_STAT
+#define TLST TRACE_LSTAT
+#define TFST TRACE_FSTAT
+#define TSTA TRACE_STAT_LIKE
+#define TSF TRACE_STATFS
+#define TFSF TRACE_FSTATFS
+#define TSFA TRACE_STATFS_LIKE
#define NF SYSCALL_NEVER_FAILS
#define MA MAX_ARGS
#define SI STACKTRACE_INVALIDATE_CACHE
#undef TP
#undef TS
#undef TM
+#undef TST
+#undef TLST
+#undef TFST
+#undef TSTA
+#undef TSF
+#undef TFSF
+#undef TSFA
#undef NF
#undef MA
#undef SI
if (call < 1 || call >= SYS_socket_nsubcalls)
return;
- const kernel_scno_t scno = SYS_socket_subcall + call;
+ const kernel_ulong_t scno = SYS_socket_subcall + call;
const unsigned int nargs = sysent[scno].nargs;
uint64_t buf[nargs];
* Shuffle syscall numbers so that we don't have huge gaps in syscall table.
* The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
*/
-static kernel_scno_t
-shuffle_scno(kernel_scno_t scno)
+static kernel_ulong_t
+shuffle_scno(kernel_ulong_t scno)
{
#if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
get_regs_error = -1;
}
+static void get_regs(pid_t pid);
static int get_syscall_args(struct tcb *);
static int get_syscall_result(struct tcb *);
static int arch_get_scno(struct tcb *tcp);
-static int arch_set_scno(struct tcb *, kernel_scno_t);
+static int arch_set_scno(struct tcb *, kernel_ulong_t);
static void get_error(struct tcb *, const bool);
static int arch_set_error(struct tcb *);
+static int arch_set_success(struct tcb *);
-struct fault_opts *fault_vec[SUPPORTED_PERSONALITIES];
+struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
-static struct fault_opts *
-tcb_fault_opts(struct tcb *tcp)
+static struct inject_opts *
+tcb_inject_opts(struct tcb *tcp)
{
- return (scno_in_range(tcp->scno) && tcp->fault_vec[current_personality])
- ? &tcp->fault_vec[current_personality][tcp->scno] : NULL;
+ return (scno_in_range(tcp->scno) && tcp->inject_vec[current_personality])
+ ? &tcp->inject_vec[current_personality][tcp->scno] : NULL;
}
static long
-inject_syscall_fault_entering(struct tcb *tcp)
+tamper_with_syscall_entering(struct tcb *tcp, unsigned int *signo)
{
- if (!tcp->fault_vec[current_personality]) {
- tcp->fault_vec[current_personality] =
- xcalloc(nsyscalls, sizeof(**fault_vec));
- memcpy(tcp->fault_vec[current_personality],
- fault_vec[current_personality],
- nsyscalls * sizeof(**fault_vec));
+ if (!tcp->inject_vec[current_personality]) {
+ tcp->inject_vec[current_personality] =
+ xcalloc(nsyscalls, sizeof(**inject_vec));
+ memcpy(tcp->inject_vec[current_personality],
+ inject_vec[current_personality],
+ nsyscalls * sizeof(**inject_vec));
}
- struct fault_opts *opts = tcb_fault_opts(tcp);
+ struct inject_opts *opts = tcb_inject_opts(tcp);
if (!opts || opts->first == 0)
return 0;
opts->first = opts->step;
- if (!arch_set_scno(tcp, -1))
- tcp->flags |= TCB_FAULT_INJ;
+ if (opts->signo > 0)
+ *signo = opts->signo;
+ if (opts->rval != INJECT_OPTS_RVAL_DEFAULT && !arch_set_scno(tcp, -1))
+ tcp->flags |= TCB_TAMPERED;
return 0;
}
static long
-update_syscall_fault_exiting(struct tcb *tcp)
+tamper_with_syscall_exiting(struct tcb *tcp)
{
- struct fault_opts *opts = tcb_fault_opts(tcp);
+ struct inject_opts *opts = tcb_inject_opts(tcp);
+
+ if (!opts)
+ return 0;
+
+ if (opts->rval >= 0) {
+ kernel_long_t u_rval = tcp->u_rval;
+
+ tcp->u_rval = opts->rval;
+ if (arch_set_success(tcp)) {
+ tcp->u_rval = u_rval;
+ } else {
+ tcp->u_error = 0;
+ }
+ } else {
+ unsigned long new_error = -opts->rval;
- if (opts && opts->err && tcp->u_error != opts->err) {
- unsigned long u_error = tcp->u_error;
- tcp->u_error = opts->err;
- if (arch_set_error(tcp))
- tcp->u_error = u_error;
+ if (new_error != tcp->u_error && new_error <= MAX_ERRNO_VALUE) {
+ unsigned long u_error = tcp->u_error;
+
+ tcp->u_error = new_error;
+ if (arch_set_error(tcp)) {
+ tcp->u_error = u_error;
+ }
+ }
}
return 0;
}
-static int
-trace_syscall_entering(struct tcb *tcp)
+/*
+ * Returns:
+ * 0: "ignore this ptrace stop", bail out silently.
+ * 1: ok, decoded; call
+ * syscall_entering_finish(tcp, syscall_entering_trace(tcp, ...)).
+ * other: error; call syscall_entering_finish(tcp, res), where res is the value
+ * returned.
+ */
+int
+syscall_entering_decode(struct tcb *tcp)
{
- int res, scno_good;
-
- scno_good = res = get_scno(tcp);
+ int res = get_scno(tcp);
if (res == 0)
return res;
- if (res == 1)
- res = get_syscall_args(tcp);
-
- if (res != 1) {
+ int scno_good = res;
+ if (res != 1 || (res = get_syscall_args(tcp)) != 1) {
printleader(tcp);
tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
/*
* " <unavailable>" will be added later by the code which
* detects ptrace errors.
*/
- goto ret;
+ return res;
}
#ifdef LINUX_MIPSO32
}
#endif
+ return 1;
+}
+
+int
+syscall_entering_trace(struct tcb *tcp, unsigned int *sig)
+{
/* Restrain from fault injection while the trace executes strace code. */
if (hide_log(tcp)) {
- tcp->qual_flg &= ~QUAL_FAULT;
+ tcp->qual_flg &= ~QUAL_INJECT;
}
switch (tcp->s_ent->sen) {
if (!(tcp->qual_flg & QUAL_TRACE)
|| (tracing_paths && !pathtrace_match(tcp))
) {
- tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
- tcp->sys_func_rval = 0;
+ tcp->flags |= TCB_FILTERED;
return 0;
}
tcp->flags &= ~TCB_FILTERED;
if (hide_log(tcp)) {
- res = 0;
- goto ret;
+ return 0;
}
- if (tcp->qual_flg & QUAL_FAULT)
- inject_syscall_fault_entering(tcp);
+ if (tcp->qual_flg & QUAL_INJECT)
+ tamper_with_syscall_entering(tcp, sig);
if (cflag == CFLAG_ONLY_STATS) {
- res = 0;
- goto ret;
+ return 0;
}
#ifdef USE_LIBUNWIND
printleader(tcp);
tprintf("%s(", tcp->s_ent->sys_name);
- if (tcp->qual_flg & QUAL_RAW)
- res = printargs(tcp);
- else
- res = tcp->s_ent->sys_func(tcp);
-
+ int res = (tcp->qual_flg & QUAL_RAW)
+ ? printargs(tcp) : tcp->s_ent->sys_func(tcp);
fflush(tcp->outf);
- ret:
+ return res;
+}
+
+void
+syscall_entering_finish(struct tcb *tcp, int res)
+{
tcp->flags |= TCB_INSYSCALL;
tcp->sys_func_rval = res;
/* Measure the entrance time as late as possible to avoid errors. */
- if (Tflag || cflag)
+ if ((Tflag || cflag) && !filtered(tcp))
gettimeofday(&tcp->etime, NULL);
- return res;
}
static bool
-syscall_fault_injected(struct tcb *tcp)
+syscall_tampered(struct tcb *tcp)
{
- return tcp->flags & TCB_FAULT_INJ;
+ return tcp->flags & TCB_TAMPERED;
}
-static int
-trace_syscall_exiting(struct tcb *tcp)
+/* Returns:
+ * 0: "bail out".
+ * 1: ok.
+ * -1: error in one of ptrace ops.
+ *
+ * If not 0, call syscall_exiting_trace(tcp, res), where res is the return
+ * value. Anyway, call syscall_exiting_finish(tcp) then.
+ */
+int
+syscall_exiting_decode(struct tcb *tcp, struct timeval *ptv)
{
- int sys_res;
- struct timeval tv;
- int res;
- unsigned long u_error;
- const char *u_error_str;
-
/* Measure the exit time as early as possible to avoid errors. */
- if (Tflag || cflag)
- gettimeofday(&tv, NULL);
+ if ((Tflag || cflag) && !(filtered(tcp) || hide_log(tcp)))
+ gettimeofday(ptv, NULL);
#ifdef USE_LIBUNWIND
if (stack_trace_enabled) {
}
#endif
+ if (filtered(tcp) || hide_log(tcp))
+ return 0;
+
+ get_regs(tcp->pid);
#if SUPPORTED_PERSONALITIES > 1
update_personality(tcp, tcp->currpers);
#endif
- res = (get_regs_error ? -1 : get_syscall_result(tcp));
- if (filtered(tcp) || hide_log(tcp))
- goto ret;
+ return get_regs_error ? -1 : get_syscall_result(tcp);
+}
- if (syserror(tcp) && syscall_fault_injected(tcp))
- update_syscall_fault_exiting(tcp);
+int
+syscall_exiting_trace(struct tcb *tcp, struct timeval tv, int res)
+{
+ if (syserror(tcp) && syscall_tampered(tcp))
+ tamper_with_syscall_exiting(tcp);
if (cflag) {
count_syscall(tcp, &tv);
if (cflag == CFLAG_ONLY_STATS) {
- goto ret;
+ return 0;
}
}
tabto();
tprints("= ? <unavailable>\n");
line_ended();
- tcp->flags &= ~(TCB_INSYSCALL | TCB_FAULT_INJ);
- tcp->sys_func_rval = 0;
- free_tcb_priv_data(tcp);
return res;
}
tcp->s_prev_ent = tcp->s_ent;
- sys_res = 0;
+ int sys_res = 0;
if (tcp->qual_flg & QUAL_RAW) {
/* sys_res = printargs(tcp); - but it's nop on sysexit */
} else {
/* FIXME: not_failing_only (IOW, option -z) is broken:
* failure of syscall is known only after syscall return.
* Thus we end up with something like this on, say, ENOENT:
- * open("doesnt_exist", O_RDONLY <unfinished ...>
+ * open("does_not_exist", O_RDONLY <unfinished ...>
* {next syscall decode}
* whereas the intended result is that open(...) line
* is not shown at all.
*/
if (not_failing_only && tcp->u_error)
- goto ret; /* ignore failed syscalls */
+ return 0; /* ignore failed syscalls */
if (tcp->sys_func_rval & RVAL_DECODED)
sys_res = tcp->sys_func_rval;
else
tprints(") ");
tabto();
- u_error = tcp->u_error;
+ unsigned long u_error = tcp->u_error;
if (tcp->qual_flg & QUAL_RAW) {
if (u_error) {
tprintf("= -1 (errno %lu)", u_error);
- if (syscall_fault_injected(tcp))
- tprints(" (INJECTED)");
} else {
- tprintf("= %#lx", tcp->u_rval);
+ tprintf("= %#" PRI_klx, tcp->u_rval);
}
- }
- else if (!(sys_res & RVAL_NONE) && u_error) {
+ if (syscall_tampered(tcp))
+ tprints(" (INJECTED)");
+ } else if (!(sys_res & RVAL_NONE) && u_error) {
+ const char *u_error_str;
+
switch (u_error) {
/* Blocked signals do not interrupt any syscalls.
* In this case syscalls don't return ERESTARTfoo codes.
u_error, strerror(u_error));
break;
}
- if (syscall_fault_injected(tcp))
- tprintf(" (INJECTED)");
+ if (syscall_tampered(tcp))
+ tprints(" (INJECTED)");
if ((sys_res & RVAL_STR) && tcp->auxstr)
tprintf(" (%s)", tcp->auxstr);
- }
- else {
+ } else {
if (sys_res & RVAL_NONE)
tprints("= ?");
else {
switch (sys_res & RVAL_MASK) {
case RVAL_HEX:
-#if SUPPORTED_PERSONALITIES > 1
- if (current_wordsize < sizeof(tcp->u_rval))
+#if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
+ if (current_wordsize < sizeof(tcp->u_rval)) {
tprintf("= %#x",
(unsigned int) tcp->u_rval);
- else
+ } else
#endif
- tprintf("= %#lx", tcp->u_rval);
+ {
+ tprintf("= %#" PRI_klx, tcp->u_rval);
+ }
break;
case RVAL_OCTAL:
tprints("= ");
print_numeric_long_umask(tcp->u_rval);
break;
case RVAL_UDECIMAL:
-#if SUPPORTED_PERSONALITIES > 1
- if (current_wordsize < sizeof(tcp->u_rval))
+#if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
+ if (current_wordsize < sizeof(tcp->u_rval)) {
tprintf("= %u",
(unsigned int) tcp->u_rval);
- else
+ } else
#endif
- tprintf("= %lu", tcp->u_rval);
+ {
+ tprintf("= %" PRI_klu, tcp->u_rval);
+ }
break;
case RVAL_DECIMAL:
- tprintf("= %ld", tcp->u_rval);
+ tprintf("= %" PRI_kld, tcp->u_rval);
break;
case RVAL_FD:
if (show_fd_path) {
tprints("= ");
printfd(tcp, tcp->u_rval);
- }
- else
- tprintf("= %ld", tcp->u_rval);
- break;
-#if HAVE_STRUCT_TCB_EXT_ARG
- /*
- case RVAL_LHEX:
- tprintf("= %#llx", tcp->u_lrval);
- break;
- case RVAL_LOCTAL:
- tprintf("= %#llo", tcp->u_lrval);
- break;
- */
- case RVAL_LUDECIMAL:
- tprintf("= %llu", tcp->u_lrval);
+ } else
+ tprintf("= %" PRI_kld, tcp->u_rval);
break;
- /*
- case RVAL_LDECIMAL:
- tprintf("= %lld", tcp->u_lrval);
- break;
- */
-#endif /* HAVE_STRUCT_TCB_EXT_ARG */
default:
error_msg("invalid rval format");
break;
}
if ((sys_res & RVAL_STR) && tcp->auxstr)
tprintf(" (%s)", tcp->auxstr);
+ if (syscall_tampered(tcp))
+ tprints(" (INJECTED)");
}
if (Tflag) {
tv_sub(&tv, &tv, &tcp->etime);
if (stack_trace_enabled)
unwind_print_stacktrace(tcp);
#endif
-
- ret:
- tcp->flags &= ~(TCB_INSYSCALL | TCB_FAULT_INJ);
- tcp->sys_func_rval = 0;
- free_tcb_priv_data(tcp);
return 0;
}
-int
-trace_syscall(struct tcb *tcp)
+void
+syscall_exiting_finish(struct tcb *tcp)
{
- return exiting(tcp) ?
- trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
+ tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED);
+ tcp->sys_func_rval = 0;
+ free_tcb_priv_data(tcp);
}
bool
#if defined ARCH_PC_REG
# define ARCH_GET_PC 0
#elif defined ARCH_PC_PEEK_ADDR
- kernel_ureg_t pc;
+ kernel_ulong_t pc;
# define ARCH_PC_REG pc
# define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
#else
# error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
#endif
+ get_regs(tcp->pid);
if (get_regs_error || ARCH_GET_PC)
tprints(current_wordsize == 4 ? "[????????] "
: "[????????????????] ");
else
- tprintf(current_wordsize == 4 ? "[%08lx] " : "[%016lx] ",
- (unsigned long) ARCH_PC_REG);
+ tprintf(current_wordsize == 4
+ ? "[%08" PRI_klx "] " : "[%016" PRI_klx "] ",
+ (kernel_ulong_t) ARCH_PC_REG);
}
#include "getregs_old.h"
#endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
-void
+static void
get_regs(pid_t pid)
{
#undef USE_GET_SYSCALL_RESULT_REGS
#ifdef ptrace_getregset_or_getregs
+ if (get_regs_error != -1)
+ return;
+
# ifdef HAVE_GETREGS_OLD
/*
* Try PTRACE_GETREGSET/PTRACE_GETREGS first,
struct sysent_buf {
struct tcb *tcp;
struct_sysent ent;
- char buf[sizeof("syscall_%lu") + sizeof(long) * 3];
+ char buf[sizeof("syscall_%lu") + sizeof(kernel_ulong_t) * 3];
};
static void
/*
* Returns:
- * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
- * 1: ok, continue in trace_syscall_entering().
- * other: error, trace_syscall_entering() should print error indicator
- * ("????" etc) and bail out.
+ * 0: "ignore this ptrace stop", syscall_entering_decode() should return a "bail
+ * out silently" code.
+ * 1: ok, continue in syscall_entering_decode().
+ * other: error, syscall_entering_decode() should print error indicator
+ * ("????" etc) and return an appropriate code.
*/
int
get_scno(struct tcb *tcp)
{
+ get_regs(tcp->pid);
+
if (get_regs_error)
return -1;
s->ent.sen = SEN_printargs;
s->ent.sys_func = printargs;
s->ent.sys_name = s->buf;
- sprintf(s->buf, "syscall_%lu", shuffle_scno(tcp->scno));
+ sprintf(s->buf, "syscall_%" PRI_klu, shuffle_scno(tcp->scno));
tcp->s_ent = &s->ent;
tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
set_tcb_priv_data(tcp, s, free_sysent_buf);
if (debug_flag)
- error_msg("pid %d invalid syscall %ld", tcp->pid, tcp->scno);
+ error_msg("pid %d invalid syscall %" PRI_kld,
+ tcp->pid, tcp->scno);
}
return 1;
}
#endif
/* Returns:
- * 1: ok, continue in trace_syscall_exiting().
- * -1: error, trace_syscall_exiting() should print error indicator
+ * 1: ok, continue in syscall_exiting_trace().
+ * -1: error, syscall_exiting_trace() should print error indicator
* ("????" etc) and bail out.
*/
static int
#endif
const char *
-syscall_name(kernel_scno_t scno)
+syscall_name(kernel_ulong_t scno)
{
#if defined X32_PERSONALITY_NUMBER && defined __X32_SYSCALL_BIT
if (current_personality == X32_PERSONALITY_NUMBER)
scno &= ~__X32_SYSCALL_BIT;
#endif
- return scno_is_valid(scno) ? sysent[scno].sys_name: NULL;
+ return scno_is_valid(scno) ? sysent[scno].sys_name : NULL;
}