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) 2001-2017 The strace developers.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/types.h>
47 /* Open-coding isprint(ch) et al proved more efficient than calling
48 * generalized libc interface. We don't *want* to do non-ASCII anyway.
50 /* #include <ctype.h> */
56 #include "error_prints.h"
57 #include "gcc_compat.h"
58 #include "kernel_types.h"
60 #include "mpers_type.h"
61 #include "string_to_uint.h"
66 const char *strerror(int);
69 /* Some libc have stpcpy, some don't. Sigh...
70 * Roll our private implementation...
73 #define stpcpy strace_stpcpy
74 extern char *stpcpy(char *dst, const char *src);
79 # define MAX(a, b) (((a) > (b)) ? (a) : (b))
82 # define MIN(a, b) (((a) < (b)) ? (a) : (b))
84 #define CLAMP(val, min, max) MIN(MAX(min, val), max)
86 /* Glibc has an efficient macro for sigemptyset
87 * (it just does one or two assignments of 0 to internal vector of longs).
89 #if defined(__GLIBC__) && defined(__sigemptyset) && !defined(sigemptyset)
90 # define sigemptyset __sigemptyset
93 /* Configuration section */
94 #ifndef DEFAULT_STRLEN
95 /* default maximum # of bytes printed in `printstr', change with -s switch */
96 # define DEFAULT_STRLEN 32
98 #ifndef DEFAULT_ACOLUMN
99 # define DEFAULT_ACOLUMN 40 /* default alignment column for results */
102 * Maximum number of args to a syscall.
104 * Make sure that all entries in all syscallent.h files have nargs <= MAX_ARGS!
105 * linux/<ARCH>/syscallent*.h:
106 * all have nargs <= 6 except mips o32 which has nargs <= 7.
109 # ifdef LINUX_MIPSO32
115 /* default sorting method for call profiling */
116 #ifndef DEFAULT_SORTBY
117 # define DEFAULT_SORTBY "time"
120 * Experimental code using PTRACE_SEIZE can be enabled here.
121 * This needs Linux kernel 3.4.x or later to work.
124 /* To force NOMMU build, set to 1 */
125 #define NOMMU_SYSTEM 0
128 # define ERESTARTSYS 512
130 #ifndef ERESTARTNOINTR
131 # define ERESTARTNOINTR 513
133 #ifndef ERESTARTNOHAND
134 # define ERESTARTNOHAND 514
136 #ifndef ERESTART_RESTARTBLOCK
137 # define ERESTART_RESTARTBLOCK 516
141 # define SUPPORTED_PERSONALITIES 3
142 # define PERSONALITY2_WORDSIZE 4
143 # define PERSONALITY2_KLONGSIZE PERSONALITY0_KLONGSIZE
144 #elif defined AARCH64 \
145 || defined POWERPC64 \
150 # define SUPPORTED_PERSONALITIES 2
152 # define SUPPORTED_PERSONALITIES 1
155 #if defined TILE && defined __tilepro__
156 # define DEFAULT_PERSONALITY 1
158 # define DEFAULT_PERSONALITY 0
161 #define PERSONALITY0_WORDSIZE SIZEOF_LONG
162 #define PERSONALITY0_KLONGSIZE SIZEOF_KERNEL_LONG_T
163 #define PERSONALITY0_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
164 #define PERSONALITY0_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
166 #if SUPPORTED_PERSONALITIES > 1
167 # define PERSONALITY1_WORDSIZE 4
168 # define PERSONALITY1_KLONGSIZE PERSONALITY1_WORDSIZE
171 #if SUPPORTED_PERSONALITIES > 1 && defined HAVE_M32_MPERS
172 # define PERSONALITY1_INCLUDE_PRINTERS_DECLS "m32_printer_decls.h"
173 # define PERSONALITY1_INCLUDE_PRINTERS_DEFS "m32_printer_defs.h"
174 # define PERSONALITY1_INCLUDE_FUNCS "m32_funcs.h"
175 # define MPERS_m32_IOCTL_MACROS "ioctl_redefs1.h"
177 # define PERSONALITY1_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
178 # define PERSONALITY1_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
179 # define PERSONALITY1_INCLUDE_FUNCS "empty.h"
182 #if SUPPORTED_PERSONALITIES > 2 && defined HAVE_MX32_MPERS
183 # define PERSONALITY2_INCLUDE_FUNCS "mx32_funcs.h"
184 # define PERSONALITY2_INCLUDE_PRINTERS_DECLS "mx32_printer_decls.h"
185 # define PERSONALITY2_INCLUDE_PRINTERS_DEFS "mx32_printer_defs.h"
186 # define MPERS_mx32_IOCTL_MACROS "ioctl_redefs2.h"
188 # define PERSONALITY2_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
189 # define PERSONALITY2_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
190 # define PERSONALITY2_INCLUDE_FUNCS "empty.h"
193 typedef struct ioctlent {
205 #define MAX_ERRNO_VALUE 4095
206 #define INJECT_OPTS_RVAL_DEFAULT (-(MAX_ERRNO_VALUE + 1))
208 /* Trace Control Block */
210 int flags; /* See below for TCB_ values */
211 int pid; /* If 0, this tcb is free */
212 int qual_flg; /* qual_flags[scno] or DEFAULT_QUAL_FLAGS + RAW */
213 unsigned long u_error; /* Error code */
214 kernel_ulong_t scno; /* System call number */
215 kernel_ulong_t u_arg[MAX_ARGS]; /* System call arguments */
216 kernel_long_t u_rval; /* Return value */
217 #if SUPPORTED_PERSONALITIES > 1
218 unsigned int currpers; /* Personality at the time of scno update */
220 int sys_func_rval; /* Syscall entry parser's return value */
221 int curcol; /* Output column for this process */
222 FILE *outf; /* Output file for this process */
223 const char *auxstr; /* Auxiliary info from syscall (see RVAL_STR) */
224 void *_priv_data; /* Private data for syscall decoding functions */
225 void (*_free_priv_data)(void *); /* Callback for freeing priv_data */
226 const struct_sysent *s_ent; /* sysent[scno] or dummy struct for bad scno */
227 const struct_sysent *s_prev_ent; /* for "resuming interrupted SYSCALL" msg */
228 struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
229 struct timeval stime; /* System time usage as of last process wait */
230 struct timeval dtime; /* Delta for system time usage */
231 struct timeval etime; /* Syscall entry time */
234 struct UPT_info *libunwind_ui;
235 struct mmap_cache_t *mmap_cache;
236 unsigned int mmap_cache_size;
237 unsigned int mmap_cache_generation;
238 struct queue_t *queue;
243 /* We have attached to this process, but did not see it stopping yet */
244 #define TCB_STARTUP 0x01
245 #define TCB_IGNORE_ONE_SIGSTOP 0x02 /* Next SIGSTOP is to be ignored */
247 * Are we in system call entry or in syscall exit?
249 * This bit is set in syscall_entering_finish() and cleared in
250 * syscall_exiting_finish().
251 * Other stops which are possible directly after syscall entry (death, ptrace
252 * event stop) are handled without calling syscall_{entering,exiting}_*().
254 * Use entering(tcp) / exiting(tcp) to check this bit to make code more
257 #define TCB_INSYSCALL 0x04
258 #define TCB_ATTACHED 0x08 /* We attached to it already */
259 #define TCB_REPRINT 0x10 /* We should reprint this syscall on exit */
260 #define TCB_FILTERED 0x20 /* This system call has been filtered out */
261 #define TCB_TAMPERED 0x40 /* A syscall has been tampered with */
262 #define TCB_HIDE_LOG 0x80 /* We should hide everything (until execve) */
263 #define TCB_SKIP_DETACH_ON_FIRST_EXEC 0x100 /* -b execve should skip detach on first execve */
265 /* qualifier flags */
266 #define QUAL_TRACE 0x001 /* this system call should be traced */
267 #define QUAL_ABBREV 0x002 /* abbreviate the structures of this syscall */
268 #define QUAL_VERBOSE 0x004 /* decode the structures of this syscall */
269 #define QUAL_RAW 0x008 /* print all args in hex for this syscall */
270 #define QUAL_INJECT 0x010 /* tamper with this system call on purpose */
271 #define QUAL_SIGNAL 0x100 /* report events with this signal */
272 #define QUAL_READ 0x200 /* dump data read from this file descriptor */
273 #define QUAL_WRITE 0x400 /* dump data written to this file descriptor */
275 #define DEFAULT_QUAL_FLAGS (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
277 #define entering(tcp) (!((tcp)->flags & TCB_INSYSCALL))
278 #define exiting(tcp) ((tcp)->flags & TCB_INSYSCALL)
279 #define syserror(tcp) ((tcp)->u_error != 0)
280 #define verbose(tcp) ((tcp)->qual_flg & QUAL_VERBOSE)
281 #define abbrev(tcp) ((tcp)->qual_flg & QUAL_ABBREV)
282 #define filtered(tcp) ((tcp)->flags & TCB_FILTERED)
283 #define hide_log(tcp) ((tcp)->flags & TCB_HIDE_LOG)
287 extern const struct xlat addrfams[];
288 extern const struct xlat arp_hardware_types[];
289 extern const struct xlat at_flags[];
290 extern const struct xlat clocknames[];
291 extern const struct xlat dirent_types[];
292 extern const struct xlat ethernet_protocols[];
293 extern const struct xlat evdev_abs[];
294 extern const struct xlat iffflags[];
295 extern const struct xlat inet_protocols[];
296 extern const struct xlat ip_type_of_services[];
297 extern const struct xlat msg_flags[];
298 extern const struct xlat netlink_protocols[];
299 extern const struct xlat nl_route_types[];
300 extern const struct xlat open_access_modes[];
301 extern const struct xlat open_mode_flags[];
302 extern const struct xlat resource_flags[];
303 extern const struct xlat routing_scopes[];
304 extern const struct xlat routing_table_ids[];
305 extern const struct xlat setns_types[];
306 extern const struct xlat sg_io_info[];
307 extern const struct xlat socketlayers[];
308 extern const struct xlat socktypes[];
309 extern const struct xlat tcp_state_flags[];
310 extern const struct xlat tcp_states[];
311 extern const struct xlat whence_codes[];
313 /* Format of syscall return values */
314 #define RVAL_DECIMAL 000 /* decimal format */
315 #define RVAL_HEX 001 /* hex format */
316 #define RVAL_OCTAL 002 /* octal format */
317 #define RVAL_UDECIMAL 003 /* unsigned decimal format */
318 #define RVAL_FD 010 /* file descriptor */
319 #define RVAL_MASK 013 /* mask for these values */
321 #define RVAL_STR 020 /* Print `auxstr' field after return val */
322 #define RVAL_NONE 040 /* Print nothing */
324 #define RVAL_DECODED 0100 /* syscall decoding finished */
326 #define IOCTL_NUMBER_UNKNOWN 0
327 #define IOCTL_NUMBER_HANDLED 1
328 #define IOCTL_NUMBER_STOP_LOOKUP 010
330 #define indirect_ipccall(tcp) (tcp->s_ent->sys_flags & TRACE_INDIRECT_SUBCALL)
332 #if defined(ARM) || defined(AARCH64) \
333 || defined(I386) || defined(X32) || defined(X86_64) \
337 || defined(MICROBLAZE) \
340 || defined(SH) || defined(SH64) \
341 || defined(SPARC) || defined(SPARC64) \
343 # define NEED_UID16_PARSERS 1
345 # define NEED_UID16_PARSERS 0
357 extern enum sock_proto get_proto_by_name(const char *);
370 extern cflag_t cflag;
371 extern bool debug_flag;
374 extern bool count_wallclock;
375 extern unsigned int qflag;
376 extern bool not_failing_only;
377 extern unsigned int show_fd_path;
378 /* are we filtering traces based on paths? */
379 extern struct path_set {
380 const char **paths_selected;
381 unsigned int num_selected;
383 #define tracing_paths (global_path_set.num_selected != 0)
384 extern unsigned xflag;
385 extern unsigned followfork;
387 /* if this is true do the stack trace for every system call */
388 extern bool stack_trace_enabled;
390 extern unsigned ptrace_setoptions;
391 extern unsigned max_strlen;
392 extern unsigned os_release;
393 #undef KERNEL_VERSION
394 #define KERNEL_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
396 extern int read_int_from_file(const char *, int *);
398 extern void set_sortby(const char *);
399 extern void set_overhead(int);
400 extern void print_pc(struct tcb *);
402 extern int syscall_entering_decode(struct tcb *);
403 extern int syscall_entering_trace(struct tcb *, unsigned int *);
404 extern void syscall_entering_finish(struct tcb *, int);
406 extern int syscall_exiting_decode(struct tcb *, struct timeval *);
407 extern int syscall_exiting_trace(struct tcb *, struct timeval, int);
408 extern void syscall_exiting_finish(struct tcb *);
410 extern void count_syscall(struct tcb *, const struct timeval *);
411 extern void call_summary(FILE *);
413 extern void clear_regs(void);
414 extern int get_scno(struct tcb *);
415 extern kernel_ulong_t get_rt_sigframe_addr(struct tcb *);
418 * Convert syscall number to syscall name.
420 * @param scno Syscall number.
421 * @return String literal corresponding to the syscall number in case latter
422 * is valid; NULL otherwise.
424 extern const char *syscall_name(kernel_ulong_t scno);
425 extern const char *err_name(unsigned long err);
427 extern bool is_erestart(struct tcb *);
428 extern void temporarily_clear_syserror(struct tcb *);
429 extern void restore_cleared_syserror(struct tcb *);
431 extern void *get_tcb_priv_data(const struct tcb *);
432 extern int set_tcb_priv_data(struct tcb *, void *priv_data,
433 void (*free_priv_data)(void *));
434 extern void free_tcb_priv_data(struct tcb *);
436 static inline unsigned long get_tcb_priv_ulong(const struct tcb *tcp)
438 return (unsigned long) get_tcb_priv_data(tcp);
441 static inline int set_tcb_priv_ulong(struct tcb *tcp, unsigned long val)
443 return set_tcb_priv_data(tcp, (void *) val, 0);
447 umoven(struct tcb *, kernel_ulong_t addr, unsigned int len, void *laddr);
448 #define umove(pid, addr, objp) \
449 umoven((pid), (addr), sizeof(*(objp)), (void *) (objp))
452 umoven_or_printaddr(struct tcb *, kernel_ulong_t addr,
453 unsigned int len, void *laddr);
454 #define umove_or_printaddr(pid, addr, objp) \
455 umoven_or_printaddr((pid), (addr), sizeof(*(objp)), (void *) (objp))
458 umoven_or_printaddr_ignore_syserror(struct tcb *, kernel_ulong_t addr,
459 unsigned int len, void *laddr);
462 umovestr(struct tcb *, kernel_ulong_t addr, unsigned int len, char *laddr);
464 extern int upeek(int pid, unsigned long, kernel_ulong_t *);
465 extern int upoke(int pid, unsigned long, kernel_ulong_t);
468 print_array(struct tcb *,
469 kernel_ulong_t start_addr,
473 int (*umoven_func)(struct tcb *,
477 bool (*print_func)(struct tcb *,
483 #if defined ALPHA || defined IA64 || defined MIPS \
484 || defined SH || defined SPARC || defined SPARC64
485 # define HAVE_GETRVAL2
486 extern long getrval2(struct tcb *);
488 # undef HAVE_GETRVAL2
491 extern const char *signame(const int);
492 extern void pathtrace_select_set(const char *, struct path_set *);
493 extern bool pathtrace_match_set(struct tcb *, struct path_set *);
494 #define pathtrace_select(tcp) \
495 pathtrace_select_set(tcp, &global_path_set)
496 #define pathtrace_match(tcp) \
497 pathtrace_match_set(tcp, &global_path_set)
498 extern int getfdpath(struct tcb *, int, char *, unsigned);
499 extern unsigned long getfdinode(struct tcb *, int);
500 extern enum sock_proto getfdproto(struct tcb *, int);
502 extern const char *xlookup(const struct xlat *, const uint64_t);
503 extern const char *xlat_search(const struct xlat *, const size_t, const uint64_t);
506 struct dyxlat *dyxlat_alloc(size_t nmemb);
507 void dyxlat_free(struct dyxlat *);
508 const struct xlat *dyxlat_get(const struct dyxlat *);
509 void dyxlat_add_pair(struct dyxlat *, uint64_t val, const char *str, size_t len);
511 const struct xlat *genl_families_xlat(void);
513 extern unsigned long get_pagesize(void);
514 extern int next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits);
517 * Returns STR if it does not start with PREFIX,
518 * or a pointer to the first char in STR after PREFIX.
519 * The length of PREFIX is specified by PREFIX_LEN.
521 static inline const char *
522 str_strip_prefix_len(const char *str, const char *prefix, size_t prefix_len)
524 return strncmp(str, prefix, prefix_len) ? str : str + prefix_len;
527 #define STR_STRIP_PREFIX(str, prefix) \
528 str_strip_prefix_len((str), (prefix), sizeof(prefix) - 1)
530 #define QUOTE_0_TERMINATED 0x01
531 #define QUOTE_OMIT_LEADING_TRAILING_QUOTES 0x02
532 #define QUOTE_OMIT_TRAILING_0 0x08
533 #define QUOTE_FORCE_HEX 0x10
535 extern int string_quote(const char *, char *, unsigned int, unsigned int);
536 extern int print_quoted_string(const char *, unsigned int, unsigned int);
537 extern int print_quoted_cstring(const char *, unsigned int);
539 /* a refers to the lower numbered u_arg,
540 * b refers to the higher numbered u_arg
542 #ifdef WORDS_BIGENDIAN
543 # define ULONG_LONG(a, b) \
544 ((unsigned long long)(unsigned)(b) | ((unsigned long long)(a)<<32))
546 # define ULONG_LONG(a, b) \
547 ((unsigned long long)(unsigned)(a) | ((unsigned long long)(b)<<32))
549 extern int getllval(struct tcb *, unsigned long long *, int);
550 extern int printllval(struct tcb *, const char *, int)
551 ATTRIBUTE_FORMAT((printf, 2, 0));
553 extern void printaddr(kernel_ulong_t addr);
554 extern int printxvals(const uint64_t, const char *, const struct xlat *, ...)
556 extern int printxval_searchn(const struct xlat *xlat, size_t xlat_size,
557 uint64_t val, const char *dflt);
558 #define printxval_search(xlat__, val__, dflt__) \
559 printxval_searchn(xlat__, ARRAY_SIZE(xlat__), val__, dflt__)
560 extern int printargs(struct tcb *);
561 extern int printargs_u(struct tcb *);
562 extern int printargs_d(struct tcb *);
564 extern void addflags(const struct xlat *, uint64_t);
565 extern int printflags_ex(uint64_t, const char *, const struct xlat *, ...)
567 extern const char *sprintflags(const char *, const struct xlat *, uint64_t);
568 extern const char *sprinttime(long long sec);
569 extern const char *sprinttime_nsec(long long sec, unsigned long long nsec);
570 extern const char *sprinttime_usec(long long sec, unsigned long long usec);
571 extern void print_symbolic_mode_t(unsigned int);
572 extern void print_numeric_umode_t(unsigned short);
573 extern void print_numeric_long_umask(unsigned long);
574 extern void print_dev_t(unsigned long long dev);
575 extern void print_abnormal_hi(kernel_ulong_t);
578 dumpiov_in_msghdr(struct tcb *, kernel_ulong_t addr, kernel_ulong_t data_size);
581 dumpiov_in_mmsghdr(struct tcb *, kernel_ulong_t addr);
584 dumpiov_upto(struct tcb *, int len, kernel_ulong_t addr, kernel_ulong_t data_size);
587 dumpstr(struct tcb *, kernel_ulong_t addr, int len);
590 printstr_ex(struct tcb *, kernel_ulong_t addr, kernel_ulong_t len,
591 unsigned int user_style);
594 printpathn(struct tcb *, kernel_ulong_t addr, unsigned int n);
597 printpath(struct tcb *, kernel_ulong_t addr);
599 #define TIMESPEC_TEXT_BUFSIZE \
600 (sizeof(long long) * 3 * 2 + sizeof("{tv_sec=-, tv_nsec=}"))
601 extern void printfd(struct tcb *, int);
602 extern void print_sockaddr(const void *sa, int len);
604 print_inet_addr(int af, const void *addr, unsigned int len, const char *var_name);
606 decode_inet_addr(struct tcb *, kernel_ulong_t addr,
607 unsigned int len, int family, const char *var_name);
608 extern const char *get_sockaddr_by_inode(struct tcb *, int fd, unsigned long inode);
609 extern bool print_sockaddr_by_inode(struct tcb *, int fd, unsigned long inode);
610 extern void print_dirfd(struct tcb *, int);
613 decode_sockaddr(struct tcb *, kernel_ulong_t addr, int addrlen);
615 extern void printuid(const char *, const unsigned int);
618 print_sigset_addr_len(struct tcb *, kernel_ulong_t addr, kernel_ulong_t len);
620 print_sigset_addr(struct tcb *, kernel_ulong_t addr);
622 extern const char *sprintsigmask_n(const char *, const void *, unsigned int);
623 #define tprintsigmask_addr(prefix, mask) \
624 tprints(sprintsigmask_n((prefix), (mask), sizeof(mask)))
625 extern void printsignal(int);
628 tprint_iov_upto(struct tcb *, kernel_ulong_t len, kernel_ulong_t addr,
629 enum iov_decode, kernel_ulong_t data_size);
632 decode_netlink(struct tcb *, int fd, kernel_ulong_t addr, kernel_ulong_t len);
634 extern void tprint_open_modes(unsigned int);
635 extern const char *sprint_open_modes(unsigned int);
638 decode_seccomp_fprog(struct tcb *, kernel_ulong_t addr);
641 print_seccomp_fprog(struct tcb *, kernel_ulong_t addr, unsigned short len);
644 decode_sock_fprog(struct tcb *, kernel_ulong_t addr);
647 print_sock_fprog(struct tcb *, kernel_ulong_t addr, unsigned short len);
650 extern void print_struct_stat(struct tcb *, const struct strace_stat *const st);
652 struct strace_statfs;
655 print_struct_statfs(struct tcb *, kernel_ulong_t addr);
658 print_struct_statfs64(struct tcb *, kernel_ulong_t addr, kernel_ulong_t size);
660 extern void print_ifindex(unsigned int);
663 extern struct number_set read_set;
664 extern struct number_set write_set;
665 extern struct number_set signal_set;
667 extern bool is_number_in_set(unsigned int number, const struct number_set *);
668 extern void qualify(const char *);
669 extern unsigned int qual_flags(const unsigned int);
671 #define DECL_IOCTL(name) \
673 name ## _ioctl(struct tcb *, unsigned int request, kernel_ulong_t arg) \
674 /* End of DECL_IOCTL definition. */
687 extern int decode_sg_io_v4(struct tcb *, const kernel_ulong_t arg);
691 typedef bool (*netlink_decoder_t)(struct tcb *, const struct nlmsghdr *,
692 kernel_ulong_t addr, unsigned int len);
694 #define DECL_NETLINK(name) \
696 decode_netlink_ ## name(struct tcb *, const struct nlmsghdr *, \
697 kernel_ulong_t addr, unsigned int len) \
698 /* End of DECL_NETLINK definition. */
700 DECL_NETLINK(crypto);
702 DECL_NETLINK(selinux);
703 DECL_NETLINK(sock_diag);
705 extern int tv_nz(const struct timeval *);
706 extern int tv_cmp(const struct timeval *, const struct timeval *);
707 extern double tv_float(const struct timeval *);
708 extern void tv_add(struct timeval *, const struct timeval *, const struct timeval *);
709 extern void tv_sub(struct timeval *, const struct timeval *, const struct timeval *);
710 extern void tv_mul(struct timeval *, const struct timeval *, int);
711 extern void tv_div(struct timeval *, const struct timeval *, int);
714 extern void unwind_init(void);
715 extern void unwind_tcb_init(struct tcb *);
716 extern void unwind_tcb_fin(struct tcb *);
717 extern void unwind_cache_invalidate(struct tcb *);
718 extern void unwind_print_stacktrace(struct tcb *);
719 extern void unwind_capture_stacktrace(struct tcb *);
723 printstrn(struct tcb *tcp, kernel_ulong_t addr, kernel_ulong_t len)
725 printstr_ex(tcp, addr, len, 0);
729 printstr(struct tcb *tcp, kernel_ulong_t addr)
731 printstr_ex(tcp, addr, -1, QUOTE_0_TERMINATED);
735 printflags64(const struct xlat *x, uint64_t flags, const char *dflt)
737 return printflags_ex(flags, dflt, x, NULL);
741 printflags(const struct xlat *x, unsigned int flags, const char *dflt)
743 return printflags64(x, flags, dflt);
747 printxval64(const struct xlat *x, const uint64_t val, const char *dflt)
749 return printxvals(val, dflt, x, NULL);
753 printxval(const struct xlat *x, const unsigned int val, const char *dflt)
755 return printxvals(val, dflt, x, NULL);
759 tprint_iov(struct tcb *tcp, kernel_ulong_t len, kernel_ulong_t addr,
760 enum iov_decode decode_iov)
762 tprint_iov_upto(tcp, len, addr, decode_iov, -1);
770 extern void print_timeval32_t(const timeval32_t *);
771 extern void printrusage32(struct tcb *, kernel_ulong_t);
772 extern const char *sprint_timeval32(struct tcb *, kernel_ulong_t addr);
773 extern void print_timeval32(struct tcb *, kernel_ulong_t addr);
774 extern void print_timeval32_utimes(struct tcb *, kernel_ulong_t addr);
775 extern void print_itimerval32(struct tcb *, kernel_ulong_t addr);
778 #ifdef HAVE_STRUCT_USER_DESC
779 extern void print_user_desc(struct tcb *, kernel_ulong_t addr);
782 /* Strace log generation machinery.
784 * printing_tcp: tcb which has incomplete line being printed right now.
785 * NULL if last line has been completed ('\n'-terminated).
786 * printleader(tcp) examines it, finishes incomplete line if needed,
787 * the sets it to tcp.
788 * line_ended() clears printing_tcp and resets ->curcol = 0.
789 * tcp->curcol == 0 check is also used to detect completeness
790 * of last line, since in -ff mode just checking printing_tcp for NULL
793 * If you change this code, test log generation in both -f and -ff modes
795 * strace -oLOG -f[f] test/threaded_execve
796 * strace -oLOG -f[f] test/sigkill_rain
797 * strace -oLOG -f[f] -p "`pidof web_browser`"
799 extern struct tcb *printing_tcp;
800 extern void printleader(struct tcb *);
801 extern void line_ended(void);
802 extern void tabto(void);
803 extern void tprintf(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
804 extern void tprints(const char *str);
805 extern void tprintf_comment(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
806 extern void tprints_comment(const char *str);
808 #if SUPPORTED_PERSONALITIES > 1
809 extern void set_personality(int personality);
810 extern unsigned current_personality;
812 # define set_personality(personality) ((void)0)
813 # define current_personality 0
816 #if SUPPORTED_PERSONALITIES == 1
817 # define current_wordsize PERSONALITY0_WORDSIZE
818 # define current_klongsize PERSONALITY0_KLONGSIZE
820 # if SUPPORTED_PERSONALITIES == 2 && PERSONALITY0_WORDSIZE == PERSONALITY1_WORDSIZE
821 # define current_wordsize PERSONALITY0_WORDSIZE
823 extern unsigned current_wordsize;
825 # if SUPPORTED_PERSONALITIES == 2 && PERSONALITY0_KLONGSIZE == PERSONALITY1_KLONGSIZE
826 # define current_klongsize PERSONALITY0_KLONGSIZE
828 extern unsigned current_klongsize;
832 #define ANY_WORDSIZE_LESS_THAN_KERNEL_LONG \
833 (SIZEOF_KERNEL_LONG_T > 4 \
834 && (SIZEOF_LONG < SIZEOF_KERNEL_LONG_T || !defined(current_wordsize)))
836 #define DECL_PRINTNUM(name) \
838 printnum_ ## name(struct tcb *, kernel_ulong_t addr, const char *fmt) \
839 ATTRIBUTE_FORMAT((printf, 3, 0)) \
840 /* End of DECL_PRINTNUM definition. */
842 DECL_PRINTNUM(short);
844 DECL_PRINTNUM(int64);
847 #define DECL_PRINTNUM_ADDR(name) \
849 printnum_addr_ ## name(struct tcb *, kernel_ulong_t addr) \
850 /* End of DECL_PRINTNUM_ADDR definition. */
852 DECL_PRINTNUM_ADDR(int);
853 DECL_PRINTNUM_ADDR(int64);
854 #undef DECL_PRINTNUM_ADDR
856 #ifndef current_wordsize
858 printnum_long_int(struct tcb *, kernel_ulong_t addr,
859 const char *fmt_long, const char *fmt_int)
860 ATTRIBUTE_FORMAT((printf, 3, 0))
861 ATTRIBUTE_FORMAT((printf, 4, 0));
862 extern bool printnum_addr_long_int(struct tcb *, kernel_ulong_t addr);
863 # define printnum_slong(tcp, addr) \
864 printnum_long_int((tcp), (addr), "%" PRId64, "%d")
865 # define printnum_ulong(tcp, addr) \
866 printnum_long_int((tcp), (addr), "%" PRIu64, "%u")
867 # define printnum_ptr(tcp, addr) \
868 printnum_addr_long_int((tcp), (addr))
869 #elif current_wordsize > 4
870 # define printnum_slong(tcp, addr) \
871 printnum_int64((tcp), (addr), "%" PRId64)
872 # define printnum_ulong(tcp, addr) \
873 printnum_int64((tcp), (addr), "%" PRIu64)
874 # define printnum_ptr(tcp, addr) \
875 printnum_addr_int64((tcp), (addr))
876 #else /* current_wordsize == 4 */
877 # define printnum_slong(tcp, addr) \
878 printnum_int((tcp), (addr), "%d")
879 # define printnum_ulong(tcp, addr) \
880 printnum_int((tcp), (addr), "%u")
881 # define printnum_ptr(tcp, addr) \
882 printnum_addr_int((tcp), (addr))
885 #ifndef current_klongsize
886 extern bool printnum_addr_klong_int(struct tcb *, kernel_ulong_t addr);
887 # define printnum_kptr(tcp, addr) \
888 printnum_addr_klong_int((tcp), (addr))
889 #elif current_klongsize > 4
890 # define printnum_kptr(tcp, addr) \
891 printnum_addr_int64((tcp), (addr))
892 #else /* current_klongsize == 4 */
893 # define printnum_kptr(tcp, addr) \
894 printnum_addr_int((tcp), (addr))
897 #define DECL_PRINTPAIR(name) \
899 printpair_ ## name(struct tcb *, kernel_ulong_t addr, const char *fmt) \
900 ATTRIBUTE_FORMAT((printf, 3, 0)) \
901 /* End of DECL_PRINTPAIR definition. */
904 DECL_PRINTPAIR(int64);
905 #undef DECL_PRINTPAIR
907 static inline kernel_long_t
908 truncate_klong_to_current_wordsize(const kernel_long_t v)
910 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
911 if (current_wordsize < sizeof(v)) {
920 static inline kernel_ulong_t
921 truncate_kulong_to_current_wordsize(const kernel_ulong_t v)
923 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
924 if (current_wordsize < sizeof(v)) {
925 return (unsigned int) v;
934 * Cast a pointer or a pointer-sized integer to kernel_ulong_t.
936 #define ptr_to_kulong(v) ((kernel_ulong_t) (unsigned long) (v))
939 * Zero-extend a signed integer type to unsigned long long.
941 #define zero_extend_signed_to_ull(v) \
942 (sizeof(v) == sizeof(char) ? (unsigned long long) (unsigned char) (v) : \
943 sizeof(v) == sizeof(short) ? (unsigned long long) (unsigned short) (v) : \
944 sizeof(v) == sizeof(int) ? (unsigned long long) (unsigned int) (v) : \
945 sizeof(v) == sizeof(long) ? (unsigned long long) (unsigned long) (v) : \
946 (unsigned long long) (v))
949 * Sign-extend an unsigned integer type to long long.
951 #define sign_extend_unsigned_to_ll(v) \
952 (sizeof(v) == sizeof(char) ? (long long) (char) (v) : \
953 sizeof(v) == sizeof(short) ? (long long) (short) (v) : \
954 sizeof(v) == sizeof(int) ? (long long) (int) (v) : \
955 sizeof(v) == sizeof(long) ? (long long) (long) (v) : \
958 extern const struct_sysent sysent0[];
959 extern const char *const errnoent0[];
960 extern const char *const signalent0[];
961 extern const struct_ioctlent ioctlent0[];
963 #if SUPPORTED_PERSONALITIES > 1
964 extern const struct_sysent *sysent;
965 extern const char *const *errnoent;
966 extern const char *const *signalent;
967 extern const struct_ioctlent *ioctlent;
969 # define sysent sysent0
970 # define errnoent errnoent0
971 # define signalent signalent0
972 # define ioctlent ioctlent0
975 extern unsigned nsyscalls;
976 extern unsigned nerrnos;
977 extern unsigned nsignals;
978 extern unsigned nioctlents;
980 extern const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES];
981 extern const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES];
982 extern struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
984 #ifdef IN_MPERS_BOOTSTRAP
985 /* Transform multi-line MPERS_PRINTER_DECL statements to one-liners. */
986 # define MPERS_PRINTER_DECL(type, name, ...) MPERS_PRINTER_DECL(type, name, __VA_ARGS__)
987 #else /* !IN_MPERS_BOOTSTRAP */
988 # if SUPPORTED_PERSONALITIES > 1
989 # include "printers.h"
991 # include "native_printer_decls.h"
993 # define MPERS_PRINTER_DECL(type, name, ...) type MPERS_FUNC_NAME(name)(__VA_ARGS__)
994 #endif /* !IN_MPERS_BOOTSTRAP */
996 /* Checks that sysent[scno] is not out of range. */
998 scno_in_range(kernel_ulong_t scno)
1000 return scno < nsyscalls;
1004 * Checks whether scno is not out of range,
1005 * its corresponding sysent[scno].sys_func is non-NULL,
1006 * and its sysent[scno].sys_flags has no TRACE_INDIRECT_SUBCALL flag set.
1009 scno_is_valid(kernel_ulong_t scno)
1011 return scno_in_range(scno)
1012 && sysent[scno].sys_func
1013 && !(sysent[scno].sys_flags & TRACE_INDIRECT_SUBCALL);
1016 #define MPERS_FUNC_NAME__(prefix, name) prefix ## name
1017 #define MPERS_FUNC_NAME_(prefix, name) MPERS_FUNC_NAME__(prefix, name)
1018 #define MPERS_FUNC_NAME(name) MPERS_FUNC_NAME_(MPERS_PREFIX, name)
1020 #define SYS_FUNC_NAME(syscall_name) MPERS_FUNC_NAME(syscall_name)
1022 #define SYS_FUNC(syscall_name) int SYS_FUNC_NAME(sys_ ## syscall_name)(struct tcb *tcp)
1024 #endif /* !STRACE_DEFS_H */