]> granicus.if.org Git - strace/blob - defs.h
Fix preprocessor indentation
[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-2019 The strace developers.
6  * All rights reserved.
7  *
8  * SPDX-License-Identifier: LGPL-2.1-or-later
9  */
10
11 #ifndef STRACE_DEFS_H
12 # define STRACE_DEFS_H
13
14 # ifdef HAVE_CONFIG_H
15 #  include "config.h"
16 # endif
17
18 # include <features.h>
19 # include <stdbool.h>
20 # include <stdint.h>
21 # include <inttypes.h>
22 # include <sys/types.h>
23 # include <stddef.h>
24 # include <unistd.h>
25 # include <stdlib.h>
26 # include <stdio.h>
27 /* Open-coding isprint(ch) et al proved more efficient than calling
28  * generalized libc interface. We don't *want* to do non-ASCII anyway.
29  */
30 /* #include <ctype.h> */
31 # include <string.h>
32 # include <errno.h>
33 # include <time.h>
34 # include <sys/time.h>
35
36 # include "arch_defs.h"
37 # include "error_prints.h"
38 # include "gcc_compat.h"
39 # include "kernel_types.h"
40 # include "list.h"
41 # include "macros.h"
42 # include "mpers_type.h"
43 # include "string_to_uint.h"
44 # include "sysent.h"
45 # include "xmalloc.h"
46
47 # ifndef HAVE_STRERROR
48 const char *strerror(int);
49 # endif
50 # ifndef HAVE_STPCPY
51 /* Some libc have stpcpy, some don't. Sigh...
52  * Roll our private implementation...
53  */
54 #  undef stpcpy
55 #  define stpcpy strace_stpcpy
56 extern char *stpcpy(char *dst, const char *src);
57 # endif
58
59 /* Glibc has an efficient macro for sigemptyset
60  * (it just does one or two assignments of 0 to internal vector of longs).
61  */
62 # if defined(__GLIBC__) && defined(__sigemptyset) && !defined(sigemptyset)
63 #  define sigemptyset __sigemptyset
64 # endif
65
66 /* Configuration section */
67 # ifndef DEFAULT_STRLEN
68 /* default maximum # of bytes printed in `printstr', change with -s switch */
69 #  define DEFAULT_STRLEN        32
70 # endif
71 # ifndef DEFAULT_ACOLUMN
72 #  define DEFAULT_ACOLUMN       40      /* default alignment column for results */
73 # endif
74 /*
75  * Maximum number of args to a syscall.
76  *
77  * Make sure that all entries in all syscallent.h files have nargs <= MAX_ARGS!
78  * linux/<ARCH>/syscallent*.h:
79  *      all have nargs <= 6 except mips o32 which has nargs <= 7.
80  */
81 # ifndef MAX_ARGS
82 #  ifdef LINUX_MIPSO32
83 #   define MAX_ARGS     7
84 #  else
85 #   define MAX_ARGS     6
86 #  endif
87 # endif
88 /* default sorting method for call profiling */
89 # ifndef DEFAULT_SORTBY
90 #  define DEFAULT_SORTBY "time"
91 # endif
92
93 /* To force NOMMU build, set to 1 */
94 # define NOMMU_SYSTEM 0
95
96 # ifndef ERESTARTSYS
97 #  define ERESTARTSYS    512
98 # endif
99 # ifndef ERESTARTNOINTR
100 #  define ERESTARTNOINTR 513
101 # endif
102 # ifndef ERESTARTNOHAND
103 #  define ERESTARTNOHAND 514
104 # endif
105 # ifndef ERESTART_RESTARTBLOCK
106 #  define ERESTART_RESTARTBLOCK 516
107 # endif
108
109 # define PERSONALITY0_WORDSIZE  SIZEOF_LONG
110 # define PERSONALITY0_KLONGSIZE SIZEOF_KERNEL_LONG_T
111 # define PERSONALITY0_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
112 # define PERSONALITY0_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
113
114 # if SUPPORTED_PERSONALITIES > 1
115 #  define PERSONALITY1_WORDSIZE  4
116 #  define PERSONALITY1_KLONGSIZE PERSONALITY1_WORDSIZE
117 # endif
118
119 # if SUPPORTED_PERSONALITIES > 2
120 #  define PERSONALITY2_WORDSIZE  4
121 #  define PERSONALITY2_KLONGSIZE PERSONALITY0_KLONGSIZE
122 # endif
123
124 # if SUPPORTED_PERSONALITIES > 1 && defined HAVE_M32_MPERS
125 #  define PERSONALITY1_INCLUDE_PRINTERS_DECLS "m32_printer_decls.h"
126 #  define PERSONALITY1_INCLUDE_PRINTERS_DEFS "m32_printer_defs.h"
127 #  define PERSONALITY1_INCLUDE_FUNCS "m32_funcs.h"
128 #  define MPERS_m32_IOCTL_MACROS "ioctl_redefs1.h"
129 #  define HAVE_PERSONALITY_1_MPERS 1
130 # else
131 #  define PERSONALITY1_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
132 #  define PERSONALITY1_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
133 #  define PERSONALITY1_INCLUDE_FUNCS "empty.h"
134 #  define HAVE_PERSONALITY_1_MPERS 0
135 # endif
136
137 # if SUPPORTED_PERSONALITIES > 2 && defined HAVE_MX32_MPERS
138 #  define PERSONALITY2_INCLUDE_FUNCS "mx32_funcs.h"
139 #  define PERSONALITY2_INCLUDE_PRINTERS_DECLS "mx32_printer_decls.h"
140 #  define PERSONALITY2_INCLUDE_PRINTERS_DEFS "mx32_printer_defs.h"
141 #  define MPERS_mx32_IOCTL_MACROS "ioctl_redefs2.h"
142 #  define HAVE_PERSONALITY_2_MPERS 1
143 # else
144 #  define PERSONALITY2_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
145 #  define PERSONALITY2_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
146 #  define PERSONALITY2_INCLUDE_FUNCS "empty.h"
147 #  define HAVE_PERSONALITY_2_MPERS 0
148 # endif
149
150 # ifdef WORDS_BIGENDIAN
151 #  define is_bigendian true
152 # else
153 #  define is_bigendian false
154 # endif
155
156 # if SUPPORTED_PERSONALITIES > 1
157 extern void set_personality(unsigned int personality);
158 extern unsigned current_personality;
159 # else
160 #  define set_personality(personality) ((void)0)
161 #  define current_personality 0
162 # endif
163
164 # if SUPPORTED_PERSONALITIES == 1
165 #  define current_wordsize PERSONALITY0_WORDSIZE
166 #  define current_klongsize PERSONALITY0_KLONGSIZE
167 # else
168 #  if SUPPORTED_PERSONALITIES == 2 && PERSONALITY0_WORDSIZE == PERSONALITY1_WORDSIZE
169 #   define current_wordsize PERSONALITY0_WORDSIZE
170 #  else
171 extern unsigned current_wordsize;
172 #  endif
173 #  if SUPPORTED_PERSONALITIES == 2 && PERSONALITY0_KLONGSIZE == PERSONALITY1_KLONGSIZE
174 #   define current_klongsize PERSONALITY0_KLONGSIZE
175 #  else
176 extern unsigned current_klongsize;
177 #  endif
178 # endif
179
180 # define max_addr() (~0ULL >> ((8 - current_wordsize) * 8))
181 # define max_kaddr() (~0ULL >> ((8 - current_klongsize) * 8))
182
183 /* Shorthands for defining word/klong-based dispatcher function bodies */
184 # ifndef current_wordsize
185 #  define opt_wordsize(opt_64_, opt_32_) \
186         ((current_wordsize > sizeof(uint32_t)) ? (opt_64_) : (opt_32_))
187 #  define dispatch_wordsize(call_64_, call_32_, ...) \
188         ((current_wordsize > sizeof(uint32_t)) \
189                 ? (call_64_)(__VA_ARGS__) : (call_32_)(__VA_ARGS__))
190 # elif current_wordsize > 4
191 #  define opt_wordsize(opt_64_, opt_32_) (opt_64_)
192 #  define dispatch_wordsize(call_64_, call_32_, ...) ((call_64_)(__VA_ARGS__))
193 # else /* current_wordsize == 4 */
194 #  define opt_wordsize(opt_64_, opt_32_) (opt_32_)
195 #  define dispatch_wordsize(call_64_, call_32_, ...) ((call_32_)(__VA_ARGS__))
196 # endif
197
198 # ifndef current_klongsize
199 #  define opt_klongsize(opt_64_, opt_32_) \
200         ((current_klongsize > sizeof(uint32_t)) ? (opt_64_) : (opt_32_))
201 #  define dispatch_klongsize(call_64_, call_32_, ...) \
202         ((current_klongsize > sizeof(uint32_t)) \
203                 ? (call_64_)(__VA_ARGS__) : (call_32_)(__VA_ARGS__))
204 # elif current_klongsize > 4
205 #  define opt_klongsize(opt_64_, opt_32_) (opt_64_)
206 #  define dispatch_klongsize(call_64_, call_32_, ...) ((call_64_)(__VA_ARGS__))
207 # else /* current_klongsize == 4 */
208 #  define opt_klongsize(opt_64_, opt_32_) (opt_32_)
209 #  define dispatch_klongsize(call_64_, call_32_, ...) ((call_32_)(__VA_ARGS__))
210 # endif
211
212
213 typedef struct ioctlent {
214         const char *symbol;
215         unsigned int code;
216 } struct_ioctlent;
217
218 # define INJECT_F_SIGNAL                0x01
219 # define INJECT_F_ERROR         0x02
220 # define INJECT_F_RETVAL                0x04
221 # define INJECT_F_DELAY_ENTER   0x08
222 # define INJECT_F_DELAY_EXIT    0x10
223 # define INJECT_F_SYSCALL       0x20
224
225 # define INJECT_ACTION_FLAGS    \
226         (INJECT_F_SIGNAL        \
227         |INJECT_F_ERROR         \
228         |INJECT_F_RETVAL        \
229         |INJECT_F_DELAY_ENTER   \
230         |INJECT_F_DELAY_EXIT    \
231         )
232
233 struct inject_data {
234         uint8_t flags;          /* 6 of 8 flags are used so far */
235         uint8_t signo;          /* NSIG <= 128 */
236         uint16_t rval_idx;      /* index in retval_vec */
237         uint16_t delay_idx;     /* index in delay_data_vec */
238         uint16_t scno;          /* syscall to be injected instead of -1 */
239 };
240
241 struct inject_opts {
242         uint16_t first;
243         uint16_t step;
244         struct inject_data data;
245 };
246
247 # define MAX_ERRNO_VALUE                        4095
248
249 /* Trace Control Block */
250 struct tcb {
251         int flags;              /* See below for TCB_ values */
252         int pid;                /* If 0, this tcb is free */
253         int qual_flg;           /* qual_flags[scno] or DEFAULT_QUAL_FLAGS + RAW */
254         unsigned long u_error;  /* Error code */
255         kernel_ulong_t scno;    /* System call number */
256         kernel_ulong_t u_arg[MAX_ARGS]; /* System call arguments */
257         kernel_long_t u_rval;   /* Return value */
258 # if SUPPORTED_PERSONALITIES > 1
259         unsigned int currpers;  /* Personality at the time of scno update */
260 # endif
261         int sys_func_rval;      /* Syscall entry parser's return value */
262         int curcol;             /* Output column for this process */
263         FILE *outf;             /* Output file for this process */
264         struct staged_output_data *staged_output_data;
265
266         const char *auxstr;     /* Auxiliary info from syscall (see RVAL_STR) */
267         void *_priv_data;       /* Private data for syscall decoding functions */
268         void (*_free_priv_data)(void *); /* Callback for freeing priv_data */
269         const struct_sysent *s_ent; /* sysent[scno] or a stub struct for bad
270                                      * scno.  Use tcp_sysent() macro for access.
271                                      */
272         const struct_sysent *s_prev_ent; /* for "resuming interrupted SYSCALL" msg */
273         struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
274         struct timespec stime;  /* System time usage as of last process wait */
275         struct timespec ltime;  /* System time usage as of last syscall entry */
276         struct timespec atime;  /* System time right after attach */
277         struct timespec etime;  /* Syscall entry time (CLOCK_MONOTONIC) */
278         struct timespec delay_expiration_time; /* When does the delay end */
279
280         struct mmap_cache_t *mmap_cache;
281
282         /*
283          * Data that is stored during process wait traversal.
284          * We use indices as the actual data is stored in an array
285          * that is realloc'ed at runtime.
286          */
287         size_t wait_data_idx;
288         /** Wait data storage for a delayed process. */
289         struct tcb_wait_data *delayed_wait_data;
290         struct list_item wait_list;
291
292
293 # ifdef HAVE_LINUX_KVM_H
294         struct vcpu_info *vcpu_info_list;
295 # endif
296
297 # ifdef ENABLE_STACKTRACE
298         void *unwind_ctx;
299         struct unwind_queue_t *unwind_queue;
300 # endif
301 };
302
303 /* TCB flags */
304 /* We have attached to this process, but did not see it stopping yet */
305 # define TCB_STARTUP            0x01
306 # define TCB_IGNORE_ONE_SIGSTOP 0x02    /* Next SIGSTOP is to be ignored */
307 /*
308  * Are we in system call entry or in syscall exit?
309  *
310  * This bit is set in syscall_entering_finish() and cleared in
311  * syscall_exiting_finish().
312  * Other stops which are possible directly after syscall entry (death, ptrace
313  * event stop) are handled without calling syscall_{entering,exiting}_*().
314  *
315  * Use entering(tcp) / exiting(tcp) to check this bit to make code more
316  * readable.
317  */
318 # define TCB_INSYSCALL  0x04
319 # define TCB_ATTACHED   0x08    /* We attached to it already */
320 # define TCB_REPRINT    0x10    /* We should reprint this syscall on exit */
321 # define TCB_FILTERED   0x20    /* This system call has been filtered out */
322 # define TCB_TAMPERED   0x40    /* A syscall has been tampered with */
323 # define TCB_HIDE_LOG   0x80    /* We should hide everything (until execve) */
324 # define TCB_CHECK_EXEC_SYSCALL 0x100   /* Check whether this execve syscall succeeded */
325 # define TCB_SKIP_DETACH_ON_FIRST_EXEC  0x200   /* -b execve should skip detach on first execve */
326 # define TCB_GRABBED    0x400   /* We grab the process and can catch it
327                                  * in the middle of a syscall */
328 # define TCB_RECOVERING 0x800   /* We try to recover after detecting incorrect
329                                  * syscall entering/exiting state */
330 # define TCB_INJECT_DELAY_EXIT  0x1000  /* Current syscall needs to be delayed
331                                            on exit */
332 # define TCB_DELAYED    0x2000  /* Current syscall has been delayed */
333 # define TCB_TAMPERED_NO_FAIL 0x4000    /* We tamper tcb with syscall
334                                            that should not fail. */
335 # define TCB_SECCOMP_FILTER     0x8000  /* This process has a seccomp filter
336                                          * attached.
337                                          */
338
339 /* qualifier flags */
340 # define QUAL_TRACE     0x001   /* this system call should be traced */
341 # define QUAL_ABBREV    0x002   /* abbreviate the structures of this syscall */
342 # define QUAL_VERBOSE   0x004   /* decode the structures of this syscall */
343 # define QUAL_RAW       0x008   /* print all args in hex for this syscall */
344 # define QUAL_INJECT    0x010   /* tamper with this system call on purpose */
345
346 # define DEFAULT_QUAL_FLAGS (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
347
348 # define entering(tcp)  (!((tcp)->flags & TCB_INSYSCALL))
349 # define exiting(tcp)   ((tcp)->flags & TCB_INSYSCALL)
350 # define syserror(tcp)  ((tcp)->u_error != 0)
351 # define traced(tcp)    ((tcp)->qual_flg & QUAL_TRACE)
352 # define verbose(tcp)   ((tcp)->qual_flg & QUAL_VERBOSE)
353 # define abbrev(tcp)    ((tcp)->qual_flg & QUAL_ABBREV)
354 # define raw(tcp)       ((tcp)->qual_flg & QUAL_RAW)
355 # define inject(tcp)    ((tcp)->qual_flg & QUAL_INJECT)
356 # define filtered(tcp)  ((tcp)->flags & TCB_FILTERED)
357 # define hide_log(tcp)  ((tcp)->flags & TCB_HIDE_LOG)
358 # define check_exec_syscall(tcp)        ((tcp)->flags & TCB_CHECK_EXEC_SYSCALL)
359 # define syscall_tampered(tcp)  ((tcp)->flags & TCB_TAMPERED)
360 # define recovering(tcp)        ((tcp)->flags & TCB_RECOVERING)
361 # define inject_delay_exit(tcp) ((tcp)->flags & TCB_INJECT_DELAY_EXIT)
362 # define syscall_delayed(tcp)   ((tcp)->flags & TCB_DELAYED)
363 # define syscall_tampered_nofail(tcp) ((tcp)->flags & TCB_TAMPERED_NO_FAIL)
364 # define has_seccomp_filter(tcp)        ((tcp)->flags & TCB_SECCOMP_FILTER)
365
366 extern const struct_sysent stub_sysent;
367 # define tcp_sysent(tcp) (tcp->s_ent ?: &stub_sysent)
368 # define n_args(tcp) (tcp_sysent(tcp)->nargs)
369
370 # include "xlat.h"
371
372 extern const struct xlat addrfams[];
373 extern const struct xlat arp_hardware_types[];
374 extern const struct xlat at_flags[];
375 extern const struct xlat clocknames[];
376 extern const struct xlat dirent_types[];
377 extern const struct xlat ethernet_protocols[];
378 extern const struct xlat inet_protocols[];
379 extern const struct xlat evdev_abs[];
380 extern const struct xlat audit_arch[];
381 extern const struct xlat evdev_ev[];
382 extern const struct xlat iffflags[];
383 extern const struct xlat ip_type_of_services[];
384 extern const struct xlat ipc_private[];
385 extern const struct xlat msg_flags[];
386 extern const struct xlat netlink_protocols[];
387 extern const struct xlat nl_netfilter_msg_types[];
388 extern const struct xlat nl_route_types[];
389 extern const struct xlat open_access_modes[];
390 extern const struct xlat open_mode_flags[];
391 extern const struct xlat pollflags[];
392 extern const struct xlat ptrace_cmds[];
393 extern const struct xlat resource_flags[];
394 extern const struct xlat routing_scopes[];
395 extern const struct xlat routing_table_ids[];
396 extern const struct xlat routing_types[];
397 extern const struct xlat rwf_flags[];
398 extern const struct xlat seccomp_filter_flags[];
399 extern const struct xlat seccomp_ret_action[];
400 extern const struct xlat setns_types[];
401 extern const struct xlat sg_io_info[];
402 extern const struct xlat socketlayers[];
403 extern const struct xlat socktypes[];
404 extern const struct xlat tcp_state_flags[];
405 extern const struct xlat tcp_states[];
406 extern const struct xlat whence_codes[];
407
408 /* Format of syscall return values */
409 # define RVAL_UDECIMAL  000     /* unsigned decimal format */
410 # define RVAL_HEX       001     /* hex format */
411 # define RVAL_OCTAL     002     /* octal format */
412 # define RVAL_FD                010     /* file descriptor */
413 # define RVAL_MASK      013     /* mask for these values */
414
415 # define RVAL_STR       020     /* Print `auxstr' field after return val */
416 # define RVAL_NONE      040     /* Print nothing */
417
418 # define RVAL_DECODED   0100    /* syscall decoding finished */
419 # define RVAL_IOCTL_DECODED 0200        /* ioctl sub-parser successfully decoded
420                                    the argument */
421
422 # define IOCTL_NUMBER_UNKNOWN 0
423 # define IOCTL_NUMBER_HANDLED 1
424 # define IOCTL_NUMBER_STOP_LOOKUP 010
425
426 # define indirect_ipccall(tcp) (tcp_sysent(tcp)->sys_flags & TRACE_INDIRECT_SUBCALL)
427
428 enum sock_proto {
429         SOCK_PROTO_UNKNOWN,
430         SOCK_PROTO_UNIX,
431         SOCK_PROTO_TCP,
432         SOCK_PROTO_UDP,
433         SOCK_PROTO_UDPLITE,
434         SOCK_PROTO_DCCP,
435         SOCK_PROTO_SCTP,
436         SOCK_PROTO_L2TP_IP,
437         SOCK_PROTO_PING,
438         SOCK_PROTO_RAW,
439         SOCK_PROTO_TCPv6,
440         SOCK_PROTO_UDPv6,
441         SOCK_PROTO_UDPLITEv6,
442         SOCK_PROTO_DCCPv6,
443         SOCK_PROTO_L2TP_IPv6,
444         SOCK_PROTO_SCTPv6,
445         SOCK_PROTO_PINGv6,
446         SOCK_PROTO_RAWv6,
447         SOCK_PROTO_NETLINK,
448 };
449 extern enum sock_proto get_proto_by_name(const char *);
450 extern int get_family_by_proto(enum sock_proto proto);
451
452 enum iov_decode {
453         IOV_DECODE_ADDR,
454         IOV_DECODE_STR,
455         IOV_DECODE_NETLINK
456 };
457
458 typedef enum {
459         CFLAG_NONE = 0,
460         CFLAG_ONLY_STATS,
461         CFLAG_BOTH
462 } cflag_t;
463 extern cflag_t cflag;
464 extern bool Tflag;
465 extern bool iflag;
466 extern bool count_wallclock;
467 extern unsigned int qflag;
468 extern unsigned int show_fd_path;
469 /* are we filtering traces based on paths? */
470 extern struct path_set {
471         const char **paths_selected;
472         size_t num_selected;
473         size_t size;
474 } global_path_set;
475 # define tracing_paths (global_path_set.num_selected != 0)
476 extern unsigned xflag;
477 extern unsigned followfork;
478 # ifdef ENABLE_STACKTRACE
479 /* if this is true do the stack trace for every system call */
480 extern bool stack_trace_enabled;
481 # else
482 #  define stack_trace_enabled 0
483 # endif
484 extern unsigned ptrace_setoptions;
485 extern unsigned max_strlen;
486 extern unsigned os_release;
487 # undef KERNEL_VERSION
488 # define KERNEL_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
489
490 extern int read_int_from_file(struct tcb *, const char *, int *);
491
492 extern void set_sortby(const char *);
493 extern int set_overhead(const char *);
494
495 extern bool get_instruction_pointer(struct tcb *, kernel_ulong_t *);
496 extern bool get_stack_pointer(struct tcb *, kernel_ulong_t *);
497 extern void print_instruction_pointer(struct tcb *);
498
499 extern void print_syscall_resume(struct tcb *tcp);
500
501 extern int syscall_entering_decode(struct tcb *);
502 extern int syscall_entering_trace(struct tcb *, unsigned int *);
503 extern void syscall_entering_finish(struct tcb *, int);
504
505 extern int syscall_exiting_decode(struct tcb *, struct timespec *);
506 extern int syscall_exiting_trace(struct tcb *, struct timespec *, int);
507 extern void syscall_exiting_finish(struct tcb *);
508
509 extern void count_syscall(struct tcb *, const struct timespec *);
510 extern void call_summary(FILE *);
511
512 extern void clear_regs(struct tcb *tcp);
513 extern int get_scno(struct tcb *);
514 extern kernel_ulong_t get_rt_sigframe_addr(struct tcb *);
515
516 /**
517  * Convert a (shuffled) syscall number to the corresponding syscall name.
518  *
519  * @param scno Syscall number.
520  * @return     String literal corresponding to the syscall number in case latter
521  *             is valid; NULL otherwise.
522  */
523 extern const char *syscall_name(kernel_ulong_t scno);
524 /**
525  * Convert a syscall name to the corresponding (shuffled) syscall number.
526  *
527  * @param s     Syscall name.
528  * @param p     Personality.
529  * @param start From which position in syscall entry table resume the search.
530  * @return      Shuffled syscall number (ready to use against sysent_vec)
531  *              if syscall name is found; -1 otherwise.
532  */
533 extern kernel_long_t scno_by_name(const char *s, unsigned p,
534                                   kernel_long_t start);
535 /**
536  * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
537  * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
538  *
539  * @param scno Raw or shuffled syscall number.
540  * @return     Shuffled or raw syscall number, respectively.
541  */
542 extern kernel_ulong_t shuffle_scno(kernel_ulong_t scno);
543 /**
544  * Print error name in accordance with current xlat style setting.
545  *
546  * @param err     Error value.
547  * @param negated If set to true, negative values of the err parameter indicate
548  *                error condition, otherwise positive.
549  */
550 extern void print_err(int64_t err, bool negated);
551
552 extern bool is_erestart(struct tcb *);
553 extern void temporarily_clear_syserror(struct tcb *);
554 extern void restore_cleared_syserror(struct tcb *);
555
556 extern void *get_tcb_priv_data(const struct tcb *);
557 extern int set_tcb_priv_data(struct tcb *, void *priv_data,
558                              void (*free_priv_data)(void *));
559 extern void free_tcb_priv_data(struct tcb *);
560
561 static inline unsigned long get_tcb_priv_ulong(const struct tcb *tcp)
562 {
563         return (unsigned long) get_tcb_priv_data(tcp);
564 }
565
566 static inline int set_tcb_priv_ulong(struct tcb *tcp, unsigned long val)
567 {
568         return set_tcb_priv_data(tcp, (void *) val, 0);
569 }
570
571 /**
572  * @return 0 on success, -1 on error.
573  */
574 extern int
575 umoven(struct tcb *, kernel_ulong_t addr, unsigned int len, void *laddr);
576 # define umove(pid, addr, objp) \
577         umoven((pid), (addr), sizeof(*(objp)), (void *) (objp))
578
579 /**
580  * @return true on success, false on error.
581  */
582 extern bool
583 tfetch_mem64(struct tcb *, uint64_t addr, unsigned int len, void *laddr);
584
585 static inline bool
586 tfetch_mem(struct tcb *tcp, const kernel_ulong_t addr,
587            unsigned int len, void *laddr)
588 {
589         return tfetch_mem64(tcp, addr, len, laddr);
590 }
591 # define tfetch_obj(pid, addr, objp)    \
592         tfetch_mem((pid), (addr), sizeof(*(objp)), (void *) (objp))
593
594 /**
595  * @return true on success, false on error.
596  */
597 extern bool
598 tfetch_mem64_ignore_syserror(struct tcb *, uint64_t addr,
599                              unsigned int len, void *laddr);
600
601 static inline bool
602 tfetch_mem_ignore_syserror(struct tcb *tcp, const kernel_ulong_t addr,
603                            unsigned int len, void *laddr)
604 {
605         return tfetch_mem64_ignore_syserror(tcp, addr, len, laddr);
606 }
607
608 /**
609  * @return 0 on success, -1 on error (and print addr).
610  */
611 extern int
612 umoven_or_printaddr64(struct tcb *, uint64_t addr,
613                       unsigned int len, void *laddr);
614 # define umove_or_printaddr64(pid, addr, objp)  \
615         umoven_or_printaddr64((pid), (addr), sizeof(*(objp)), (void *) (objp))
616
617 static inline int
618 umoven_or_printaddr(struct tcb *tcp, const kernel_ulong_t addr,
619                     unsigned int len, void *laddr)
620 {
621         return umoven_or_printaddr64(tcp, addr, len, laddr);
622 }
623 # define umove_or_printaddr(pid, addr, objp)    \
624         umoven_or_printaddr((pid), (addr), sizeof(*(objp)), (void *) (objp))
625
626 /**
627  * @return 0 on success, -1 on error (and print addr).
628  */
629 extern int
630 umoven_or_printaddr64_ignore_syserror(struct tcb *, uint64_t addr,
631                                       unsigned int len, void *laddr);
632 # define umove_or_printaddr64_ignore_syserror(pid, addr, objp)  \
633         umoven_or_printaddr64_ignore_syserror((pid), (addr), sizeof(*(objp)), \
634                                               (void *) (objp))
635
636 static inline int
637 umoven_or_printaddr_ignore_syserror(struct tcb *tcp, const kernel_ulong_t addr,
638                                     unsigned int len, void *laddr)
639 {
640         return umoven_or_printaddr64_ignore_syserror(tcp, addr, len, laddr);
641 }
642 # define umove_or_printaddr_ignore_syserror(pid, addr, objp)    \
643         umoven_or_printaddr_ignore_syserror((pid), (addr), sizeof(*(objp)), \
644                                             (void *) (objp))
645
646 /**
647  * @return strlen + 1 on success, 0 on success and no NUL seen, -1 on error.
648  */
649 extern int
650 umovestr(struct tcb *, kernel_ulong_t addr, unsigned int len, char *laddr);
651
652 /* Invalidate the cache used by umove* functions.  */
653 extern void invalidate_umove_cache(void);
654
655 extern int upeek(struct tcb *tcp, unsigned long, kernel_ulong_t *);
656 extern int upoke(struct tcb *tcp, unsigned long, kernel_ulong_t);
657
658 # if HAVE_ARCH_GETRVAL2
659 extern long getrval2(struct tcb *);
660 # endif
661
662 extern const char *signame(const int);
663 extern const char *sprintsigname(const int);
664 extern void pathtrace_select_set(const char *, struct path_set *);
665 extern bool pathtrace_match_set(struct tcb *, struct path_set *);
666
667 static inline void
668 pathtrace_select(const char *path)
669 {
670         return pathtrace_select_set(path, &global_path_set);
671 }
672
673 static inline bool
674 pathtrace_match(struct tcb *tcp)
675 {
676         return pathtrace_match_set(tcp, &global_path_set);
677 }
678
679 extern int getfdpath(struct tcb *, int, char *, unsigned);
680 extern unsigned long getfdinode(struct tcb *, int);
681 extern enum sock_proto getfdproto(struct tcb *, int);
682
683 extern const char *xlookup(const struct xlat *, const uint64_t);
684 extern const char *xlookup_le(const struct xlat *, uint64_t *);
685
686 struct dyxlat;
687 struct dyxlat *dyxlat_alloc(size_t nmemb);
688 void dyxlat_free(struct dyxlat *);
689 const struct xlat *dyxlat_get(const struct dyxlat *);
690 void dyxlat_add_pair(struct dyxlat *, uint64_t val, const char *str, size_t len);
691
692 const struct xlat *genl_families_xlat(struct tcb *tcp);
693
694 extern unsigned long get_pagesize(void);
695 extern int next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits);
696
697 /*
698  * Returns STR if it does not start with PREFIX,
699  * or a pointer to the first char in STR after PREFIX.
700  * The length of PREFIX is specified by PREFIX_LEN.
701  */
702 static inline const char *
703 str_strip_prefix_len(const char *str, const char *prefix, size_t prefix_len)
704 {
705         return strncmp(str, prefix, prefix_len) ? str : str + prefix_len;
706 }
707
708 # define STR_STRIP_PREFIX(str, prefix)  \
709         str_strip_prefix_len((str), (prefix), sizeof(prefix) - 1)
710
711 # define QUOTE_0_TERMINATED                     0x01
712 # define QUOTE_OMIT_LEADING_TRAILING_QUOTES     0x02
713 # define QUOTE_OMIT_TRAILING_0                  0x08
714 # define QUOTE_FORCE_HEX                                0x10
715 # define QUOTE_EMIT_COMMENT                     0x20
716
717 extern int string_quote(const char *, char *, unsigned int, unsigned int,
718                         const char *escape_chars);
719 extern int print_quoted_string_ex(const char *, unsigned int, unsigned int,
720                                   const char *escape_chars);
721 extern int print_quoted_string(const char *, unsigned int, unsigned int);
722 extern int print_quoted_cstring(const char *, unsigned int);
723
724 /* a refers to the lower numbered u_arg,
725  * b refers to the higher numbered u_arg
726  */
727 # ifdef WORDS_BIGENDIAN
728 #  define ULONG_LONG(a, b) \
729         ((unsigned long long)(unsigned)(b) | ((unsigned long long)(a)<<32))
730 # else
731 #  define ULONG_LONG(a, b) \
732         ((unsigned long long)(unsigned)(a) | ((unsigned long long)(b)<<32))
733 # endif
734 extern int getllval(struct tcb *, unsigned long long *, int);
735 extern int printllval(struct tcb *, const char *, int)
736         ATTRIBUTE_FORMAT((printf, 2, 0));
737
738 extern void printaddr64(uint64_t addr);
739
740 static inline void
741 printaddr(const kernel_ulong_t addr)
742 {
743         printaddr64(addr);
744 }
745
746 # define xlat_verbose(style_) ((style_) & XLAT_STYLE_VERBOSITY_MASK)
747 # define xlat_format(style_)  ((style_) & XLAT_STYLE_FORMAT_MASK)
748
749 extern enum xlat_style xlat_verbosity;
750
751 extern int printxvals_ex(uint64_t val, const char *dflt,
752                          enum xlat_style, const struct xlat *, ...)
753         ATTRIBUTE_SENTINEL;
754 # define printxvals(val_, dflt_, ...) \
755         printxvals_ex((val_), (dflt_), XLAT_STYLE_DEFAULT, __VA_ARGS__)
756 # define printxval_ex(xlat_, val_, dflt_, style_) \
757         printxvals_ex((val_), (dflt_), (style_), (xlat_), NULL)
758
759 extern int sprintxval_ex(char *buf, size_t size, const struct xlat *,
760                          unsigned int val, const char *dflt, enum xlat_style);
761
762 static inline int
763 sprintxval(char *buf, size_t size, const struct xlat *xlat, unsigned int val,
764            const char *dflt)
765 {
766         return sprintxval_ex(buf, size, xlat, val, dflt, XLAT_STYLE_DEFAULT);
767 }
768
769 enum xlat_style_private_flag_bits {
770         /* print_array */
771         PAF_PRINT_INDICES_BIT = XLAT_STYLE_SPEC_BITS + 1,
772         PAF_ARRAY_TRUNCATED_BIT,
773
774         /* print_xlat */
775         PXF_DEFAULT_STR_BIT,
776 };
777
778 # define FLAG_(name_) name_ = 1 << name_##_BIT
779
780 enum xlat_style_private_flags {
781         /* print_array */
782         FLAG_(PAF_PRINT_INDICES),
783         FLAG_(PAF_ARRAY_TRUNCATED),
784
785         /* print_xlat */
786         FLAG_(PXF_DEFAULT_STR),
787 };
788
789 # undef FLAG_
790
791 /** Print a value in accordance with xlat formatting settings. */
792 extern void print_xlat_ex(uint64_t val, const char *str, enum xlat_style style);
793 # define print_xlat(val_) \
794         print_xlat_ex((val_), #val_, XLAT_STYLE_DEFAULT)
795 # define print_xlat32(val_) \
796         print_xlat_ex((uint32_t) (val_), #val_, XLAT_STYLE_DEFAULT)
797 # define print_xlat_u(val_) \
798         print_xlat_ex((val_), #val_, XLAT_STYLE_FMT_U)
799 # define print_xlat_d(val_) \
800         print_xlat_ex((val_), #val_, XLAT_STYLE_FMT_D)
801
802 extern int printargs(struct tcb *);
803 extern int printargs_u(struct tcb *);
804 extern int printargs_d(struct tcb *);
805
806 extern int printflags_ex(uint64_t flags, const char *dflt,
807                          enum xlat_style, const struct xlat *, ...)
808         ATTRIBUTE_SENTINEL;
809 extern const char *sprintflags_ex(const char *prefix, const struct xlat *,
810                                   uint64_t flags, char sep, enum xlat_style);
811
812 static inline const char *
813 sprintflags(const char *prefix, const struct xlat *xlat, uint64_t flags)
814 {
815         return sprintflags_ex(prefix, xlat, flags, '\0', XLAT_STYLE_DEFAULT);
816 }
817
818 extern const char *sprinttime(long long sec);
819 extern const char *sprinttime_nsec(long long sec, unsigned long long nsec);
820 extern const char *sprinttime_usec(long long sec, unsigned long long usec);
821
822 # ifndef MAX_ADDR_LEN
823 #  define MAX_ADDR_LEN 32
824 # endif
825
826 extern const char *sprint_mac_addr(const uint8_t addr[], size_t size);
827 extern void print_mac_addr(const char *prefix,
828                            const uint8_t addr[], size_t size);
829
830 extern const char *sprint_hwaddr(const uint8_t addr[], size_t size,
831                                  uint32_t devtype);
832 extern void print_hwaddr(const char *prefix,
833                          const uint8_t addr[], size_t size, uint32_t devtype);
834
835 extern void print_uuid(const unsigned char *uuid);
836
837 extern void print_symbolic_mode_t(unsigned int);
838 extern void print_numeric_umode_t(unsigned short);
839 extern void print_numeric_long_umask(unsigned long);
840 extern void print_dev_t(unsigned long long dev);
841 extern void print_kernel_version(unsigned long version);
842 extern void print_abnormal_hi(kernel_ulong_t);
843 extern void print_ioprio(unsigned int ioprio);
844
845 extern bool print_int32_array_member(struct tcb *, void *elem_buf,
846                                      size_t elem_size, void *data);
847 extern bool print_uint32_array_member(struct tcb *, void *elem_buf,
848                                       size_t elem_size, void *data);
849 extern bool print_uint64_array_member(struct tcb *, void *elem_buf,
850                                       size_t elem_size, void *data);
851 extern bool print_xint32_array_member(struct tcb *, void *elem_buf,
852                                       size_t elem_size, void *data);
853 extern bool print_xint64_array_member(struct tcb *, void *elem_buf,
854                                       size_t elem_size, void *data);
855
856 static inline bool
857 print_xlong_array_member(struct tcb *tcp, void *elem_buf, size_t elem_size,
858                          void *data)
859 {
860         return dispatch_wordsize(print_xint64_array_member,
861                                  print_xint32_array_member,
862                                  tcp, elem_buf, elem_size, data);
863 }
864
865
866 typedef bool (*tfetch_mem_fn)(struct tcb *, kernel_ulong_t addr,
867                               unsigned int size, void *dest);
868 typedef bool (*print_fn)(struct tcb *, void *elem_buf,
869                          size_t elem_size, void *opaque_data);
870 typedef int (*print_obj_by_addr_fn)(struct tcb *, kernel_ulong_t);
871 typedef const char * (*sprint_obj_by_addr_fn)(struct tcb *, kernel_ulong_t);
872
873
874 /**
875  * Array printing function with over-engineered interface.
876  *
877  * @param start_addr       If tfetch_mem_fn is non-NULL: address in tracee's
878  *                         memory where the start of the array is located.
879  *                         If tfetch_mem_fn is NULL: ignored.
880  * @param nmemb            Number of elements in array.
881  * @param elem_buf         If tfetch_mem_fn is non-NULL: a buffer where each
882  *                         element fetched by tfetch_mem_fn is stored.
883  *                         If tfetch_mem_fn is NULL: address of the start of
884  *                         the array in local memory.
885  * @param elem_size        Size (in bytes) of each element in the array.
886  * @param tfetch_mem_fn    Fetching function. If NULL, then elem_buf is treated
887  *                         as local array of nmemb members elem_size each;
888  *                         start_addr is ignored.
889  * @param print_func       Element printing callback.
890  * @param opaque_data      A value that is unconditionally passed to print_func
891  *                         in opaque_data argument.
892  * @param flags            Combination of xlat style settings and additional
893  *                         flags from enum print_array_flags.
894  * @param index_xlat       Xlat array that is used for printing indices.
895  * @param index_xlat_size  The size of xlat array.
896  * @param index_dflt       Default string for the values not found
897  *                         in index_xlat.
898  */
899 extern bool
900 print_array_ex(struct tcb *,
901                kernel_ulong_t start_addr,
902                size_t nmemb,
903                void *elem_buf,
904                size_t elem_size,
905                tfetch_mem_fn tfetch_mem_func,
906                print_fn print_func,
907                void *opaque_data,
908                unsigned int flags,
909                const struct xlat *index_xlat,
910                const char *index_dflt);
911
912 /** Print an array from tracee's memory without any index printing features. */
913 static inline bool
914 print_array(struct tcb *const tcp,
915             const kernel_ulong_t start_addr,
916             const size_t nmemb,
917             void *const elem_buf,
918             const size_t elem_size,
919             tfetch_mem_fn tfetch_mem_func,
920             print_fn print_func,
921             void *const opaque_data)
922 {
923         return print_array_ex(tcp, start_addr, nmemb, elem_buf, elem_size,
924                               tfetch_mem_func, print_func, opaque_data,
925                               0, NULL, NULL);
926 }
927
928 /** Shorthand for printing local arrays. */
929 static inline bool
930 print_local_array(struct tcb *tcp,
931                   void *start_addr,
932                   const size_t nmemb,
933                   void *const elem_buf,
934                   const size_t elem_size,
935                   print_fn print_func,
936                   void *const opaque_data,
937                   unsigned int flags)
938 {
939         return print_array_ex(tcp, (uintptr_t) start_addr , nmemb,
940                               elem_buf, elem_size, NULL, print_func,
941                               opaque_data, flags, NULL, NULL);
942 }
943
944 extern kernel_ulong_t *
945 fetch_indirect_syscall_args(struct tcb *, kernel_ulong_t addr, unsigned int n_args);
946
947 extern void
948 dumpiov_in_msghdr(struct tcb *, kernel_ulong_t addr, kernel_ulong_t data_size);
949
950 extern void
951 dumpiov_in_mmsghdr(struct tcb *, kernel_ulong_t addr);
952
953 extern void
954 dumpiov_upto(struct tcb *, int len, kernel_ulong_t addr, kernel_ulong_t data_size);
955
956 extern void
957 dumpstr(struct tcb *, kernel_ulong_t addr, kernel_ulong_t len);
958
959 extern int
960 printstr_ex(struct tcb *, kernel_ulong_t addr, kernel_ulong_t len,
961             unsigned int user_style);
962
963 extern int
964 printpathn(struct tcb *, kernel_ulong_t addr, unsigned int n);
965
966 extern int
967 printpath(struct tcb *, kernel_ulong_t addr);
968
969 # define TIMESPEC_TEXT_BUFSIZE \
970                 (sizeof(long long) * 3 * 2 + sizeof("{tv_sec=-, tv_nsec=}"))
971 extern void printfd(struct tcb *, int);
972 extern void print_sockaddr(const void *sa, int len);
973 extern bool
974 print_inet_addr(int af, const void *addr, unsigned int len, const char *var_name);
975 extern bool
976 decode_inet_addr(struct tcb *, kernel_ulong_t addr,
977                  unsigned int len, int family, const char *var_name);
978 extern void print_ax25_addr(const void /* ax25_address */ *addr);
979 extern void print_x25_addr(const void /* struct x25_address */ *addr);
980 extern const char *get_sockaddr_by_inode(struct tcb *, int fd, unsigned long inode);
981 extern bool print_sockaddr_by_inode(struct tcb *, int fd, unsigned long inode);
982 extern void print_dirfd(struct tcb *, int);
983
984 extern int
985 decode_sockaddr(struct tcb *, kernel_ulong_t addr, int addrlen);
986
987 extern void printuid(const char *, const unsigned int);
988
989 extern void
990 print_sigset_addr_len(struct tcb *, kernel_ulong_t addr, kernel_ulong_t len);
991 extern void
992 print_sigset_addr(struct tcb *, kernel_ulong_t addr);
993
994 extern const char *sprintsigmask_n(const char *, const void *, unsigned int);
995 # define tprintsigmask_addr(prefix, mask) \
996         tprints(sprintsigmask_n((prefix), (mask), sizeof(mask)))
997 extern void printsignal(int);
998
999 extern void
1000 tprint_iov_upto(struct tcb *, kernel_ulong_t len, kernel_ulong_t addr,
1001                 enum iov_decode, kernel_ulong_t data_size);
1002
1003 extern void
1004 decode_netlink(struct tcb *, int fd, kernel_ulong_t addr, kernel_ulong_t len);
1005
1006 extern void tprint_open_modes(unsigned int);
1007 extern const char *sprint_open_modes(unsigned int);
1008
1009 extern void
1010 decode_seccomp_fprog(struct tcb *, kernel_ulong_t addr);
1011
1012 extern void
1013 print_seccomp_fprog(struct tcb *, kernel_ulong_t addr, unsigned short len);
1014
1015 extern void
1016 decode_sock_fprog(struct tcb *, kernel_ulong_t addr);
1017
1018 extern void
1019 print_sock_fprog(struct tcb *, kernel_ulong_t addr, unsigned short len);
1020
1021 struct strace_stat;
1022 extern void print_struct_stat(struct tcb *, const struct strace_stat *const st);
1023
1024 struct strace_statfs;
1025 struct strace_keyctl_kdf_params;
1026
1027 extern void
1028 print_struct_statfs(struct tcb *, kernel_ulong_t addr);
1029
1030 extern void
1031 print_struct_statfs64(struct tcb *, kernel_ulong_t addr, kernel_ulong_t size);
1032
1033 extern int
1034 fetch_perf_event_attr(struct tcb *const tcp, const kernel_ulong_t addr);
1035 extern void
1036 print_perf_event_attr(struct tcb *const tcp, const kernel_ulong_t addr);
1037
1038 extern const char *get_ifname(const unsigned int ifindex);
1039 extern void print_ifindex(unsigned int);
1040
1041 extern void print_bpf_filter_code(const uint16_t code, bool extended);
1042
1043 extern void qualify(const char *);
1044 extern unsigned int qual_flags(const unsigned int);
1045
1046 # define DECL_IOCTL(name)                                               \
1047 extern int                                                              \
1048 name ## _ioctl(struct tcb *, unsigned int request, kernel_ulong_t arg)  \
1049 /* End of DECL_IOCTL definition. */
1050
1051 DECL_IOCTL(dm);
1052 DECL_IOCTL(evdev);
1053 DECL_IOCTL(file);
1054 DECL_IOCTL(fs_x);
1055 DECL_IOCTL(inotify);
1056 DECL_IOCTL(kvm);
1057 DECL_IOCTL(nbd);
1058 DECL_IOCTL(nsfs);
1059 DECL_IOCTL(ptp);
1060 DECL_IOCTL(random);
1061 DECL_IOCTL(scsi);
1062 DECL_IOCTL(term);
1063 DECL_IOCTL(ubi);
1064 DECL_IOCTL(uffdio);
1065 DECL_IOCTL(watchdog);
1066 # undef DECL_IOCTL
1067
1068 extern int decode_sg_io_v4(struct tcb *, const kernel_ulong_t arg);
1069 extern void print_evdev_ff_type(const kernel_ulong_t val);
1070
1071 struct nlmsghdr;
1072
1073 typedef bool (*netlink_decoder_t)(struct tcb *, const struct nlmsghdr *,
1074                                   kernel_ulong_t addr, unsigned int len);
1075
1076 # define DECL_NETLINK(name)                                             \
1077 extern bool                                                             \
1078 decode_netlink_ ## name(struct tcb *, const struct nlmsghdr *,          \
1079                         kernel_ulong_t addr, unsigned int len)          \
1080 /* End of DECL_NETLINK definition. */
1081
1082 DECL_NETLINK(crypto);
1083 DECL_NETLINK(netfilter);
1084 DECL_NETLINK(route);
1085 DECL_NETLINK(selinux);
1086 DECL_NETLINK(sock_diag);
1087
1088 extern void
1089 decode_netlink_kobject_uevent(struct tcb *, kernel_ulong_t addr,
1090                               kernel_ulong_t len);
1091
1092 extern int ts_nz(const struct timespec *);
1093 extern int ts_cmp(const struct timespec *, const struct timespec *);
1094 extern double ts_float(const struct timespec *);
1095 extern void ts_add(struct timespec *, const struct timespec *, const struct timespec *);
1096 extern void ts_sub(struct timespec *, const struct timespec *, const struct timespec *);
1097 extern void ts_mul(struct timespec *, const struct timespec *, int);
1098 extern void ts_div(struct timespec *, const struct timespec *, int);
1099 extern const struct timespec *ts_min(const struct timespec *, const struct timespec *);
1100 extern const struct timespec *ts_max(const struct timespec *, const struct timespec *);
1101 extern int parse_ts(const char *s, struct timespec *t);
1102
1103 # ifdef ENABLE_STACKTRACE
1104 extern void unwind_init(void);
1105 extern void unwind_tcb_init(struct tcb *);
1106 extern void unwind_tcb_fin(struct tcb *);
1107 extern void unwind_tcb_print(struct tcb *);
1108 extern void unwind_tcb_capture(struct tcb *);
1109 # endif
1110
1111 # ifdef HAVE_LINUX_KVM_H
1112 extern void kvm_run_structure_decoder_init(void);
1113 extern void kvm_vcpu_info_free(struct tcb *);
1114 # endif
1115
1116 static inline int
1117 printstrn(struct tcb *tcp, kernel_ulong_t addr, kernel_ulong_t len)
1118 {
1119         return printstr_ex(tcp, addr, len, 0);
1120 }
1121
1122 static inline int
1123 printstr(struct tcb *tcp, kernel_ulong_t addr)
1124 {
1125         return printstr_ex(tcp, addr, -1, QUOTE_0_TERMINATED);
1126 }
1127
1128 static inline int
1129 printflags64(const struct xlat *x, uint64_t flags, const char *dflt)
1130 {
1131         return printflags_ex(flags, dflt, XLAT_STYLE_DEFAULT, x, NULL);
1132 }
1133
1134 static inline int
1135 printflags(const struct xlat *x, unsigned int flags, const char *dflt)
1136 {
1137         return printflags64(x, flags, dflt);
1138 }
1139
1140 static inline int
1141 printxval64(const struct xlat *x, const uint64_t val, const char *dflt)
1142 {
1143         return printxvals(val, dflt, x, NULL);
1144 }
1145
1146 static inline int
1147 printxval(const struct xlat *x, const unsigned int val, const char *dflt)
1148 {
1149         return printxvals(val, dflt, x, NULL);
1150 }
1151
1152 static inline int
1153 printxval64_u(const struct xlat *x, const uint64_t val, const char *dflt)
1154 {
1155         return printxvals_ex(val, dflt, XLAT_STYLE_FMT_U, x, NULL);
1156 }
1157
1158 static inline int
1159 printxval_u(const struct xlat *x, const unsigned int val, const char *dflt)
1160 {
1161         return printxvals_ex(val, dflt, XLAT_STYLE_FMT_U, x, NULL);
1162 }
1163
1164 static inline int
1165 printxval64_d(const struct xlat *x, const int64_t val, const char *dflt)
1166 {
1167         return printxvals_ex(val, dflt, XLAT_STYLE_FMT_D, x, NULL);
1168 }
1169
1170 static inline int
1171 printxval_d(const struct xlat *x, const int val, const char *dflt)
1172 {
1173         return printxvals_ex(val, dflt, XLAT_STYLE_FMT_D, x, NULL);
1174 }
1175
1176 static inline void
1177 tprint_iov(struct tcb *tcp, kernel_ulong_t len, kernel_ulong_t addr,
1178            enum iov_decode decode_iov)
1179 {
1180         tprint_iov_upto(tcp, len, addr, decode_iov, -1);
1181 }
1182
1183 # if HAVE_ARCH_TIME32_SYSCALLS
1184 extern bool print_timespec32_data_size(const void *arg, size_t size);
1185 extern bool print_timespec32_array_data_size(const void *arg,
1186                                              unsigned int nmemb,
1187                                              size_t size);
1188 extern int print_timespec32(struct tcb *, kernel_ulong_t);
1189 extern const char *sprint_timespec32(struct tcb *, kernel_ulong_t);
1190 extern int print_timespec32_utime_pair(struct tcb *, kernel_ulong_t);
1191 extern int print_itimerspec32(struct tcb *, kernel_ulong_t);
1192 extern int print_timex32(struct tcb *, kernel_ulong_t);
1193 # endif /* HAVE_ARCH_TIME32_SYSCALLS */
1194
1195 extern bool print_timespec64_data_size(const void *arg, size_t size);
1196 extern bool print_timespec64_array_data_size(const void *arg,
1197                                              unsigned int nmemb,
1198                                              size_t size);
1199 extern int print_timespec64(struct tcb *, kernel_ulong_t);
1200 extern const char *sprint_timespec64(struct tcb *, kernel_ulong_t);
1201 extern int print_timespec64_utime_pair(struct tcb *, kernel_ulong_t);
1202 extern int print_itimerspec64(struct tcb *, kernel_ulong_t);
1203
1204 extern bool print_timeval64_data_size(const void *arg, size_t size);
1205
1206 extern int print_timex64(struct tcb *, kernel_ulong_t);
1207
1208 # ifdef SPARC64
1209 extern int print_sparc64_timex(struct tcb *, kernel_ulong_t);
1210 # endif
1211
1212 # ifdef ALPHA
1213 typedef struct {
1214         int tv_sec, tv_usec;
1215 } timeval32_t;
1216
1217 extern void print_timeval32_t(const timeval32_t *);
1218 extern void printrusage32(struct tcb *, kernel_ulong_t);
1219 extern const char *sprint_timeval32(struct tcb *, kernel_ulong_t addr);
1220 extern int print_timeval32(struct tcb *, kernel_ulong_t addr);
1221 extern int print_timeval32_utimes(struct tcb *, kernel_ulong_t addr);
1222 extern int print_itimerval32(struct tcb *, kernel_ulong_t addr);
1223 # endif
1224
1225 # ifdef HAVE_STRUCT_USER_DESC
1226 /**
1227  * Filter what to print from the point of view of the get_thread_area syscall.
1228  * Kernel copies only entry_number field at first and then tries to write the
1229  * whole structure.
1230  */
1231 enum user_desc_print_filter {
1232         /* Print the "entering" part of struct user_desc - entry_number.  */
1233         USER_DESC_ENTERING = 1,
1234         /* Print the "exiting" part of the structure.  */
1235         USER_DESC_EXITING  = 2,
1236         USER_DESC_BOTH     = USER_DESC_ENTERING | USER_DESC_EXITING,
1237 };
1238
1239 extern void print_user_desc(struct tcb *, kernel_ulong_t addr,
1240                             enum user_desc_print_filter filter);
1241 # endif
1242
1243 /* Strace log generation machinery.
1244  *
1245  * printing_tcp: tcb which has incomplete line being printed right now.
1246  * NULL if last line has been completed ('\n'-terminated).
1247  * printleader(tcp) examines it, finishes incomplete line if needed,
1248  * the sets it to tcp.
1249  * line_ended() clears printing_tcp and resets ->curcol = 0.
1250  * tcp->curcol == 0 check is also used to detect completeness
1251  * of last line, since in -ff mode just checking printing_tcp for NULL
1252  * is not enough.
1253  *
1254  * If you change this code, test log generation in both -f and -ff modes
1255  * using:
1256  * strace -oLOG -f[f] test/threaded_execve
1257  * strace -oLOG -f[f] test/sigkill_rain
1258  * strace -oLOG -f[f] -p "`pidof web_browser`"
1259  */
1260 extern struct tcb *printing_tcp;
1261 extern void printleader(struct tcb *);
1262 extern void line_ended(void);
1263 extern void tabto(void);
1264 extern void tprintf(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
1265 extern void tprints(const char *str);
1266 extern void tprintf_comment(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
1267 extern void tprints_comment(const char *str);
1268
1269 /*
1270  * Staging output for status qualifier.
1271  */
1272 extern FILE *strace_open_memstream(struct tcb *tcp);
1273 extern void strace_close_memstream(struct tcb *tcp, bool publish);
1274
1275 static inline void
1276 printaddr_comment(const kernel_ulong_t addr)
1277 {
1278         tprintf_comment("%#llx", (unsigned long long) addr);
1279 }
1280
1281 /*
1282  * When u64 is interpreted by the kernel as an address, there is a difference
1283  * in behaviour between 32-bit and 64-bit kernel in the way u64_to_user_ptr
1284  * works (32-bit kernel trims higher bits during conversion which may result
1285  * to a valid address).  Since 32-bit strace cannot figure out what kind of
1286  * kernel the tracee is running on, it has to account for both possibilities.
1287  */
1288 # if CAN_ARCH_BE_COMPAT_ON_64BIT_KERNEL
1289
1290 /**
1291  * Print raw 64-bit value as an address if it's too big to fit in strace's
1292  * kernel_long_t.
1293  */
1294 static inline void
1295 print_big_u64_addr(const uint64_t addr)
1296 {
1297         if (sizeof(kernel_long_t) < 8 && addr > max_kaddr()) {
1298                 printaddr64(addr);
1299                 tprints(" or ");
1300         }
1301 }
1302 # else /* !CAN_ARCH_BE_COMPAT_ON_64BIT_KERNEL */
1303 #  define print_big_u64_addr(addr_) ((void) 0)
1304 # endif /* CAN_ARCH_BE_COMPAT_ON_64BIT_KERNEL */
1305
1306 # if SIZEOF_KERNEL_LONG_T > 4           \
1307  && (SIZEOF_LONG < SIZEOF_KERNEL_LONG_T || !defined(current_wordsize))
1308 #  define ANY_WORDSIZE_LESS_THAN_KERNEL_LONG    1
1309 # else
1310 #  define ANY_WORDSIZE_LESS_THAN_KERNEL_LONG    0
1311 # endif
1312
1313 # define DECL_PRINTNUM(name)                                            \
1314 extern bool                                                             \
1315 printnum_ ## name(struct tcb *, kernel_ulong_t addr, const char *fmt)   \
1316         ATTRIBUTE_FORMAT((printf, 3, 0))                                \
1317 /* End of DECL_PRINTNUM definition. */
1318
1319 DECL_PRINTNUM(short);
1320 DECL_PRINTNUM(int);
1321 DECL_PRINTNUM(int64);
1322 # undef DECL_PRINTNUM
1323
1324 # define DECL_PRINTNUM_ADDR(name)                                       \
1325 extern bool                                                             \
1326 printnum_addr_ ## name(struct tcb *, kernel_ulong_t addr)               \
1327 /* End of DECL_PRINTNUM_ADDR definition. */
1328
1329 DECL_PRINTNUM_ADDR(int);
1330 DECL_PRINTNUM_ADDR(int64);
1331 # undef DECL_PRINTNUM_ADDR
1332
1333 extern bool
1334 printnum_fd(struct tcb *, kernel_ulong_t addr);
1335
1336 static inline bool
1337 printnum_slong(struct tcb *tcp, kernel_ulong_t addr)
1338 {
1339         return dispatch_wordsize(printnum_int64, printnum_int,
1340                                  tcp, addr, opt_wordsize("%" PRId64, "%d"));
1341 }
1342
1343 static inline bool
1344 printnum_ulong(struct tcb *tcp, kernel_ulong_t addr)
1345 {
1346         return dispatch_wordsize(printnum_int64, printnum_int,
1347                                  tcp, addr, opt_wordsize("%" PRIu64, "%u"));
1348 }
1349
1350 static inline bool
1351 printnum_ptr(struct tcb *tcp, kernel_ulong_t addr)
1352 {
1353         return dispatch_wordsize(printnum_addr_int64, printnum_addr_int,
1354                                  tcp, addr);
1355 }
1356
1357 static inline bool
1358 printnum_kptr(struct tcb *tcp, kernel_ulong_t addr)
1359 {
1360         return dispatch_klongsize(printnum_addr_int64, printnum_addr_int,
1361                                   tcp, addr);
1362 }
1363
1364
1365 # define DECL_PRINTPAIR(name)                                           \
1366 extern bool                                                             \
1367 printpair_ ## name(struct tcb *, kernel_ulong_t addr, const char *fmt)  \
1368         ATTRIBUTE_FORMAT((printf, 3, 0))                                \
1369 /* End of DECL_PRINTPAIR definition. */
1370
1371 DECL_PRINTPAIR(int);
1372 DECL_PRINTPAIR(int64);
1373 # undef DECL_PRINTPAIR
1374
1375 static inline kernel_long_t
1376 truncate_klong_to_current_wordsize(const kernel_long_t v)
1377 {
1378 # if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
1379         if (current_wordsize < sizeof(v)) {
1380                 return (int) v;
1381         } else
1382 # endif
1383         {
1384                 return v;
1385         }
1386 }
1387
1388 static inline kernel_ulong_t
1389 truncate_kulong_to_current_wordsize(const kernel_ulong_t v)
1390 {
1391 # if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
1392         if (current_wordsize < sizeof(v)) {
1393                 return (unsigned int) v;
1394         } else
1395 # endif
1396         {
1397                 return v;
1398         }
1399 }
1400
1401 /*
1402  * Cast a pointer or a pointer-sized integer to kernel_ulong_t.
1403  */
1404 # define ptr_to_kulong(v) ((kernel_ulong_t) (unsigned long) (v))
1405
1406 /*
1407  * Zero-extend a signed integer type to unsigned long long.
1408  */
1409 # define zero_extend_signed_to_ull(v) \
1410         (sizeof(v) == sizeof(char) ? (unsigned long long) (unsigned char) (v) : \
1411          sizeof(v) == sizeof(short) ? (unsigned long long) (unsigned short) (v) : \
1412          sizeof(v) == sizeof(int) ? (unsigned long long) (unsigned int) (v) : \
1413          sizeof(v) == sizeof(long) ? (unsigned long long) (unsigned long) (v) : \
1414          (unsigned long long) (v))
1415
1416 /*
1417  * Sign-extend an unsigned integer type to long long.
1418  */
1419 # define sign_extend_unsigned_to_ll(v) \
1420         (sizeof(v) == sizeof(char) ? (long long) (char) (v) : \
1421          sizeof(v) == sizeof(short) ? (long long) (short) (v) : \
1422          sizeof(v) == sizeof(int) ? (long long) (int) (v) : \
1423          sizeof(v) == sizeof(long) ? (long long) (long) (v) : \
1424          (long long) (v))
1425
1426 /*
1427  * Computes the popcount of a vector of 32-bit values.
1428  */
1429 static inline unsigned int
1430 popcount32(const uint32_t *a, unsigned int size)
1431 {
1432         unsigned int count = 0;
1433
1434         for (; size; ++a, --size) {
1435                 uint32_t x = *a;
1436
1437 # ifdef HAVE___BUILTIN_POPCOUNT
1438                 count += __builtin_popcount(x);
1439 # else
1440                 for (; x; ++count)
1441                         x &= x - 1;
1442 # endif
1443         }
1444
1445         return count;
1446 }
1447
1448 extern const char *const errnoent[];
1449 extern const char *const signalent[];
1450 extern const unsigned int nerrnos;
1451 extern const unsigned int nsignals;
1452
1453 extern const struct_sysent sysent0[];
1454 extern const struct_ioctlent ioctlent0[];
1455
1456 extern const char *const personality_names[];
1457 /* Personality designators to be used for specifying personality */
1458 extern const char *const personality_designators[];
1459
1460 # if SUPPORTED_PERSONALITIES > 1
1461 extern const struct_sysent *sysent;
1462 extern const struct_ioctlent *ioctlent;
1463 # else
1464 #  define sysent     sysent0
1465 #  define ioctlent   ioctlent0
1466 # endif
1467
1468 extern unsigned nsyscalls;
1469 extern unsigned nioctlents;
1470
1471 extern const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES];
1472 extern const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES];
1473 extern struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
1474
1475 # ifdef IN_MPERS_BOOTSTRAP
1476 /* Transform multi-line MPERS_PRINTER_DECL statements to one-liners.  */
1477 #  define MPERS_PRINTER_DECL(type, name, ...) MPERS_PRINTER_DECL(type, name, __VA_ARGS__)
1478 # else /* !IN_MPERS_BOOTSTRAP */
1479 #  if SUPPORTED_PERSONALITIES > 1
1480 #   include "printers.h"
1481 #  else
1482 #   include "native_printer_decls.h"
1483 #  endif
1484 #  define MPERS_PRINTER_DECL(type, name, ...) type MPERS_FUNC_NAME(name)(__VA_ARGS__)
1485 # endif /* !IN_MPERS_BOOTSTRAP */
1486
1487 /* Checks that sysent[scno] is not out of range. */
1488 static inline bool
1489 scno_in_range(kernel_ulong_t scno)
1490 {
1491         return scno < nsyscalls;
1492 }
1493
1494 /*
1495  * Checks whether scno is not out of range,
1496  * its corresponding sysent[scno].sys_func is non-NULL,
1497  * and its sysent[scno].sys_flags has no TRACE_INDIRECT_SUBCALL flag set.
1498  */
1499 static inline bool
1500 scno_is_valid(kernel_ulong_t scno)
1501 {
1502         return scno_in_range(scno)
1503                && sysent[scno].sys_func
1504                && !(sysent[scno].sys_flags & TRACE_INDIRECT_SUBCALL);
1505 }
1506
1507 # define MPERS_FUNC_NAME__(prefix, name) prefix ## name
1508 # define MPERS_FUNC_NAME_(prefix, name) MPERS_FUNC_NAME__(prefix, name)
1509 # define MPERS_FUNC_NAME(name) MPERS_FUNC_NAME_(MPERS_PREFIX, name)
1510
1511 # define SYS_FUNC_NAME(syscall_name) MPERS_FUNC_NAME(syscall_name)
1512
1513 # define SYS_FUNC(syscall_name) int SYS_FUNC_NAME(sys_ ## syscall_name)(struct tcb *tcp)
1514
1515 #endif /* !STRACE_DEFS_H */