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 {
213 #if defined LINUX_MIPSN32 || defined X32
214 # define HAVE_STRUCT_TCB_EXT_ARG 1
216 # define HAVE_STRUCT_TCB_EXT_ARG 0
219 /* Trace Control Block */
221 int flags; /* See below for TCB_ values */
222 int pid; /* If 0, this tcb is free */
223 int qual_flg; /* qual_flags[scno] or DEFAULT_QUAL_FLAGS + RAW */
224 unsigned long u_error; /* Error code */
225 long scno; /* System call number */
226 long u_arg[MAX_ARGS]; /* System call arguments */
227 #if HAVE_STRUCT_TCB_EXT_ARG
228 long long ext_arg[MAX_ARGS];
229 long long u_lrval; /* long long return value */
231 long u_rval; /* Return value */
232 #if SUPPORTED_PERSONALITIES > 1
233 unsigned int currpers; /* Personality at the time of scno update */
235 int sys_func_rval; /* Syscall entry parser's return value */
236 int curcol; /* Output column for this process */
237 FILE *outf; /* Output file for this process */
238 const char *auxstr; /* Auxiliary info from syscall (see RVAL_STR) */
239 void *_priv_data; /* Private data for syscall decoding functions */
240 void (*_free_priv_data)(void *); /* Callback for freeing priv_data */
241 const struct_sysent *s_ent; /* sysent[scno] or dummy struct for bad scno */
242 const struct_sysent *s_prev_ent; /* for "resuming interrupted SYSCALL" msg */
243 struct timeval stime; /* System time usage as of last process wait */
244 struct timeval dtime; /* Delta for system time usage */
245 struct timeval etime; /* Syscall entry time */
248 struct UPT_info* libunwind_ui;
249 struct mmap_cache_t* mmap_cache;
250 unsigned int mmap_cache_size;
251 unsigned int mmap_cache_generation;
252 struct queue_t* queue;
257 /* We have attached to this process, but did not see it stopping yet */
258 #define TCB_STARTUP 0x01
259 #define TCB_IGNORE_ONE_SIGSTOP 0x02 /* Next SIGSTOP is to be ignored */
261 * Are we in system call entry or in syscall exit?
263 * This bit is set after all syscall entry processing is done.
264 * Therefore, this bit will be set when next ptrace stop occurs,
265 * which should be syscall exit stop. Other stops which are possible
266 * directly after syscall entry (death, ptrace event stop)
267 * are simpler and handled without calling trace_syscall(), therefore
268 * the places where TCB_INSYSCALL can be set but we aren't in syscall stop
269 * are limited to trace(), this condition is never observed in trace_syscall()
271 * The bit is cleared after all syscall exit processing is done.
273 * Use entering(tcp) / exiting(tcp) to check this bit to make code more readable.
275 #define TCB_INSYSCALL 0x04
276 #define TCB_ATTACHED 0x08 /* We attached to it already */
277 #define TCB_REPRINT 0x10 /* We should reprint this syscall on exit */
278 #define TCB_FILTERED 0x20 /* This system call has been filtered out */
280 /* qualifier flags */
281 #define QUAL_TRACE 0x001 /* this system call should be traced */
282 #define QUAL_ABBREV 0x002 /* abbreviate the structures of this syscall */
283 #define QUAL_VERBOSE 0x004 /* decode the structures of this syscall */
284 #define QUAL_RAW 0x008 /* print all args in hex for this syscall */
285 #define QUAL_SIGNAL 0x010 /* report events with this signal */
286 #define QUAL_READ 0x020 /* dump data read on this file descriptor */
287 #define QUAL_WRITE 0x040 /* dump data written to this file descriptor */
288 typedef uint8_t qualbits_t;
290 #define DEFAULT_QUAL_FLAGS (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
292 #define entering(tcp) (!((tcp)->flags & TCB_INSYSCALL))
293 #define exiting(tcp) ((tcp)->flags & TCB_INSYSCALL)
294 #define syserror(tcp) ((tcp)->u_error != 0)
295 #define verbose(tcp) ((tcp)->qual_flg & QUAL_VERBOSE)
296 #define abbrev(tcp) ((tcp)->qual_flg & QUAL_ABBREV)
297 #define filtered(tcp) ((tcp)->flags & TCB_FILTERED)
301 extern const struct xlat addrfams[];
302 extern const struct xlat at_flags[];
303 extern const struct xlat dirent_types[];
304 extern const struct xlat evdev_abs[];
305 extern const struct xlat msg_flags[];
306 extern const struct xlat open_access_modes[];
307 extern const struct xlat open_mode_flags[];
308 extern const struct xlat resource_flags[];
309 extern const struct xlat socketlayers[];
310 extern const struct xlat whence_codes[];
312 /* Format of syscall return values */
313 #define RVAL_DECIMAL 000 /* decimal format */
314 #define RVAL_HEX 001 /* hex format */
315 #define RVAL_OCTAL 002 /* octal format */
316 #define RVAL_UDECIMAL 003 /* unsigned decimal format */
317 #if HAVE_STRUCT_TCB_EXT_ARG
318 # if 0 /* unused so far */
319 # define RVAL_LDECIMAL 004 /* long decimal format */
320 # define RVAL_LHEX 005 /* long hex format */
321 # define RVAL_LOCTAL 006 /* long octal format */
323 # define RVAL_LUDECIMAL 007 /* long unsigned decimal format */
324 #endif /* HAVE_STRUCT_TCB_EXT_ARG */
325 #define RVAL_FD 010 /* file descriptor */
326 #define RVAL_MASK 017 /* mask for these values */
328 #define RVAL_STR 020 /* Print `auxstr' field after return val */
329 #define RVAL_NONE 040 /* Print nothing */
331 #define RVAL_DECODED 0100 /* syscall decoding finished */
333 #define TRACE_FILE 001 /* Trace file-related syscalls. */
334 #define TRACE_IPC 002 /* Trace IPC-related syscalls. */
335 #define TRACE_NETWORK 004 /* Trace network-related syscalls. */
336 #define TRACE_PROCESS 010 /* Trace process-related syscalls. */
337 #define TRACE_SIGNAL 020 /* Trace signal-related syscalls. */
338 #define TRACE_DESC 040 /* Trace file descriptor-related syscalls. */
339 #define TRACE_MEMORY 0100 /* Trace memory mapping-related syscalls. */
340 #define SYSCALL_NEVER_FAILS 0200 /* Syscall is always successful. */
341 #define STACKTRACE_INVALIDATE_CACHE 0400 /* Trigger proc/maps cache updating */
342 #define STACKTRACE_CAPTURE_ON_ENTER 01000 /* Capture stacktrace on "entering" stage */
343 #define TRACE_INDIRECT_SUBCALL 02000 /* Syscall is an indirect socket/ipc subcall. */
345 #define IOCTL_NUMBER_UNKNOWN 0
346 #define IOCTL_NUMBER_HANDLED 1
347 #define IOCTL_NUMBER_STOP_LOOKUP 010
349 #define indirect_ipccall(tcp) (tcp->s_ent->sys_flags & TRACE_INDIRECT_SUBCALL)
351 #if defined(ARM) || defined(AARCH64) \
352 || defined(I386) || defined(X32) || defined(X86_64) \
356 || defined(MICROBLAZE) \
359 || defined(SH) || defined(SH64) \
360 || defined(SPARC) || defined(SPARC64) \
362 # define NEED_UID16_PARSERS 1
364 # define NEED_UID16_PARSERS 0
376 extern enum sock_proto get_proto_by_name(const char *);
389 extern cflag_t cflag;
390 extern bool debug_flag;
393 extern bool count_wallclock;
394 extern unsigned int qflag;
395 extern bool not_failing_only;
396 extern unsigned int show_fd_path;
397 extern bool hide_log_until_execve;
398 /* are we filtering traces based on paths? */
399 extern const char **paths_selected;
400 #define tracing_paths (paths_selected != NULL)
401 extern unsigned xflag;
402 extern unsigned followfork;
404 /* if this is true do the stack trace for every system call */
405 extern bool stack_trace_enabled;
407 extern unsigned ptrace_setoptions;
408 extern unsigned max_strlen;
409 extern unsigned os_release;
410 #undef KERNEL_VERSION
411 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
413 void error_msg(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
414 void perror_msg(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
415 void error_msg_and_die(const char *fmt, ...)
416 ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
417 void error_msg_and_help(const char *fmt, ...)
418 ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
419 void perror_msg_and_die(const char *fmt, ...)
420 ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
421 void die_out_of_memory(void) ATTRIBUTE_NORETURN;
423 void *xmalloc(size_t size) ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1));
424 void *xcalloc(size_t nmemb, size_t size)
425 ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1, 2));
426 void *xreallocarray(void *ptr, size_t nmemb, size_t size)
427 ATTRIBUTE_ALLOC_SIZE((2, 3));
428 char *xstrdup(const char *str) ATTRIBUTE_MALLOC;
430 #if USE_CUSTOM_PRINTF
432 * See comment in vsprintf.c for allowed formats.
433 * Short version: %h[h]u, %zu, %tu are not allowed, use %[l[l]]u.
435 int strace_vfprintf(FILE *fp, const char *fmt, va_list args);
437 # define strace_vfprintf vfprintf
440 extern int read_int_from_file(const char *, int *);
442 extern void set_sortby(const char *);
443 extern void set_overhead(int);
444 extern void qualify(const char *);
445 extern void print_pc(struct tcb *);
446 extern int trace_syscall(struct tcb *);
447 extern void count_syscall(struct tcb *, const struct timeval *);
448 extern void call_summary(FILE *);
450 extern void clear_regs(void);
451 extern void get_regs(pid_t pid);
452 extern int get_scno(struct tcb *tcp);
453 extern const char *syscall_name(long scno);
454 extern const char *err_name(unsigned long err);
456 extern bool is_erestart(struct tcb *);
457 extern void temporarily_clear_syserror(struct tcb *);
458 extern void restore_cleared_syserror(struct tcb *);
460 extern void *get_tcb_priv_data(const struct tcb *);
461 extern int set_tcb_priv_data(struct tcb *, void *priv_data,
462 void (*free_priv_data)(void *));
463 extern void free_tcb_priv_data(struct tcb *);
465 static inline unsigned long get_tcb_priv_ulong(const struct tcb *tcp)
467 return (unsigned long) get_tcb_priv_data(tcp);
470 static inline int set_tcb_priv_ulong(struct tcb *tcp, unsigned long val)
472 return set_tcb_priv_data(tcp, (void *) val, 0);
475 extern int umoven(struct tcb *, long, unsigned int, void *);
476 #define umove(pid, addr, objp) \
477 umoven((pid), (addr), sizeof(*(objp)), (void *) (objp))
478 extern int umoven_or_printaddr(struct tcb *, long, unsigned int, void *);
479 #define umove_or_printaddr(pid, addr, objp) \
480 umoven_or_printaddr((pid), (addr), sizeof(*(objp)), (void *) (objp))
482 umoven_or_printaddr_ignore_syserror(struct tcb *tcp, const long addr,
483 const unsigned int len, void *our_addr);
484 extern int umovestr(struct tcb *, long, unsigned int, char *);
485 extern int upeek(int pid, long, long *);
486 extern int upoke(int pid, long, long);
489 print_array(struct tcb *tcp,
490 const unsigned long start_addr,
492 void *const elem_buf,
493 const size_t elem_size,
494 int (*const umoven_func)(struct tcb *,
498 bool (*const print_func)(struct tcb *,
502 void *const opaque_data);
504 #if defined ALPHA || defined IA64 || defined MIPS \
505 || defined SH || defined SPARC || defined SPARC64
506 # define HAVE_GETRVAL2
507 extern long getrval2(struct tcb *);
509 # undef HAVE_GETRVAL2
512 extern const char *signame(const int);
513 extern void pathtrace_select(const char *);
514 extern int pathtrace_match(struct tcb *);
515 extern int getfdpath(struct tcb *, int, char *, unsigned);
516 extern enum sock_proto getfdproto(struct tcb *, int);
518 extern const char *xlookup(const struct xlat *, const uint64_t);
519 extern const char *xlat_search(const struct xlat *, const size_t, const uint64_t);
521 extern unsigned long get_pagesize(void);
522 extern int string_to_uint(const char *str);
523 extern int next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits);
525 #define QUOTE_0_TERMINATED 0x01
526 #define QUOTE_OMIT_LEADING_TRAILING_QUOTES 0x02
527 #define QUOTE_OMIT_TRAILING_0 0x08
529 extern int string_quote(const char *, char *, unsigned int, unsigned int);
530 extern int print_quoted_string(const char *, unsigned int, unsigned int);
532 /* a refers to the lower numbered u_arg,
533 * b refers to the higher numbered u_arg
535 #ifdef WORDS_BIGENDIAN
536 # define LONG_LONG(a,b) \
537 ((long long)((unsigned long long)(unsigned)(b) | ((unsigned long long)(a)<<32)))
539 # define LONG_LONG(a,b) \
540 ((long long)((unsigned long long)(unsigned)(a) | ((unsigned long long)(b)<<32)))
542 extern int getllval(struct tcb *, unsigned long long *, int);
543 extern int printllval(struct tcb *, const char *, int)
544 ATTRIBUTE_FORMAT((printf, 2, 0));
546 extern void printaddr_ull(unsigned long long);
547 extern void printxvals(const uint64_t, const char *, const struct xlat *, ...)
549 extern void printxval_searchn(const struct xlat *xlat, size_t xlat_size,
550 uint64_t val, const char *dflt);
551 #define printxval_search(xlat__, val__, dflt__) \
552 printxval_searchn(xlat__, ARRAY_SIZE(xlat__), val__, dflt__)
553 extern long long getarg_ll(struct tcb *tcp, int argn);
554 extern unsigned long long getarg_ull(struct tcb *tcp, int argn);
555 extern int printargs(struct tcb *);
556 extern int printargs_u(struct tcb *);
557 extern int printargs_d(struct tcb *);
559 extern void addflags(const struct xlat *, uint64_t);
560 extern int printflags64(const struct xlat *, uint64_t, const char *);
561 extern const char *sprintflags(const char *, const struct xlat *, uint64_t);
562 extern const char *sprinttime(time_t);
563 extern void print_symbolic_mode_t(unsigned int);
564 extern void print_numeric_umode_t(unsigned short);
565 extern void print_numeric_long_umask(unsigned long);
566 extern void dumpiov_in_msghdr(struct tcb *, long, unsigned long);
567 extern void dumpiov_in_mmsghdr(struct tcb *, long);
568 extern void dumpiov_upto(struct tcb *, int, long, unsigned long);
569 #define dumpiov(tcp, len, addr) \
570 dumpiov_upto((tcp), (len), (addr), (unsigned long) -1L)
571 extern void dumpstr(struct tcb *, long, int);
572 extern void printstr_ex(struct tcb *, long addr, long len,
573 unsigned int user_style);
574 extern bool printnum_short(struct tcb *, long, const char *)
575 ATTRIBUTE_FORMAT((printf, 3, 0));
576 extern bool printnum_int(struct tcb *, long, const char *)
577 ATTRIBUTE_FORMAT((printf, 3, 0));
578 extern bool printnum_int64(struct tcb *, long, const char *)
579 ATTRIBUTE_FORMAT((printf, 3, 0));
581 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
582 extern bool printnum_long_int(struct tcb *, long, const char *, const char *)
583 ATTRIBUTE_FORMAT((printf, 3, 0))
584 ATTRIBUTE_FORMAT((printf, 4, 0));
585 # define printnum_slong(tcp, addr) \
586 printnum_long_int((tcp), (addr), "%" PRId64, "%d")
587 # define printnum_ulong(tcp, addr) \
588 printnum_long_int((tcp), (addr), "%" PRIu64, "%u")
589 # define printnum_ptr(tcp, addr) \
590 printnum_long_int((tcp), (addr), "%#" PRIx64, "%#x")
591 #elif SIZEOF_LONG > 4
592 # define printnum_slong(tcp, addr) \
593 printnum_int64((tcp), (addr), "%" PRId64)
594 # define printnum_ulong(tcp, addr) \
595 printnum_int64((tcp), (addr), "%" PRIu64)
596 # define printnum_ptr(tcp, addr) \
597 printnum_int64((tcp), (addr), "%#" PRIx64)
599 # define printnum_slong(tcp, addr) \
600 printnum_int((tcp), (addr), "%d")
601 # define printnum_ulong(tcp, addr) \
602 printnum_int((tcp), (addr), "%u")
603 # define printnum_ptr(tcp, addr) \
604 printnum_int((tcp), (addr), "%#x")
607 extern bool printpair_int(struct tcb *, long, const char *)
608 ATTRIBUTE_FORMAT((printf, 3, 0));
609 extern bool printpair_int64(struct tcb *, long, const char *)
610 ATTRIBUTE_FORMAT((printf, 3, 0));
611 extern void printpath(struct tcb *, long);
612 extern void printpathn(struct tcb *, long, unsigned int);
613 #define TIMESPEC_TEXT_BUFSIZE \
614 (sizeof(intmax_t)*3 * 2 + sizeof("{tv_sec=%jd, tv_nsec=%jd}"))
615 extern void printfd(struct tcb *, int);
616 extern void print_sockaddr(struct tcb *tcp, const void *, int);
617 extern bool print_sockaddr_by_inode(const unsigned long, const enum sock_proto);
618 extern bool print_sockaddr_by_inode_cached(const unsigned long);
619 extern void print_dirfd(struct tcb *, int);
620 extern int decode_sockaddr(struct tcb *, long, int);
622 extern void printrusage32(struct tcb *, long);
623 extern const char *sprint_timeval32(struct tcb *tcp, long);
624 extern void print_timeval32(struct tcb *tcp, long);
625 extern void print_timeval32_pair(struct tcb *tcp, long);
626 extern void print_itimerval32(struct tcb *tcp, long);
628 extern void printuid(const char *, const unsigned int);
629 extern void print_sigset_addr_len(struct tcb *, long, long);
630 extern const char *sprintsigmask_n(const char *, const void *, unsigned int);
631 #define tprintsigmask_addr(prefix, mask) \
632 tprints(sprintsigmask_n((prefix), (mask), sizeof(mask)))
633 extern void printsignal(int);
634 extern void tprint_iov(struct tcb *, unsigned long, unsigned long, enum iov_decode);
635 extern void tprint_iov_upto(struct tcb *, unsigned long, unsigned long,
636 enum iov_decode, unsigned long);
637 extern void decode_netlink(struct tcb *, unsigned long, unsigned long);
638 extern void tprint_open_modes(unsigned int);
639 extern const char *sprint_open_modes(unsigned int);
640 extern void print_seccomp_filter(struct tcb *, unsigned long);
641 extern void print_seccomp_fprog(struct tcb *, unsigned long, unsigned short);
644 extern void print_struct_stat(struct tcb *tcp, const struct strace_stat *const st);
646 struct strace_statfs;
647 extern void print_struct_statfs(struct tcb *tcp, long);
648 extern void print_struct_statfs64(struct tcb *tcp, long, unsigned long);
650 extern void print_ifindex(unsigned int);
652 extern int dm_ioctl(struct tcb *, const unsigned int, long);
653 extern int file_ioctl(struct tcb *, const unsigned int, long);
654 extern int fs_x_ioctl(struct tcb *, const unsigned int, long);
655 extern int loop_ioctl(struct tcb *, const unsigned int, long);
656 extern int ptp_ioctl(struct tcb *, const unsigned int, long);
657 extern int scsi_ioctl(struct tcb *, const unsigned int, long);
658 extern int sock_ioctl(struct tcb *, const unsigned int, long);
659 extern int term_ioctl(struct tcb *, const unsigned int, long);
660 extern int ubi_ioctl(struct tcb *, const unsigned int, long);
661 extern int uffdio_ioctl(struct tcb *, const unsigned int, long);
663 extern int tv_nz(const struct timeval *);
664 extern int tv_cmp(const struct timeval *, const struct timeval *);
665 extern double tv_float(const struct timeval *);
666 extern void tv_add(struct timeval *, const struct timeval *, const struct timeval *);
667 extern void tv_sub(struct timeval *, const struct timeval *, const struct timeval *);
668 extern void tv_mul(struct timeval *, const struct timeval *, int);
669 extern void tv_div(struct timeval *, const struct timeval *, int);
672 extern void unwind_init(void);
673 extern void unwind_tcb_init(struct tcb *tcp);
674 extern void unwind_tcb_fin(struct tcb *tcp);
675 extern void unwind_cache_invalidate(struct tcb* tcp);
676 extern void unwind_print_stacktrace(struct tcb* tcp);
677 extern void unwind_capture_stacktrace(struct tcb* tcp);
681 printaddr(unsigned long addr)
687 printstr(struct tcb *tcp, long addr, long len)
689 printstr_ex(tcp, addr, len, 0);
693 printflags(const struct xlat *x, unsigned int flags, const char *dflt)
695 return printflags64(x, flags, dflt);
699 printflags_long(const struct xlat *x, unsigned long flags, const char *dflt)
701 return printflags64(x, flags, dflt);
705 printxval64(const struct xlat *x, const uint64_t val, const char *dflt)
707 printxvals(val, dflt, x, NULL);
711 printxval(const struct xlat *x, const unsigned int val, const char *dflt)
713 printxvals(val, dflt, x, NULL);
717 printxval_long(const struct xlat *x, const unsigned long val, const char *dflt)
719 printxvals(val, dflt, x, NULL);
722 /* Strace log generation machinery.
724 * printing_tcp: tcb which has incomplete line being printed right now.
725 * NULL if last line has been completed ('\n'-terminated).
726 * printleader(tcp) examines it, finishes incomplete line if needed,
727 * the sets it to tcp.
728 * line_ended() clears printing_tcp and resets ->curcol = 0.
729 * tcp->curcol == 0 check is also used to detect completeness
730 * of last line, since in -ff mode just checking printing_tcp for NULL
733 * If you change this code, test log generation in both -f and -ff modes
735 * strace -oLOG -f[f] test/threaded_execve
736 * strace -oLOG -f[f] test/sigkill_rain
737 * strace -oLOG -f[f] -p "`pidof web_browser`"
739 extern struct tcb *printing_tcp;
740 extern void printleader(struct tcb *);
741 extern void line_ended(void);
742 extern void tabto(void);
743 extern void tprintf(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
744 extern void tprints(const char *str);
746 #if SUPPORTED_PERSONALITIES > 1
747 extern void set_personality(int personality);
748 extern unsigned current_personality;
750 # define set_personality(personality) ((void)0)
751 # define current_personality 0
754 #if SUPPORTED_PERSONALITIES == 1
755 # define current_wordsize PERSONALITY0_WORDSIZE
757 # if SUPPORTED_PERSONALITIES == 2 && PERSONALITY0_WORDSIZE == PERSONALITY1_WORDSIZE
758 # define current_wordsize PERSONALITY0_WORDSIZE
760 extern unsigned current_wordsize;
764 /* In many, many places we play fast and loose and use
765 * tprintf("%d", (int) tcp->u_arg[N]) to print fds, pids etc.
766 * We probably need to use widen_to_long() instead:
768 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
769 # define widen_to_long(v) (current_wordsize == 4 ? (long)(int32_t)(v) : (long)(v))
771 # define widen_to_long(v) ((long)(v))
774 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
775 # define widen_to_ulong(v) \
776 (current_wordsize == 4 ? (unsigned long) (uint32_t) (v) : \
779 # define widen_to_ulong(v) ((unsigned long)(v))
783 * Zero-extend a signed integer type to unsigned long long.
785 #define zero_extend_signed_to_ull(v) \
786 (sizeof(v) == sizeof(char) ? (unsigned long long) (unsigned char) (v) : \
787 sizeof(v) == sizeof(short) ? (unsigned long long) (unsigned short) (v) : \
788 sizeof(v) == sizeof(int) ? (unsigned long long) (unsigned int) (v) : \
789 sizeof(v) == sizeof(long) ? (unsigned long long) (unsigned long) (v) : \
790 (unsigned long long) (v))
793 * Sign-extend an unsigned integer type to long long.
795 #define sign_extend_unsigned_to_ll(v) \
796 (sizeof(v) == sizeof(char) ? (long long) (char) (v) : \
797 sizeof(v) == sizeof(short) ? (long long) (short) (v) : \
798 sizeof(v) == sizeof(int) ? (long long) (int) (v) : \
799 sizeof(v) == sizeof(long) ? (long long) (long) (v) : \
802 extern const struct_sysent sysent0[];
803 extern const char *const errnoent0[];
804 extern const char *const signalent0[];
805 extern const struct_ioctlent ioctlent0[];
806 extern qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
807 #define qual_flags (qual_vec[current_personality])
809 #if SUPPORTED_PERSONALITIES > 1
810 extern const struct_sysent *sysent;
811 extern const char *const *errnoent;
812 extern const char *const *signalent;
813 extern const struct_ioctlent *ioctlent;
815 # define sysent sysent0
816 # define errnoent errnoent0
817 # define signalent signalent0
818 # define ioctlent ioctlent0
821 extern unsigned nsyscalls;
822 extern unsigned nerrnos;
823 extern unsigned nsignals;
824 extern unsigned nioctlents;
825 extern unsigned num_quals;
827 #ifdef IN_MPERS_BOOTSTRAP
828 /* Transform multi-line MPERS_PRINTER_DECL statements to one-liners. */
829 # define MPERS_PRINTER_DECL(type, name, ...) MPERS_PRINTER_DECL(type, name, __VA_ARGS__)
830 #else /* !IN_MPERS_BOOTSTRAP */
831 # if SUPPORTED_PERSONALITIES > 1
832 # include "printers.h"
834 # include "native_printer_decls.h"
836 # define MPERS_PRINTER_DECL(type, name, ...) type MPERS_FUNC_NAME(name)(__VA_ARGS__)
837 #endif /* !IN_MPERS_BOOTSTRAP */
840 * If you need non-NULL sysent[scno].sys_func, non-NULL sysent[scno].sys_name,
841 * and non-indirect sysent[scno].sys_flags.
843 #define SCNO_IS_VALID(scno) \
844 ((unsigned long)(scno) < nsyscalls \
845 && sysent[scno].sys_func \
846 && !(sysent[scno].sys_flags & TRACE_INDIRECT_SUBCALL))
848 /* Only ensures that sysent[scno] isn't out of range */
849 #define SCNO_IN_RANGE(scno) \
850 ((unsigned long)(scno) < nsyscalls)
852 #define MPERS_FUNC_NAME__(prefix, name) prefix ## name
853 #define MPERS_FUNC_NAME_(prefix, name) MPERS_FUNC_NAME__(prefix, name)
854 #define MPERS_FUNC_NAME(name) MPERS_FUNC_NAME_(MPERS_PREFIX, name)
856 #define SYS_FUNC_NAME(syscall_name) MPERS_FUNC_NAME(syscall_name)
858 #define SYS_FUNC(syscall_name) int SYS_FUNC_NAME(sys_ ## syscall_name)(struct tcb *tcp)
861 * The kernel used to define 64-bit types on 64-bit systems on a per-arch
862 * basis. Some architectures would use unsigned long and others would use
863 * unsigned long long. These types were exported as part of the
864 * kernel-userspace ABI and now must be maintained forever. This matches
865 * what the kernel exports for each architecture so we don't need to cast
866 * every printing of __u64 or __s64 to stdint types.
869 # define PRI__64 "ll"
870 #elif defined ALPHA || defined IA64 || defined MIPS || defined POWERPC
873 # define PRI__64 "ll"
876 #define PRI__d64 PRI__64"d"
877 #define PRI__u64 PRI__64"u"
878 #define PRI__x64 PRI__64"x"
880 #endif /* !STRACE_DEFS_H */