]> granicus.if.org Git - strace/blob - defs.h
bpf_filter.c: add support for decoding eBPF instruction codes
[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-2018 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 "arch_defs.h"
57 #include "error_prints.h"
58 #include "gcc_compat.h"
59 #include "kernel_types.h"
60 #include "macros.h"
61 #include "mpers_type.h"
62 #include "string_to_uint.h"
63 #include "sysent.h"
64 #include "xmalloc.h"
65
66 #ifndef HAVE_STRERROR
67 const char *strerror(int);
68 #endif
69 #ifndef HAVE_STPCPY
70 /* Some libc have stpcpy, some don't. Sigh...
71  * Roll our private implementation...
72  */
73 #undef stpcpy
74 #define stpcpy strace_stpcpy
75 extern char *stpcpy(char *dst, const char *src);
76 #endif
77
78 /* Glibc has an efficient macro for sigemptyset
79  * (it just does one or two assignments of 0 to internal vector of longs).
80  */
81 #if defined(__GLIBC__) && defined(__sigemptyset) && !defined(sigemptyset)
82 # define sigemptyset __sigemptyset
83 #endif
84
85 /* Configuration section */
86 #ifndef DEFAULT_STRLEN
87 /* default maximum # of bytes printed in `printstr', change with -s switch */
88 # define DEFAULT_STRLEN 32
89 #endif
90 #ifndef DEFAULT_ACOLUMN
91 # define DEFAULT_ACOLUMN        40      /* default alignment column for results */
92 #endif
93 /*
94  * Maximum number of args to a syscall.
95  *
96  * Make sure that all entries in all syscallent.h files have nargs <= MAX_ARGS!
97  * linux/<ARCH>/syscallent*.h:
98  *      all have nargs <= 6 except mips o32 which has nargs <= 7.
99  */
100 #ifndef MAX_ARGS
101 # ifdef LINUX_MIPSO32
102 #  define MAX_ARGS      7
103 # else
104 #  define MAX_ARGS      6
105 # endif
106 #endif
107 /* default sorting method for call profiling */
108 #ifndef DEFAULT_SORTBY
109 # define DEFAULT_SORTBY "time"
110 #endif
111
112 /* To force NOMMU build, set to 1 */
113 #define NOMMU_SYSTEM 0
114
115 #ifndef ERESTARTSYS
116 # define ERESTARTSYS    512
117 #endif
118 #ifndef ERESTARTNOINTR
119 # define ERESTARTNOINTR 513
120 #endif
121 #ifndef ERESTARTNOHAND
122 # define ERESTARTNOHAND 514
123 #endif
124 #ifndef ERESTART_RESTARTBLOCK
125 # define ERESTART_RESTARTBLOCK 516
126 #endif
127
128 #define PERSONALITY0_WORDSIZE  SIZEOF_LONG
129 #define PERSONALITY0_KLONGSIZE SIZEOF_KERNEL_LONG_T
130 #define PERSONALITY0_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
131 #define PERSONALITY0_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
132
133 #if SUPPORTED_PERSONALITIES > 1
134 # define PERSONALITY1_WORDSIZE  4
135 # define PERSONALITY1_KLONGSIZE PERSONALITY1_WORDSIZE
136 #endif
137
138 #if SUPPORTED_PERSONALITIES > 2
139 # define PERSONALITY2_WORDSIZE  4
140 # define PERSONALITY2_KLONGSIZE PERSONALITY0_KLONGSIZE
141 #endif
142
143 #if SUPPORTED_PERSONALITIES > 1 && defined HAVE_M32_MPERS
144 # define PERSONALITY1_INCLUDE_PRINTERS_DECLS "m32_printer_decls.h"
145 # define PERSONALITY1_INCLUDE_PRINTERS_DEFS "m32_printer_defs.h"
146 # define PERSONALITY1_INCLUDE_FUNCS "m32_funcs.h"
147 # define MPERS_m32_IOCTL_MACROS "ioctl_redefs1.h"
148 # define HAVE_PERSONALITY_1_MPERS 1
149 #else
150 # define PERSONALITY1_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
151 # define PERSONALITY1_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
152 # define PERSONALITY1_INCLUDE_FUNCS "empty.h"
153 # define HAVE_PERSONALITY_1_MPERS 0
154 #endif
155
156 #if SUPPORTED_PERSONALITIES > 2 && defined HAVE_MX32_MPERS
157 # define PERSONALITY2_INCLUDE_FUNCS "mx32_funcs.h"
158 # define PERSONALITY2_INCLUDE_PRINTERS_DECLS "mx32_printer_decls.h"
159 # define PERSONALITY2_INCLUDE_PRINTERS_DEFS "mx32_printer_defs.h"
160 # define MPERS_mx32_IOCTL_MACROS "ioctl_redefs2.h"
161 # define HAVE_PERSONALITY_2_MPERS 1
162 #else
163 # define PERSONALITY2_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
164 # define PERSONALITY2_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
165 # define PERSONALITY2_INCLUDE_FUNCS "empty.h"
166 # define HAVE_PERSONALITY_2_MPERS 0
167 #endif
168
169 typedef struct ioctlent {
170         const char *symbol;
171         unsigned int code;
172 } struct_ioctlent;
173
174 #define INJECT_F_SIGNAL         0x01
175 #define INJECT_F_ERROR          0x02
176 #define INJECT_F_RETVAL         0x04
177 #define INJECT_F_DELAY_ENTER    0x08
178 #define INJECT_F_DELAY_EXIT     0x10
179
180 struct inject_data {
181         uint8_t flags;          /* 5 of 8 flags are used so far */
182         uint8_t signo;          /* NSIG <= 128 */
183         uint16_t rval_idx;      /* index in retval_vec */
184         uint16_t delay_idx;     /* index in delay_data_vec */
185 };
186
187 struct inject_opts {
188         uint16_t first;
189         uint16_t step;
190         struct inject_data data;
191 };
192
193 #define MAX_ERRNO_VALUE                 4095
194
195 /* Trace Control Block */
196 struct tcb {
197         int flags;              /* See below for TCB_ values */
198         int pid;                /* If 0, this tcb is free */
199         int qual_flg;           /* qual_flags[scno] or DEFAULT_QUAL_FLAGS + RAW */
200         unsigned long u_error;  /* Error code */
201         kernel_ulong_t scno;    /* System call number */
202         kernel_ulong_t u_arg[MAX_ARGS]; /* System call arguments */
203         kernel_long_t u_rval;   /* Return value */
204 #if SUPPORTED_PERSONALITIES > 1
205         unsigned int currpers;  /* Personality at the time of scno update */
206 #endif
207         int sys_func_rval;      /* Syscall entry parser's return value */
208         int curcol;             /* Output column for this process */
209         FILE *outf;             /* Output file for this process */
210         const char *auxstr;     /* Auxiliary info from syscall (see RVAL_STR) */
211         void *_priv_data;       /* Private data for syscall decoding functions */
212         void (*_free_priv_data)(void *); /* Callback for freeing priv_data */
213         const struct_sysent *s_ent; /* sysent[scno] or dummy struct for bad scno */
214         const struct_sysent *s_prev_ent; /* for "resuming interrupted SYSCALL" msg */
215         struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
216         struct timespec stime;  /* System time usage as of last process wait */
217         struct timespec dtime;  /* Delta for system time usage */
218         struct timespec etime;  /* Syscall entry time */
219         struct timespec delay_expiration_time; /* When does the delay end */
220
221         struct mmap_cache_t *mmap_cache;
222         unsigned int mmap_cache_size;
223         unsigned int mmap_cache_generation;
224
225 #ifdef ENABLE_STACKTRACE
226         void *unwind_ctx;
227         struct unwind_queue_t *unwind_queue;
228 #endif
229 };
230
231 /* TCB flags */
232 /* We have attached to this process, but did not see it stopping yet */
233 #define TCB_STARTUP             0x01
234 #define TCB_IGNORE_ONE_SIGSTOP  0x02    /* Next SIGSTOP is to be ignored */
235 /*
236  * Are we in system call entry or in syscall exit?
237  *
238  * This bit is set in syscall_entering_finish() and cleared in
239  * syscall_exiting_finish().
240  * Other stops which are possible directly after syscall entry (death, ptrace
241  * event stop) are handled without calling syscall_{entering,exiting}_*().
242  *
243  * Use entering(tcp) / exiting(tcp) to check this bit to make code more
244  * readable.
245  */
246 #define TCB_INSYSCALL   0x04
247 #define TCB_ATTACHED    0x08    /* We attached to it already */
248 #define TCB_REPRINT     0x10    /* We should reprint this syscall on exit */
249 #define TCB_FILTERED    0x20    /* This system call has been filtered out */
250 #define TCB_TAMPERED    0x40    /* A syscall has been tampered with */
251 #define TCB_HIDE_LOG    0x80    /* We should hide everything (until execve) */
252 #define TCB_SKIP_DETACH_ON_FIRST_EXEC   0x100   /* -b execve should skip detach on first execve */
253 #define TCB_GRABBED     0x200   /* We grab the process and can catch it
254                                  * in the middle of a syscall */
255 #define TCB_RECOVERING  0x400   /* We try to recover after detecting incorrect
256                                  * syscall entering/exiting state */
257 #define TCB_INJECT_DELAY_EXIT   0x800   /* Current syscall needs to be delayed
258                                            on exit */
259 #define TCB_DELAYED     0x1000  /* Current syscall has been delayed */
260
261 /* qualifier flags */
262 #define QUAL_TRACE      0x001   /* this system call should be traced */
263 #define QUAL_ABBREV     0x002   /* abbreviate the structures of this syscall */
264 #define QUAL_VERBOSE    0x004   /* decode the structures of this syscall */
265 #define QUAL_RAW        0x008   /* print all args in hex for this syscall */
266 #define QUAL_INJECT     0x010   /* tamper with this system call on purpose */
267
268 #define DEFAULT_QUAL_FLAGS (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
269
270 #define entering(tcp)   (!((tcp)->flags & TCB_INSYSCALL))
271 #define exiting(tcp)    ((tcp)->flags & TCB_INSYSCALL)
272 #define syserror(tcp)   ((tcp)->u_error != 0)
273 #define traced(tcp)     ((tcp)->qual_flg & QUAL_TRACE)
274 #define verbose(tcp)    ((tcp)->qual_flg & QUAL_VERBOSE)
275 #define abbrev(tcp)     ((tcp)->qual_flg & QUAL_ABBREV)
276 #define raw(tcp)        ((tcp)->qual_flg & QUAL_RAW)
277 #define inject(tcp)     ((tcp)->qual_flg & QUAL_INJECT)
278 #define filtered(tcp)   ((tcp)->flags & TCB_FILTERED)
279 #define hide_log(tcp)   ((tcp)->flags & TCB_HIDE_LOG)
280 #define syscall_tampered(tcp)   ((tcp)->flags & TCB_TAMPERED)
281 #define recovering(tcp) ((tcp)->flags & TCB_RECOVERING)
282 #define inject_delay_exit(tcp)  ((tcp)->flags & TCB_INJECT_DELAY_EXIT)
283 #define syscall_delayed(tcp)    ((tcp)->flags & TCB_DELAYED)
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
293 /** Ethernet protocols list, sorted and unterminated, defined in sockaddr.c. */
294 extern const struct xlat ethernet_protocols[];
295 /** Ethernet protocols array size without terminating record. */
296 extern const size_t ethernet_protocols_size;
297
298 extern const struct xlat evdev_abs[];
299 extern const struct xlat iffflags[];
300 extern const struct xlat inet_protocols[];
301 extern const struct xlat ip_type_of_services[];
302 extern const struct xlat msg_flags[];
303 extern const struct xlat netlink_protocols[];
304 extern const struct xlat nl_netfilter_msg_types[];
305 extern const struct xlat nl_route_types[];
306 extern const struct xlat open_access_modes[];
307 extern const struct xlat open_mode_flags[];
308 extern const struct xlat resource_flags[];
309 extern const struct xlat routing_scopes[];
310 extern const struct xlat routing_table_ids[];
311 extern const struct xlat routing_types[];
312 extern const struct xlat seccomp_filter_flags[];
313 extern const struct xlat seccomp_ret_action[];
314 extern const struct xlat setns_types[];
315 extern const struct xlat sg_io_info[];
316 extern const struct xlat socketlayers[];
317 extern const struct xlat socktypes[];
318 extern const struct xlat tcp_state_flags[];
319 extern const struct xlat tcp_states[];
320 extern const struct xlat whence_codes[];
321
322 /* Format of syscall return values */
323 #define RVAL_UDECIMAL   000     /* unsigned decimal format */
324 #define RVAL_HEX        001     /* hex format */
325 #define RVAL_OCTAL      002     /* octal format */
326 #define RVAL_FD         010     /* file descriptor */
327 #define RVAL_MASK       013     /* mask for these values */
328
329 #define RVAL_STR        020     /* Print `auxstr' field after return val */
330 #define RVAL_NONE       040     /* Print nothing */
331
332 #define RVAL_DECODED    0100    /* syscall decoding finished */
333 #define RVAL_IOCTL_DECODED 0200 /* ioctl sub-parser successfully decoded
334                                    the argument */
335
336 #define IOCTL_NUMBER_UNKNOWN 0
337 #define IOCTL_NUMBER_HANDLED 1
338 #define IOCTL_NUMBER_STOP_LOOKUP 010
339
340 #define indirect_ipccall(tcp) (tcp->s_ent->sys_flags & TRACE_INDIRECT_SUBCALL)
341
342 enum sock_proto {
343         SOCK_PROTO_UNKNOWN,
344         SOCK_PROTO_UNIX,
345         SOCK_PROTO_TCP,
346         SOCK_PROTO_UDP,
347         SOCK_PROTO_TCPv6,
348         SOCK_PROTO_UDPv6,
349         SOCK_PROTO_NETLINK
350 };
351 extern enum sock_proto get_proto_by_name(const char *);
352
353 enum iov_decode {
354         IOV_DECODE_ADDR,
355         IOV_DECODE_STR,
356         IOV_DECODE_NETLINK
357 };
358
359 typedef enum {
360         CFLAG_NONE = 0,
361         CFLAG_ONLY_STATS,
362         CFLAG_BOTH
363 } cflag_t;
364 extern cflag_t cflag;
365 extern bool Tflag;
366 extern bool iflag;
367 extern bool count_wallclock;
368 extern unsigned int qflag;
369 extern bool not_failing_only;
370 extern unsigned int show_fd_path;
371 /* are we filtering traces based on paths? */
372 extern struct path_set {
373         const char **paths_selected;
374         size_t num_selected;
375         size_t size;
376 } global_path_set;
377 #define tracing_paths (global_path_set.num_selected != 0)
378 extern unsigned xflag;
379 extern unsigned followfork;
380 #ifdef ENABLE_STACKTRACE
381 /* if this is true do the stack trace for every system call */
382 extern bool stack_trace_enabled;
383 #endif
384 extern unsigned ptrace_setoptions;
385 extern unsigned max_strlen;
386 extern unsigned os_release;
387 #undef KERNEL_VERSION
388 #define KERNEL_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
389
390 extern int read_int_from_file(struct tcb *, const char *, int *);
391
392 extern void set_sortby(const char *);
393 extern void set_overhead(int);
394 extern void print_pc(struct tcb *);
395
396 extern int syscall_entering_decode(struct tcb *);
397 extern int syscall_entering_trace(struct tcb *, unsigned int *);
398 extern void syscall_entering_finish(struct tcb *, int);
399
400 extern int syscall_exiting_decode(struct tcb *, struct timespec *);
401 extern int syscall_exiting_trace(struct tcb *, struct timespec *, int);
402 extern void syscall_exiting_finish(struct tcb *);
403
404 extern void count_syscall(struct tcb *, const struct timespec *);
405 extern void call_summary(FILE *);
406
407 extern void clear_regs(struct tcb *tcp);
408 extern int get_scno(struct tcb *);
409 extern kernel_ulong_t get_rt_sigframe_addr(struct tcb *);
410
411 /**
412  * Convert a (shuffled) syscall number to the corresponding syscall name.
413  *
414  * @param scno Syscall number.
415  * @return     String literal corresponding to the syscall number in case latter
416  *             is valid; NULL otherwise.
417  */
418 extern const char *syscall_name(kernel_ulong_t scno);
419 /**
420  * Convert a syscall name to the corresponding (shuffled) syscall number.
421  *
422  * @param s     Syscall name.
423  * @param p     Personality.
424  * @param start From which position in syscall entry table resume the search.
425  * @return      Shuffled syscall number (ready to use against sysent_vec)
426  *              if syscall name is found; -1 otherwise.
427  */
428 extern kernel_long_t scno_by_name(const char *s, unsigned p,
429                                   kernel_long_t start);
430 /**
431  * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
432  * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
433  *
434  * @param scno Raw or shuffled syscall number.
435  * @return     Shuffled or raw syscall number, respectively.
436  */
437 extern kernel_ulong_t shuffle_scno(kernel_ulong_t scno);
438 extern const char *err_name(unsigned long err);
439
440 extern bool is_erestart(struct tcb *);
441 extern void temporarily_clear_syserror(struct tcb *);
442 extern void restore_cleared_syserror(struct tcb *);
443
444 extern void *get_tcb_priv_data(const struct tcb *);
445 extern int set_tcb_priv_data(struct tcb *, void *priv_data,
446                              void (*free_priv_data)(void *));
447 extern void free_tcb_priv_data(struct tcb *);
448
449 static inline unsigned long get_tcb_priv_ulong(const struct tcb *tcp)
450 {
451         return (unsigned long) get_tcb_priv_data(tcp);
452 }
453
454 static inline int set_tcb_priv_ulong(struct tcb *tcp, unsigned long val)
455 {
456         return set_tcb_priv_data(tcp, (void *) val, 0);
457 }
458
459 extern int
460 umoven(struct tcb *, kernel_ulong_t addr, unsigned int len, void *laddr);
461 #define umove(pid, addr, objp)  \
462         umoven((pid), (addr), sizeof(*(objp)), (void *) (objp))
463
464 extern int
465 umoven_or_printaddr(struct tcb *, kernel_ulong_t addr,
466                     unsigned int len, void *laddr);
467 #define umove_or_printaddr(pid, addr, objp)     \
468         umoven_or_printaddr((pid), (addr), sizeof(*(objp)), (void *) (objp))
469
470 extern int
471 umoven_or_printaddr_ignore_syserror(struct tcb *, kernel_ulong_t addr,
472                                     unsigned int len, void *laddr);
473
474 extern int
475 umovestr(struct tcb *, kernel_ulong_t addr, unsigned int len, char *laddr);
476
477 extern int upeek(struct tcb *tcp, unsigned long, kernel_ulong_t *);
478 extern int upoke(struct tcb *tcp, unsigned long, kernel_ulong_t);
479
480 extern bool
481 print_array(struct tcb *,
482             kernel_ulong_t start_addr,
483             size_t nmemb,
484             void *elem_buf,
485             size_t elem_size,
486             int (*umoven_func)(struct tcb *,
487                                      kernel_ulong_t,
488                                      unsigned int,
489                                      void *),
490             bool (*print_func)(struct tcb *,
491                                      void *elem_buf,
492                                      size_t elem_size,
493                                      void *opaque_data),
494             void *opaque_data);
495
496 #if HAVE_ARCH_GETRVAL2
497 extern long getrval2(struct tcb *);
498 #endif
499
500 extern const char *signame(const int);
501 extern void pathtrace_select_set(const char *, struct path_set *);
502 extern bool pathtrace_match_set(struct tcb *, struct path_set *);
503 #define pathtrace_select(tcp)   \
504         pathtrace_select_set(tcp, &global_path_set)
505 #define pathtrace_match(tcp)    \
506         pathtrace_match_set(tcp, &global_path_set)
507 extern int getfdpath(struct tcb *, int, char *, unsigned);
508 extern unsigned long getfdinode(struct tcb *, int);
509 extern enum sock_proto getfdproto(struct tcb *, int);
510
511 extern const char *xlookup(const struct xlat *, const uint64_t);
512 extern const char *xlat_search(const struct xlat *, const size_t, const uint64_t);
513
514 struct dyxlat;
515 struct dyxlat *dyxlat_alloc(size_t nmemb);
516 void dyxlat_free(struct dyxlat *);
517 const struct xlat *dyxlat_get(const struct dyxlat *);
518 void dyxlat_add_pair(struct dyxlat *, uint64_t val, const char *str, size_t len);
519
520 const struct xlat *genl_families_xlat(struct tcb *tcp);
521
522 extern unsigned long get_pagesize(void);
523 extern int next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits);
524
525 /*
526  * Returns STR if it does not start with PREFIX,
527  * or a pointer to the first char in STR after PREFIX.
528  * The length of PREFIX is specified by PREFIX_LEN.
529  */
530 static inline const char *
531 str_strip_prefix_len(const char *str, const char *prefix, size_t prefix_len)
532 {
533         return strncmp(str, prefix, prefix_len) ? str : str + prefix_len;
534 }
535
536 #define STR_STRIP_PREFIX(str, prefix)   \
537         str_strip_prefix_len((str), (prefix), sizeof(prefix) - 1)
538
539 #define QUOTE_0_TERMINATED                      0x01
540 #define QUOTE_OMIT_LEADING_TRAILING_QUOTES      0x02
541 #define QUOTE_OMIT_TRAILING_0                   0x08
542 #define QUOTE_FORCE_HEX                         0x10
543 #define QUOTE_EMIT_COMMENT                      0x20
544
545 extern int string_quote(const char *, char *, unsigned int, unsigned int,
546                         const char *escape_chars);
547 extern int print_quoted_string_ex(const char *, unsigned int, unsigned int,
548                                   const char *escape_chars);
549 extern int print_quoted_string(const char *, unsigned int, unsigned int);
550 extern int print_quoted_cstring(const char *, unsigned int);
551
552 /* a refers to the lower numbered u_arg,
553  * b refers to the higher numbered u_arg
554  */
555 #ifdef WORDS_BIGENDIAN
556 # define ULONG_LONG(a, b) \
557         ((unsigned long long)(unsigned)(b) | ((unsigned long long)(a)<<32))
558 #else
559 # define ULONG_LONG(a, b) \
560         ((unsigned long long)(unsigned)(a) | ((unsigned long long)(b)<<32))
561 #endif
562 extern int getllval(struct tcb *, unsigned long long *, int);
563 extern int printllval(struct tcb *, const char *, int)
564         ATTRIBUTE_FORMAT((printf, 2, 0));
565
566 extern void printaddr64(uint64_t addr);
567 extern void printaddr(kernel_ulong_t addr);
568 extern int printxvals(const uint64_t, const char *, const struct xlat *, ...)
569         ATTRIBUTE_SENTINEL;
570 extern int printxval_searchn(const struct xlat *xlat, size_t xlat_size,
571         uint64_t val, const char *dflt);
572 /**
573  * Wrapper around printxval_searchn that passes ARRAY_SIZE - 1
574  * as the array size, as all arrays are XLAT_END-terminated and
575  * printxval_searchn expects a size without the terminating record.
576  */
577 #define printxval_search(xlat__, val__, dflt__) \
578         printxval_searchn(xlat__, ARRAY_SIZE(xlat__) - 1, val__, dflt__)
579 extern int sprintxval(char *buf, size_t size, const struct xlat *,
580         unsigned int val, const char *dflt);
581 extern int printargs(struct tcb *);
582 extern int printargs_u(struct tcb *);
583 extern int printargs_d(struct tcb *);
584
585 extern int printflags_ex(uint64_t, const char *, const struct xlat *, ...)
586         ATTRIBUTE_SENTINEL;
587 extern const char *sprintflags(const char *, const struct xlat *, uint64_t);
588 extern const char *sprinttime(long long sec);
589 extern const char *sprinttime_nsec(long long sec, unsigned long long nsec);
590 extern const char *sprinttime_usec(long long sec, unsigned long long usec);
591 extern void print_symbolic_mode_t(unsigned int);
592 extern void print_numeric_umode_t(unsigned short);
593 extern void print_numeric_long_umask(unsigned long);
594 extern void print_dev_t(unsigned long long dev);
595 extern void print_abnormal_hi(kernel_ulong_t);
596
597 extern kernel_ulong_t *
598 fetch_indirect_syscall_args(struct tcb *, kernel_ulong_t addr, unsigned int n_args);
599
600 extern void
601 dumpiov_in_msghdr(struct tcb *, kernel_ulong_t addr, kernel_ulong_t data_size);
602
603 extern void
604 dumpiov_in_mmsghdr(struct tcb *, kernel_ulong_t addr);
605
606 extern void
607 dumpiov_upto(struct tcb *, int len, kernel_ulong_t addr, kernel_ulong_t data_size);
608
609 extern void
610 dumpstr(struct tcb *, kernel_ulong_t addr, int len);
611
612 extern int
613 printstr_ex(struct tcb *, kernel_ulong_t addr, kernel_ulong_t len,
614             unsigned int user_style);
615
616 extern int
617 printpathn(struct tcb *, kernel_ulong_t addr, unsigned int n);
618
619 extern int
620 printpath(struct tcb *, kernel_ulong_t addr);
621
622 #define TIMESPEC_TEXT_BUFSIZE \
623                 (sizeof(long long) * 3 * 2 + sizeof("{tv_sec=-, tv_nsec=}"))
624 extern void printfd(struct tcb *, int);
625 extern void print_sockaddr(const void *sa, int len);
626 extern bool
627 print_inet_addr(int af, const void *addr, unsigned int len, const char *var_name);
628 extern bool
629 decode_inet_addr(struct tcb *, kernel_ulong_t addr,
630                  unsigned int len, int family, const char *var_name);
631 extern const char *get_sockaddr_by_inode(struct tcb *, int fd, unsigned long inode);
632 extern bool print_sockaddr_by_inode(struct tcb *, int fd, unsigned long inode);
633 extern void print_dirfd(struct tcb *, int);
634
635 extern int
636 decode_sockaddr(struct tcb *, kernel_ulong_t addr, int addrlen);
637
638 extern void printuid(const char *, const unsigned int);
639
640 extern void
641 print_sigset_addr_len(struct tcb *, kernel_ulong_t addr, kernel_ulong_t len);
642 extern void
643 print_sigset_addr(struct tcb *, kernel_ulong_t addr);
644
645 extern const char *sprintsigmask_n(const char *, const void *, unsigned int);
646 #define tprintsigmask_addr(prefix, mask) \
647         tprints(sprintsigmask_n((prefix), (mask), sizeof(mask)))
648 extern void printsignal(int);
649
650 extern void
651 tprint_iov_upto(struct tcb *, kernel_ulong_t len, kernel_ulong_t addr,
652                 enum iov_decode, kernel_ulong_t data_size);
653
654 extern void
655 decode_netlink(struct tcb *, int fd, kernel_ulong_t addr, kernel_ulong_t len);
656
657 extern void tprint_open_modes(unsigned int);
658 extern const char *sprint_open_modes(unsigned int);
659
660 extern void
661 decode_seccomp_fprog(struct tcb *, kernel_ulong_t addr);
662
663 extern void
664 print_seccomp_fprog(struct tcb *, kernel_ulong_t addr, unsigned short len);
665
666 extern void
667 decode_sock_fprog(struct tcb *, kernel_ulong_t addr);
668
669 extern void
670 print_sock_fprog(struct tcb *, kernel_ulong_t addr, unsigned short len);
671
672 struct strace_stat;
673 extern void print_struct_stat(struct tcb *, const struct strace_stat *const st);
674
675 struct strace_statfs;
676 struct strace_keyctl_kdf_params;
677
678 extern void
679 print_struct_statfs(struct tcb *, kernel_ulong_t addr);
680
681 extern void
682 print_struct_statfs64(struct tcb *, kernel_ulong_t addr, kernel_ulong_t size);
683
684 extern void print_ifindex(unsigned int);
685
686 extern void print_bpf_filter_code(const uint16_t code, bool extended);
687
688 extern void qualify(const char *);
689 extern unsigned int qual_flags(const unsigned int);
690
691 #define DECL_IOCTL(name)                                                \
692 extern int                                                              \
693 name ## _ioctl(struct tcb *, unsigned int request, kernel_ulong_t arg)  \
694 /* End of DECL_IOCTL definition. */
695
696 DECL_IOCTL(dm);
697 DECL_IOCTL(evdev);
698 DECL_IOCTL(file);
699 DECL_IOCTL(fs_x);
700 DECL_IOCTL(kvm);
701 DECL_IOCTL(nsfs);
702 DECL_IOCTL(ptp);
703 DECL_IOCTL(scsi);
704 DECL_IOCTL(term);
705 DECL_IOCTL(ubi);
706 DECL_IOCTL(uffdio);
707 #undef DECL_IOCTL
708
709 extern int decode_sg_io_v4(struct tcb *, const kernel_ulong_t arg);
710 extern void print_evdev_ff_type(const kernel_ulong_t val);
711
712 struct nlmsghdr;
713
714 typedef bool (*netlink_decoder_t)(struct tcb *, const struct nlmsghdr *,
715                                   kernel_ulong_t addr, unsigned int len);
716
717 #define DECL_NETLINK(name)                                              \
718 extern bool                                                             \
719 decode_netlink_ ## name(struct tcb *, const struct nlmsghdr *,          \
720                         kernel_ulong_t addr, unsigned int len)          \
721 /* End of DECL_NETLINK definition. */
722
723 DECL_NETLINK(crypto);
724 DECL_NETLINK(netfilter);
725 DECL_NETLINK(route);
726 DECL_NETLINK(selinux);
727 DECL_NETLINK(sock_diag);
728
729 extern int ts_nz(const struct timespec *);
730 extern int ts_cmp(const struct timespec *, const struct timespec *);
731 extern double ts_float(const struct timespec *);
732 extern void ts_add(struct timespec *, const struct timespec *, const struct timespec *);
733 extern void ts_sub(struct timespec *, const struct timespec *, const struct timespec *);
734 extern void ts_mul(struct timespec *, const struct timespec *, int);
735 extern void ts_div(struct timespec *, const struct timespec *, int);
736
737 #ifdef ENABLE_STACKTRACE
738 extern void unwind_init(void);
739 extern void unwind_tcb_init(struct tcb *);
740 extern void unwind_tcb_fin(struct tcb *);
741 extern void unwind_tcb_print(struct tcb *);
742 extern void unwind_tcb_capture(struct tcb *);
743 #endif
744
745 static inline int
746 printstrn(struct tcb *tcp, kernel_ulong_t addr, kernel_ulong_t len)
747 {
748         return printstr_ex(tcp, addr, len, 0);
749 }
750
751 static inline int
752 printstr(struct tcb *tcp, kernel_ulong_t addr)
753 {
754         return printstr_ex(tcp, addr, -1, QUOTE_0_TERMINATED);
755 }
756
757 static inline int
758 printflags64(const struct xlat *x, uint64_t flags, const char *dflt)
759 {
760         return printflags_ex(flags, dflt, x, NULL);
761 }
762
763 static inline int
764 printflags(const struct xlat *x, unsigned int flags, const char *dflt)
765 {
766         return printflags64(x, flags, dflt);
767 }
768
769 static inline int
770 printxval64(const struct xlat *x, const uint64_t val, const char *dflt)
771 {
772         return printxvals(val, dflt, x, NULL);
773 }
774
775 static inline int
776 printxval(const struct xlat *x, const unsigned int val, const char *dflt)
777 {
778         return printxvals(val, dflt, x, NULL);
779 }
780
781 static inline void
782 tprint_iov(struct tcb *tcp, kernel_ulong_t len, kernel_ulong_t addr,
783            enum iov_decode decode_iov)
784 {
785         tprint_iov_upto(tcp, len, addr, decode_iov, -1);
786 }
787
788 #ifdef ALPHA
789 typedef struct {
790         int tv_sec, tv_usec;
791 } timeval32_t;
792
793 extern void print_timeval32_t(const timeval32_t *);
794 extern void printrusage32(struct tcb *, kernel_ulong_t);
795 extern const char *sprint_timeval32(struct tcb *, kernel_ulong_t addr);
796 extern void print_timeval32(struct tcb *, kernel_ulong_t addr);
797 extern void print_timeval32_utimes(struct tcb *, kernel_ulong_t addr);
798 extern void print_itimerval32(struct tcb *, kernel_ulong_t addr);
799 #endif
800
801 #ifdef HAVE_STRUCT_USER_DESC
802 /**
803  * Filter what to print from the point of view of the get_thread_area syscall.
804  * Kernel copies only entry_number field at first and then tries to write the
805  * whole structure.
806  */
807 enum user_desc_print_filter {
808         /* Print the "entering" part of struct user_desc - entry_number.  */
809         USER_DESC_ENTERING = 1,
810         /* Print the "exiting" part of the structure.  */
811         USER_DESC_EXITING  = 2,
812         USER_DESC_BOTH     = USER_DESC_ENTERING | USER_DESC_EXITING,
813 };
814
815 extern void print_user_desc(struct tcb *, kernel_ulong_t addr,
816                             enum user_desc_print_filter filter);
817 #endif
818
819 /* Strace log generation machinery.
820  *
821  * printing_tcp: tcb which has incomplete line being printed right now.
822  * NULL if last line has been completed ('\n'-terminated).
823  * printleader(tcp) examines it, finishes incomplete line if needed,
824  * the sets it to tcp.
825  * line_ended() clears printing_tcp and resets ->curcol = 0.
826  * tcp->curcol == 0 check is also used to detect completeness
827  * of last line, since in -ff mode just checking printing_tcp for NULL
828  * is not enough.
829  *
830  * If you change this code, test log generation in both -f and -ff modes
831  * using:
832  * strace -oLOG -f[f] test/threaded_execve
833  * strace -oLOG -f[f] test/sigkill_rain
834  * strace -oLOG -f[f] -p "`pidof web_browser`"
835  */
836 extern struct tcb *printing_tcp;
837 extern void printleader(struct tcb *);
838 extern void line_ended(void);
839 extern void tabto(void);
840 extern void tprintf(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
841 extern void tprints(const char *str);
842 extern void tprintf_comment(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
843 extern void tprints_comment(const char *str);
844
845 #if SUPPORTED_PERSONALITIES > 1
846 extern void set_personality(unsigned int personality);
847 extern unsigned current_personality;
848 #else
849 # define set_personality(personality) ((void)0)
850 # define current_personality 0
851 #endif
852
853 #if SUPPORTED_PERSONALITIES == 1
854 # define current_wordsize PERSONALITY0_WORDSIZE
855 # define current_klongsize PERSONALITY0_KLONGSIZE
856 #else
857 # if SUPPORTED_PERSONALITIES == 2 && PERSONALITY0_WORDSIZE == PERSONALITY1_WORDSIZE
858 #  define current_wordsize PERSONALITY0_WORDSIZE
859 # else
860 extern unsigned current_wordsize;
861 # endif
862 # if SUPPORTED_PERSONALITIES == 2 && PERSONALITY0_KLONGSIZE == PERSONALITY1_KLONGSIZE
863 #  define current_klongsize PERSONALITY0_KLONGSIZE
864 # else
865 extern unsigned current_klongsize;
866 # endif
867 #endif
868
869 #define max_addr() (~0ULL >> ((8 - current_wordsize) * 8))
870 #define max_kaddr() (~0ULL >> ((8 - current_klongsize) * 8))
871
872 /*
873  * When u64 is interpreted by the kernel as an address, there is a difference
874  * in behaviour between 32-bit and 64-bit kernel in the way u64_to_user_ptr
875  * works (32-bit kernel trims higher bits during conversion which may result
876  * to a valid address).  Since 32-bit strace cannot figure out what kind of
877  * kernel the tracee is running on, it has to account for both possibilities.
878  */
879 #if CAN_ARCH_BE_COMPAT_ON_64BIT_KERNEL
880
881 /**
882  * Print raw 64-bit value as an address if it's too big to fit in strace's
883  * kernel_long_t.
884  */
885 static inline void
886 print_big_u64_addr(const uint64_t addr)
887 {
888         if (sizeof(kernel_long_t) < 8 && addr > max_kaddr()) {
889                 printaddr64(addr);
890                 tprints(" or ");
891         }
892 }
893 #else /* !CAN_ARCH_BE_COMPAT_ON_64BIT_KERNEL */
894 # define print_big_u64_addr(addr_) ((void) 0)
895 #endif /* CAN_ARCH_BE_COMPAT_ON_64BIT_KERNEL */
896
897 #if SIZEOF_KERNEL_LONG_T > 4            \
898  && (SIZEOF_LONG < SIZEOF_KERNEL_LONG_T || !defined(current_wordsize))
899 # define ANY_WORDSIZE_LESS_THAN_KERNEL_LONG     1
900 #else
901 # define ANY_WORDSIZE_LESS_THAN_KERNEL_LONG     0
902 #endif
903
904 #define DECL_PRINTNUM(name)                                             \
905 extern bool                                                             \
906 printnum_ ## name(struct tcb *, kernel_ulong_t addr, const char *fmt)   \
907         ATTRIBUTE_FORMAT((printf, 3, 0))                                \
908 /* End of DECL_PRINTNUM definition. */
909
910 DECL_PRINTNUM(short);
911 DECL_PRINTNUM(int);
912 DECL_PRINTNUM(int64);
913 #undef DECL_PRINTNUM
914
915 #define DECL_PRINTNUM_ADDR(name)                                        \
916 extern bool                                                             \
917 printnum_addr_ ## name(struct tcb *, kernel_ulong_t addr)               \
918 /* End of DECL_PRINTNUM_ADDR definition. */
919
920 DECL_PRINTNUM_ADDR(int);
921 DECL_PRINTNUM_ADDR(int64);
922 #undef DECL_PRINTNUM_ADDR
923
924 #ifndef current_wordsize
925 extern bool
926 printnum_long_int(struct tcb *, kernel_ulong_t addr,
927                   const char *fmt_long, const char *fmt_int)
928         ATTRIBUTE_FORMAT((printf, 3, 0))
929         ATTRIBUTE_FORMAT((printf, 4, 0));
930 extern bool printnum_addr_long_int(struct tcb *, kernel_ulong_t addr);
931 # define printnum_slong(tcp, addr) \
932         printnum_long_int((tcp), (addr), "%" PRId64, "%d")
933 # define printnum_ulong(tcp, addr) \
934         printnum_long_int((tcp), (addr), "%" PRIu64, "%u")
935 # define printnum_ptr(tcp, addr) \
936         printnum_addr_long_int((tcp), (addr))
937 #elif current_wordsize > 4
938 # define printnum_slong(tcp, addr) \
939         printnum_int64((tcp), (addr), "%" PRId64)
940 # define printnum_ulong(tcp, addr) \
941         printnum_int64((tcp), (addr), "%" PRIu64)
942 # define printnum_ptr(tcp, addr) \
943         printnum_addr_int64((tcp), (addr))
944 #else /* current_wordsize == 4 */
945 # define printnum_slong(tcp, addr) \
946         printnum_int((tcp), (addr), "%d")
947 # define printnum_ulong(tcp, addr) \
948         printnum_int((tcp), (addr), "%u")
949 # define printnum_ptr(tcp, addr) \
950         printnum_addr_int((tcp), (addr))
951 #endif
952
953 #ifndef current_klongsize
954 extern bool printnum_addr_klong_int(struct tcb *, kernel_ulong_t addr);
955 # define printnum_kptr(tcp, addr) \
956         printnum_addr_klong_int((tcp), (addr))
957 #elif current_klongsize > 4
958 # define printnum_kptr(tcp, addr) \
959         printnum_addr_int64((tcp), (addr))
960 #else /* current_klongsize == 4 */
961 # define printnum_kptr(tcp, addr) \
962         printnum_addr_int((tcp), (addr))
963 #endif
964
965 #define DECL_PRINTPAIR(name)                                            \
966 extern bool                                                             \
967 printpair_ ## name(struct tcb *, kernel_ulong_t addr, const char *fmt)  \
968         ATTRIBUTE_FORMAT((printf, 3, 0))                                \
969 /* End of DECL_PRINTPAIR definition. */
970
971 DECL_PRINTPAIR(int);
972 DECL_PRINTPAIR(int64);
973 #undef DECL_PRINTPAIR
974
975 static inline kernel_long_t
976 truncate_klong_to_current_wordsize(const kernel_long_t v)
977 {
978 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
979         if (current_wordsize < sizeof(v)) {
980                 return (int) v;
981         } else
982 #endif
983         {
984                 return v;
985         }
986 }
987
988 static inline kernel_ulong_t
989 truncate_kulong_to_current_wordsize(const kernel_ulong_t v)
990 {
991 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
992         if (current_wordsize < sizeof(v)) {
993                 return (unsigned int) v;
994         } else
995 #endif
996         {
997                 return v;
998         }
999 }
1000
1001 /*
1002  * Cast a pointer or a pointer-sized integer to kernel_ulong_t.
1003  */
1004 #define ptr_to_kulong(v) ((kernel_ulong_t) (unsigned long) (v))
1005
1006 /*
1007  * Zero-extend a signed integer type to unsigned long long.
1008  */
1009 #define zero_extend_signed_to_ull(v) \
1010         (sizeof(v) == sizeof(char) ? (unsigned long long) (unsigned char) (v) : \
1011          sizeof(v) == sizeof(short) ? (unsigned long long) (unsigned short) (v) : \
1012          sizeof(v) == sizeof(int) ? (unsigned long long) (unsigned int) (v) : \
1013          sizeof(v) == sizeof(long) ? (unsigned long long) (unsigned long) (v) : \
1014          (unsigned long long) (v))
1015
1016 /*
1017  * Sign-extend an unsigned integer type to long long.
1018  */
1019 #define sign_extend_unsigned_to_ll(v) \
1020         (sizeof(v) == sizeof(char) ? (long long) (char) (v) : \
1021          sizeof(v) == sizeof(short) ? (long long) (short) (v) : \
1022          sizeof(v) == sizeof(int) ? (long long) (int) (v) : \
1023          sizeof(v) == sizeof(long) ? (long long) (long) (v) : \
1024          (long long) (v))
1025
1026 extern const struct_sysent sysent0[];
1027 extern const char *const errnoent0[];
1028 extern const char *const signalent0[];
1029 extern const struct_ioctlent ioctlent0[];
1030
1031 extern const char *const personality_names[];
1032
1033 #if SUPPORTED_PERSONALITIES > 1
1034 extern const struct_sysent *sysent;
1035 extern const char *const *errnoent;
1036 extern const char *const *signalent;
1037 extern const struct_ioctlent *ioctlent;
1038 #else
1039 # define sysent     sysent0
1040 # define errnoent   errnoent0
1041 # define signalent  signalent0
1042 # define ioctlent   ioctlent0
1043 #endif
1044
1045 extern unsigned nsyscalls;
1046 extern unsigned nerrnos;
1047 extern unsigned nsignals;
1048 extern unsigned nioctlents;
1049
1050 extern const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES];
1051 extern const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES];
1052 extern struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
1053
1054 #ifdef IN_MPERS_BOOTSTRAP
1055 /* Transform multi-line MPERS_PRINTER_DECL statements to one-liners.  */
1056 # define MPERS_PRINTER_DECL(type, name, ...) MPERS_PRINTER_DECL(type, name, __VA_ARGS__)
1057 #else /* !IN_MPERS_BOOTSTRAP */
1058 # if SUPPORTED_PERSONALITIES > 1
1059 #  include "printers.h"
1060 # else
1061 #  include "native_printer_decls.h"
1062 # endif
1063 # define MPERS_PRINTER_DECL(type, name, ...) type MPERS_FUNC_NAME(name)(__VA_ARGS__)
1064 #endif /* !IN_MPERS_BOOTSTRAP */
1065
1066 /* Checks that sysent[scno] is not out of range. */
1067 static inline bool
1068 scno_in_range(kernel_ulong_t scno)
1069 {
1070         return scno < nsyscalls;
1071 }
1072
1073 /*
1074  * Checks whether scno is not out of range,
1075  * its corresponding sysent[scno].sys_func is non-NULL,
1076  * and its sysent[scno].sys_flags has no TRACE_INDIRECT_SUBCALL flag set.
1077  */
1078 static inline bool
1079 scno_is_valid(kernel_ulong_t scno)
1080 {
1081         return scno_in_range(scno)
1082                && sysent[scno].sys_func
1083                && !(sysent[scno].sys_flags & TRACE_INDIRECT_SUBCALL);
1084 }
1085
1086 #define MPERS_FUNC_NAME__(prefix, name) prefix ## name
1087 #define MPERS_FUNC_NAME_(prefix, name) MPERS_FUNC_NAME__(prefix, name)
1088 #define MPERS_FUNC_NAME(name) MPERS_FUNC_NAME_(MPERS_PREFIX, name)
1089
1090 #define SYS_FUNC_NAME(syscall_name) MPERS_FUNC_NAME(syscall_name)
1091
1092 #define SYS_FUNC(syscall_name) int SYS_FUNC_NAME(sys_ ## syscall_name)(struct tcb *tcp)
1093
1094 #endif /* !STRACE_DEFS_H */