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>
46 /* Open-coding isprint(ch) et al proved more efficient than calling
47 * generalized libc interface. We don't *want* to do non-ASCII anyway.
49 /* #include <ctype.h> */
54 #include <asm/unistd.h>
56 #include "mpers_type.h"
57 #include "gcc_compat.h"
60 const char *strerror(int);
63 /* Some libc have stpcpy, some don't. Sigh...
64 * Roll our private implementation...
67 #define stpcpy strace_stpcpy
68 extern char *stpcpy(char *dst, const char *src);
72 # define offsetofend(type, member) \
73 (offsetof(type, member) + sizeof(((type *)NULL)->member))
76 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]) + MUST_BE_ARRAY(a))
80 # define MAX(a, b) (((a) > (b)) ? (a) : (b))
83 # define MIN(a, b) (((a) < (b)) ? (a) : (b))
85 #define CLAMP(val, min, max) MIN(MAX(min, val), max)
87 /* Glibc has an efficient macro for sigemptyset
88 * (it just does one or two assignments of 0 to internal vector of longs).
90 #if defined(__GLIBC__) && defined(__sigemptyset) && !defined(sigemptyset)
91 # define sigemptyset __sigemptyset
94 /* Configuration section */
95 #ifndef DEFAULT_STRLEN
96 /* default maximum # of bytes printed in `printstr', change with -s switch */
97 # define DEFAULT_STRLEN 32
99 #ifndef DEFAULT_ACOLUMN
100 # define DEFAULT_ACOLUMN 40 /* default alignment column for results */
103 * Maximum number of args to a syscall.
105 * Make sure that all entries in all syscallent.h files have nargs <= MAX_ARGS!
106 * linux/<ARCH>/syscallent*.h:
107 * all have nargs <= 6 except mips o32 which has nargs <= 7.
110 # ifdef LINUX_MIPSO32
116 /* default sorting method for call profiling */
117 #ifndef DEFAULT_SORTBY
118 # define DEFAULT_SORTBY "time"
121 * Experimental code using PTRACE_SEIZE can be enabled here.
122 * This needs Linux kernel 3.4.x or later to work.
125 /* To force NOMMU build, set to 1 */
126 #define NOMMU_SYSTEM 0
128 * Set to 1 to use speed-optimized vfprintf implementation.
129 * It results in strace using about 5% less CPU in user space
130 * (compared to glibc version).
131 * But strace spends a lot of time in kernel space,
132 * so overall it does not appear to be a significant win.
133 * Thus disabled by default.
135 #define USE_CUSTOM_PRINTF 0
138 # define ERESTARTSYS 512
140 #ifndef ERESTARTNOINTR
141 # define ERESTARTNOINTR 513
143 #ifndef ERESTARTNOHAND
144 # define ERESTARTNOHAND 514
146 #ifndef ERESTART_RESTARTBLOCK
147 # define ERESTART_RESTARTBLOCK 516
151 # define SUPPORTED_PERSONALITIES 3
152 # define PERSONALITY2_WORDSIZE 4
153 #elif defined AARCH64 \
154 || defined POWERPC64 \
159 # define SUPPORTED_PERSONALITIES 2
161 # define SUPPORTED_PERSONALITIES 1
164 #if defined TILE && defined __tilepro__
165 # define DEFAULT_PERSONALITY 1
167 # define DEFAULT_PERSONALITY 0
170 #define PERSONALITY0_WORDSIZE SIZEOF_LONG
171 #define PERSONALITY0_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
172 #define PERSONALITY0_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
174 #if SUPPORTED_PERSONALITIES > 1
175 # define PERSONALITY1_WORDSIZE 4
178 #if SUPPORTED_PERSONALITIES > 1 && defined HAVE_M32_MPERS
179 # define PERSONALITY1_INCLUDE_PRINTERS_DECLS "m32_printer_decls.h"
180 # define PERSONALITY1_INCLUDE_PRINTERS_DEFS "m32_printer_defs.h"
181 # define PERSONALITY1_INCLUDE_FUNCS "m32_funcs.h"
182 # define MPERS_m32_IOCTL_MACROS "ioctl_redefs1.h"
184 # define PERSONALITY1_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
185 # define PERSONALITY1_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
186 # define PERSONALITY1_INCLUDE_FUNCS "empty.h"
189 #if SUPPORTED_PERSONALITIES > 2 && defined HAVE_MX32_MPERS
190 # define PERSONALITY2_INCLUDE_FUNCS "mx32_funcs.h"
191 # define PERSONALITY2_INCLUDE_PRINTERS_DECLS "mx32_printer_decls.h"
192 # define PERSONALITY2_INCLUDE_PRINTERS_DEFS "mx32_printer_defs.h"
193 # define MPERS_mx32_IOCTL_MACROS "ioctl_redefs2.h"
195 # define PERSONALITY2_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
196 # define PERSONALITY2_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
197 # define PERSONALITY2_INCLUDE_FUNCS "empty.h"
200 typedef struct sysent {
205 const char *sys_name;
208 typedef struct ioctlent {
219 #if defined LINUX_MIPSN32 || defined X32
220 # define HAVE_STRUCT_TCB_EXT_ARG 1
222 # define HAVE_STRUCT_TCB_EXT_ARG 0
225 /* Trace Control Block */
227 int flags; /* See below for TCB_ values */
228 int pid; /* If 0, this tcb is free */
229 int qual_flg; /* qual_flags[scno] or DEFAULT_QUAL_FLAGS + RAW */
230 unsigned long u_error; /* Error code */
231 long scno; /* System call number */
232 long u_arg[MAX_ARGS]; /* System call arguments */
233 #if HAVE_STRUCT_TCB_EXT_ARG
234 long long ext_arg[MAX_ARGS];
235 long long u_lrval; /* long long return value */
237 long u_rval; /* Return value */
238 #if SUPPORTED_PERSONALITIES > 1
239 unsigned int currpers; /* Personality at the time of scno update */
241 int sys_func_rval; /* Syscall entry parser's return value */
242 int curcol; /* Output column for this process */
243 FILE *outf; /* Output file for this process */
244 const char *auxstr; /* Auxiliary info from syscall (see RVAL_STR) */
245 void *_priv_data; /* Private data for syscall decoding functions */
246 void (*_free_priv_data)(void *); /* Callback for freeing priv_data */
247 const struct_sysent *s_ent; /* sysent[scno] or dummy struct for bad scno */
248 const struct_sysent *s_prev_ent; /* for "resuming interrupted SYSCALL" msg */
249 struct fault_opts *fault_vec[SUPPORTED_PERSONALITIES];
250 struct timeval stime; /* System time usage as of last process wait */
251 struct timeval dtime; /* Delta for system time usage */
252 struct timeval etime; /* Syscall entry time */
255 struct UPT_info* libunwind_ui;
256 struct mmap_cache_t* mmap_cache;
257 unsigned int mmap_cache_size;
258 unsigned int mmap_cache_generation;
259 struct queue_t* queue;
264 /* We have attached to this process, but did not see it stopping yet */
265 #define TCB_STARTUP 0x01
266 #define TCB_IGNORE_ONE_SIGSTOP 0x02 /* Next SIGSTOP is to be ignored */
268 * Are we in system call entry or in syscall exit?
270 * This bit is set after all syscall entry processing is done.
271 * Therefore, this bit will be set when next ptrace stop occurs,
272 * which should be syscall exit stop. Other stops which are possible
273 * directly after syscall entry (death, ptrace event stop)
274 * are simpler and handled without calling trace_syscall(), therefore
275 * the places where TCB_INSYSCALL can be set but we aren't in syscall stop
276 * are limited to trace(), this condition is never observed in trace_syscall()
278 * The bit is cleared after all syscall exit processing is done.
280 * Use entering(tcp) / exiting(tcp) to check this bit to make code more readable.
282 #define TCB_INSYSCALL 0x04
283 #define TCB_ATTACHED 0x08 /* We attached to it already */
284 #define TCB_REPRINT 0x10 /* We should reprint this syscall on exit */
285 #define TCB_FILTERED 0x20 /* This system call has been filtered out */
286 #define TCB_FAULT_INJ 0x40 /* A syscall fault has been injected */
287 #define TCB_HIDE_LOG 0x80 /* We should hide everything (until execve) */
288 #define TCB_SKIP_DETACH_ON_FIRST_EXEC 0x100 /* -b execve should skip detach on first execve */
290 /* qualifier flags */
291 #define QUAL_TRACE 0x001 /* this system call should be traced */
292 #define QUAL_ABBREV 0x002 /* abbreviate the structures of this syscall */
293 #define QUAL_VERBOSE 0x004 /* decode the structures of this syscall */
294 #define QUAL_RAW 0x008 /* print all args in hex for this syscall */
295 #define QUAL_FAULT 0x010 /* fail this system call on purpose */
296 #define QUAL_SIGNAL 0x100 /* report events with this signal */
297 #define QUAL_READ 0x200 /* dump data read from this file descriptor */
298 #define QUAL_WRITE 0x400 /* dump data written to this file descriptor */
300 #define DEFAULT_QUAL_FLAGS (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
302 #define entering(tcp) (!((tcp)->flags & TCB_INSYSCALL))
303 #define exiting(tcp) ((tcp)->flags & TCB_INSYSCALL)
304 #define syserror(tcp) ((tcp)->u_error != 0)
305 #define verbose(tcp) ((tcp)->qual_flg & QUAL_VERBOSE)
306 #define abbrev(tcp) ((tcp)->qual_flg & QUAL_ABBREV)
307 #define filtered(tcp) ((tcp)->flags & TCB_FILTERED)
308 #define hide_log(tcp) ((tcp)->flags & TCB_HIDE_LOG)
312 extern const struct xlat addrfams[];
313 extern const struct xlat at_flags[];
314 extern const struct xlat dirent_types[];
315 extern const struct xlat evdev_abs[];
316 extern const struct xlat msg_flags[];
317 extern const struct xlat open_access_modes[];
318 extern const struct xlat open_mode_flags[];
319 extern const struct xlat resource_flags[];
320 extern const struct xlat socketlayers[];
321 extern const struct xlat whence_codes[];
323 /* Format of syscall return values */
324 #define RVAL_DECIMAL 000 /* decimal format */
325 #define RVAL_HEX 001 /* hex format */
326 #define RVAL_OCTAL 002 /* octal format */
327 #define RVAL_UDECIMAL 003 /* unsigned decimal format */
328 #if HAVE_STRUCT_TCB_EXT_ARG
329 # if 0 /* unused so far */
330 # define RVAL_LDECIMAL 004 /* long decimal format */
331 # define RVAL_LHEX 005 /* long hex format */
332 # define RVAL_LOCTAL 006 /* long octal format */
334 # define RVAL_LUDECIMAL 007 /* long unsigned decimal format */
335 #endif /* HAVE_STRUCT_TCB_EXT_ARG */
336 #define RVAL_FD 010 /* file descriptor */
337 #define RVAL_MASK 017 /* mask for these values */
339 #define RVAL_STR 020 /* Print `auxstr' field after return val */
340 #define RVAL_NONE 040 /* Print nothing */
342 #define RVAL_DECODED 0100 /* syscall decoding finished */
344 #define TRACE_FILE 001 /* Trace file-related syscalls. */
345 #define TRACE_IPC 002 /* Trace IPC-related syscalls. */
346 #define TRACE_NETWORK 004 /* Trace network-related syscalls. */
347 #define TRACE_PROCESS 010 /* Trace process-related syscalls. */
348 #define TRACE_SIGNAL 020 /* Trace signal-related syscalls. */
349 #define TRACE_DESC 040 /* Trace file descriptor-related syscalls. */
350 #define TRACE_MEMORY 0100 /* Trace memory mapping-related syscalls. */
351 #define SYSCALL_NEVER_FAILS 0200 /* Syscall is always successful. */
352 #define STACKTRACE_INVALIDATE_CACHE 0400 /* Trigger proc/maps cache updating */
353 #define STACKTRACE_CAPTURE_ON_ENTER 01000 /* Capture stacktrace on "entering" stage */
354 #define TRACE_INDIRECT_SUBCALL 02000 /* Syscall is an indirect socket/ipc subcall. */
356 #define IOCTL_NUMBER_UNKNOWN 0
357 #define IOCTL_NUMBER_HANDLED 1
358 #define IOCTL_NUMBER_STOP_LOOKUP 010
360 #define indirect_ipccall(tcp) (tcp->s_ent->sys_flags & TRACE_INDIRECT_SUBCALL)
362 #if defined(ARM) || defined(AARCH64) \
363 || defined(I386) || defined(X32) || defined(X86_64) \
367 || defined(MICROBLAZE) \
370 || defined(SH) || defined(SH64) \
371 || defined(SPARC) || defined(SPARC64) \
373 # define NEED_UID16_PARSERS 1
375 # define NEED_UID16_PARSERS 0
387 extern enum sock_proto get_proto_by_name(const char *);
400 extern cflag_t cflag;
401 extern bool debug_flag;
404 extern bool count_wallclock;
405 extern unsigned int qflag;
406 extern bool not_failing_only;
407 extern unsigned int show_fd_path;
408 /* are we filtering traces based on paths? */
409 extern const char **paths_selected;
410 #define tracing_paths (paths_selected != NULL)
411 extern unsigned xflag;
412 extern unsigned followfork;
414 /* if this is true do the stack trace for every system call */
415 extern bool stack_trace_enabled;
417 extern unsigned ptrace_setoptions;
418 extern unsigned max_strlen;
419 extern unsigned os_release;
420 #undef KERNEL_VERSION
421 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
423 void error_msg(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
424 void perror_msg(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
425 void error_msg_and_die(const char *fmt, ...)
426 ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
427 void error_msg_and_help(const char *fmt, ...)
428 ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
429 void perror_msg_and_die(const char *fmt, ...)
430 ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
431 void die_out_of_memory(void) ATTRIBUTE_NORETURN;
433 void *xmalloc(size_t size) ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1));
434 void *xcalloc(size_t nmemb, size_t size)
435 ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1, 2));
436 void *xreallocarray(void *ptr, size_t nmemb, size_t size)
437 ATTRIBUTE_ALLOC_SIZE((2, 3));
438 char *xstrdup(const char *str) ATTRIBUTE_MALLOC;
440 #if USE_CUSTOM_PRINTF
442 * See comment in vsprintf.c for allowed formats.
443 * Short version: %h[h]u, %zu, %tu are not allowed, use %[l[l]]u.
445 int strace_vfprintf(FILE *fp, const char *fmt, va_list args);
447 # define strace_vfprintf vfprintf
450 extern int read_int_from_file(const char *, int *);
452 extern void set_sortby(const char *);
453 extern void set_overhead(int);
454 extern void print_pc(struct tcb *);
455 extern int trace_syscall(struct tcb *);
456 extern void count_syscall(struct tcb *, const struct timeval *);
457 extern void call_summary(FILE *);
459 extern void clear_regs(void);
460 extern void get_regs(pid_t pid);
461 extern int get_scno(struct tcb *tcp);
463 * Convert syscall number to syscall name.
465 * @param scno Syscall number.
466 * @return String literal corresponding to the syscall number in case latter
467 * is valid; NULL otherwise.
469 extern const char *syscall_name(long scno);
470 extern const char *err_name(unsigned long err);
472 extern bool is_erestart(struct tcb *);
473 extern void temporarily_clear_syserror(struct tcb *);
474 extern void restore_cleared_syserror(struct tcb *);
476 extern void *get_tcb_priv_data(const struct tcb *);
477 extern int set_tcb_priv_data(struct tcb *, void *priv_data,
478 void (*free_priv_data)(void *));
479 extern void free_tcb_priv_data(struct tcb *);
481 static inline unsigned long get_tcb_priv_ulong(const struct tcb *tcp)
483 return (unsigned long) get_tcb_priv_data(tcp);
486 static inline int set_tcb_priv_ulong(struct tcb *tcp, unsigned long val)
488 return set_tcb_priv_data(tcp, (void *) val, 0);
491 extern int umoven(struct tcb *, long, unsigned int, void *);
492 #define umove(pid, addr, objp) \
493 umoven((pid), (addr), sizeof(*(objp)), (void *) (objp))
494 extern int umoven_or_printaddr(struct tcb *, long, unsigned int, void *);
495 #define umove_or_printaddr(pid, addr, objp) \
496 umoven_or_printaddr((pid), (addr), sizeof(*(objp)), (void *) (objp))
498 umoven_or_printaddr_ignore_syserror(struct tcb *tcp, const long addr,
499 const unsigned int len, void *our_addr);
500 extern int umovestr(struct tcb *, long, unsigned int, char *);
501 extern int upeek(int pid, long, long *);
502 extern int upoke(int pid, long, long);
505 print_array(struct tcb *tcp,
506 const unsigned long start_addr,
508 void *const elem_buf,
509 const size_t elem_size,
510 int (*const umoven_func)(struct tcb *,
514 bool (*const print_func)(struct tcb *,
518 void *const opaque_data);
520 #if defined ALPHA || defined IA64 || defined MIPS \
521 || defined SH || defined SPARC || defined SPARC64
522 # define HAVE_GETRVAL2
523 extern long getrval2(struct tcb *);
525 # undef HAVE_GETRVAL2
528 extern const char *signame(const int);
529 extern void pathtrace_select(const char *);
530 extern int pathtrace_match(struct tcb *);
531 extern int getfdpath(struct tcb *, int, char *, unsigned);
532 extern enum sock_proto getfdproto(struct tcb *, int);
534 extern const char *xlookup(const struct xlat *, const uint64_t);
535 extern const char *xlat_search(const struct xlat *, const size_t, const uint64_t);
537 extern unsigned long get_pagesize(void);
539 string_to_uint_ex(const char *str, char **endptr,
540 unsigned int max_val, const char *accepted_ending);
541 extern int string_to_uint(const char *str);
543 string_to_uint_upto(const char *const str, unsigned int max_val)
545 return string_to_uint_ex(str, NULL, max_val, NULL);
547 extern int next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits);
549 #define QUOTE_0_TERMINATED 0x01
550 #define QUOTE_OMIT_LEADING_TRAILING_QUOTES 0x02
551 #define QUOTE_OMIT_TRAILING_0 0x08
553 extern int string_quote(const char *, char *, unsigned int, unsigned int);
554 extern int print_quoted_string(const char *, unsigned int, unsigned int);
556 /* a refers to the lower numbered u_arg,
557 * b refers to the higher numbered u_arg
559 #ifdef WORDS_BIGENDIAN
560 # define LONG_LONG(a,b) \
561 ((long long)((unsigned long long)(unsigned)(b) | ((unsigned long long)(a)<<32)))
563 # define LONG_LONG(a,b) \
564 ((long long)((unsigned long long)(unsigned)(a) | ((unsigned long long)(b)<<32)))
566 extern int getllval(struct tcb *, unsigned long long *, int);
567 extern int printllval(struct tcb *, const char *, int)
568 ATTRIBUTE_FORMAT((printf, 2, 0));
570 extern void printaddr_ull(unsigned long long);
571 extern int printxvals(const uint64_t, const char *, const struct xlat *, ...)
573 extern int printxval_searchn(const struct xlat *xlat, size_t xlat_size,
574 uint64_t val, const char *dflt);
575 #define printxval_search(xlat__, val__, dflt__) \
576 printxval_searchn(xlat__, ARRAY_SIZE(xlat__), val__, dflt__)
577 extern long long getarg_ll(struct tcb *tcp, int argn);
578 extern unsigned long long getarg_ull(struct tcb *tcp, int argn);
579 extern int printargs(struct tcb *);
580 extern int printargs_u(struct tcb *);
581 extern int printargs_d(struct tcb *);
583 extern void addflags(const struct xlat *, uint64_t);
584 extern int printflags64(const struct xlat *, uint64_t, const char *);
585 extern const char *sprintflags(const char *, const struct xlat *, uint64_t);
586 extern const char *sprinttime(time_t);
587 extern void print_symbolic_mode_t(unsigned int);
588 extern void print_numeric_umode_t(unsigned short);
589 extern void print_numeric_long_umask(unsigned long);
590 extern void dumpiov_in_msghdr(struct tcb *, long, unsigned long);
591 extern void dumpiov_in_mmsghdr(struct tcb *, long);
592 extern void dumpiov_upto(struct tcb *, int, long, unsigned long);
593 #define dumpiov(tcp, len, addr) \
594 dumpiov_upto((tcp), (len), (addr), (unsigned long) -1L)
595 extern void dumpstr(struct tcb *, long, int);
596 extern void printstr_ex(struct tcb *, long addr, long len,
597 unsigned int user_style);
598 extern bool printnum_short(struct tcb *, long, const char *)
599 ATTRIBUTE_FORMAT((printf, 3, 0));
600 extern bool printnum_int(struct tcb *, long, const char *)
601 ATTRIBUTE_FORMAT((printf, 3, 0));
602 extern bool printnum_int64(struct tcb *, long, const char *)
603 ATTRIBUTE_FORMAT((printf, 3, 0));
605 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
606 extern bool printnum_long_int(struct tcb *, long, const char *, const char *)
607 ATTRIBUTE_FORMAT((printf, 3, 0))
608 ATTRIBUTE_FORMAT((printf, 4, 0));
609 # define printnum_slong(tcp, addr) \
610 printnum_long_int((tcp), (addr), "%" PRId64, "%d")
611 # define printnum_ulong(tcp, addr) \
612 printnum_long_int((tcp), (addr), "%" PRIu64, "%u")
613 # define printnum_ptr(tcp, addr) \
614 printnum_long_int((tcp), (addr), "%#" PRIx64, "%#x")
615 #elif SIZEOF_LONG > 4
616 # define printnum_slong(tcp, addr) \
617 printnum_int64((tcp), (addr), "%" PRId64)
618 # define printnum_ulong(tcp, addr) \
619 printnum_int64((tcp), (addr), "%" PRIu64)
620 # define printnum_ptr(tcp, addr) \
621 printnum_int64((tcp), (addr), "%#" PRIx64)
623 # define printnum_slong(tcp, addr) \
624 printnum_int((tcp), (addr), "%d")
625 # define printnum_ulong(tcp, addr) \
626 printnum_int((tcp), (addr), "%u")
627 # define printnum_ptr(tcp, addr) \
628 printnum_int((tcp), (addr), "%#x")
631 extern bool printpair_int(struct tcb *, long, const char *)
632 ATTRIBUTE_FORMAT((printf, 3, 0));
633 extern bool printpair_int64(struct tcb *, long, const char *)
634 ATTRIBUTE_FORMAT((printf, 3, 0));
635 extern void printpath(struct tcb *, long);
636 extern void printpathn(struct tcb *, long, unsigned int);
637 #define TIMESPEC_TEXT_BUFSIZE \
638 (sizeof(intmax_t)*3 * 2 + sizeof("{tv_sec=%jd, tv_nsec=%jd}"))
639 extern void printfd(struct tcb *, int);
640 extern void print_sockaddr(struct tcb *tcp, const void *, int);
641 extern bool print_sockaddr_by_inode(const unsigned long, const enum sock_proto);
642 extern bool print_sockaddr_by_inode_cached(const unsigned long);
643 extern void print_dirfd(struct tcb *, int);
644 extern int decode_sockaddr(struct tcb *, long, int);
645 extern void printuid(const char *, const unsigned int);
646 extern void print_sigset_addr_len(struct tcb *, long, long);
647 extern const char *sprintsigmask_n(const char *, const void *, unsigned int);
648 #define tprintsigmask_addr(prefix, mask) \
649 tprints(sprintsigmask_n((prefix), (mask), sizeof(mask)))
650 extern void printsignal(int);
651 extern void tprint_iov(struct tcb *, unsigned long, unsigned long, enum iov_decode);
652 extern void tprint_iov_upto(struct tcb *, unsigned long, unsigned long,
653 enum iov_decode, unsigned long);
654 extern void decode_netlink(struct tcb *, unsigned long, unsigned long);
655 extern void tprint_open_modes(unsigned int);
656 extern const char *sprint_open_modes(unsigned int);
657 extern void print_seccomp_filter(struct tcb *, unsigned long);
658 extern void print_seccomp_fprog(struct tcb *, unsigned long, unsigned short);
661 extern void print_struct_stat(struct tcb *tcp, const struct strace_stat *const st);
663 struct strace_statfs;
664 extern void print_struct_statfs(struct tcb *tcp, long);
665 extern void print_struct_statfs64(struct tcb *tcp, long, unsigned long);
667 extern void print_ifindex(unsigned int);
670 extern struct number_set read_set;
671 extern struct number_set write_set;
672 extern struct number_set signal_set;
674 extern bool is_number_in_set(unsigned int number, const struct number_set *);
675 extern void qualify(const char *);
676 extern unsigned int qual_flags(const unsigned int);
678 extern int dm_ioctl(struct tcb *, const unsigned int, long);
679 extern int file_ioctl(struct tcb *, const unsigned int, long);
680 extern int fs_x_ioctl(struct tcb *, const unsigned int, long);
681 extern int loop_ioctl(struct tcb *, const unsigned int, long);
682 extern int ptp_ioctl(struct tcb *, const unsigned int, long);
683 extern int scsi_ioctl(struct tcb *, const unsigned int, long);
684 extern int sock_ioctl(struct tcb *, const unsigned int, long);
685 extern int term_ioctl(struct tcb *, const unsigned int, long);
686 extern int ubi_ioctl(struct tcb *, const unsigned int, long);
687 extern int uffdio_ioctl(struct tcb *, const unsigned int, long);
689 extern int tv_nz(const struct timeval *);
690 extern int tv_cmp(const struct timeval *, const struct timeval *);
691 extern double tv_float(const struct timeval *);
692 extern void tv_add(struct timeval *, const struct timeval *, const struct timeval *);
693 extern void tv_sub(struct timeval *, const struct timeval *, const struct timeval *);
694 extern void tv_mul(struct timeval *, const struct timeval *, int);
695 extern void tv_div(struct timeval *, const struct timeval *, int);
698 extern void unwind_init(void);
699 extern void unwind_tcb_init(struct tcb *tcp);
700 extern void unwind_tcb_fin(struct tcb *tcp);
701 extern void unwind_cache_invalidate(struct tcb* tcp);
702 extern void unwind_print_stacktrace(struct tcb* tcp);
703 extern void unwind_capture_stacktrace(struct tcb* tcp);
707 printaddr(unsigned long addr)
713 printstr(struct tcb *tcp, long addr, long len)
715 printstr_ex(tcp, addr, len, 0);
719 printflags(const struct xlat *x, unsigned int flags, const char *dflt)
721 return printflags64(x, flags, dflt);
725 printflags_long(const struct xlat *x, unsigned long flags, const char *dflt)
727 return printflags64(x, flags, dflt);
731 printxval64(const struct xlat *x, const uint64_t val, const char *dflt)
733 return printxvals(val, dflt, x, NULL);
737 printxval(const struct xlat *x, const unsigned int val, const char *dflt)
739 return printxvals(val, dflt, x, NULL);
743 printxval_long(const struct xlat *x, const unsigned long val, const char *dflt)
745 return printxvals(val, dflt, x, NULL);
753 extern void print_timeval32_t(const timeval32_t *);
754 extern void printrusage32(struct tcb *, long);
755 extern const char *sprint_timeval32(struct tcb *tcp, long);
756 extern void print_timeval32(struct tcb *tcp, long);
757 extern void print_timeval32_pair(struct tcb *tcp, long);
758 extern void print_itimerval32(struct tcb *tcp, long);
761 /* Strace log generation machinery.
763 * printing_tcp: tcb which has incomplete line being printed right now.
764 * NULL if last line has been completed ('\n'-terminated).
765 * printleader(tcp) examines it, finishes incomplete line if needed,
766 * the sets it to tcp.
767 * line_ended() clears printing_tcp and resets ->curcol = 0.
768 * tcp->curcol == 0 check is also used to detect completeness
769 * of last line, since in -ff mode just checking printing_tcp for NULL
772 * If you change this code, test log generation in both -f and -ff modes
774 * strace -oLOG -f[f] test/threaded_execve
775 * strace -oLOG -f[f] test/sigkill_rain
776 * strace -oLOG -f[f] -p "`pidof web_browser`"
778 extern struct tcb *printing_tcp;
779 extern void printleader(struct tcb *);
780 extern void line_ended(void);
781 extern void tabto(void);
782 extern void tprintf(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
783 extern void tprints(const char *str);
785 #if SUPPORTED_PERSONALITIES > 1
786 extern void set_personality(int personality);
787 extern unsigned current_personality;
789 # define set_personality(personality) ((void)0)
790 # define current_personality 0
793 #if SUPPORTED_PERSONALITIES == 1
794 # define current_wordsize PERSONALITY0_WORDSIZE
796 # if SUPPORTED_PERSONALITIES == 2 && PERSONALITY0_WORDSIZE == PERSONALITY1_WORDSIZE
797 # define current_wordsize PERSONALITY0_WORDSIZE
799 extern unsigned current_wordsize;
803 /* In many, many places we play fast and loose and use
804 * tprintf("%d", (int) tcp->u_arg[N]) to print fds, pids etc.
805 * We probably need to use widen_to_long() instead:
807 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
808 # define widen_to_long(v) (current_wordsize == 4 ? (long)(int32_t)(v) : (long)(v))
810 # define widen_to_long(v) ((long)(v))
813 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
814 # define widen_to_ulong(v) \
815 (current_wordsize == 4 ? (unsigned long) (uint32_t) (v) : \
818 # define widen_to_ulong(v) ((unsigned long)(v))
822 * Zero-extend a signed integer type to unsigned long long.
824 #define zero_extend_signed_to_ull(v) \
825 (sizeof(v) == sizeof(char) ? (unsigned long long) (unsigned char) (v) : \
826 sizeof(v) == sizeof(short) ? (unsigned long long) (unsigned short) (v) : \
827 sizeof(v) == sizeof(int) ? (unsigned long long) (unsigned int) (v) : \
828 sizeof(v) == sizeof(long) ? (unsigned long long) (unsigned long) (v) : \
829 (unsigned long long) (v))
832 * Sign-extend an unsigned integer type to long long.
834 #define sign_extend_unsigned_to_ll(v) \
835 (sizeof(v) == sizeof(char) ? (long long) (char) (v) : \
836 sizeof(v) == sizeof(short) ? (long long) (short) (v) : \
837 sizeof(v) == sizeof(int) ? (long long) (int) (v) : \
838 sizeof(v) == sizeof(long) ? (long long) (long) (v) : \
841 extern const struct_sysent sysent0[];
842 extern const char *const errnoent0[];
843 extern const char *const signalent0[];
844 extern const struct_ioctlent ioctlent0[];
846 #if SUPPORTED_PERSONALITIES > 1
847 extern const struct_sysent *sysent;
848 extern const char *const *errnoent;
849 extern const char *const *signalent;
850 extern const struct_ioctlent *ioctlent;
852 # define sysent sysent0
853 # define errnoent errnoent0
854 # define signalent signalent0
855 # define ioctlent ioctlent0
858 extern unsigned nsyscalls;
859 extern unsigned nerrnos;
860 extern unsigned nsignals;
861 extern unsigned nioctlents;
863 extern const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES];
864 extern const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES];
865 extern struct fault_opts *fault_vec[SUPPORTED_PERSONALITIES];
867 #ifdef IN_MPERS_BOOTSTRAP
868 /* Transform multi-line MPERS_PRINTER_DECL statements to one-liners. */
869 # define MPERS_PRINTER_DECL(type, name, ...) MPERS_PRINTER_DECL(type, name, __VA_ARGS__)
870 #else /* !IN_MPERS_BOOTSTRAP */
871 # if SUPPORTED_PERSONALITIES > 1
872 # include "printers.h"
874 # include "native_printer_decls.h"
876 # define MPERS_PRINTER_DECL(type, name, ...) type MPERS_FUNC_NAME(name)(__VA_ARGS__)
877 #endif /* !IN_MPERS_BOOTSTRAP */
880 * If you need non-NULL sysent[scno].sys_func, non-NULL sysent[scno].sys_name,
881 * and non-indirect sysent[scno].sys_flags.
883 #define SCNO_IS_VALID(scno) \
884 ((unsigned long)(scno) < nsyscalls \
885 && sysent[scno].sys_func \
886 && !(sysent[scno].sys_flags & TRACE_INDIRECT_SUBCALL))
888 /* Only ensures that sysent[scno] isn't out of range */
889 #define SCNO_IN_RANGE(scno) \
890 ((unsigned long)(scno) < nsyscalls)
892 #define MPERS_FUNC_NAME__(prefix, name) prefix ## name
893 #define MPERS_FUNC_NAME_(prefix, name) MPERS_FUNC_NAME__(prefix, name)
894 #define MPERS_FUNC_NAME(name) MPERS_FUNC_NAME_(MPERS_PREFIX, name)
896 #define SYS_FUNC_NAME(syscall_name) MPERS_FUNC_NAME(syscall_name)
898 #define SYS_FUNC(syscall_name) int SYS_FUNC_NAME(sys_ ## syscall_name)(struct tcb *tcp)
901 * The kernel used to define 64-bit types on 64-bit systems on a per-arch
902 * basis. Some architectures would use unsigned long and others would use
903 * unsigned long long. These types were exported as part of the
904 * kernel-userspace ABI and now must be maintained forever. This matches
905 * what the kernel exports for each architecture so we don't need to cast
906 * every printing of __u64 or __s64 to stdint types.
909 # define PRI__64 "ll"
910 #elif defined ALPHA || defined IA64 || defined MIPS || defined POWERPC
913 # define PRI__64 "ll"
916 #define PRI__d64 PRI__64"d"
917 #define PRI__u64 PRI__64"u"
918 #define PRI__x64 PRI__64"x"
920 #endif /* !STRACE_DEFS_H */