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>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include <sys/types.h>
48 /* Open-coding isprint(ch) et al proved more efficient than calling
49 * generalized libc interface. We don't *want* to do non-ASCII anyway.
51 /* #include <ctype.h> */
56 #include <asm/unistd.h>
58 #include "mpers_type.h"
59 #include "gcc_compat.h"
62 const char *strerror(int);
65 /* Some libc have stpcpy, some don't. Sigh...
66 * Roll our private implementation...
69 #define stpcpy strace_stpcpy
70 extern char *stpcpy(char *dst, const char *src);
74 # define offsetof(type, member) \
75 (((char *) &(((type *) NULL)->member)) - ((char *) (type *) NULL))
78 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
82 # define MAX(a, b) (((a) > (b)) ? (a) : (b))
85 # define MIN(a, b) (((a) < (b)) ? (a) : (b))
87 #define CLAMP(val, min, max) MIN(MAX(min, val), max)
89 /* Glibc has an efficient macro for sigemptyset
90 * (it just does one or two assignments of 0 to internal vector of longs).
92 #if defined(__GLIBC__) && defined(__sigemptyset) && !defined(sigemptyset)
93 # define sigemptyset __sigemptyset
96 /* Configuration section */
97 #ifndef DEFAULT_STRLEN
98 /* default maximum # of bytes printed in `printstr', change with -s switch */
99 # define DEFAULT_STRLEN 32
101 #ifndef DEFAULT_ACOLUMN
102 # define DEFAULT_ACOLUMN 40 /* default alignment column for results */
105 * Maximum number of args to a syscall.
107 * Make sure that all entries in all syscallent.h files have nargs <= MAX_ARGS!
108 * linux/<ARCH>/syscallent*.h:
109 * all have nargs <= 6 except mips o32 which has nargs <= 7.
112 # ifdef LINUX_MIPSO32
118 /* default sorting method for call profiling */
119 #ifndef DEFAULT_SORTBY
120 # define DEFAULT_SORTBY "time"
123 * Experimental code using PTRACE_SEIZE can be enabled here.
124 * This needs Linux kernel 3.4.x or later to work.
127 /* To force NOMMU build, set to 1 */
128 #define NOMMU_SYSTEM 0
130 * Set to 1 to use speed-optimized vfprintf implementation.
131 * It results in strace using about 5% less CPU in user space
132 * (compared to glibc version).
133 * But strace spends a lot of time in kernel space,
134 * so overall it does not appear to be a significant win.
135 * Thus disabled by default.
137 #define USE_CUSTOM_PRINTF 0
140 # define ERESTARTSYS 512
142 #ifndef ERESTARTNOINTR
143 # define ERESTARTNOINTR 513
145 #ifndef ERESTARTNOHAND
146 # define ERESTARTNOHAND 514
148 #ifndef ERESTART_RESTARTBLOCK
149 # define ERESTART_RESTARTBLOCK 516
153 # define SUPPORTED_PERSONALITIES 3
154 # define PERSONALITY2_WORDSIZE 4
155 #elif defined AARCH64 \
156 || defined POWERPC64 \
161 # define SUPPORTED_PERSONALITIES 2
163 # define SUPPORTED_PERSONALITIES 1
166 #if defined TILE && defined __tilepro__
167 # define DEFAULT_PERSONALITY 1
169 # define DEFAULT_PERSONALITY 0
172 #define PERSONALITY0_WORDSIZE SIZEOF_LONG
173 #define PERSONALITY0_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
174 #define PERSONALITY0_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
176 #if SUPPORTED_PERSONALITIES > 1
177 # define PERSONALITY1_WORDSIZE 4
180 #if SUPPORTED_PERSONALITIES > 1 && defined HAVE_M32_MPERS
181 # define PERSONALITY1_INCLUDE_PRINTERS_DECLS "m32_printer_decls.h"
182 # define PERSONALITY1_INCLUDE_PRINTERS_DEFS "m32_printer_defs.h"
183 # define PERSONALITY1_INCLUDE_FUNCS "m32_funcs.h"
184 # define MPERS_m32_IOCTL_MACROS "ioctl_redefs1.h"
186 # define PERSONALITY1_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
187 # define PERSONALITY1_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
188 # define PERSONALITY1_INCLUDE_FUNCS "empty.h"
191 #if SUPPORTED_PERSONALITIES > 2 && defined HAVE_MX32_MPERS
192 # define PERSONALITY2_INCLUDE_FUNCS "mx32_funcs.h"
193 # define PERSONALITY2_INCLUDE_PRINTERS_DECLS "mx32_printer_decls.h"
194 # define PERSONALITY2_INCLUDE_PRINTERS_DEFS "mx32_printer_defs.h"
195 # define MPERS_mx32_IOCTL_MACROS "ioctl_redefs2.h"
197 # define PERSONALITY2_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
198 # define PERSONALITY2_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
199 # define PERSONALITY2_INCLUDE_FUNCS "empty.h"
202 typedef struct sysent {
207 const char *sys_name;
210 typedef struct ioctlent {
215 #if defined LINUX_MIPSN32 || defined X32
216 # define HAVE_STRUCT_TCB_EXT_ARG 1
218 # define HAVE_STRUCT_TCB_EXT_ARG 0
221 /* Trace Control Block */
223 int flags; /* See below for TCB_ values */
224 int pid; /* If 0, this tcb is free */
225 int qual_flg; /* qual_flags[scno] or DEFAULT_QUAL_FLAGS + RAW */
226 int u_error; /* Error code */
227 long scno; /* System call number */
228 long u_arg[MAX_ARGS]; /* System call arguments */
229 #if HAVE_STRUCT_TCB_EXT_ARG
230 long long ext_arg[MAX_ARGS];
231 long long u_lrval; /* long long return value */
233 long u_rval; /* Return value */
234 #if SUPPORTED_PERSONALITIES > 1
235 unsigned int currpers; /* Personality at the time of scno update */
237 int sys_func_rval; /* Syscall entry parser's return value */
238 int curcol; /* Output column for this process */
239 FILE *outf; /* Output file for this process */
240 const char *auxstr; /* Auxiliary info from syscall (see RVAL_STR) */
241 void *_priv_data; /* Private data for syscall decoding functions */
242 void (*_free_priv_data)(void *); /* Callback for freeing priv_data */
243 const struct_sysent *s_ent; /* sysent[scno] or dummy struct for bad scno */
244 const struct_sysent *s_prev_ent; /* for "resuming interrupted SYSCALL" msg */
245 struct timeval stime; /* System time usage as of last process wait */
246 struct timeval dtime; /* Delta for system time usage */
247 struct timeval etime; /* Syscall entry time */
250 struct UPT_info* libunwind_ui;
251 struct mmap_cache_t* mmap_cache;
252 unsigned int mmap_cache_size;
253 unsigned int mmap_cache_generation;
254 struct queue_t* queue;
259 /* We have attached to this process, but did not see it stopping yet */
260 #define TCB_STARTUP 0x01
261 #define TCB_IGNORE_ONE_SIGSTOP 0x02 /* Next SIGSTOP is to be ignored */
263 * Are we in system call entry or in syscall exit?
265 * This bit is set after all syscall entry processing is done.
266 * Therefore, this bit will be set when next ptrace stop occurs,
267 * which should be syscall exit stop. Other stops which are possible
268 * directly after syscall entry (death, ptrace event stop)
269 * are simpler and handled without calling trace_syscall(), therefore
270 * the places where TCB_INSYSCALL can be set but we aren't in syscall stop
271 * are limited to trace(), this condition is never observed in trace_syscall()
273 * The bit is cleared after all syscall exit processing is done.
275 * Use entering(tcp) / exiting(tcp) to check this bit to make code more readable.
277 #define TCB_INSYSCALL 0x04
278 #define TCB_ATTACHED 0x08 /* We attached to it already */
279 #define TCB_REPRINT 0x10 /* We should reprint this syscall on exit */
280 #define TCB_FILTERED 0x20 /* This system call has been filtered out */
282 /* qualifier flags */
283 #define QUAL_TRACE 0x001 /* this system call should be traced */
284 #define QUAL_ABBREV 0x002 /* abbreviate the structures of this syscall */
285 #define QUAL_VERBOSE 0x004 /* decode the structures of this syscall */
286 #define QUAL_RAW 0x008 /* print all args in hex for this syscall */
287 #define QUAL_SIGNAL 0x010 /* report events with this signal */
288 #define QUAL_READ 0x020 /* dump data read on this file descriptor */
289 #define QUAL_WRITE 0x040 /* dump data written to this file descriptor */
290 typedef uint8_t qualbits_t;
292 #define DEFAULT_QUAL_FLAGS (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
294 #define entering(tcp) (!((tcp)->flags & TCB_INSYSCALL))
295 #define exiting(tcp) ((tcp)->flags & TCB_INSYSCALL)
296 #define syserror(tcp) ((tcp)->u_error != 0)
297 #define verbose(tcp) ((tcp)->qual_flg & QUAL_VERBOSE)
298 #define abbrev(tcp) ((tcp)->qual_flg & QUAL_ABBREV)
299 #define filtered(tcp) ((tcp)->flags & TCB_FILTERED)
303 extern const struct xlat addrfams[];
304 extern const struct xlat at_flags[];
305 extern const struct xlat dirent_types[];
306 extern const struct xlat evdev_abs[];
307 extern const struct xlat msg_flags[];
308 extern const struct xlat open_access_modes[];
309 extern const struct xlat open_mode_flags[];
310 extern const struct xlat resource_flags[];
311 extern const struct xlat socketlayers[];
312 extern const struct xlat whence_codes[];
314 /* Format of syscall return values */
315 #define RVAL_DECIMAL 000 /* decimal format */
316 #define RVAL_HEX 001 /* hex format */
317 #define RVAL_OCTAL 002 /* octal format */
318 #define RVAL_UDECIMAL 003 /* unsigned decimal format */
319 #if HAVE_STRUCT_TCB_EXT_ARG
320 # if 0 /* unused so far */
321 # define RVAL_LDECIMAL 004 /* long decimal format */
322 # define RVAL_LHEX 005 /* long hex format */
323 # define RVAL_LOCTAL 006 /* long octal format */
325 # define RVAL_LUDECIMAL 007 /* long unsigned decimal format */
326 #endif /* HAVE_STRUCT_TCB_EXT_ARG */
327 #define RVAL_FD 010 /* file descriptor */
328 #define RVAL_MASK 017 /* mask for these values */
330 #define RVAL_STR 020 /* Print `auxstr' field after return val */
331 #define RVAL_NONE 040 /* Print nothing */
333 #define RVAL_DECODED 0100 /* syscall decoding finished */
335 #define TRACE_FILE 001 /* Trace file-related syscalls. */
336 #define TRACE_IPC 002 /* Trace IPC-related syscalls. */
337 #define TRACE_NETWORK 004 /* Trace network-related syscalls. */
338 #define TRACE_PROCESS 010 /* Trace process-related syscalls. */
339 #define TRACE_SIGNAL 020 /* Trace signal-related syscalls. */
340 #define TRACE_DESC 040 /* Trace file descriptor-related syscalls. */
341 #define TRACE_MEMORY 0100 /* Trace memory mapping-related syscalls. */
342 #define SYSCALL_NEVER_FAILS 0200 /* Syscall is always successful. */
343 #define STACKTRACE_INVALIDATE_CACHE 0400 /* Trigger proc/maps cache updating */
344 #define STACKTRACE_CAPTURE_ON_ENTER 01000 /* Capture stacktrace on "entering" stage */
345 #define TRACE_INDIRECT_SUBCALL 02000 /* Syscall is an indirect socket/ipc subcall. */
347 #define IOCTL_NUMBER_UNKNOWN 0
348 #define IOCTL_NUMBER_HANDLED 1
349 #define IOCTL_NUMBER_STOP_LOOKUP 010
351 #define indirect_ipccall(tcp) (tcp->s_ent->sys_flags & TRACE_INDIRECT_SUBCALL)
353 #if defined(ARM) || defined(AARCH64) \
354 || defined(I386) || defined(X32) || defined(X86_64) \
358 || defined(MICROBLAZE) \
361 || defined(SH) || defined(SH64) \
362 || defined(SPARC) || defined(SPARC64) \
364 # define NEED_UID16_PARSERS 1
366 # define NEED_UID16_PARSERS 0
378 extern enum sock_proto get_proto_by_name(const char *);
391 extern cflag_t cflag;
392 extern bool debug_flag;
395 extern bool count_wallclock;
396 extern unsigned int qflag;
397 extern bool not_failing_only;
398 extern unsigned int show_fd_path;
399 extern bool hide_log_until_execve;
400 /* are we filtering traces based on paths? */
401 extern const char **paths_selected;
402 #define tracing_paths (paths_selected != NULL)
403 extern unsigned xflag;
404 extern unsigned followfork;
406 /* if this is true do the stack trace for every system call */
407 extern bool stack_trace_enabled;
409 extern unsigned ptrace_setoptions;
410 extern unsigned max_strlen;
411 extern unsigned os_release;
412 #undef KERNEL_VERSION
413 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
415 void error_msg(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
416 void perror_msg(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
417 void error_msg_and_die(const char *fmt, ...)
418 ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
419 void error_msg_and_help(const char *fmt, ...)
420 ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
421 void perror_msg_and_die(const char *fmt, ...)
422 ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
423 void die_out_of_memory(void) ATTRIBUTE_NORETURN;
425 void *xmalloc(size_t size) ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1));
426 void *xcalloc(size_t nmemb, size_t size)
427 ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1, 2));
428 void *xreallocarray(void *ptr, size_t nmemb, size_t size)
429 ATTRIBUTE_ALLOC_SIZE((2, 3));
430 char *xstrdup(const char *str) ATTRIBUTE_MALLOC;
432 #if USE_CUSTOM_PRINTF
434 * See comment in vsprintf.c for allowed formats.
435 * Short version: %h[h]u, %zu, %tu are not allowed, use %[l[l]]u.
437 int strace_vfprintf(FILE *fp, const char *fmt, va_list args);
439 # define strace_vfprintf vfprintf
442 extern int read_int_from_file(const char *, int *);
444 extern void set_sortby(const char *);
445 extern void set_overhead(int);
446 extern void qualify(const char *);
447 extern void print_pc(struct tcb *);
448 extern int trace_syscall(struct tcb *);
449 extern void count_syscall(struct tcb *, const struct timeval *);
450 extern void call_summary(FILE *);
452 extern void clear_regs(void);
453 extern void get_regs(pid_t pid);
454 extern int get_scno(struct tcb *tcp);
455 extern const char *syscall_name(long scno);
457 extern bool is_erestart(struct tcb *);
458 extern void temporarily_clear_syserror(struct tcb *);
459 extern void restore_cleared_syserror(struct tcb *);
461 extern void *get_tcb_priv_data(const struct tcb *);
462 extern int set_tcb_priv_data(struct tcb *, void *priv_data,
463 void (*free_priv_data)(void *));
464 extern void free_tcb_priv_data(struct tcb *);
466 static inline unsigned long get_tcb_priv_ulong(const struct tcb *tcp)
468 return (unsigned long) get_tcb_priv_data(tcp);
471 static inline int set_tcb_priv_ulong(struct tcb *tcp, unsigned long val)
473 return set_tcb_priv_data(tcp, (void *) val, 0);
476 extern int umoven(struct tcb *, long, unsigned int, void *);
477 #define umove(pid, addr, objp) \
478 umoven((pid), (addr), sizeof(*(objp)), (void *) (objp))
479 extern int umoven_or_printaddr(struct tcb *, long, unsigned int, void *);
480 #define umove_or_printaddr(pid, addr, objp) \
481 umoven_or_printaddr((pid), (addr), sizeof(*(objp)), (void *) (objp))
482 extern int umovestr(struct tcb *, long, unsigned int, char *);
483 extern int upeek(int pid, long, long *);
486 print_array(struct tcb *tcp,
487 const unsigned long start_addr,
489 void *const elem_buf,
490 const size_t elem_size,
491 int (*const umoven_func)(struct tcb *,
495 bool (*const print_func)(struct tcb *,
499 void *const opaque_data);
501 #if defined ALPHA || defined IA64 || defined MIPS \
502 || defined SH || defined SPARC || defined SPARC64
503 # define HAVE_GETRVAL2
504 extern long getrval2(struct tcb *);
506 # undef HAVE_GETRVAL2
509 extern const char *signame(const int);
510 extern void pathtrace_select(const char *);
511 extern int pathtrace_match(struct tcb *);
512 extern int getfdpath(struct tcb *, int, char *, unsigned);
513 extern enum sock_proto getfdproto(struct tcb *, int);
515 extern const char *xlookup(const struct xlat *, const uint64_t);
516 extern const char *xlat_search(const struct xlat *, const size_t, const uint64_t);
518 extern unsigned long get_pagesize(void);
519 extern int string_to_uint(const char *str);
520 extern int next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits);
522 #define QUOTE_0_TERMINATED 0x01
523 #define QUOTE_OMIT_LEADING_TRAILING_QUOTES 0x02
525 extern int string_quote(const char *, char *, unsigned int, unsigned int);
526 extern int print_quoted_string(const char *, unsigned int, unsigned int);
528 /* a refers to the lower numbered u_arg,
529 * b refers to the higher numbered u_arg
531 #ifdef WORDS_BIGENDIAN
532 # define LONG_LONG(a,b) \
533 ((long long)((unsigned long long)(unsigned)(b) | ((unsigned long long)(a)<<32)))
535 # define LONG_LONG(a,b) \
536 ((long long)((unsigned long long)(unsigned)(a) | ((unsigned long long)(b)<<32)))
538 extern int getllval(struct tcb *, unsigned long long *, int);
539 extern int printllval(struct tcb *, const char *, int)
540 ATTRIBUTE_FORMAT((printf, 2, 0));
542 extern void printaddr(long);
543 extern void printxvals(const uint64_t, const char *, const struct xlat *, ...)
545 extern int printargs(struct tcb *);
546 extern int printargs_u(struct tcb *);
547 extern int printargs_d(struct tcb *);
549 extern void addflags(const struct xlat *, uint64_t);
550 extern int printflags64(const struct xlat *, uint64_t, const char *);
551 extern const char *sprintflags(const char *, const struct xlat *, uint64_t);
552 extern const char *sprinttime(time_t);
553 extern void print_symbolic_mode_t(unsigned int);
554 extern void print_numeric_umode_t(unsigned short);
555 extern void print_numeric_long_umask(unsigned long);
556 extern void dumpiov_in_msghdr(struct tcb *, long, unsigned long);
557 extern void dumpiov_in_mmsghdr(struct tcb *, long);
558 extern void dumpiov_upto(struct tcb *, int, long, unsigned long);
559 #define dumpiov(tcp, len, addr) \
560 dumpiov_upto((tcp), (len), (addr), (unsigned long) -1L)
561 extern void dumpstr(struct tcb *, long, int);
562 extern void printstr(struct tcb *, long, long);
563 extern bool printnum_short(struct tcb *, long, const char *)
564 ATTRIBUTE_FORMAT((printf, 3, 0));
565 extern bool printnum_int(struct tcb *, long, const char *)
566 ATTRIBUTE_FORMAT((printf, 3, 0));
567 extern bool printnum_int64(struct tcb *, long, const char *)
568 ATTRIBUTE_FORMAT((printf, 3, 0));
570 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
571 extern bool printnum_long_int(struct tcb *, long, const char *, const char *)
572 ATTRIBUTE_FORMAT((printf, 3, 0))
573 ATTRIBUTE_FORMAT((printf, 4, 0));
574 # define printnum_slong(tcp, addr) \
575 printnum_long_int((tcp), (addr), "%" PRId64, "%d")
576 # define printnum_ulong(tcp, addr) \
577 printnum_long_int((tcp), (addr), "%" PRIu64, "%u")
578 # define printnum_ptr(tcp, addr) \
579 printnum_long_int((tcp), (addr), "%#" PRIx64, "%#x")
580 #elif SIZEOF_LONG > 4
581 # define printnum_slong(tcp, addr) \
582 printnum_int64((tcp), (addr), "%" PRId64)
583 # define printnum_ulong(tcp, addr) \
584 printnum_int64((tcp), (addr), "%" PRIu64)
585 # define printnum_ptr(tcp, addr) \
586 printnum_int64((tcp), (addr), "%#" PRIx64)
588 # define printnum_slong(tcp, addr) \
589 printnum_int((tcp), (addr), "%d")
590 # define printnum_ulong(tcp, addr) \
591 printnum_int((tcp), (addr), "%u")
592 # define printnum_ptr(tcp, addr) \
593 printnum_int((tcp), (addr), "%#x")
596 extern bool printpair_int(struct tcb *, long, const char *)
597 ATTRIBUTE_FORMAT((printf, 3, 0));
598 extern bool printpair_int64(struct tcb *, long, const char *)
599 ATTRIBUTE_FORMAT((printf, 3, 0));
600 extern void printpath(struct tcb *, long);
601 extern void printpathn(struct tcb *, long, unsigned int);
602 #define TIMESPEC_TEXT_BUFSIZE \
603 (sizeof(intmax_t)*3 * 2 + sizeof("{tv_sec=%jd, tv_nsec=%jd}"))
604 extern void printfd(struct tcb *, int);
605 extern void print_sockaddr(struct tcb *tcp, const void *, int);
606 extern bool print_sockaddr_by_inode(const unsigned long, const enum sock_proto);
607 extern bool print_sockaddr_by_inode_cached(const unsigned long);
608 extern void print_dirfd(struct tcb *, int);
609 extern int decode_sockaddr(struct tcb *, long, int);
611 extern void printrusage32(struct tcb *, long);
612 extern const char *sprint_timeval32(struct tcb *tcp, long);
613 extern void print_timeval32(struct tcb *tcp, long);
614 extern void print_timeval32_pair(struct tcb *tcp, long);
615 extern void print_itimerval32(struct tcb *tcp, long);
617 extern void printuid(const char *, const unsigned int);
618 extern void print_sigset_addr_len(struct tcb *, long, long);
619 extern const char *sprintsigmask_n(const char *, const void *, unsigned int);
620 #define tprintsigmask_addr(prefix, mask) \
621 tprints(sprintsigmask_n((prefix), (mask), sizeof(mask)))
622 extern void printsignal(int);
623 extern void tprint_iov(struct tcb *, unsigned long, unsigned long, enum iov_decode);
624 extern void tprint_iov_upto(struct tcb *, unsigned long, unsigned long,
625 enum iov_decode, unsigned long);
626 extern void decode_netlink(struct tcb *, unsigned long, unsigned long);
627 extern void tprint_open_modes(unsigned int);
628 extern const char *sprint_open_modes(unsigned int);
629 extern void print_seccomp_filter(struct tcb *, unsigned long);
630 extern void print_seccomp_fprog(struct tcb *, unsigned long, unsigned short);
633 extern void print_struct_stat(struct tcb *tcp, const struct strace_stat *const st);
635 struct strace_statfs;
636 extern void print_struct_statfs(struct tcb *tcp, long);
637 extern void print_struct_statfs64(struct tcb *tcp, long, unsigned long);
639 extern void print_ifindex(unsigned int);
641 extern int file_ioctl(struct tcb *, const unsigned int, long);
642 extern int fs_x_ioctl(struct tcb *, const unsigned int, long);
643 extern int loop_ioctl(struct tcb *, const unsigned int, long);
644 extern int ptp_ioctl(struct tcb *, const unsigned int, long);
645 extern int scsi_ioctl(struct tcb *, const unsigned int, long);
646 extern int sock_ioctl(struct tcb *, const unsigned int, long);
647 extern int term_ioctl(struct tcb *, const unsigned int, long);
648 extern int ubi_ioctl(struct tcb *, const unsigned int, long);
649 extern int uffdio_ioctl(struct tcb *, const unsigned int, long);
651 extern int tv_nz(const struct timeval *);
652 extern int tv_cmp(const struct timeval *, const struct timeval *);
653 extern double tv_float(const struct timeval *);
654 extern void tv_add(struct timeval *, const struct timeval *, const struct timeval *);
655 extern void tv_sub(struct timeval *, const struct timeval *, const struct timeval *);
656 extern void tv_mul(struct timeval *, const struct timeval *, int);
657 extern void tv_div(struct timeval *, const struct timeval *, int);
660 extern void unwind_init(void);
661 extern void unwind_tcb_init(struct tcb *tcp);
662 extern void unwind_tcb_fin(struct tcb *tcp);
663 extern void unwind_cache_invalidate(struct tcb* tcp);
664 extern void unwind_print_stacktrace(struct tcb* tcp);
665 extern void unwind_capture_stacktrace(struct tcb* tcp);
669 printflags(const struct xlat *x, unsigned int flags, const char *dflt)
671 return printflags64(x, flags, dflt);
675 printflags_long(const struct xlat *x, unsigned long flags, const char *dflt)
677 return printflags64(x, flags, dflt);
681 printxval64(const struct xlat *x, const uint64_t val, const char *dflt)
683 printxvals(val, dflt, x, NULL);
687 printxval(const struct xlat *x, const unsigned int val, const char *dflt)
689 printxvals(val, dflt, x, NULL);
693 printxval_long(const struct xlat *x, const unsigned long val, const char *dflt)
695 printxvals(val, dflt, x, NULL);
698 /* Strace log generation machinery.
700 * printing_tcp: tcb which has incomplete line being printed right now.
701 * NULL if last line has been completed ('\n'-terminated).
702 * printleader(tcp) examines it, finishes incomplete line if needed,
703 * the sets it to tcp.
704 * line_ended() clears printing_tcp and resets ->curcol = 0.
705 * tcp->curcol == 0 check is also used to detect completeness
706 * of last line, since in -ff mode just checking printing_tcp for NULL
709 * If you change this code, test log generation in both -f and -ff modes
711 * strace -oLOG -f[f] test/threaded_execve
712 * strace -oLOG -f[f] test/sigkill_rain
713 * strace -oLOG -f[f] -p "`pidof web_browser`"
715 extern struct tcb *printing_tcp;
716 extern void printleader(struct tcb *);
717 extern void line_ended(void);
718 extern void tabto(void);
719 extern void tprintf(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
720 extern void tprints(const char *str);
722 #if SUPPORTED_PERSONALITIES > 1
723 extern void set_personality(int personality);
724 extern unsigned current_personality;
726 # define set_personality(personality) ((void)0)
727 # define current_personality 0
730 #if SUPPORTED_PERSONALITIES == 1
731 # define current_wordsize PERSONALITY0_WORDSIZE
733 # if SUPPORTED_PERSONALITIES == 2 && PERSONALITY0_WORDSIZE == PERSONALITY1_WORDSIZE
734 # define current_wordsize PERSONALITY0_WORDSIZE
736 extern unsigned current_wordsize;
740 /* In many, many places we play fast and loose and use
741 * tprintf("%d", (int) tcp->u_arg[N]) to print fds, pids etc.
742 * We probably need to use widen_to_long() instead:
744 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
745 # define widen_to_long(v) (current_wordsize == 4 ? (long)(int32_t)(v) : (long)(v))
747 # define widen_to_long(v) ((long)(v))
751 * Zero-extend a signed integer type to unsigned long long.
753 #define zero_extend_signed_to_ull(v) \
754 (sizeof(v) == sizeof(short) ? (unsigned long long) (unsigned short) (v) : \
755 sizeof(v) == sizeof(int) ? (unsigned long long) (unsigned int) (v) : \
756 sizeof(v) == sizeof(long) ? (unsigned long long) (unsigned long) (v) : \
757 (unsigned long long) (v))
760 * Sign-extend an unsigned integer type to long long.
762 #define sign_extend_unsigned_to_ll(v) \
763 (sizeof(v) == sizeof(short) ? (long long) (short) (v) : \
764 sizeof(v) == sizeof(int) ? (long long) (int) (v) : \
765 sizeof(v) == sizeof(long) ? (long long) (long) (v) : \
768 extern const struct_sysent sysent0[];
769 extern const char *const errnoent0[];
770 extern const char *const signalent0[];
771 extern const struct_ioctlent ioctlent0[];
772 extern qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
773 #define qual_flags (qual_vec[current_personality])
775 #if SUPPORTED_PERSONALITIES > 1
776 extern const struct_sysent *sysent;
777 extern const char *const *errnoent;
778 extern const char *const *signalent;
779 extern const struct_ioctlent *ioctlent;
781 # define sysent sysent0
782 # define errnoent errnoent0
783 # define signalent signalent0
784 # define ioctlent ioctlent0
787 extern unsigned nsyscalls;
788 extern unsigned nerrnos;
789 extern unsigned nsignals;
790 extern unsigned nioctlents;
791 extern unsigned num_quals;
793 #ifdef IN_MPERS_BOOTSTRAP
794 /* Transform multi-line MPERS_PRINTER_DECL statements to one-liners. */
795 # define MPERS_PRINTER_DECL(type, name, ...) MPERS_PRINTER_DECL(type, name, __VA_ARGS__)
796 #else /* !IN_MPERS_BOOTSTRAP */
797 # if SUPPORTED_PERSONALITIES > 1
798 # include "printers.h"
800 # include "native_printer_decls.h"
802 # define MPERS_PRINTER_DECL(type, name, ...) type MPERS_FUNC_NAME(name)(__VA_ARGS__)
803 #endif /* !IN_MPERS_BOOTSTRAP */
806 * If you need non-NULL sysent[scno].sys_func, non-NULL sysent[scno].sys_name,
807 * and non-indirect sysent[scno].sys_flags.
809 #define SCNO_IS_VALID(scno) \
810 ((unsigned long)(scno) < nsyscalls \
811 && sysent[scno].sys_func \
812 && !(sysent[scno].sys_flags & TRACE_INDIRECT_SUBCALL))
814 /* Only ensures that sysent[scno] isn't out of range */
815 #define SCNO_IN_RANGE(scno) \
816 ((unsigned long)(scno) < nsyscalls)
818 #define MPERS_FUNC_NAME__(prefix, name) prefix ## name
819 #define MPERS_FUNC_NAME_(prefix, name) MPERS_FUNC_NAME__(prefix, name)
820 #define MPERS_FUNC_NAME(name) MPERS_FUNC_NAME_(MPERS_PREFIX, name)
822 #define SYS_FUNC_NAME(syscall_name) MPERS_FUNC_NAME(syscall_name)
824 #define SYS_FUNC(syscall_name) int SYS_FUNC_NAME(sys_ ## syscall_name)(struct tcb *tcp)
827 * The kernel used to define 64-bit types on 64-bit systems on a per-arch
828 * basis. Some architectures would use unsigned long and others would use
829 * unsigned long long. These types were exported as part of the
830 * kernel-userspace ABI and now must be maintained forever. This matches
831 * what the kernel exports for each architecture so we don't need to cast
832 * every printing of __u64 or __s64 to stdint types.
835 # define PRI__64 "ll"
836 #elif defined ALPHA || defined IA64 || defined MIPS || defined POWERPC
839 # define PRI__64 "ll"
842 #define PRI__d64 PRI__64"d"
843 #define PRI__u64 PRI__64"u"
844 #define PRI__x64 PRI__64"x"
846 #endif /* !STRACE_DEFS_H */