]> granicus.if.org Git - strace/blob - defs.h
5e40f4db0f0fad7d06f365ad41404fb601a0f64a
[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 adjtimex_modes[];
414 extern const struct xlat adjtimex_status[];
415 extern const struct xlat at_flags[];
416 extern const struct xlat dirent_types[];
417 extern const struct xlat open_access_modes[];
418 extern const struct xlat open_mode_flags[];
419 extern const struct xlat resource_flags[];
420 extern const struct xlat sigev_value[];
421 extern const struct xlat whence_codes[];
422
423 /* Format of syscall return values */
424 #define RVAL_DECIMAL    000     /* decimal format */
425 #define RVAL_HEX        001     /* hex format */
426 #define RVAL_OCTAL      002     /* octal format */
427 #define RVAL_UDECIMAL   003     /* unsigned decimal format */
428 #if defined(LINUX_MIPSN32) || defined(X32)
429 # if 0 /* unused so far */
430 #  define RVAL_LDECIMAL 004     /* long decimal format */
431 #  define RVAL_LHEX     005     /* long hex format */
432 #  define RVAL_LOCTAL   006     /* long octal format */
433 # endif
434 # define RVAL_LUDECIMAL 007     /* long unsigned decimal format */
435 #endif
436 #define RVAL_FD         010     /* file descriptor */
437 #define RVAL_MASK       017     /* mask for these values */
438
439 #define RVAL_STR        020     /* Print `auxstr' field after return val */
440 #define RVAL_NONE       040     /* Print nothing */
441
442 #define RVAL_DECODED    0100    /* syscall decoding finished */
443
444 #define TRACE_FILE      001     /* Trace file-related syscalls. */
445 #define TRACE_IPC       002     /* Trace IPC-related syscalls. */
446 #define TRACE_NETWORK   004     /* Trace network-related syscalls. */
447 #define TRACE_PROCESS   010     /* Trace process-related syscalls. */
448 #define TRACE_SIGNAL    020     /* Trace signal-related syscalls. */
449 #define TRACE_DESC      040     /* Trace file descriptor-related syscalls. */
450 #define TRACE_MEMORY    0100    /* Trace memory mapping-related syscalls. */
451 #define SYSCALL_NEVER_FAILS     0200    /* Syscall is always successful. */
452 #define STACKTRACE_INVALIDATE_CACHE 0400  /* Trigger proc/maps cache updating */
453 #define STACKTRACE_CAPTURE_ON_ENTER 01000 /* Capture stacktrace on "entering" stage */
454 #define TRACE_INDIRECT_SUBCALL  02000   /* Syscall is an indirect socket/ipc subcall. */
455
456 #define indirect_ipccall(tcp) (tcp->s_ent->sys_flags & TRACE_INDIRECT_SUBCALL)
457
458 #if defined(ARM) || defined(AARCH64) \
459  || defined(I386) || defined(X32) || defined(X86_64) \
460  || defined(IA64) \
461  || defined(BFIN) \
462  || defined(M68K) \
463  || defined(MICROBLAZE) \
464  || defined(S390) \
465  || defined(SH) || defined(SH64) \
466  || defined(SPARC) || defined(SPARC64) \
467  /**/
468 # define NEED_UID16_PARSERS 1
469 #else
470 # define NEED_UID16_PARSERS 0
471 #endif
472
473 typedef enum {
474         CFLAG_NONE = 0,
475         CFLAG_ONLY_STATS,
476         CFLAG_BOTH
477 } cflag_t;
478 extern cflag_t cflag;
479 extern bool debug_flag;
480 extern bool Tflag;
481 extern bool iflag;
482 extern bool count_wallclock;
483 extern unsigned int qflag;
484 extern bool not_failing_only;
485 extern unsigned int show_fd_path;
486 extern bool hide_log_until_execve;
487 /* are we filtering traces based on paths? */
488 extern const char **paths_selected;
489 #define tracing_paths (paths_selected != NULL)
490 extern unsigned xflag;
491 extern unsigned followfork;
492 #ifdef USE_LIBUNWIND
493 /* if this is true do the stack trace for every system call */
494 extern bool stack_trace_enabled;
495 #endif
496 extern unsigned ptrace_setoptions;
497 extern unsigned max_strlen;
498 extern unsigned os_release;
499 #undef KERNEL_VERSION
500 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
501
502 void error_msg(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
503 void perror_msg(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
504 void error_msg_and_die(const char *fmt, ...)
505         ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
506 void perror_msg_and_die(const char *fmt, ...)
507         ATTRIBUTE_FORMAT((printf, 1, 2)) ATTRIBUTE_NORETURN;
508 void die_out_of_memory(void) ATTRIBUTE_NORETURN;
509
510 void *xmalloc(size_t size) ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1));
511 void *xcalloc(size_t nmemb, size_t size)
512         ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE((1, 2));
513 void *xreallocarray(void *ptr, size_t nmemb, size_t size)
514         ATTRIBUTE_ALLOC_SIZE((2, 3));
515 char *xstrdup(const char *str) ATTRIBUTE_MALLOC;
516
517 #if USE_CUSTOM_PRINTF
518 /*
519  * See comment in vsprintf.c for allowed formats.
520  * Short version: %h[h]u, %zu, %tu are not allowed, use %[l[l]]u.
521  */
522 int strace_vfprintf(FILE *fp, const char *fmt, va_list args);
523 #else
524 # define strace_vfprintf vfprintf
525 #endif
526
527 extern void set_sortby(const char *);
528 extern void set_overhead(int);
529 extern void qualify(const char *);
530 extern void print_pc(struct tcb *);
531 extern int trace_syscall(struct tcb *);
532 extern void count_syscall(struct tcb *, const struct timeval *);
533 extern void call_summary(FILE *);
534
535 extern void clear_regs(void);
536 extern void get_regs(pid_t pid);
537 extern int get_scno(struct tcb *tcp);
538 extern const char *syscall_name(long scno);
539
540 extern bool is_erestart(struct tcb *);
541 extern void temporarily_clear_syserror(struct tcb *);
542 extern void restore_cleared_syserror(struct tcb *);
543
544 extern int umoven(struct tcb *, long, unsigned int, void *);
545 #define umove(pid, addr, objp)  \
546         umoven((pid), (addr), sizeof(*(objp)), (void *) (objp))
547 extern int umoven_or_printaddr(struct tcb *, long, unsigned int, void *);
548 #define umove_or_printaddr(pid, addr, objp)     \
549         umoven_or_printaddr((pid), (addr), sizeof(*(objp)), (void *) (objp))
550 extern int umove_ulong_or_printaddr(struct tcb *, long, unsigned long *);
551 extern int umove_ulong_array_or_printaddr(struct tcb *, long, unsigned long *, size_t);
552 extern int umovestr(struct tcb *, long, unsigned int, char *);
553 extern int upeek(int pid, long, long *);
554
555 #if defined ALPHA || defined IA64 || defined MIPS \
556  || defined SH || defined SPARC || defined SPARC64
557 # define HAVE_GETRVAL2
558 extern long getrval2(struct tcb *);
559 #else
560 # undef HAVE_GETRVAL2
561 #endif
562
563 extern const char *signame(const int);
564 extern void pathtrace_select(const char *);
565 extern int pathtrace_match(struct tcb *);
566 extern int getfdpath(struct tcb *, int, char *, unsigned);
567
568 extern const char *xlookup(const struct xlat *, const unsigned int);
569 extern const char *xlat_search(const struct xlat *, const size_t, const unsigned int);
570
571 extern unsigned long get_pagesize(void);
572 extern int string_to_uint(const char *str);
573 extern int next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits);
574
575 #define QUOTE_0_TERMINATED                      0x01
576 #define QUOTE_OMIT_LEADING_TRAILING_QUOTES      0x02
577
578 extern int print_quoted_string(const char *, unsigned int, unsigned int);
579
580 /* a refers to the lower numbered u_arg,
581  * b refers to the higher numbered u_arg
582  */
583 #ifdef HAVE_LITTLE_ENDIAN_LONG_LONG
584 # define LONG_LONG(a,b) \
585         ((long long)((unsigned long long)(unsigned)(a) | ((unsigned long long)(b)<<32)))
586 #else
587 # define LONG_LONG(a,b) \
588         ((long long)((unsigned long long)(unsigned)(b) | ((unsigned long long)(a)<<32)))
589 #endif
590 extern int getllval(struct tcb *, unsigned long long *, int);
591 extern int printllval(struct tcb *, const char *, int)
592         ATTRIBUTE_FORMAT((printf, 2, 0));
593
594 extern void printaddr(long);
595 extern void printxvals(const unsigned int, const char *, const struct xlat *, ...);
596 #define printxval(xlat, val, dflt) printxvals(val, dflt, xlat, NULL)
597 extern int printargs(struct tcb *);
598 extern int printargs_lu(struct tcb *);
599 extern int printargs_ld(struct tcb *);
600 extern void addflags(const struct xlat *, int);
601 extern int printflags(const struct xlat *, int, const char *);
602 extern const char *sprintflags(const char *, const struct xlat *, int);
603 extern const char *sprintmode(int);
604 extern const char *sprinttime(time_t);
605 extern void dumpiov_in_msghdr(struct tcb *, long);
606 extern void dumpiov_in_mmsghdr(struct tcb *, long);
607 extern void dumpiov(struct tcb *, int, long);
608 extern void dumpstr(struct tcb *, long, int);
609 extern void printstr(struct tcb *, long, long);
610 extern bool printnum_short(struct tcb *, long, const char *)
611         ATTRIBUTE_FORMAT((printf, 3, 0));
612 extern bool printnum_int(struct tcb *, long, const char *)
613         ATTRIBUTE_FORMAT((printf, 3, 0));
614 extern bool printnum_int64(struct tcb *, long, const char *)
615         ATTRIBUTE_FORMAT((printf, 3, 0));
616
617 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
618 extern bool printnum_long_int(struct tcb *, long, const char *, const char *)
619         ATTRIBUTE_FORMAT((printf, 3, 0))
620         ATTRIBUTE_FORMAT((printf, 4, 0));
621 # define printnum_slong(tcp, addr) \
622         printnum_long_int((tcp), (addr), "%" PRId64, "%d")
623 # define printnum_ulong(tcp, addr) \
624         printnum_long_int((tcp), (addr), "%" PRIu64, "%u")
625 # define printnum_ptr(tcp, addr) \
626         printnum_long_int((tcp), (addr), "%#" PRIx64, "%#x")
627 #elif SIZEOF_LONG > 4
628 # define printnum_slong(tcp, addr) \
629         printnum_int64((tcp), (addr), "%" PRId64)
630 # define printnum_ulong(tcp, addr) \
631         printnum_int64((tcp), (addr), "%" PRIu64)
632 # define printnum_ptr(tcp, addr) \
633         printnum_int64((tcp), (addr), "%#" PRIx64)
634 #else
635 # define printnum_slong(tcp, addr) \
636         printnum_int((tcp), (addr), "%d")
637 # define printnum_ulong(tcp, addr) \
638         printnum_int((tcp), (addr), "%u")
639 # define printnum_ptr(tcp, addr) \
640         printnum_int((tcp), (addr), "%#x")
641 #endif
642
643 extern bool printpair_int(struct tcb *, long, const char *)
644         ATTRIBUTE_FORMAT((printf, 3, 0));
645 extern bool printpair_int64(struct tcb *, long, const char *)
646         ATTRIBUTE_FORMAT((printf, 3, 0));
647 extern void printpath(struct tcb *, long);
648 extern void printpathn(struct tcb *, long, unsigned int);
649 #define TIMESPEC_TEXT_BUFSIZE \
650                 (sizeof(intmax_t)*3 * 2 + sizeof("{tv_sec=%jd, tv_nsec=%jd}"))
651 extern void printfd(struct tcb *, int);
652 extern bool print_sockaddr_by_inode(const unsigned long, const char *);
653 extern void print_dirfd(struct tcb *, int);
654 extern void printsock(struct tcb *, long, int);
655 extern void print_sock_optmgmt(struct tcb *, long, int);
656 #ifdef ALPHA
657 extern void printrusage32(struct tcb *, long);
658 extern const char *sprint_timeval32(struct tcb *tcp, long);
659 extern void print_timeval32(struct tcb *tcp, long);
660 extern void print_timeval32_pair(struct tcb *tcp, long);
661 extern void print_itimerval32(struct tcb *tcp, long);
662 #endif
663 extern void printuid(const char *, const unsigned int);
664 extern void print_sigset_addr_len(struct tcb *, long, long);
665 extern const char *sprintsigmask_n(const char *, const void *, unsigned int);
666 #define tprintsigmask_addr(prefix, mask) \
667         tprints(sprintsigmask_n((prefix), (mask), sizeof(mask)))
668 extern void printsignal(int);
669 extern void tprint_iov(struct tcb *, unsigned long, unsigned long, int decode_iov);
670 extern void tprint_iov_upto(struct tcb *, unsigned long, unsigned long, int decode_iov, unsigned long);
671 extern void tprint_open_modes(int);
672 extern const char *sprint_open_modes(int);
673 extern void print_seccomp_filter(struct tcb *tcp, unsigned long);
674
675 extern int block_ioctl(struct tcb *, const unsigned int, long);
676 extern int evdev_ioctl(struct tcb *, const unsigned int, long);
677 extern int loop_ioctl(struct tcb *, const unsigned int, long);
678 extern int mtd_ioctl(struct tcb *, const unsigned int, long);
679 extern int ptp_ioctl(struct tcb *, const unsigned int, long);
680 extern int rtc_ioctl(struct tcb *, const unsigned int, long);
681 extern int scsi_ioctl(struct tcb *, const unsigned int, long);
682 extern int sock_ioctl(struct tcb *, const unsigned int, long);
683 extern int term_ioctl(struct tcb *, const unsigned int, long);
684 extern int ubi_ioctl(struct tcb *, const unsigned int, long);
685 extern int v4l2_ioctl(struct tcb *, const unsigned int, long);
686
687 extern int tv_nz(const struct timeval *);
688 extern int tv_cmp(const struct timeval *, const struct timeval *);
689 extern double tv_float(const struct timeval *);
690 extern void tv_add(struct timeval *, const struct timeval *, const struct timeval *);
691 extern void tv_sub(struct timeval *, const struct timeval *, const struct timeval *);
692 extern void tv_mul(struct timeval *, const struct timeval *, int);
693 extern void tv_div(struct timeval *, const struct timeval *, int);
694
695 #ifdef USE_LIBUNWIND
696 extern void unwind_init(void);
697 extern void unwind_tcb_init(struct tcb *tcp);
698 extern void unwind_tcb_fin(struct tcb *tcp);
699 extern void unwind_cache_invalidate(struct tcb* tcp);
700 extern void unwind_print_stacktrace(struct tcb* tcp);
701 extern void unwind_capture_stacktrace(struct tcb* tcp);
702 #endif
703
704 /* Strace log generation machinery.
705  *
706  * printing_tcp: tcb which has incomplete line being printed right now.
707  * NULL if last line has been completed ('\n'-terminated).
708  * printleader(tcp) examines it, finishes incomplete line if needed,
709  * the sets it to tcp.
710  * line_ended() clears printing_tcp and resets ->curcol = 0.
711  * tcp->curcol == 0 check is also used to detect completeness
712  * of last line, since in -ff mode just checking printing_tcp for NULL
713  * is not enough.
714  *
715  * If you change this code, test log generation in both -f and -ff modes
716  * using:
717  * strace -oLOG -f[f] test/threaded_execve
718  * strace -oLOG -f[f] test/sigkill_rain
719  * strace -oLOG -f[f] -p "`pidof web_browser`"
720  */
721 extern struct tcb *printing_tcp;
722 extern void printleader(struct tcb *);
723 extern void line_ended(void);
724 extern void tabto(void);
725 extern void tprintf(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
726 extern void tprints(const char *str);
727
728 #if SUPPORTED_PERSONALITIES > 1
729 extern void set_personality(int personality);
730 extern unsigned current_personality;
731 #else
732 # define set_personality(personality) ((void)0)
733 # define current_personality 0
734 #endif
735
736 #if SUPPORTED_PERSONALITIES == 1
737 # define current_wordsize PERSONALITY0_WORDSIZE
738 #else
739 # if SUPPORTED_PERSONALITIES == 2 && PERSONALITY0_WORDSIZE == PERSONALITY1_WORDSIZE
740 #  define current_wordsize PERSONALITY0_WORDSIZE
741 # else
742 extern unsigned current_wordsize;
743 # endif
744 #endif
745
746 /* In many, many places we play fast and loose and use
747  * tprintf("%d", (int) tcp->u_arg[N]) to print fds, pids etc.
748  * We probably need to use widen_to_long() instead:
749  */
750 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
751 # define widen_to_long(v) (current_wordsize == 4 ? (long)(int32_t)(v) : (long)(v))
752 #else
753 # define widen_to_long(v) ((long)(v))
754 #endif
755
756 extern const struct_sysent sysent0[];
757 extern const char *const errnoent0[];
758 extern const char *const signalent0[];
759 extern const struct_ioctlent ioctlent0[];
760 extern qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
761 #define qual_flags (qual_vec[current_personality])
762
763 #if SUPPORTED_PERSONALITIES > 1
764 extern const struct_sysent *sysent;
765 extern const char *const *errnoent;
766 extern const char *const *signalent;
767 extern const struct_ioctlent *ioctlent;
768 #else
769 # define sysent     sysent0
770 # define errnoent   errnoent0
771 # define signalent  signalent0
772 # define ioctlent   ioctlent0
773 #endif
774
775 extern unsigned nsyscalls;
776 extern unsigned nerrnos;
777 extern unsigned nsignals;
778 extern unsigned nioctlents;
779 extern unsigned num_quals;
780
781 #if SUPPORTED_PERSONALITIES > 1
782 # include "printers.h"
783 #else
784 # include "native_printer_decls.h"
785 #endif
786
787 /*
788  * If you need non-NULL sysent[scno].sys_func and sysent[scno].sys_name
789  */
790 #define SCNO_IS_VALID(scno) \
791         ((unsigned long)(scno) < nsyscalls && sysent[scno].sys_func)
792
793 /* Only ensures that sysent[scno] isn't out of range */
794 #define SCNO_IN_RANGE(scno) \
795         ((unsigned long)(scno) < nsyscalls)
796
797 #define MPERS_FUNC_NAME__(prefix, name) prefix ## name
798 #define MPERS_FUNC_NAME_(prefix, name) MPERS_FUNC_NAME__(prefix, name)
799 #define MPERS_FUNC_NAME(name) MPERS_FUNC_NAME_(MPERS_PREFIX, name)
800
801 #define SYS_FUNC_NAME(syscall_name) MPERS_FUNC_NAME(sys_ ## syscall_name)
802
803 #define SYS_FUNC(syscall_name) int SYS_FUNC_NAME(syscall_name)(struct tcb *tcp)
804
805 #define MPERS_PRINTER_DECL(type, name) type MPERS_FUNC_NAME(name)