]> granicus.if.org Git - strace/blob - defs.h
Move string_to_uint* functions to a separate file
[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  * Copyright (c) 2001-2017 The strace developers.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #ifndef STRACE_DEFS_H
32 #define STRACE_DEFS_H
33
34 #ifdef HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #include <features.h>
39 #include <stdbool.h>
40 #include <stdint.h>
41 #include <inttypes.h>
42 #include <sys/types.h>
43 #include <stddef.h>
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
56 #include "error_prints.h"
57 #include "gcc_compat.h"
58 #include "kernel_types.h"
59 #include "macros.h"
60 #include "mpers_type.h"
61 #include "string_to_uint.h"
62 #include "sysent.h"
63 #include "xmalloc.h"
64
65 #ifndef HAVE_STRERROR
66 const char *strerror(int);
67 #endif
68 #ifndef HAVE_STPCPY
69 /* Some libc have stpcpy, some don't. Sigh...
70  * Roll our private implementation...
71  */
72 #undef stpcpy
73 #define stpcpy strace_stpcpy
74 extern char *stpcpy(char *dst, const char *src);
75 #endif
76
77 /* macros */
78 #ifndef MAX
79 # define MAX(a, b)              (((a) > (b)) ? (a) : (b))
80 #endif
81 #ifndef MIN
82 # define MIN(a, b)              (((a) < (b)) ? (a) : (b))
83 #endif
84 #define CLAMP(val, min, max) MIN(MAX(min, val), max)
85
86 /* Glibc has an efficient macro for sigemptyset
87  * (it just does one or two assignments of 0 to internal vector of longs).
88  */
89 #if defined(__GLIBC__) && defined(__sigemptyset) && !defined(sigemptyset)
90 # define sigemptyset __sigemptyset
91 #endif
92
93 /* Configuration section */
94 #ifndef DEFAULT_STRLEN
95 /* default maximum # of bytes printed in `printstr', change with -s switch */
96 # define DEFAULT_STRLEN 32
97 #endif
98 #ifndef DEFAULT_ACOLUMN
99 # define DEFAULT_ACOLUMN        40      /* default alignment column for results */
100 #endif
101 /*
102  * Maximum number of args to a syscall.
103  *
104  * Make sure that all entries in all syscallent.h files have nargs <= MAX_ARGS!
105  * linux/<ARCH>/syscallent*.h:
106  *      all have nargs <= 6 except mips o32 which has nargs <= 7.
107  */
108 #ifndef MAX_ARGS
109 # ifdef LINUX_MIPSO32
110 #  define MAX_ARGS      7
111 # else
112 #  define MAX_ARGS      6
113 # endif
114 #endif
115 /* default sorting method for call profiling */
116 #ifndef DEFAULT_SORTBY
117 # define DEFAULT_SORTBY "time"
118 #endif
119 /*
120  * Experimental code using PTRACE_SEIZE can be enabled here.
121  * This needs Linux kernel 3.4.x or later to work.
122  */
123 #define USE_SEIZE 1
124 /* To force NOMMU build, set to 1 */
125 #define NOMMU_SYSTEM 0
126
127 #ifndef ERESTARTSYS
128 # define ERESTARTSYS    512
129 #endif
130 #ifndef ERESTARTNOINTR
131 # define ERESTARTNOINTR 513
132 #endif
133 #ifndef ERESTARTNOHAND
134 # define ERESTARTNOHAND 514
135 #endif
136 #ifndef ERESTART_RESTARTBLOCK
137 # define ERESTART_RESTARTBLOCK 516
138 #endif
139
140 #if defined X86_64
141 # define SUPPORTED_PERSONALITIES 3
142 # define PERSONALITY2_WORDSIZE  4
143 # define PERSONALITY2_KLONGSIZE PERSONALITY0_KLONGSIZE
144 #elif defined AARCH64 \
145    || defined POWERPC64 \
146    || defined RISCV \
147    || defined SPARC64 \
148    || defined TILE \
149    || defined X32
150 # define SUPPORTED_PERSONALITIES 2
151 #else
152 # define SUPPORTED_PERSONALITIES 1
153 #endif
154
155 #if defined TILE && defined __tilepro__
156 # define DEFAULT_PERSONALITY 1
157 #else
158 # define DEFAULT_PERSONALITY 0
159 #endif
160
161 #define PERSONALITY0_WORDSIZE  SIZEOF_LONG
162 #define PERSONALITY0_KLONGSIZE SIZEOF_KERNEL_LONG_T
163 #define PERSONALITY0_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
164 #define PERSONALITY0_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
165
166 #if SUPPORTED_PERSONALITIES > 1
167 # define PERSONALITY1_WORDSIZE  4
168 # define PERSONALITY1_KLONGSIZE PERSONALITY1_WORDSIZE
169 #endif
170
171 #if SUPPORTED_PERSONALITIES > 1 && defined HAVE_M32_MPERS
172 # define PERSONALITY1_INCLUDE_PRINTERS_DECLS "m32_printer_decls.h"
173 # define PERSONALITY1_INCLUDE_PRINTERS_DEFS "m32_printer_defs.h"
174 # define PERSONALITY1_INCLUDE_FUNCS "m32_funcs.h"
175 # define MPERS_m32_IOCTL_MACROS "ioctl_redefs1.h"
176 #else
177 # define PERSONALITY1_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
178 # define PERSONALITY1_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
179 # define PERSONALITY1_INCLUDE_FUNCS "empty.h"
180 #endif
181
182 #if SUPPORTED_PERSONALITIES > 2 && defined HAVE_MX32_MPERS
183 # define PERSONALITY2_INCLUDE_FUNCS "mx32_funcs.h"
184 # define PERSONALITY2_INCLUDE_PRINTERS_DECLS "mx32_printer_decls.h"
185 # define PERSONALITY2_INCLUDE_PRINTERS_DEFS "mx32_printer_defs.h"
186 # define MPERS_mx32_IOCTL_MACROS "ioctl_redefs2.h"
187 #else
188 # define PERSONALITY2_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
189 # define PERSONALITY2_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
190 # define PERSONALITY2_INCLUDE_FUNCS "empty.h"
191 #endif
192
193 typedef struct ioctlent {
194         const char *symbol;
195         unsigned int code;
196 } struct_ioctlent;
197
198 struct inject_opts {
199         uint16_t first;
200         uint16_t step;
201         uint16_t signo;
202         int rval;
203 };
204
205 #define MAX_ERRNO_VALUE                 4095
206 #define INJECT_OPTS_RVAL_DEFAULT        (-(MAX_ERRNO_VALUE + 1))
207
208 /* Trace Control Block */
209 struct tcb {
210         int flags;              /* See below for TCB_ values */
211         int pid;                /* If 0, this tcb is free */
212         int qual_flg;           /* qual_flags[scno] or DEFAULT_QUAL_FLAGS + RAW */
213         unsigned long u_error;  /* Error code */
214         kernel_ulong_t scno;    /* System call number */
215         kernel_ulong_t u_arg[MAX_ARGS]; /* System call arguments */
216         kernel_long_t u_rval;   /* Return value */
217 #if SUPPORTED_PERSONALITIES > 1
218         unsigned int currpers;  /* Personality at the time of scno update */
219 #endif
220         int sys_func_rval;      /* Syscall entry parser's return value */
221         int curcol;             /* Output column for this process */
222         FILE *outf;             /* Output file for this process */
223         const char *auxstr;     /* Auxiliary info from syscall (see RVAL_STR) */
224         void *_priv_data;       /* Private data for syscall decoding functions */
225         void (*_free_priv_data)(void *); /* Callback for freeing priv_data */
226         const struct_sysent *s_ent; /* sysent[scno] or dummy struct for bad scno */
227         const struct_sysent *s_prev_ent; /* for "resuming interrupted SYSCALL" msg */
228         struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
229         struct timeval stime;   /* System time usage as of last process wait */
230         struct timeval dtime;   /* Delta for system time usage */
231         struct timeval etime;   /* Syscall entry time */
232
233 #ifdef USE_LIBUNWIND
234         struct UPT_info *libunwind_ui;
235         struct mmap_cache_t *mmap_cache;
236         unsigned int mmap_cache_size;
237         unsigned int mmap_cache_generation;
238         struct queue_t *queue;
239 #endif
240 };
241
242 /* TCB flags */
243 /* We have attached to this process, but did not see it stopping yet */
244 #define TCB_STARTUP             0x01
245 #define TCB_IGNORE_ONE_SIGSTOP  0x02    /* Next SIGSTOP is to be ignored */
246 /*
247  * Are we in system call entry or in syscall exit?
248  *
249  * This bit is set in syscall_entering_finish() and cleared in
250  * syscall_exiting_finish().
251  * Other stops which are possible directly after syscall entry (death, ptrace
252  * event stop) are handled without calling syscall_{entering,exiting}_*().
253  *
254  * Use entering(tcp) / exiting(tcp) to check this bit to make code more
255  * readable.
256  */
257 #define TCB_INSYSCALL   0x04
258 #define TCB_ATTACHED    0x08    /* We attached to it already */
259 #define TCB_REPRINT     0x10    /* We should reprint this syscall on exit */
260 #define TCB_FILTERED    0x20    /* This system call has been filtered out */
261 #define TCB_TAMPERED    0x40    /* A syscall has been tampered with */
262 #define TCB_HIDE_LOG    0x80    /* We should hide everything (until execve) */
263 #define TCB_SKIP_DETACH_ON_FIRST_EXEC   0x100   /* -b execve should skip detach on first execve */
264
265 /* qualifier flags */
266 #define QUAL_TRACE      0x001   /* this system call should be traced */
267 #define QUAL_ABBREV     0x002   /* abbreviate the structures of this syscall */
268 #define QUAL_VERBOSE    0x004   /* decode the structures of this syscall */
269 #define QUAL_RAW        0x008   /* print all args in hex for this syscall */
270 #define QUAL_INJECT     0x010   /* tamper with this system call on purpose */
271 #define QUAL_SIGNAL     0x100   /* report events with this signal */
272 #define QUAL_READ       0x200   /* dump data read from this file descriptor */
273 #define QUAL_WRITE      0x400   /* dump data written to this file descriptor */
274
275 #define DEFAULT_QUAL_FLAGS (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
276
277 #define entering(tcp)   (!((tcp)->flags & TCB_INSYSCALL))
278 #define exiting(tcp)    ((tcp)->flags & TCB_INSYSCALL)
279 #define syserror(tcp)   ((tcp)->u_error != 0)
280 #define verbose(tcp)    ((tcp)->qual_flg & QUAL_VERBOSE)
281 #define abbrev(tcp)     ((tcp)->qual_flg & QUAL_ABBREV)
282 #define filtered(tcp)   ((tcp)->flags & TCB_FILTERED)
283 #define hide_log(tcp)   ((tcp)->flags & TCB_HIDE_LOG)
284
285 #include "xlat.h"
286
287 extern const struct xlat addrfams[];
288 extern const struct xlat arp_hardware_types[];
289 extern const struct xlat at_flags[];
290 extern const struct xlat clocknames[];
291 extern const struct xlat dirent_types[];
292 extern const struct xlat ethernet_protocols[];
293 extern const struct xlat evdev_abs[];
294 extern const struct xlat iffflags[];
295 extern const struct xlat inet_protocols[];
296 extern const struct xlat ip_type_of_services[];
297 extern const struct xlat msg_flags[];
298 extern const struct xlat netlink_protocols[];
299 extern const struct xlat nl_route_types[];
300 extern const struct xlat open_access_modes[];
301 extern const struct xlat open_mode_flags[];
302 extern const struct xlat resource_flags[];
303 extern const struct xlat routing_scopes[];
304 extern const struct xlat routing_table_ids[];
305 extern const struct xlat setns_types[];
306 extern const struct xlat sg_io_info[];
307 extern const struct xlat socketlayers[];
308 extern const struct xlat socktypes[];
309 extern const struct xlat tcp_state_flags[];
310 extern const struct xlat tcp_states[];
311 extern const struct xlat whence_codes[];
312
313 /* Format of syscall return values */
314 #define RVAL_DECIMAL    000     /* decimal format */
315 #define RVAL_HEX        001     /* hex format */
316 #define RVAL_OCTAL      002     /* octal format */
317 #define RVAL_UDECIMAL   003     /* unsigned decimal format */
318 #define RVAL_FD         010     /* file descriptor */
319 #define RVAL_MASK       013     /* mask for these values */
320
321 #define RVAL_STR        020     /* Print `auxstr' field after return val */
322 #define RVAL_NONE       040     /* Print nothing */
323
324 #define RVAL_DECODED    0100    /* syscall decoding finished */
325
326 #define IOCTL_NUMBER_UNKNOWN 0
327 #define IOCTL_NUMBER_HANDLED 1
328 #define IOCTL_NUMBER_STOP_LOOKUP 010
329
330 #define indirect_ipccall(tcp) (tcp->s_ent->sys_flags & TRACE_INDIRECT_SUBCALL)
331
332 #if defined(ARM) || defined(AARCH64) \
333  || defined(I386) || defined(X32) || defined(X86_64) \
334  || defined(IA64) \
335  || defined(BFIN) \
336  || defined(M68K) \
337  || defined(MICROBLAZE) \
338  || defined(RISCV) \
339  || defined(S390) \
340  || defined(SH) || defined(SH64) \
341  || defined(SPARC) || defined(SPARC64) \
342  /**/
343 # define NEED_UID16_PARSERS 1
344 #else
345 # define NEED_UID16_PARSERS 0
346 #endif
347
348 enum sock_proto {
349         SOCK_PROTO_UNKNOWN,
350         SOCK_PROTO_UNIX,
351         SOCK_PROTO_TCP,
352         SOCK_PROTO_UDP,
353         SOCK_PROTO_TCPv6,
354         SOCK_PROTO_UDPv6,
355         SOCK_PROTO_NETLINK
356 };
357 extern enum sock_proto get_proto_by_name(const char *);
358
359 enum iov_decode {
360         IOV_DECODE_ADDR,
361         IOV_DECODE_STR,
362         IOV_DECODE_NETLINK
363 };
364
365 typedef enum {
366         CFLAG_NONE = 0,
367         CFLAG_ONLY_STATS,
368         CFLAG_BOTH
369 } cflag_t;
370 extern cflag_t cflag;
371 extern bool debug_flag;
372 extern bool Tflag;
373 extern bool iflag;
374 extern bool count_wallclock;
375 extern unsigned int qflag;
376 extern bool not_failing_only;
377 extern unsigned int show_fd_path;
378 /* are we filtering traces based on paths? */
379 extern struct path_set {
380         const char **paths_selected;
381         unsigned int num_selected;
382 } global_path_set;
383 #define tracing_paths (global_path_set.num_selected != 0)
384 extern unsigned xflag;
385 extern unsigned followfork;
386 #ifdef USE_LIBUNWIND
387 /* if this is true do the stack trace for every system call */
388 extern bool stack_trace_enabled;
389 #endif
390 extern unsigned ptrace_setoptions;
391 extern unsigned max_strlen;
392 extern unsigned os_release;
393 #undef KERNEL_VERSION
394 #define KERNEL_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
395
396 extern int read_int_from_file(const char *, int *);
397
398 extern void set_sortby(const char *);
399 extern void set_overhead(int);
400 extern void print_pc(struct tcb *);
401
402 extern int syscall_entering_decode(struct tcb *);
403 extern int syscall_entering_trace(struct tcb *, unsigned int *);
404 extern void syscall_entering_finish(struct tcb *, int);
405
406 extern int syscall_exiting_decode(struct tcb *, struct timeval *);
407 extern int syscall_exiting_trace(struct tcb *, struct timeval, int);
408 extern void syscall_exiting_finish(struct tcb *);
409
410 extern void count_syscall(struct tcb *, const struct timeval *);
411 extern void call_summary(FILE *);
412
413 extern void clear_regs(void);
414 extern int get_scno(struct tcb *);
415 extern kernel_ulong_t get_rt_sigframe_addr(struct tcb *);
416
417 /**
418  * Convert syscall number to syscall name.
419  *
420  * @param scno Syscall number.
421  * @return     String literal corresponding to the syscall number in case latter
422  *             is valid; NULL otherwise.
423  */
424 extern const char *syscall_name(kernel_ulong_t scno);
425 extern const char *err_name(unsigned long err);
426
427 extern bool is_erestart(struct tcb *);
428 extern void temporarily_clear_syserror(struct tcb *);
429 extern void restore_cleared_syserror(struct tcb *);
430
431 extern void *get_tcb_priv_data(const struct tcb *);
432 extern int set_tcb_priv_data(struct tcb *, void *priv_data,
433                              void (*free_priv_data)(void *));
434 extern void free_tcb_priv_data(struct tcb *);
435
436 static inline unsigned long get_tcb_priv_ulong(const struct tcb *tcp)
437 {
438         return (unsigned long) get_tcb_priv_data(tcp);
439 }
440
441 static inline int set_tcb_priv_ulong(struct tcb *tcp, unsigned long val)
442 {
443         return set_tcb_priv_data(tcp, (void *) val, 0);
444 }
445
446 extern int
447 umoven(struct tcb *, kernel_ulong_t addr, unsigned int len, void *laddr);
448 #define umove(pid, addr, objp)  \
449         umoven((pid), (addr), sizeof(*(objp)), (void *) (objp))
450
451 extern int
452 umoven_or_printaddr(struct tcb *, kernel_ulong_t addr,
453                     unsigned int len, void *laddr);
454 #define umove_or_printaddr(pid, addr, objp)     \
455         umoven_or_printaddr((pid), (addr), sizeof(*(objp)), (void *) (objp))
456
457 extern int
458 umoven_or_printaddr_ignore_syserror(struct tcb *, kernel_ulong_t addr,
459                                     unsigned int len, void *laddr);
460
461 extern int
462 umovestr(struct tcb *, kernel_ulong_t addr, unsigned int len, char *laddr);
463
464 extern int upeek(int pid, unsigned long, kernel_ulong_t *);
465 extern int upoke(int pid, unsigned long, kernel_ulong_t);
466
467 extern bool
468 print_array(struct tcb *,
469             kernel_ulong_t start_addr,
470             size_t nmemb,
471             void *elem_buf,
472             size_t elem_size,
473             int (*umoven_func)(struct tcb *,
474                                      kernel_ulong_t,
475                                      unsigned int,
476                                      void *),
477             bool (*print_func)(struct tcb *,
478                                      void *elem_buf,
479                                      size_t elem_size,
480                                      void *opaque_data),
481             void *opaque_data);
482
483 #if defined ALPHA || defined IA64 || defined MIPS \
484  || defined SH || defined SPARC || defined SPARC64
485 # define HAVE_GETRVAL2
486 extern long getrval2(struct tcb *);
487 #else
488 # undef HAVE_GETRVAL2
489 #endif
490
491 extern const char *signame(const int);
492 extern void pathtrace_select_set(const char *, struct path_set *);
493 extern bool pathtrace_match_set(struct tcb *, struct path_set *);
494 #define pathtrace_select(tcp)   \
495         pathtrace_select_set(tcp, &global_path_set)
496 #define pathtrace_match(tcp)    \
497         pathtrace_match_set(tcp, &global_path_set)
498 extern int getfdpath(struct tcb *, int, char *, unsigned);
499 extern unsigned long getfdinode(struct tcb *, int);
500 extern enum sock_proto getfdproto(struct tcb *, int);
501
502 extern const char *xlookup(const struct xlat *, const uint64_t);
503 extern const char *xlat_search(const struct xlat *, const size_t, const uint64_t);
504
505 struct dyxlat;
506 struct dyxlat *dyxlat_alloc(size_t nmemb);
507 void dyxlat_free(struct dyxlat *);
508 const struct xlat *dyxlat_get(const struct dyxlat *);
509 void dyxlat_add_pair(struct dyxlat *, uint64_t val, const char *str, size_t len);
510
511 const struct xlat *genl_families_xlat(void);
512
513 extern unsigned long get_pagesize(void);
514 extern int next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits);
515
516 /*
517  * Returns STR if it does not start with PREFIX,
518  * or a pointer to the first char in STR after PREFIX.
519  * The length of PREFIX is specified by PREFIX_LEN.
520  */
521 static inline const char *
522 str_strip_prefix_len(const char *str, const char *prefix, size_t prefix_len)
523 {
524         return strncmp(str, prefix, prefix_len) ? str : str + prefix_len;
525 }
526
527 #define STR_STRIP_PREFIX(str, prefix)   \
528         str_strip_prefix_len((str), (prefix), sizeof(prefix) - 1)
529
530 #define QUOTE_0_TERMINATED                      0x01
531 #define QUOTE_OMIT_LEADING_TRAILING_QUOTES      0x02
532 #define QUOTE_OMIT_TRAILING_0                   0x08
533 #define QUOTE_FORCE_HEX                         0x10
534
535 extern int string_quote(const char *, char *, unsigned int, unsigned int);
536 extern int print_quoted_string(const char *, unsigned int, unsigned int);
537 extern int print_quoted_cstring(const char *, unsigned int);
538
539 /* a refers to the lower numbered u_arg,
540  * b refers to the higher numbered u_arg
541  */
542 #ifdef WORDS_BIGENDIAN
543 # define ULONG_LONG(a, b) \
544         ((unsigned long long)(unsigned)(b) | ((unsigned long long)(a)<<32))
545 #else
546 # define ULONG_LONG(a, b) \
547         ((unsigned long long)(unsigned)(a) | ((unsigned long long)(b)<<32))
548 #endif
549 extern int getllval(struct tcb *, unsigned long long *, int);
550 extern int printllval(struct tcb *, const char *, int)
551         ATTRIBUTE_FORMAT((printf, 2, 0));
552
553 extern void printaddr(kernel_ulong_t addr);
554 extern int printxvals(const uint64_t, const char *, const struct xlat *, ...)
555         ATTRIBUTE_SENTINEL;
556 extern int printxval_searchn(const struct xlat *xlat, size_t xlat_size,
557         uint64_t val, const char *dflt);
558 #define printxval_search(xlat__, val__, dflt__) \
559         printxval_searchn(xlat__, ARRAY_SIZE(xlat__), val__, dflt__)
560 extern int printargs(struct tcb *);
561 extern int printargs_u(struct tcb *);
562 extern int printargs_d(struct tcb *);
563
564 extern void addflags(const struct xlat *, uint64_t);
565 extern int printflags_ex(uint64_t, const char *, const struct xlat *, ...)
566         ATTRIBUTE_SENTINEL;
567 extern const char *sprintflags(const char *, const struct xlat *, uint64_t);
568 extern const char *sprinttime(long long sec);
569 extern const char *sprinttime_nsec(long long sec, unsigned long long nsec);
570 extern const char *sprinttime_usec(long long sec, unsigned long long usec);
571 extern void print_symbolic_mode_t(unsigned int);
572 extern void print_numeric_umode_t(unsigned short);
573 extern void print_numeric_long_umask(unsigned long);
574 extern void print_dev_t(unsigned long long dev);
575 extern void print_abnormal_hi(kernel_ulong_t);
576
577 extern void
578 dumpiov_in_msghdr(struct tcb *, kernel_ulong_t addr, kernel_ulong_t data_size);
579
580 extern void
581 dumpiov_in_mmsghdr(struct tcb *, kernel_ulong_t addr);
582
583 extern void
584 dumpiov_upto(struct tcb *, int len, kernel_ulong_t addr, kernel_ulong_t data_size);
585
586 extern void
587 dumpstr(struct tcb *, kernel_ulong_t addr, int len);
588
589 extern void
590 printstr_ex(struct tcb *, kernel_ulong_t addr, kernel_ulong_t len,
591             unsigned int user_style);
592
593 extern void
594 printpathn(struct tcb *, kernel_ulong_t addr, unsigned int n);
595
596 extern void
597 printpath(struct tcb *, kernel_ulong_t addr);
598
599 #define TIMESPEC_TEXT_BUFSIZE \
600                 (sizeof(long long) * 3 * 2 + sizeof("{tv_sec=-, tv_nsec=}"))
601 extern void printfd(struct tcb *, int);
602 extern void print_sockaddr(const void *sa, int len);
603 extern bool
604 print_inet_addr(int af, const void *addr, unsigned int len, const char *var_name);
605 extern bool
606 decode_inet_addr(struct tcb *, kernel_ulong_t addr,
607                  unsigned int len, int family, const char *var_name);
608 extern const char *get_sockaddr_by_inode(struct tcb *, int fd, unsigned long inode);
609 extern bool print_sockaddr_by_inode(struct tcb *, int fd, unsigned long inode);
610 extern void print_dirfd(struct tcb *, int);
611
612 extern int
613 decode_sockaddr(struct tcb *, kernel_ulong_t addr, int addrlen);
614
615 extern void printuid(const char *, const unsigned int);
616
617 extern void
618 print_sigset_addr_len(struct tcb *, kernel_ulong_t addr, kernel_ulong_t len);
619 extern void
620 print_sigset_addr(struct tcb *, kernel_ulong_t addr);
621
622 extern const char *sprintsigmask_n(const char *, const void *, unsigned int);
623 #define tprintsigmask_addr(prefix, mask) \
624         tprints(sprintsigmask_n((prefix), (mask), sizeof(mask)))
625 extern void printsignal(int);
626
627 extern void
628 tprint_iov_upto(struct tcb *, kernel_ulong_t len, kernel_ulong_t addr,
629                 enum iov_decode, kernel_ulong_t data_size);
630
631 extern void
632 decode_netlink(struct tcb *, int fd, kernel_ulong_t addr, kernel_ulong_t len);
633
634 extern void tprint_open_modes(unsigned int);
635 extern const char *sprint_open_modes(unsigned int);
636
637 extern void
638 decode_seccomp_fprog(struct tcb *, kernel_ulong_t addr);
639
640 extern void
641 print_seccomp_fprog(struct tcb *, kernel_ulong_t addr, unsigned short len);
642
643 extern void
644 decode_sock_fprog(struct tcb *, kernel_ulong_t addr);
645
646 extern void
647 print_sock_fprog(struct tcb *, kernel_ulong_t addr, unsigned short len);
648
649 struct strace_stat;
650 extern void print_struct_stat(struct tcb *, const struct strace_stat *const st);
651
652 struct strace_statfs;
653
654 extern void
655 print_struct_statfs(struct tcb *, kernel_ulong_t addr);
656
657 extern void
658 print_struct_statfs64(struct tcb *, kernel_ulong_t addr, kernel_ulong_t size);
659
660 extern void print_ifindex(unsigned int);
661
662 struct number_set;
663 extern struct number_set read_set;
664 extern struct number_set write_set;
665 extern struct number_set signal_set;
666
667 extern bool is_number_in_set(unsigned int number, const struct number_set *);
668 extern void qualify(const char *);
669 extern unsigned int qual_flags(const unsigned int);
670
671 #define DECL_IOCTL(name)                                                \
672 extern int                                                              \
673 name ## _ioctl(struct tcb *, unsigned int request, kernel_ulong_t arg)  \
674 /* End of DECL_IOCTL definition. */
675
676 DECL_IOCTL(dm);
677 DECL_IOCTL(file);
678 DECL_IOCTL(fs_x);
679 DECL_IOCTL(nsfs);
680 DECL_IOCTL(ptp);
681 DECL_IOCTL(scsi);
682 DECL_IOCTL(term);
683 DECL_IOCTL(ubi);
684 DECL_IOCTL(uffdio);
685 #undef DECL_IOCTL
686
687 extern int decode_sg_io_v4(struct tcb *, const kernel_ulong_t arg);
688
689 struct nlmsghdr;
690
691 typedef bool (*netlink_decoder_t)(struct tcb *, const struct nlmsghdr *,
692                                   kernel_ulong_t addr, unsigned int len);
693
694 #define DECL_NETLINK(name)                                              \
695 extern bool                                                             \
696 decode_netlink_ ## name(struct tcb *, const struct nlmsghdr *,          \
697                         kernel_ulong_t addr, unsigned int len)          \
698 /* End of DECL_NETLINK definition. */
699
700 DECL_NETLINK(crypto);
701 DECL_NETLINK(route);
702 DECL_NETLINK(selinux);
703 DECL_NETLINK(sock_diag);
704
705 extern int tv_nz(const struct timeval *);
706 extern int tv_cmp(const struct timeval *, const struct timeval *);
707 extern double tv_float(const struct timeval *);
708 extern void tv_add(struct timeval *, const struct timeval *, const struct timeval *);
709 extern void tv_sub(struct timeval *, const struct timeval *, const struct timeval *);
710 extern void tv_mul(struct timeval *, const struct timeval *, int);
711 extern void tv_div(struct timeval *, const struct timeval *, int);
712
713 #ifdef USE_LIBUNWIND
714 extern void unwind_init(void);
715 extern void unwind_tcb_init(struct tcb *);
716 extern void unwind_tcb_fin(struct tcb *);
717 extern void unwind_cache_invalidate(struct tcb *);
718 extern void unwind_print_stacktrace(struct tcb *);
719 extern void unwind_capture_stacktrace(struct tcb *);
720 #endif
721
722 static inline void
723 printstrn(struct tcb *tcp, kernel_ulong_t addr, kernel_ulong_t len)
724 {
725         printstr_ex(tcp, addr, len, 0);
726 }
727
728 static inline void
729 printstr(struct tcb *tcp, kernel_ulong_t addr)
730 {
731         printstr_ex(tcp, addr, -1, QUOTE_0_TERMINATED);
732 }
733
734 static inline int
735 printflags64(const struct xlat *x, uint64_t flags, const char *dflt)
736 {
737         return printflags_ex(flags, dflt, x, NULL);
738 }
739
740 static inline int
741 printflags(const struct xlat *x, unsigned int flags, const char *dflt)
742 {
743         return printflags64(x, flags, dflt);
744 }
745
746 static inline int
747 printxval64(const struct xlat *x, const uint64_t val, const char *dflt)
748 {
749         return printxvals(val, dflt, x, NULL);
750 }
751
752 static inline int
753 printxval(const struct xlat *x, const unsigned int val, const char *dflt)
754 {
755         return printxvals(val, dflt, x, NULL);
756 }
757
758 static inline void
759 tprint_iov(struct tcb *tcp, kernel_ulong_t len, kernel_ulong_t addr,
760            enum iov_decode decode_iov)
761 {
762         tprint_iov_upto(tcp, len, addr, decode_iov, -1);
763 }
764
765 #ifdef ALPHA
766 typedef struct {
767         int tv_sec, tv_usec;
768 } timeval32_t;
769
770 extern void print_timeval32_t(const timeval32_t *);
771 extern void printrusage32(struct tcb *, kernel_ulong_t);
772 extern const char *sprint_timeval32(struct tcb *, kernel_ulong_t addr);
773 extern void print_timeval32(struct tcb *, kernel_ulong_t addr);
774 extern void print_timeval32_utimes(struct tcb *, kernel_ulong_t addr);
775 extern void print_itimerval32(struct tcb *, kernel_ulong_t addr);
776 #endif
777
778 #ifdef HAVE_STRUCT_USER_DESC
779 extern void print_user_desc(struct tcb *, kernel_ulong_t addr);
780 #endif
781
782 /* Strace log generation machinery.
783  *
784  * printing_tcp: tcb which has incomplete line being printed right now.
785  * NULL if last line has been completed ('\n'-terminated).
786  * printleader(tcp) examines it, finishes incomplete line if needed,
787  * the sets it to tcp.
788  * line_ended() clears printing_tcp and resets ->curcol = 0.
789  * tcp->curcol == 0 check is also used to detect completeness
790  * of last line, since in -ff mode just checking printing_tcp for NULL
791  * is not enough.
792  *
793  * If you change this code, test log generation in both -f and -ff modes
794  * using:
795  * strace -oLOG -f[f] test/threaded_execve
796  * strace -oLOG -f[f] test/sigkill_rain
797  * strace -oLOG -f[f] -p "`pidof web_browser`"
798  */
799 extern struct tcb *printing_tcp;
800 extern void printleader(struct tcb *);
801 extern void line_ended(void);
802 extern void tabto(void);
803 extern void tprintf(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
804 extern void tprints(const char *str);
805 extern void tprintf_comment(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
806 extern void tprints_comment(const char *str);
807
808 #if SUPPORTED_PERSONALITIES > 1
809 extern void set_personality(int personality);
810 extern unsigned current_personality;
811 #else
812 # define set_personality(personality) ((void)0)
813 # define current_personality 0
814 #endif
815
816 #if SUPPORTED_PERSONALITIES == 1
817 # define current_wordsize PERSONALITY0_WORDSIZE
818 # define current_klongsize PERSONALITY0_KLONGSIZE
819 #else
820 # if SUPPORTED_PERSONALITIES == 2 && PERSONALITY0_WORDSIZE == PERSONALITY1_WORDSIZE
821 #  define current_wordsize PERSONALITY0_WORDSIZE
822 # else
823 extern unsigned current_wordsize;
824 # endif
825 # if SUPPORTED_PERSONALITIES == 2 && PERSONALITY0_KLONGSIZE == PERSONALITY1_KLONGSIZE
826 #  define current_klongsize PERSONALITY0_KLONGSIZE
827 # else
828 extern unsigned current_klongsize;
829 # endif
830 #endif
831
832 #define ANY_WORDSIZE_LESS_THAN_KERNEL_LONG      \
833         (SIZEOF_KERNEL_LONG_T > 4               \
834          && (SIZEOF_LONG < SIZEOF_KERNEL_LONG_T || !defined(current_wordsize)))
835
836 #define DECL_PRINTNUM(name)                                             \
837 extern bool                                                             \
838 printnum_ ## name(struct tcb *, kernel_ulong_t addr, const char *fmt)   \
839         ATTRIBUTE_FORMAT((printf, 3, 0))                                \
840 /* End of DECL_PRINTNUM definition. */
841
842 DECL_PRINTNUM(short);
843 DECL_PRINTNUM(int);
844 DECL_PRINTNUM(int64);
845 #undef DECL_PRINTNUM
846
847 #define DECL_PRINTNUM_ADDR(name)                                        \
848 extern bool                                                             \
849 printnum_addr_ ## name(struct tcb *, kernel_ulong_t addr)               \
850 /* End of DECL_PRINTNUM_ADDR definition. */
851
852 DECL_PRINTNUM_ADDR(int);
853 DECL_PRINTNUM_ADDR(int64);
854 #undef DECL_PRINTNUM_ADDR
855
856 #ifndef current_wordsize
857 extern bool
858 printnum_long_int(struct tcb *, kernel_ulong_t addr,
859                   const char *fmt_long, const char *fmt_int)
860         ATTRIBUTE_FORMAT((printf, 3, 0))
861         ATTRIBUTE_FORMAT((printf, 4, 0));
862 extern bool printnum_addr_long_int(struct tcb *, kernel_ulong_t addr);
863 # define printnum_slong(tcp, addr) \
864         printnum_long_int((tcp), (addr), "%" PRId64, "%d")
865 # define printnum_ulong(tcp, addr) \
866         printnum_long_int((tcp), (addr), "%" PRIu64, "%u")
867 # define printnum_ptr(tcp, addr) \
868         printnum_addr_long_int((tcp), (addr))
869 #elif current_wordsize > 4
870 # define printnum_slong(tcp, addr) \
871         printnum_int64((tcp), (addr), "%" PRId64)
872 # define printnum_ulong(tcp, addr) \
873         printnum_int64((tcp), (addr), "%" PRIu64)
874 # define printnum_ptr(tcp, addr) \
875         printnum_addr_int64((tcp), (addr))
876 #else /* current_wordsize == 4 */
877 # define printnum_slong(tcp, addr) \
878         printnum_int((tcp), (addr), "%d")
879 # define printnum_ulong(tcp, addr) \
880         printnum_int((tcp), (addr), "%u")
881 # define printnum_ptr(tcp, addr) \
882         printnum_addr_int((tcp), (addr))
883 #endif
884
885 #ifndef current_klongsize
886 extern bool printnum_addr_klong_int(struct tcb *, kernel_ulong_t addr);
887 # define printnum_kptr(tcp, addr) \
888         printnum_addr_klong_int((tcp), (addr))
889 #elif current_klongsize > 4
890 # define printnum_kptr(tcp, addr) \
891         printnum_addr_int64((tcp), (addr))
892 #else /* current_klongsize == 4 */
893 # define printnum_kptr(tcp, addr) \
894         printnum_addr_int((tcp), (addr))
895 #endif
896
897 #define DECL_PRINTPAIR(name)                                            \
898 extern bool                                                             \
899 printpair_ ## name(struct tcb *, kernel_ulong_t addr, const char *fmt)  \
900         ATTRIBUTE_FORMAT((printf, 3, 0))                                \
901 /* End of DECL_PRINTPAIR definition. */
902
903 DECL_PRINTPAIR(int);
904 DECL_PRINTPAIR(int64);
905 #undef DECL_PRINTPAIR
906
907 static inline kernel_long_t
908 truncate_klong_to_current_wordsize(const kernel_long_t v)
909 {
910 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
911         if (current_wordsize < sizeof(v)) {
912                 return (int) v;
913         } else
914 #endif
915         {
916                 return v;
917         }
918 }
919
920 static inline kernel_ulong_t
921 truncate_kulong_to_current_wordsize(const kernel_ulong_t v)
922 {
923 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
924         if (current_wordsize < sizeof(v)) {
925                 return (unsigned int) v;
926         } else
927 #endif
928         {
929                 return v;
930         }
931 }
932
933 /*
934  * Cast a pointer or a pointer-sized integer to kernel_ulong_t.
935  */
936 #define ptr_to_kulong(v) ((kernel_ulong_t) (unsigned long) (v))
937
938 /*
939  * Zero-extend a signed integer type to unsigned long long.
940  */
941 #define zero_extend_signed_to_ull(v) \
942         (sizeof(v) == sizeof(char) ? (unsigned long long) (unsigned char) (v) : \
943          sizeof(v) == sizeof(short) ? (unsigned long long) (unsigned short) (v) : \
944          sizeof(v) == sizeof(int) ? (unsigned long long) (unsigned int) (v) : \
945          sizeof(v) == sizeof(long) ? (unsigned long long) (unsigned long) (v) : \
946          (unsigned long long) (v))
947
948 /*
949  * Sign-extend an unsigned integer type to long long.
950  */
951 #define sign_extend_unsigned_to_ll(v) \
952         (sizeof(v) == sizeof(char) ? (long long) (char) (v) : \
953          sizeof(v) == sizeof(short) ? (long long) (short) (v) : \
954          sizeof(v) == sizeof(int) ? (long long) (int) (v) : \
955          sizeof(v) == sizeof(long) ? (long long) (long) (v) : \
956          (long long) (v))
957
958 extern const struct_sysent sysent0[];
959 extern const char *const errnoent0[];
960 extern const char *const signalent0[];
961 extern const struct_ioctlent ioctlent0[];
962
963 #if SUPPORTED_PERSONALITIES > 1
964 extern const struct_sysent *sysent;
965 extern const char *const *errnoent;
966 extern const char *const *signalent;
967 extern const struct_ioctlent *ioctlent;
968 #else
969 # define sysent     sysent0
970 # define errnoent   errnoent0
971 # define signalent  signalent0
972 # define ioctlent   ioctlent0
973 #endif
974
975 extern unsigned nsyscalls;
976 extern unsigned nerrnos;
977 extern unsigned nsignals;
978 extern unsigned nioctlents;
979
980 extern const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES];
981 extern const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES];
982 extern struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
983
984 #ifdef IN_MPERS_BOOTSTRAP
985 /* Transform multi-line MPERS_PRINTER_DECL statements to one-liners.  */
986 # define MPERS_PRINTER_DECL(type, name, ...) MPERS_PRINTER_DECL(type, name, __VA_ARGS__)
987 #else /* !IN_MPERS_BOOTSTRAP */
988 # if SUPPORTED_PERSONALITIES > 1
989 #  include "printers.h"
990 # else
991 #  include "native_printer_decls.h"
992 # endif
993 # define MPERS_PRINTER_DECL(type, name, ...) type MPERS_FUNC_NAME(name)(__VA_ARGS__)
994 #endif /* !IN_MPERS_BOOTSTRAP */
995
996 /* Checks that sysent[scno] is not out of range. */
997 static inline bool
998 scno_in_range(kernel_ulong_t scno)
999 {
1000         return scno < nsyscalls;
1001 }
1002
1003 /*
1004  * Checks whether scno is not out of range,
1005  * its corresponding sysent[scno].sys_func is non-NULL,
1006  * and its sysent[scno].sys_flags has no TRACE_INDIRECT_SUBCALL flag set.
1007  */
1008 static inline bool
1009 scno_is_valid(kernel_ulong_t scno)
1010 {
1011         return scno_in_range(scno)
1012                && sysent[scno].sys_func
1013                && !(sysent[scno].sys_flags & TRACE_INDIRECT_SUBCALL);
1014 }
1015
1016 #define MPERS_FUNC_NAME__(prefix, name) prefix ## name
1017 #define MPERS_FUNC_NAME_(prefix, name) MPERS_FUNC_NAME__(prefix, name)
1018 #define MPERS_FUNC_NAME(name) MPERS_FUNC_NAME_(MPERS_PREFIX, name)
1019
1020 #define SYS_FUNC_NAME(syscall_name) MPERS_FUNC_NAME(syscall_name)
1021
1022 #define SYS_FUNC(syscall_name) int SYS_FUNC_NAME(sys_ ## syscall_name)(struct tcb *tcp)
1023
1024 #endif /* !STRACE_DEFS_H */