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