defs.h revision 54646b8e057443f0b76afe9a231eae63afbf5d5a
1/*
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 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
17 *
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.
28 */
29
30#ifdef HAVE_CONFIG_H
31# include "config.h"
32#endif
33
34#include <features.h>
35#ifdef HAVE_STDBOOL_H
36# include <stdbool.h>
37#endif
38#include <stdint.h>
39#include <inttypes.h>
40#include <sys/types.h>
41#ifdef STDC_HEADERS
42# include <stddef.h>
43#endif
44#include <unistd.h>
45#include <stdlib.h>
46#include <stdio.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.
49 */
50/* #include <ctype.h> */
51#include <string.h>
52#include <errno.h>
53#include <time.h>
54#include <sys/time.h>
55#include <sys/syscall.h>
56
57#ifndef HAVE_STRERROR
58const char *strerror(int);
59#endif
60#ifndef HAVE_STPCPY
61/* Some libc have stpcpy, some don't. Sigh...
62 * Roll our private implementation...
63 */
64#undef stpcpy
65#define stpcpy strace_stpcpy
66extern char *stpcpy(char *dst, const char *src);
67#endif
68
69#if defined __GNUC__ && defined __GNUC_MINOR__
70# define GNUC_PREREQ(maj, min)	\
71	((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
72#else
73# define __attribute__(x)	/* empty */
74# define GNUC_PREREQ(maj, min)	0
75#endif
76
77#if GNUC_PREREQ(2, 5)
78# define ATTRIBUTE_NORETURN	__attribute__((__noreturn__))
79#else
80# define ATTRIBUTE_NORETURN	/* empty */
81#endif
82
83#if GNUC_PREREQ(2, 7)
84# define ATTRIBUTE_FORMAT(args)	__attribute__((__format__ args))
85# define ATTRIBUTE_ALIGNED(arg)	__attribute__((__aligned__(arg)))
86# define ATTRIBUTE_PACKED	__attribute__((__packed__))
87#else
88# define ATTRIBUTE_FORMAT(args)	/* empty */
89# define ATTRIBUTE_ALIGNED(arg)	/* empty */
90# define ATTRIBUTE_PACKED	/* empty */
91#endif
92
93#if GNUC_PREREQ(3, 0)
94# define ATTRIBUTE_MALLOC	__attribute__((__malloc__))
95#else
96# define ATTRIBUTE_MALLOC	/* empty */
97#endif
98
99#if GNUC_PREREQ(3, 1)
100# define ATTRIBUTE_NOINLINE	__attribute__((__noinline__))
101#else
102# define ATTRIBUTE_NOINLINE	/* empty */
103#endif
104
105#if GNUC_PREREQ(4, 3)
106# define ATTRIBUTE_ALLOC_SIZE(args)	__attribute__((__alloc_size__ args))
107#else
108# define ATTRIBUTE_ALLOC_SIZE(args)	/* empty */
109#endif
110
111#ifndef offsetof
112# define offsetof(type, member)	\
113	(((char *) &(((type *) NULL)->member)) - ((char *) (type *) NULL))
114#endif
115
116#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
117
118/* macros */
119#ifndef MAX
120# define MAX(a, b)		(((a) > (b)) ? (a) : (b))
121#endif
122#ifndef MIN
123# define MIN(a, b)		(((a) < (b)) ? (a) : (b))
124#endif
125#define CLAMP(val, min, max) MIN(MAX(min, val), max)
126
127/* Glibc has an efficient macro for sigemptyset
128 * (it just does one or two assignments of 0 to internal vector of longs).
129 */
130#if defined(__GLIBC__) && defined(__sigemptyset) && !defined(sigemptyset)
131# define sigemptyset __sigemptyset
132#endif
133
134/* Configuration section */
135#ifndef DEFAULT_STRLEN
136/* default maximum # of bytes printed in `printstr', change with -s switch */
137# define DEFAULT_STRLEN	32
138#endif
139#ifndef DEFAULT_ACOLUMN
140# define DEFAULT_ACOLUMN	40	/* default alignment column for results */
141#endif
142/*
143 * Maximum number of args to a syscall.
144 *
145 * Make sure that all entries in all syscallent.h files have nargs <= MAX_ARGS!
146 * linux/<ARCH>/syscallent*.h:
147 * 	all have nargs <= 6 except mips o32 which has nargs <= 7.
148 */
149#ifndef MAX_ARGS
150# ifdef LINUX_MIPSO32
151#  define MAX_ARGS	7
152# else
153#  define MAX_ARGS	6
154# endif
155#endif
156/* default sorting method for call profiling */
157#ifndef DEFAULT_SORTBY
158# define DEFAULT_SORTBY "time"
159#endif
160/*
161 * Experimental code using PTRACE_SEIZE can be enabled here.
162 * This needs Linux kernel 3.4.x or later to work.
163 */
164#define USE_SEIZE 1
165/* To force NOMMU build, set to 1 */
166#define NOMMU_SYSTEM 0
167/*
168 * Set to 1 to use speed-optimized vfprintf implementation.
169 * It results in strace using about 5% less CPU in user space
170 * (compared to glibc version).
171 * But strace spends a lot of time in kernel space,
172 * so overall it does not appear to be a significant win.
173 * Thus disabled by default.
174 */
175#define USE_CUSTOM_PRINTF 0
176
177#ifndef ERESTARTSYS
178# define ERESTARTSYS    512
179#endif
180#ifndef ERESTARTNOINTR
181# define ERESTARTNOINTR 513
182#endif
183#ifndef ERESTARTNOHAND
184# define ERESTARTNOHAND 514
185#endif
186#ifndef ERESTART_RESTARTBLOCK
187# define ERESTART_RESTARTBLOCK 516
188#endif
189
190#if defined(SPARC) || defined(SPARC64)
191# define PERSONALITY0_WORDSIZE 4
192# if defined(SPARC64)
193#  define SUPPORTED_PERSONALITIES 2
194#  define PERSONALITY1_WORDSIZE 8
195# endif
196#endif
197
198#ifdef X86_64
199# define SUPPORTED_PERSONALITIES 3
200# define PERSONALITY0_WORDSIZE 8
201# define PERSONALITY1_WORDSIZE 4
202# define PERSONALITY2_WORDSIZE 4
203#endif
204
205#ifdef X32
206# define SUPPORTED_PERSONALITIES 2
207# define PERSONALITY0_WORDSIZE 4
208# define PERSONALITY1_WORDSIZE 4
209#endif
210
211#ifdef ARM
212/* one personality */
213#endif
214
215#ifdef AARCH64
216/* The existing ARM personality, then AArch64 */
217# define SUPPORTED_PERSONALITIES 2
218# define PERSONALITY0_WORDSIZE 4
219# define PERSONALITY1_WORDSIZE 8
220# define DEFAULT_PERSONALITY 1
221#endif
222
223#ifdef POWERPC64
224# define SUPPORTED_PERSONALITIES 2
225# define PERSONALITY0_WORDSIZE 8
226# define PERSONALITY1_WORDSIZE 4
227#endif
228
229#ifdef TILE
230# define SUPPORTED_PERSONALITIES 2
231# define PERSONALITY0_WORDSIZE 8
232# define PERSONALITY1_WORDSIZE 4
233# ifdef __tilepro__
234#  define DEFAULT_PERSONALITY 1
235# endif
236#endif
237
238#ifndef SUPPORTED_PERSONALITIES
239# define SUPPORTED_PERSONALITIES 1
240#endif
241#ifndef DEFAULT_PERSONALITY
242# define DEFAULT_PERSONALITY 0
243#endif
244#ifndef PERSONALITY0_WORDSIZE
245# define PERSONALITY0_WORDSIZE SIZEOF_LONG
246#endif
247
248typedef struct sysent {
249	unsigned nargs;
250	int	sys_flags;
251	int	sen;
252	int	(*sys_func)();
253	const char *sys_name;
254} struct_sysent;
255
256typedef struct ioctlent {
257	const char *symbol;
258	unsigned int code;
259} struct_ioctlent;
260
261/* Trace Control Block */
262struct tcb {
263	int flags;		/* See below for TCB_ values */
264	int pid;		/* If 0, this tcb is free */
265	int qual_flg;		/* qual_flags[scno] or DEFAULT_QUAL_FLAGS + RAW */
266	int u_error;		/* Error code */
267	long scno;		/* System call number */
268	long u_arg[MAX_ARGS];	/* System call arguments */
269#if defined(LINUX_MIPSN32) || defined(X32)
270	long long ext_arg[MAX_ARGS];
271	long long u_lrval;	/* long long return value */
272#endif
273	long u_rval;		/* Return value */
274#if SUPPORTED_PERSONALITIES > 1
275	unsigned int currpers;	/* Personality at the time of scno update */
276#endif
277	int sys_func_rval;	/* Syscall entry parser's return value */
278	int curcol;		/* Output column for this process */
279	FILE *outf;		/* Output file for this process */
280	const char *auxstr;	/* Auxiliary info from syscall (see RVAL_STR) */
281	const struct_sysent *s_ent; /* sysent[scno] or dummy struct for bad scno */
282	const struct_sysent *s_prev_ent; /* for "resuming interrupted SYSCALL" msg */
283	struct timeval stime;	/* System time usage as of last process wait */
284	struct timeval dtime;	/* Delta for system time usage */
285	struct timeval etime;	/* Syscall entry time */
286
287#ifdef USE_LIBUNWIND
288	struct UPT_info* libunwind_ui;
289	struct mmap_cache_t* mmap_cache;
290	unsigned int mmap_cache_size;
291	unsigned int mmap_cache_generation;
292	struct queue_t* queue;
293#endif
294};
295
296/* TCB flags */
297/* We have attached to this process, but did not see it stopping yet */
298#define TCB_STARTUP		0x01
299#define TCB_IGNORE_ONE_SIGSTOP	0x02	/* Next SIGSTOP is to be ignored */
300/*
301 * Are we in system call entry or in syscall exit?
302 *
303 * This bit is set after all syscall entry processing is done.
304 * Therefore, this bit will be set when next ptrace stop occurs,
305 * which should be syscall exit stop. Other stops which are possible
306 * directly after syscall entry (death, ptrace event stop)
307 * are simpler and handled without calling trace_syscall(), therefore
308 * the places where TCB_INSYSCALL can be set but we aren't in syscall stop
309 * are limited to trace(), this condition is never observed in trace_syscall()
310 * and below.
311 * The bit is cleared after all syscall exit processing is done.
312 *
313 * Use entering(tcp) / exiting(tcp) to check this bit to make code more readable.
314 */
315#define TCB_INSYSCALL	0x04
316#define TCB_ATTACHED	0x08	/* We attached to it already */
317#define TCB_REPRINT	0x10	/* We should reprint this syscall on exit */
318#define TCB_FILTERED	0x20	/* This system call has been filtered out */
319
320/* qualifier flags */
321#define QUAL_TRACE	0x001	/* this system call should be traced */
322#define QUAL_ABBREV	0x002	/* abbreviate the structures of this syscall */
323#define QUAL_VERBOSE	0x004	/* decode the structures of this syscall */
324#define QUAL_RAW	0x008	/* print all args in hex for this syscall */
325#define QUAL_SIGNAL	0x010	/* report events with this signal */
326#define QUAL_READ	0x020	/* dump data read on this file descriptor */
327#define QUAL_WRITE	0x040	/* dump data written to this file descriptor */
328typedef uint8_t qualbits_t;
329#define UNDEFINED_SCNO	0x100	/* Used only in tcp->qual_flg */
330
331#define DEFAULT_QUAL_FLAGS (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
332
333#define entering(tcp)	(!((tcp)->flags & TCB_INSYSCALL))
334#define exiting(tcp)	((tcp)->flags & TCB_INSYSCALL)
335#define syserror(tcp)	((tcp)->u_error != 0)
336#define verbose(tcp)	((tcp)->qual_flg & QUAL_VERBOSE)
337#define abbrev(tcp)	((tcp)->qual_flg & QUAL_ABBREV)
338#define filtered(tcp)	((tcp)->flags & TCB_FILTERED)
339
340struct xlat {
341	unsigned int val;
342	const char *str;
343};
344#define XLAT(x) { x, #x }
345#define XLAT_END { 0, NULL }
346
347extern const struct xlat addrfams[];
348extern const struct xlat at_flags[];
349extern const struct xlat open_access_modes[];
350extern const struct xlat open_mode_flags[];
351extern const struct xlat resource_flags[];
352extern const struct xlat whence_codes[];
353
354/* Format of syscall return values */
355#define RVAL_DECIMAL	000	/* decimal format */
356#define RVAL_HEX	001	/* hex format */
357#define RVAL_OCTAL	002	/* octal format */
358#define RVAL_UDECIMAL	003	/* unsigned decimal format */
359#if defined(LINUX_MIPSN32) || defined(X32)
360# if 0 /* unused so far */
361#  define RVAL_LDECIMAL	004	/* long decimal format */
362#  define RVAL_LHEX	005	/* long hex format */
363#  define RVAL_LOCTAL	006	/* long octal format */
364# endif
365# define RVAL_LUDECIMAL	007	/* long unsigned decimal format */
366#endif
367#define RVAL_FD		010	/* file descriptor */
368#define RVAL_MASK	017	/* mask for these values */
369
370#define RVAL_STR	020	/* Print `auxstr' field after return val */
371#define RVAL_NONE	040	/* Print nothing */
372
373#define RVAL_DECODED	0100	/* syscall decoding finished */
374
375#define TRACE_FILE	001	/* Trace file-related syscalls. */
376#define TRACE_IPC	002	/* Trace IPC-related syscalls. */
377#define TRACE_NETWORK	004	/* Trace network-related syscalls. */
378#define TRACE_PROCESS	010	/* Trace process-related syscalls. */
379#define TRACE_SIGNAL	020	/* Trace signal-related syscalls. */
380#define TRACE_DESC	040	/* Trace file descriptor-related syscalls. */
381#define TRACE_MEMORY	0100	/* Trace memory mapping-related syscalls. */
382#define SYSCALL_NEVER_FAILS	0200	/* Syscall is always successful. */
383#define STACKTRACE_INVALIDATE_CACHE 0400  /* Trigger proc/maps cache updating */
384#define STACKTRACE_CAPTURE_ON_ENTER 01000 /* Capture stacktrace on "entering" stage */
385#define TRACE_INDIRECT_SUBCALL	02000	/* Syscall is an indirect socket/ipc subcall. */
386
387#define indirect_ipccall(tcp) (tcp->s_ent->sys_flags & TRACE_INDIRECT_SUBCALL)
388
389#if defined(ARM) || defined(AARCH64) \
390 || defined(I386) || defined(X32) || defined(X86_64) \
391 || defined(IA64) \
392 || defined(BFIN) \
393 || defined(M68K) \
394 || defined(MICROBLAZE) \
395 || defined(S390) \
396 || defined(SH) || defined(SH64) \
397 || defined(SPARC) || defined(SPARC64) \
398 /**/
399# define NEED_UID16_PARSERS 1
400#else
401# define NEED_UID16_PARSERS 0
402#endif
403
404typedef enum {
405	CFLAG_NONE = 0,
406	CFLAG_ONLY_STATS,
407	CFLAG_BOTH
408} cflag_t;
409extern cflag_t cflag;
410extern bool debug_flag;
411extern bool Tflag;
412extern bool iflag;
413extern bool count_wallclock;
414extern unsigned int qflag;
415extern bool not_failing_only;
416extern unsigned int show_fd_path;
417extern bool hide_log_until_execve;
418/* are we filtering traces based on paths? */
419extern const char **paths_selected;
420#define tracing_paths (paths_selected != NULL)
421extern unsigned xflag;
422extern unsigned followfork;
423#ifdef USE_LIBUNWIND
424/* if this is true do the stack trace for every system call */
425extern bool stack_trace_enabled;
426#endif
427extern unsigned ptrace_setoptions;
428extern unsigned max_strlen;
429extern unsigned os_release;
430#undef KERNEL_VERSION
431#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
432
433enum bitness_t { BITNESS_CURRENT = 0, BITNESS_32 };
434
435void error_msg(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
436void perror_msg(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
437void error_msg_and_die(const char *fmt, ...)
438	ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
439void perror_msg_and_die(const char *fmt, ...)
440	ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
441void die_out_of_memory(void) ATTRIBUTE_NORETURN;
442
443void *xmalloc(size_t size) ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1));
444void *xcalloc(size_t nmemb, size_t size)
445	ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1, 2));
446void *xreallocarray(void *ptr, size_t nmemb, size_t size)
447	ATTRIBUTE_ALLOC_SIZE((2, 3));
448char *xstrdup(const char *str) ATTRIBUTE_MALLOC;
449
450#if USE_CUSTOM_PRINTF
451/*
452 * See comment in vsprintf.c for allowed formats.
453 * Short version: %h[h]u, %zu, %tu are not allowed, use %[l[l]]u.
454 */
455int strace_vfprintf(FILE *fp, const char *fmt, va_list args);
456#else
457# define strace_vfprintf vfprintf
458#endif
459
460extern void set_sortby(const char *);
461extern void set_overhead(int);
462extern void qualify(const char *);
463extern void print_pc(struct tcb *);
464extern int trace_syscall(struct tcb *);
465extern void count_syscall(struct tcb *, const struct timeval *);
466extern void call_summary(FILE *);
467
468extern void clear_regs(void);
469extern void get_regs(pid_t pid);
470extern int get_scno(struct tcb *tcp);
471extern const char *syscall_name(long scno);
472
473extern int umoven(struct tcb *, long, unsigned int, void *);
474#define umove(pid, addr, objp)	\
475	umoven((pid), (addr), sizeof(*(objp)), (void *) (objp))
476extern int umoven_or_printaddr(struct tcb *, long, unsigned int, void *);
477#define umove_or_printaddr(pid, addr, objp)	\
478	umoven_or_printaddr((pid), (addr), sizeof(*(objp)), (void *) (objp))
479extern int umovestr(struct tcb *, long, unsigned int, char *);
480extern int upeek(int pid, long, long *);
481
482#if defined ALPHA || defined IA64 || defined MIPS \
483 || defined SH || defined SPARC || defined SPARC64
484# define HAVE_GETRVAL2
485extern long getrval2(struct tcb *);
486#else
487# undef HAVE_GETRVAL2
488#endif
489
490extern const char *signame(const int);
491extern void pathtrace_select(const char *);
492extern int pathtrace_match(struct tcb *);
493extern int getfdpath(struct tcb *, int, char *, unsigned);
494
495extern const char *xlookup(const struct xlat *, const unsigned int);
496extern const char *xlat_search(const struct xlat *, const size_t, const unsigned int);
497
498extern unsigned long get_pagesize(void);
499extern int string_to_uint(const char *str);
500extern int next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits);
501
502#define QUOTE_0_TERMINATED			0x01
503#define QUOTE_OMIT_LEADING_TRAILING_QUOTES	0x02
504
505extern int print_quoted_string(const char *, unsigned int, unsigned int);
506
507/* a refers to the lower numbered u_arg,
508 * b refers to the higher numbered u_arg
509 */
510#ifdef HAVE_LITTLE_ENDIAN_LONG_LONG
511# define LONG_LONG(a,b) \
512	((long long)((unsigned long long)(unsigned)(a) | ((unsigned long long)(b)<<32)))
513#else
514# define LONG_LONG(a,b) \
515	((long long)((unsigned long long)(unsigned)(b) | ((unsigned long long)(a)<<32)))
516#endif
517extern int getllval(struct tcb *, unsigned long long *, int);
518extern int printllval(struct tcb *, const char *, int)
519	ATTRIBUTE_FORMAT((printf, 2, 0));
520
521extern void printaddr(long);
522extern void printxvals(const unsigned int, const char *, const struct xlat *, ...);
523#define printxval(xlat, val, dflt) printxvals(val, dflt, xlat, NULL)
524extern int printargs(struct tcb *);
525extern int printargs_lu(struct tcb *);
526extern int printargs_ld(struct tcb *);
527extern void addflags(const struct xlat *, int);
528extern int printflags(const struct xlat *, int, const char *);
529extern const char *sprintflags(const char *, const struct xlat *, int);
530extern const char *sprintmode(int);
531extern const char *sprinttime(time_t);
532extern void dumpiov_in_msghdr(struct tcb *, long);
533extern void dumpiov_in_mmsghdr(struct tcb *, long);
534extern void dumpiov(struct tcb *, int, long);
535extern void dumpstr(struct tcb *, long, int);
536extern void printstr(struct tcb *, long, long);
537extern bool printnum_short(struct tcb *, long, const char *)
538	ATTRIBUTE_FORMAT((printf, 3, 0));
539extern bool printnum_int(struct tcb *, long, const char *)
540	ATTRIBUTE_FORMAT((printf, 3, 0));
541extern bool printnum_int64(struct tcb *, long, const char *)
542	ATTRIBUTE_FORMAT((printf, 3, 0));
543
544#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
545extern bool printnum_long_int(struct tcb *, long, const char *, const char *)
546	ATTRIBUTE_FORMAT((printf, 3, 0))
547	ATTRIBUTE_FORMAT((printf, 4, 0));
548# define printnum_slong(tcp, addr) \
549	printnum_long_int((tcp), (addr), "%" PRId64, "%d")
550# define printnum_ulong(tcp, addr) \
551	printnum_long_int((tcp), (addr), "%" PRIu64, "%u")
552# define printnum_ptr(tcp, addr) \
553	printnum_long_int((tcp), (addr), "%#" PRIx64, "%#x")
554#elif SIZEOF_LONG > 4
555# define printnum_slong(tcp, addr) \
556	printnum_int64((tcp), (addr), "%" PRId64)
557# define printnum_ulong(tcp, addr) \
558	printnum_int64((tcp), (addr), "%" PRIu64)
559# define printnum_ptr(tcp, addr) \
560	printnum_int64((tcp), (addr), "%#" PRIx64)
561#else
562# define printnum_slong(tcp, addr) \
563	printnum_int((tcp), (addr), "%d")
564# define printnum_ulong(tcp, addr) \
565	printnum_int((tcp), (addr), "%u")
566# define printnum_ptr(tcp, addr) \
567	printnum_int((tcp), (addr), "%#x")
568#endif
569
570extern bool printpair_int(struct tcb *, long, const char *)
571	ATTRIBUTE_FORMAT((printf, 3, 0));
572extern bool printpair_int64(struct tcb *, long, const char *)
573	ATTRIBUTE_FORMAT((printf, 3, 0));
574extern void printpath(struct tcb *, long);
575extern void printpathn(struct tcb *, long, unsigned int);
576#define TIMESPEC_TEXT_BUFSIZE (sizeof(long)*3 * 2 + sizeof("{%u, %u}"))
577#define TIMEVAL_TEXT_BUFSIZE  TIMESPEC_TEXT_BUFSIZE
578extern void printtv_bitness(struct tcb *, long, enum bitness_t, int);
579#define printtv(tcp, addr)	\
580	printtv_bitness((tcp), (addr), BITNESS_CURRENT, 0)
581#define printtv_special(tcp, addr)	\
582	printtv_bitness((tcp), (addr), BITNESS_CURRENT, 1)
583extern char *sprinttv(char *, struct tcb *, long, enum bitness_t, int special);
584extern void print_timespec(struct tcb *, long);
585extern void sprint_timespec(char *, struct tcb *, long);
586extern void printsigevent(struct tcb *tcp, long arg);
587extern void printsiginfo_at(struct tcb *tcp, long addr);
588extern void printfd(struct tcb *, int);
589extern bool print_sockaddr_by_inode(const unsigned long, const char *);
590extern void print_dirfd(struct tcb *, int);
591extern void printsock(struct tcb *, long, int);
592extern void print_sock_optmgmt(struct tcb *, long, int);
593extern void printrusage(struct tcb *, long);
594#ifdef ALPHA
595extern void printrusage32(struct tcb *, long);
596#endif
597extern void printuid(const char *, const unsigned int);
598extern void print_sigset_addr_len(struct tcb *, long, long);
599extern const char *sprintsigmask_n(const char *, const void *, unsigned int);
600#define tprintsigmask_addr(prefix, mask) \
601	tprints(sprintsigmask_n((prefix), (mask), sizeof(mask)))
602extern void printsignal(int);
603extern void tprint_iov(struct tcb *, unsigned long, unsigned long, int decode_iov);
604extern void tprint_iov_upto(struct tcb *, unsigned long, unsigned long, int decode_iov, unsigned long);
605extern void tprint_open_modes(int);
606extern const char *sprint_open_modes(int);
607extern void print_seccomp_filter(struct tcb *tcp, unsigned long);
608
609extern int block_ioctl(struct tcb *, const unsigned int, long);
610extern int evdev_ioctl(struct tcb *, const unsigned int, long);
611extern int loop_ioctl(struct tcb *, const unsigned int, long);
612extern int mtd_ioctl(struct tcb *, const unsigned int, long);
613extern int ptp_ioctl(struct tcb *, const unsigned int, long);
614extern int rtc_ioctl(struct tcb *, const unsigned int, long);
615extern int scsi_ioctl(struct tcb *, const unsigned int, long);
616extern int sock_ioctl(struct tcb *, const unsigned int, long);
617extern int term_ioctl(struct tcb *, const unsigned int, long);
618extern int ubi_ioctl(struct tcb *, const unsigned int, long);
619extern int v4l2_ioctl(struct tcb *, const unsigned int, long);
620
621extern int tv_nz(const struct timeval *);
622extern int tv_cmp(const struct timeval *, const struct timeval *);
623extern double tv_float(const struct timeval *);
624extern void tv_add(struct timeval *, const struct timeval *, const struct timeval *);
625extern void tv_sub(struct timeval *, const struct timeval *, const struct timeval *);
626extern void tv_mul(struct timeval *, const struct timeval *, int);
627extern void tv_div(struct timeval *, const struct timeval *, int);
628
629#ifdef USE_LIBUNWIND
630extern void unwind_init(void);
631extern void unwind_tcb_init(struct tcb *tcp);
632extern void unwind_tcb_fin(struct tcb *tcp);
633extern void unwind_cache_invalidate(struct tcb* tcp);
634extern void unwind_print_stacktrace(struct tcb* tcp);
635extern void unwind_capture_stacktrace(struct tcb* tcp);
636#endif
637
638/* Strace log generation machinery.
639 *
640 * printing_tcp: tcb which has incomplete line being printed right now.
641 * NULL if last line has been completed ('\n'-terminated).
642 * printleader(tcp) examines it, finishes incomplete line if needed,
643 * the sets it to tcp.
644 * line_ended() clears printing_tcp and resets ->curcol = 0.
645 * tcp->curcol == 0 check is also used to detect completeness
646 * of last line, since in -ff mode just checking printing_tcp for NULL
647 * is not enough.
648 *
649 * If you change this code, test log generation in both -f and -ff modes
650 * using:
651 * strace -oLOG -f[f] test/threaded_execve
652 * strace -oLOG -f[f] test/sigkill_rain
653 * strace -oLOG -f[f] -p "`pidof web_browser`"
654 */
655extern struct tcb *printing_tcp;
656extern void printleader(struct tcb *);
657extern void line_ended(void);
658extern void tabto(void);
659extern void tprintf(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
660extern void tprints(const char *str);
661
662#if SUPPORTED_PERSONALITIES > 1
663extern void set_personality(int personality);
664extern unsigned current_personality;
665#else
666# define set_personality(personality) ((void)0)
667# define current_personality 0
668#endif
669
670#if SUPPORTED_PERSONALITIES == 1
671# define current_wordsize PERSONALITY0_WORDSIZE
672#else
673# if SUPPORTED_PERSONALITIES == 2 && PERSONALITY0_WORDSIZE == PERSONALITY1_WORDSIZE
674#  define current_wordsize PERSONALITY0_WORDSIZE
675# else
676extern unsigned current_wordsize;
677# endif
678#endif
679
680/* In many, many places we play fast and loose and use
681 * tprintf("%d", (int) tcp->u_arg[N]) to print fds, pids etc.
682 * We probably need to use widen_to_long() instead:
683 */
684#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
685# define widen_to_long(v) (current_wordsize == 4 ? (long)(int32_t)(v) : (long)(v))
686#else
687# define widen_to_long(v) ((long)(v))
688#endif
689
690extern const struct_sysent sysent0[];
691extern const char *const errnoent0[];
692extern const char *const signalent0[];
693extern const struct_ioctlent ioctlent0[];
694extern qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
695#define qual_flags (qual_vec[current_personality])
696#if SUPPORTED_PERSONALITIES > 1
697extern const struct_sysent *sysent;
698extern const char *const *errnoent;
699extern const char *const *signalent;
700extern const struct_ioctlent *ioctlent;
701#else
702# define sysent     sysent0
703# define errnoent   errnoent0
704# define signalent  signalent0
705# define ioctlent   ioctlent0
706#endif
707extern unsigned nsyscalls;
708extern unsigned nerrnos;
709extern unsigned nsignals;
710extern unsigned nioctlents;
711extern unsigned num_quals;
712
713/*
714 * If you need non-NULL sysent[scno].sys_func and sysent[scno].sys_name
715 */
716#define SCNO_IS_VALID(scno) \
717	((unsigned long)(scno) < nsyscalls && sysent[scno].sys_func)
718
719/* Only ensures that sysent[scno] isn't out of range */
720#define SCNO_IN_RANGE(scno) \
721	((unsigned long)(scno) < nsyscalls)
722
723#ifndef SYS_FUNC_NAME
724# define SYS_FUNC_NAME(syscall_name) sys_ ## syscall_name
725#endif
726
727#define SYS_FUNC(syscall_name) int SYS_FUNC_NAME(syscall_name)(struct tcb *tcp)
728