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