* Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
* Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
* Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
- * Copyright (c) 2001-2017 The strace developers.
+ * Copyright (c) 2001-2018 The strace developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
#include <time.h>
#include <sys/time.h>
-#include "kernel_types.h"
+#include "arch_defs.h"
+#include "error_prints.h"
#include "gcc_compat.h"
+#include "kernel_types.h"
#include "macros.h"
#include "mpers_type.h"
+#include "string_to_uint.h"
#include "sysent.h"
+#include "xmalloc.h"
#ifndef HAVE_STRERROR
const char *strerror(int);
extern char *stpcpy(char *dst, const char *src);
#endif
-#ifndef offsetofend
-# define offsetofend(type, member) \
- (offsetof(type, member) + sizeof(((type *)NULL)->member))
-#endif
-
-/* macros */
-#ifndef MAX
-# define MAX(a, b) (((a) > (b)) ? (a) : (b))
-#endif
-#ifndef MIN
-# define MIN(a, b) (((a) < (b)) ? (a) : (b))
-#endif
-#define CLAMP(val, min, max) MIN(MAX(min, val), max)
-
/* Glibc has an efficient macro for sigemptyset
* (it just does one or two assignments of 0 to internal vector of longs).
*/
# define ERESTART_RESTARTBLOCK 516
#endif
-#if defined X86_64
-# define SUPPORTED_PERSONALITIES 3
-# define PERSONALITY2_WORDSIZE 4
-# define PERSONALITY2_KLONGSIZE PERSONALITY0_KLONGSIZE
-#elif defined AARCH64 \
- || defined POWERPC64 \
- || defined RISCV \
- || defined SPARC64 \
- || defined TILE \
- || defined X32
-# define SUPPORTED_PERSONALITIES 2
-#else
-# define SUPPORTED_PERSONALITIES 1
-#endif
-
-#if defined TILE && defined __tilepro__
-# define DEFAULT_PERSONALITY 1
-#else
-# define DEFAULT_PERSONALITY 0
-#endif
-
#define PERSONALITY0_WORDSIZE SIZEOF_LONG
#define PERSONALITY0_KLONGSIZE SIZEOF_KERNEL_LONG_T
#define PERSONALITY0_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
# define PERSONALITY1_KLONGSIZE PERSONALITY1_WORDSIZE
#endif
+#if SUPPORTED_PERSONALITIES > 2
+# define PERSONALITY2_WORDSIZE 4
+# define PERSONALITY2_KLONGSIZE PERSONALITY0_KLONGSIZE
+#endif
+
#if SUPPORTED_PERSONALITIES > 1 && defined HAVE_M32_MPERS
# define PERSONALITY1_INCLUDE_PRINTERS_DECLS "m32_printer_decls.h"
# define PERSONALITY1_INCLUDE_PRINTERS_DEFS "m32_printer_defs.h"
# define PERSONALITY1_INCLUDE_FUNCS "m32_funcs.h"
# define MPERS_m32_IOCTL_MACROS "ioctl_redefs1.h"
+# define HAVE_PERSONALITY_1_MPERS 1
#else
# define PERSONALITY1_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
# define PERSONALITY1_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
# define PERSONALITY1_INCLUDE_FUNCS "empty.h"
+# define HAVE_PERSONALITY_1_MPERS 0
#endif
#if SUPPORTED_PERSONALITIES > 2 && defined HAVE_MX32_MPERS
# define PERSONALITY2_INCLUDE_PRINTERS_DECLS "mx32_printer_decls.h"
# define PERSONALITY2_INCLUDE_PRINTERS_DEFS "mx32_printer_defs.h"
# define MPERS_mx32_IOCTL_MACROS "ioctl_redefs2.h"
+# define HAVE_PERSONALITY_2_MPERS 1
#else
# define PERSONALITY2_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
# define PERSONALITY2_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
# define PERSONALITY2_INCLUDE_FUNCS "empty.h"
+# define HAVE_PERSONALITY_2_MPERS 0
#endif
typedef struct ioctlent {
unsigned int code;
} struct_ioctlent;
+#define INJECT_F_SIGNAL 1
+#define INJECT_F_RETVAL 2
+
+struct inject_data {
+ uint16_t flags;
+ uint16_t signo;
+ kernel_long_t rval;
+};
+
struct inject_opts {
uint16_t first;
uint16_t step;
- uint16_t signo;
- int rval;
+ struct inject_data data;
};
#define MAX_ERRNO_VALUE 4095
-#define INJECT_OPTS_RVAL_DEFAULT (-(MAX_ERRNO_VALUE + 1))
/* Trace Control Block */
struct tcb {
struct timeval dtime; /* Delta for system time usage */
struct timeval etime; /* Syscall entry time */
-#ifdef USE_LIBUNWIND
- struct UPT_info *libunwind_ui;
struct mmap_cache_t *mmap_cache;
unsigned int mmap_cache_size;
unsigned int mmap_cache_generation;
+
+#ifdef USE_LIBUNWIND
+ struct UPT_info *libunwind_ui;
struct queue_t *queue;
#endif
};
#define TCB_TAMPERED 0x40 /* A syscall has been tampered with */
#define TCB_HIDE_LOG 0x80 /* We should hide everything (until execve) */
#define TCB_SKIP_DETACH_ON_FIRST_EXEC 0x100 /* -b execve should skip detach on first execve */
+#define TCB_GRABBED 0x200 /* We grab the process and can catch it
+ * in the middle of a syscall */
+#define TCB_RECOVERING 0x400 /* We try to recover after detecting incorrect
+ * syscall entering/exiting state */
/* qualifier flags */
#define QUAL_TRACE 0x001 /* this system call should be traced */
#define QUAL_VERBOSE 0x004 /* decode the structures of this syscall */
#define QUAL_RAW 0x008 /* print all args in hex for this syscall */
#define QUAL_INJECT 0x010 /* tamper with this system call on purpose */
-#define QUAL_SIGNAL 0x100 /* report events with this signal */
-#define QUAL_READ 0x200 /* dump data read from this file descriptor */
-#define QUAL_WRITE 0x400 /* dump data written to this file descriptor */
#define DEFAULT_QUAL_FLAGS (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
#define entering(tcp) (!((tcp)->flags & TCB_INSYSCALL))
#define exiting(tcp) ((tcp)->flags & TCB_INSYSCALL)
#define syserror(tcp) ((tcp)->u_error != 0)
+#define traced(tcp) ((tcp)->qual_flg & QUAL_TRACE)
#define verbose(tcp) ((tcp)->qual_flg & QUAL_VERBOSE)
#define abbrev(tcp) ((tcp)->qual_flg & QUAL_ABBREV)
+#define raw(tcp) ((tcp)->qual_flg & QUAL_RAW)
+#define inject(tcp) ((tcp)->qual_flg & QUAL_INJECT)
#define filtered(tcp) ((tcp)->flags & TCB_FILTERED)
#define hide_log(tcp) ((tcp)->flags & TCB_HIDE_LOG)
+#define syscall_tampered(tcp) ((tcp)->flags & TCB_TAMPERED)
+#define recovering(tcp) ((tcp)->flags & TCB_RECOVERING)
#include "xlat.h"
extern const struct xlat addrfams[];
+extern const struct xlat arp_hardware_types[];
extern const struct xlat at_flags[];
extern const struct xlat clocknames[];
extern const struct xlat dirent_types[];
extern const struct xlat ethernet_protocols[];
extern const struct xlat evdev_abs[];
+extern const struct xlat iffflags[];
extern const struct xlat inet_protocols[];
+extern const struct xlat ip_type_of_services[];
extern const struct xlat msg_flags[];
extern const struct xlat netlink_protocols[];
+extern const struct xlat nl_route_types[];
extern const struct xlat open_access_modes[];
extern const struct xlat open_mode_flags[];
extern const struct xlat resource_flags[];
+extern const struct xlat routing_scopes[];
+extern const struct xlat routing_table_ids[];
+extern const struct xlat routing_types[];
+extern const struct xlat seccomp_ret_action[];
extern const struct xlat setns_types[];
extern const struct xlat sg_io_info[];
extern const struct xlat socketlayers[];
#define RVAL_NONE 040 /* Print nothing */
#define RVAL_DECODED 0100 /* syscall decoding finished */
+#define RVAL_IOCTL_DECODED 0200 /* ioctl sub-parser successfully decoded
+ the argument */
+#define RVAL_PRINT_ERR_VAL 0400 /* Print decoded error code along with
+ syscall return value. Needed for modify_ldt
+ that for some reason decides to return
+ an error with higher bits set to 0. */
#define IOCTL_NUMBER_UNKNOWN 0
#define IOCTL_NUMBER_HANDLED 1
#define indirect_ipccall(tcp) (tcp->s_ent->sys_flags & TRACE_INDIRECT_SUBCALL)
-#if defined(ARM) || defined(AARCH64) \
- || defined(I386) || defined(X32) || defined(X86_64) \
- || defined(IA64) \
- || defined(BFIN) \
- || defined(M68K) \
- || defined(MICROBLAZE) \
- || defined(RISCV) \
- || defined(S390) \
- || defined(SH) || defined(SH64) \
- || defined(SPARC) || defined(SPARC64) \
- /**/
-# define NEED_UID16_PARSERS 1
-#else
-# define NEED_UID16_PARSERS 0
-#endif
-
enum sock_proto {
SOCK_PROTO_UNKNOWN,
SOCK_PROTO_UNIX,
CFLAG_BOTH
} cflag_t;
extern cflag_t cflag;
-extern bool debug_flag;
extern bool Tflag;
extern bool iflag;
extern bool count_wallclock;
/* are we filtering traces based on paths? */
extern struct path_set {
const char **paths_selected;
- unsigned int num_selected;
+ size_t num_selected;
+ size_t size;
} global_path_set;
#define tracing_paths (global_path_set.num_selected != 0)
extern unsigned xflag;
#undef KERNEL_VERSION
#define KERNEL_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
-void error_msg(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
-void perror_msg(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
-void error_msg_and_die(const char *fmt, ...)
- ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
-void error_msg_and_help(const char *fmt, ...)
- ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
-void perror_msg_and_die(const char *fmt, ...)
- ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
-
-void *xmalloc(size_t size) ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1));
-void *xcalloc(size_t nmemb, size_t size)
- ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1, 2));
-void *xreallocarray(void *ptr, size_t nmemb, size_t size)
- ATTRIBUTE_ALLOC_SIZE((2, 3));
-char *xstrdup(const char *str) ATTRIBUTE_MALLOC;
-char *xstrndup(const char *str, size_t n) ATTRIBUTE_MALLOC;
-
-extern int read_int_from_file(const char *, int *);
+extern int read_int_from_file(struct tcb *, const char *, int *);
extern void set_sortby(const char *);
extern void set_overhead(int);
extern void count_syscall(struct tcb *, const struct timeval *);
extern void call_summary(FILE *);
-extern void clear_regs(void);
+extern void clear_regs(struct tcb *tcp);
extern int get_scno(struct tcb *);
extern kernel_ulong_t get_rt_sigframe_addr(struct tcb *);
/**
- * Convert syscall number to syscall name.
+ * Convert a (shuffled) syscall number to the corresponding syscall name.
*
* @param scno Syscall number.
* @return String literal corresponding to the syscall number in case latter
* is valid; NULL otherwise.
*/
extern const char *syscall_name(kernel_ulong_t scno);
+/**
+ * Convert a syscall name to the corresponding (shuffled) syscall number.
+ *
+ * @param s Syscall name.
+ * @param p Personality.
+ * @param start From which position in syscall entry table resume the search.
+ * @return Shuffled syscall number (ready to use against sysent_vec)
+ * if syscall name is found; -1 otherwise.
+ */
+extern kernel_long_t scno_by_name(const char *s, unsigned p,
+ kernel_long_t start);
+/**
+ * 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.
+ *
+ * @param scno Raw or shuffled syscall number.
+ * @return Shuffled or raw syscall number, respectively.
+ */
+extern kernel_ulong_t shuffle_scno(kernel_ulong_t scno);
extern const char *err_name(unsigned long err);
extern bool is_erestart(struct tcb *);
extern int
umovestr(struct tcb *, kernel_ulong_t addr, unsigned int len, char *laddr);
-extern int upeek(int pid, unsigned long, kernel_ulong_t *);
-extern int upoke(int pid, unsigned long, kernel_ulong_t);
+extern int upeek(struct tcb *tcp, unsigned long, kernel_ulong_t *);
+extern int upoke(struct tcb *tcp, unsigned long, kernel_ulong_t);
extern bool
print_array(struct tcb *,
void *opaque_data),
void *opaque_data);
-#if defined ALPHA || defined IA64 || defined MIPS \
- || defined SH || defined SPARC || defined SPARC64
-# define HAVE_GETRVAL2
+#if HAVE_ARCH_GETRVAL2
extern long getrval2(struct tcb *);
-#else
-# undef HAVE_GETRVAL2
#endif
extern const char *signame(const int);
const struct xlat *dyxlat_get(const struct dyxlat *);
void dyxlat_add_pair(struct dyxlat *, uint64_t val, const char *str, size_t len);
-const struct xlat *genl_families_xlat(void);
+const struct xlat *genl_families_xlat(struct tcb *tcp);
extern unsigned long get_pagesize(void);
-extern int
-string_to_uint_ex(const char *str, char **endptr,
- unsigned int max_val, const char *accepted_ending);
-extern int string_to_uint(const char *str);
-static inline int
-string_to_uint_upto(const char *const str, unsigned int max_val)
-{
- return string_to_uint_ex(str, NULL, max_val, NULL);
-}
extern int next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits);
/*
#define QUOTE_OMIT_LEADING_TRAILING_QUOTES 0x02
#define QUOTE_OMIT_TRAILING_0 0x08
#define QUOTE_FORCE_HEX 0x10
+#define QUOTE_EMIT_COMMENT 0x20
extern int string_quote(const char *, char *, unsigned int, unsigned int);
extern int print_quoted_string(const char *, unsigned int, unsigned int);
uint64_t val, const char *dflt);
#define printxval_search(xlat__, val__, dflt__) \
printxval_searchn(xlat__, ARRAY_SIZE(xlat__), val__, dflt__)
+extern int sprintxval(char *buf, size_t size, const struct xlat *,
+ unsigned int val, const char *dflt);
extern int printargs(struct tcb *);
extern int printargs_u(struct tcb *);
extern int printargs_d(struct tcb *);
extern void print_dev_t(unsigned long long dev);
extern void print_abnormal_hi(kernel_ulong_t);
+extern kernel_ulong_t *
+fetch_indirect_syscall_args(struct tcb *, kernel_ulong_t addr, unsigned int n_args);
+
extern void
dumpiov_in_msghdr(struct tcb *, kernel_ulong_t addr, kernel_ulong_t data_size);
extern void
dumpstr(struct tcb *, kernel_ulong_t addr, int len);
-extern void
+extern int
printstr_ex(struct tcb *, kernel_ulong_t addr, kernel_ulong_t len,
unsigned int user_style);
-extern void
+extern int
printpathn(struct tcb *, kernel_ulong_t addr, unsigned int n);
-extern void
+extern int
printpath(struct tcb *, kernel_ulong_t addr);
#define TIMESPEC_TEXT_BUFSIZE \
extern void print_sockaddr(const void *sa, int len);
extern bool
print_inet_addr(int af, const void *addr, unsigned int len, const char *var_name);
+extern bool
+decode_inet_addr(struct tcb *, kernel_ulong_t addr,
+ unsigned int len, int family, const char *var_name);
extern const char *get_sockaddr_by_inode(struct tcb *, int fd, unsigned long inode);
extern bool print_sockaddr_by_inode(struct tcb *, int fd, unsigned long inode);
extern void print_dirfd(struct tcb *, int);
extern void print_struct_stat(struct tcb *, const struct strace_stat *const st);
struct strace_statfs;
+struct strace_keyctl_kdf_params;
extern void
print_struct_statfs(struct tcb *, kernel_ulong_t addr);
extern void print_ifindex(unsigned int);
-struct number_set;
-extern struct number_set read_set;
-extern struct number_set write_set;
-extern struct number_set signal_set;
-
-extern bool is_number_in_set(unsigned int number, const struct number_set *);
extern void qualify(const char *);
extern unsigned int qual_flags(const unsigned int);
#define DECL_IOCTL(name) \
extern int \
-name ## _ioctl(struct tcb *, unsigned int request, kernel_ulong_t arg)
+name ## _ioctl(struct tcb *, unsigned int request, kernel_ulong_t arg) \
+/* End of DECL_IOCTL definition. */
+
DECL_IOCTL(dm);
DECL_IOCTL(file);
DECL_IOCTL(fs_x);
+DECL_IOCTL(kvm);
DECL_IOCTL(nsfs);
DECL_IOCTL(ptp);
DECL_IOCTL(scsi);
#define DECL_NETLINK(name) \
extern bool \
decode_netlink_ ## name(struct tcb *, const struct nlmsghdr *, \
- kernel_ulong_t addr, unsigned int len)
+ kernel_ulong_t addr, unsigned int len) \
+/* End of DECL_NETLINK definition. */
+
DECL_NETLINK(crypto);
+DECL_NETLINK(route);
DECL_NETLINK(selinux);
DECL_NETLINK(sock_diag);
extern void unwind_init(void);
extern void unwind_tcb_init(struct tcb *);
extern void unwind_tcb_fin(struct tcb *);
-extern void unwind_cache_invalidate(struct tcb *);
extern void unwind_print_stacktrace(struct tcb *);
extern void unwind_capture_stacktrace(struct tcb *);
#endif
-static inline void
+/*
+ * Keep a sorted array of cache entries,
+ * so that we can binary search through it.
+ */
+struct mmap_cache_t {
+ /**
+ * example entry:
+ * 7fabbb09b000-7fabbb09f000 r-xp 00179000 fc:00 1180246 /lib/libc-2.11.1.so
+ *
+ * start_addr is 0x7fabbb09b000
+ * end_addr is 0x7fabbb09f000
+ * mmap_offset is 0x179000
+ * protections is MMAP_CACHE_PROT_READABLE|MMAP_CACHE_PROT_EXECUTABLE
+ * binary_filename is "/lib/libc-2.11.1.so"
+ */
+ unsigned long start_addr;
+ unsigned long end_addr;
+ unsigned long mmap_offset;
+ unsigned char protections;
+ char *binary_filename;
+};
+
+enum mmap_cache_protection {
+ MMAP_CACHE_PROT_READABLE = 1 << 0,
+ MMAP_CACHE_PROT_WRITABLE = 1 << 1,
+ MMAP_CACHE_PROT_EXECUTABLE = 1 << 2,
+ MMAP_CACHE_PROT_SHARED = 1 << 3,
+};
+
+enum mmap_cache_rebuild_result {
+ MMAP_CACHE_REBUILD_NOCACHE,
+ MMAP_CACHE_REBUILD_READY,
+ MMAP_CACHE_REBUILD_RENEWED,
+};
+
+extern void mmap_cache_enable(void);
+extern bool mmap_cache_is_enabled(void);
+extern void mmap_cache_invalidate(struct tcb *tcp);
+extern void mmap_cache_delete(struct tcb *tcp, const char *caller);
+extern enum mmap_cache_rebuild_result mmap_cache_rebuild_if_invalid(struct tcb *tcp, const char *caller);
+extern struct mmap_cache_t *mmap_cache_search(struct tcb *tcp, unsigned long ip);
+
+static inline int
printstrn(struct tcb *tcp, kernel_ulong_t addr, kernel_ulong_t len)
{
- printstr_ex(tcp, addr, len, 0);
+ return printstr_ex(tcp, addr, len, 0);
}
-static inline void
+static inline int
printstr(struct tcb *tcp, kernel_ulong_t addr)
{
- printstr_ex(tcp, addr, -1, QUOTE_0_TERMINATED);
+ return printstr_ex(tcp, addr, -1, QUOTE_0_TERMINATED);
}
static inline int
#endif
#ifdef HAVE_STRUCT_USER_DESC
-extern void print_user_desc(struct tcb *, kernel_ulong_t addr);
+/**
+ * Filter what to print from the point of view of the get_thread_area syscall.
+ * Kernel copies only entry_number field at first and then tries to write the
+ * whole structure.
+ */
+enum user_desc_print_filter {
+ /* Print the "entering" part of struct user_desc - entry_number. */
+ USER_DESC_ENTERING = 1,
+ /* Print the "exiting" part of the structure. */
+ USER_DESC_EXITING = 2,
+ USER_DESC_BOTH = USER_DESC_ENTERING | USER_DESC_EXITING,
+};
+
+extern void print_user_desc(struct tcb *, kernel_ulong_t addr,
+ enum user_desc_print_filter filter);
#endif
/* Strace log generation machinery.
extern void tprints_comment(const char *str);
#if SUPPORTED_PERSONALITIES > 1
-extern void set_personality(int personality);
+extern void set_personality(unsigned int personality);
extern unsigned current_personality;
#else
# define set_personality(personality) ((void)0)
# endif
#endif
-#define ANY_WORDSIZE_LESS_THAN_KERNEL_LONG \
- (SIZEOF_KERNEL_LONG_T > 4 \
- && (SIZEOF_LONG < SIZEOF_KERNEL_LONG_T || !defined(current_wordsize)))
+#if SIZEOF_KERNEL_LONG_T > 4 \
+ && (SIZEOF_LONG < SIZEOF_KERNEL_LONG_T || !defined(current_wordsize))
+# define ANY_WORDSIZE_LESS_THAN_KERNEL_LONG 1
+#else
+# define ANY_WORDSIZE_LESS_THAN_KERNEL_LONG 0
+#endif
#define DECL_PRINTNUM(name) \
extern bool \
printnum_ ## name(struct tcb *, kernel_ulong_t addr, const char *fmt) \
- ATTRIBUTE_FORMAT((printf, 3, 0))
+ ATTRIBUTE_FORMAT((printf, 3, 0)) \
+/* End of DECL_PRINTNUM definition. */
+
DECL_PRINTNUM(short);
DECL_PRINTNUM(int);
DECL_PRINTNUM(int64);
#define DECL_PRINTNUM_ADDR(name) \
extern bool \
-printnum_addr_ ## name(struct tcb *, kernel_ulong_t addr)
+printnum_addr_ ## name(struct tcb *, kernel_ulong_t addr) \
+/* End of DECL_PRINTNUM_ADDR definition. */
+
DECL_PRINTNUM_ADDR(int);
DECL_PRINTNUM_ADDR(int64);
#undef DECL_PRINTNUM_ADDR
#define DECL_PRINTPAIR(name) \
extern bool \
printpair_ ## name(struct tcb *, kernel_ulong_t addr, const char *fmt) \
- ATTRIBUTE_FORMAT((printf, 3, 0))
+ ATTRIBUTE_FORMAT((printf, 3, 0)) \
+/* End of DECL_PRINTPAIR definition. */
+
DECL_PRINTPAIR(int);
DECL_PRINTPAIR(int64);
#undef DECL_PRINTPAIR
extern const char *const signalent0[];
extern const struct_ioctlent ioctlent0[];
+extern const char *const personality_names[];
+
#if SUPPORTED_PERSONALITIES > 1
extern const struct_sysent *sysent;
extern const char *const *errnoent;