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