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) 1999-2023 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 "number_set.h"
44 # include "string_to_uint.h"
45 # include "sysent.h"
46 # include "xmalloc.h"
47
48 # ifndef HAVE_STRERROR
49 const char *strerror(int);
50 # endif
51 # ifndef HAVE_STPCPY
52 /* Some libc have stpcpy, some don't. Sigh...
53 * Roll our private implementation...
54 */
55 # undef stpcpy
56 # define stpcpy strace_stpcpy
57 extern char *stpcpy(char *dst, const char *src);
58 # endif
59
60 /* Glibc has an efficient macro for sigemptyset
61 * (it just does one or two assignments of 0 to internal vector of longs).
62 */
63 # if defined(__GLIBC__) && defined(__sigemptyset) && !defined(sigemptyset)
64 # define sigemptyset __sigemptyset
65 # endif
66
67 /* Configuration section */
68 # ifndef DEFAULT_STRLEN
69 /* default maximum # of bytes printed in `printstr', change with -s switch */
70 # define DEFAULT_STRLEN 32
71 # endif
72 # ifndef DEFAULT_ACOLUMN
73 # define DEFAULT_ACOLUMN 40 /* default alignment column for results */
74 # endif
75 /*
76 * Maximum number of args to a syscall.
77 *
78 * Make sure that all entries in all syscallent.h files have nargs <= MAX_ARGS!
79 * linux/<ARCH>/syscallent*.h:
80 * all have nargs <= 6 except mips o32 which has nargs <= 7.
81 */
82 # ifndef MAX_ARGS
83 # ifdef LINUX_MIPSO32
84 # define MAX_ARGS 7
85 # else
86 # define MAX_ARGS 6
87 # endif
88 # endif
89 /* default sorting method for call profiling */
90 # ifndef DEFAULT_SORTBY
91 # define DEFAULT_SORTBY "time"
92 # endif
93
94 /* To force NOMMU build, set to 1 */
95 # define NOMMU_SYSTEM 0
96
97 # ifndef ERESTARTSYS
98 # define ERESTARTSYS 512
99 # endif
100 # ifndef ERESTARTNOINTR
101 # define ERESTARTNOINTR 513
102 # endif
103 # ifndef ERESTARTNOHAND
104 # define ERESTARTNOHAND 514
105 # endif
106 # ifndef ERESTART_RESTARTBLOCK
107 # define ERESTART_RESTARTBLOCK 516
108 # endif
109
110 # define PERSONALITY0_WORDSIZE SIZEOF_LONG
111 # define PERSONALITY0_KLONGSIZE SIZEOF_KERNEL_LONG_T
112 # define PERSONALITY0_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
113 # define PERSONALITY0_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
114
115 # if SUPPORTED_PERSONALITIES > 1
116 # define PERSONALITY1_WORDSIZE 4
117 # define PERSONALITY1_KLONGSIZE PERSONALITY1_WORDSIZE
118 # endif
119
120 # if SUPPORTED_PERSONALITIES > 2
121 # define PERSONALITY2_WORDSIZE 4
122 # define PERSONALITY2_KLONGSIZE PERSONALITY0_KLONGSIZE
123 # endif
124
125 # if SUPPORTED_PERSONALITIES > 1 && defined HAVE_M32_MPERS
126 # define PERSONALITY1_INCLUDE_PRINTERS_DECLS "m32_printer_decls.h"
127 # define PERSONALITY1_INCLUDE_PRINTERS_DEFS "m32_printer_defs.h"
128 # define PERSONALITY1_INCLUDE_FUNCS "m32_funcs.h"
129 # define MPERS_m32_IOCTL_MACROS "ioctl_redefs1.h"
130 # define HAVE_PERSONALITY_1_MPERS 1
131 # else
132 # define PERSONALITY1_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
133 # define PERSONALITY1_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
134 # define PERSONALITY1_INCLUDE_FUNCS "empty.h"
135 # define HAVE_PERSONALITY_1_MPERS 0
136 # endif
137
138 # if SUPPORTED_PERSONALITIES > 2 && defined HAVE_MX32_MPERS
139 # define PERSONALITY2_INCLUDE_FUNCS "mx32_funcs.h"
140 # define PERSONALITY2_INCLUDE_PRINTERS_DECLS "mx32_printer_decls.h"
141 # define PERSONALITY2_INCLUDE_PRINTERS_DEFS "mx32_printer_defs.h"
142 # define MPERS_mx32_IOCTL_MACROS "ioctl_redefs2.h"
143 # define HAVE_PERSONALITY_2_MPERS 1
144 # else
145 # define PERSONALITY2_INCLUDE_PRINTERS_DECLS "native_printer_decls.h"
146 # define PERSONALITY2_INCLUDE_PRINTERS_DEFS "native_printer_defs.h"
147 # define PERSONALITY2_INCLUDE_FUNCS "empty.h"
148 # define HAVE_PERSONALITY_2_MPERS 0
149 # endif
150
151 # ifdef WORDS_BIGENDIAN
152 # define is_bigendian true
153 # else
154 # define is_bigendian false
155 # endif
156
157 # if SUPPORTED_PERSONALITIES > 1
158 extern void set_personality(unsigned int personality);
159 extern unsigned current_personality;
160 # else
161 # define set_personality(personality) ((void)0)
162 # define current_personality 0
163 # endif
164
165 # if SUPPORTED_PERSONALITIES == 1
166 # define current_wordsize PERSONALITY0_WORDSIZE
167 # define current_klongsize PERSONALITY0_KLONGSIZE
168 # else
169 # if SUPPORTED_PERSONALITIES == 2 && PERSONALITY0_WORDSIZE == PERSONALITY1_WORDSIZE
170 # define current_wordsize PERSONALITY0_WORDSIZE
171 # else
172 extern unsigned current_wordsize;
173 # endif
174 # if SUPPORTED_PERSONALITIES == 2 && PERSONALITY0_KLONGSIZE == PERSONALITY1_KLONGSIZE
175 # define current_klongsize PERSONALITY0_KLONGSIZE
176 # else
177 extern unsigned current_klongsize;
178 # endif
179 # endif
180
181 # define max_addr() (~0ULL >> ((8 - current_wordsize) * 8))
182 # define max_kaddr() (~0ULL >> ((8 - current_klongsize) * 8))
183
184 /* Shorthands for defining word/klong-based dispatcher function bodies */
185 # ifndef current_wordsize
186 # define opt_wordsize(opt_64_, opt_32_) \
187 ((current_wordsize > sizeof(uint32_t)) ? (opt_64_) : (opt_32_))
188 # define dispatch_wordsize(call_64_, call_32_, ...) \
189 ((current_wordsize > sizeof(uint32_t)) \
190 ? (call_64_)(__VA_ARGS__) : (call_32_)(__VA_ARGS__))
191 # else /* defined current_wordsize */
192 # if current_wordsize > 4
193 # define opt_wordsize(opt_64_, opt_32_) (opt_64_)
194 # define dispatch_wordsize(call_64_, call_32_, ...) ((call_64_)(__VA_ARGS__))
195 # else /* current_wordsize == 4 */
196 # define opt_wordsize(opt_64_, opt_32_) (opt_32_)
197 # define dispatch_wordsize(call_64_, call_32_, ...) ((call_32_)(__VA_ARGS__))
198 # endif
199 # endif
200
201 # ifndef current_klongsize
202 # define opt_klongsize(opt_64_, opt_32_) \
203 ((current_klongsize > sizeof(uint32_t)) ? (opt_64_) : (opt_32_))
204 # define dispatch_klongsize(call_64_, call_32_, ...) \
205 ((current_klongsize > sizeof(uint32_t)) \
206 ? (call_64_)(__VA_ARGS__) : (call_32_)(__VA_ARGS__))
207 # else /* defined current_klongsize */
208 # if current_klongsize > 4
209 # define opt_klongsize(opt_64_, opt_32_) (opt_64_)
210 # define dispatch_klongsize(call_64_, call_32_, ...) ((call_64_)(__VA_ARGS__))
211 # else /* current_klongsize == 4 */
212 # define opt_klongsize(opt_64_, opt_32_) (opt_32_)
213 # define dispatch_klongsize(call_64_, call_32_, ...) ((call_32_)(__VA_ARGS__))
214 # endif
215 # endif
216
217
218 typedef struct ioctlent {
219 const char *symbol;
220 unsigned int code;
221 } struct_ioctlent;
222
223 # define INJECT_F_SIGNAL 0x01
224 # define INJECT_F_ERROR 0x02
225 # define INJECT_F_RETVAL 0x04
226 # define INJECT_F_DELAY_ENTER 0x08
227 # define INJECT_F_DELAY_EXIT 0x10
228 # define INJECT_F_SYSCALL 0x20
229 # define INJECT_F_POKE_ENTER 0x40
230 # define INJECT_F_POKE_EXIT 0x80
231
232 # define INJECT_ACTION_FLAGS \
233 (INJECT_F_SIGNAL \
234 |INJECT_F_ERROR \
235 |INJECT_F_RETVAL \
236 |INJECT_F_DELAY_ENTER \
237 |INJECT_F_DELAY_EXIT \
238 |INJECT_F_POKE_ENTER \
239 |INJECT_F_POKE_EXIT \
240 )
241
242 struct inject_data {
243 uint8_t flags; /* 8 of 8 flags are used */
244 uint8_t signo; /* NSIG <= 128 */
245 uint16_t rval_idx; /* index in retval_vec */
246 uint16_t delay_idx; /* index in delay_data_vec */
247 uint16_t poke_idx; /* index in poke_vec */
248 uint16_t scno; /* syscall to be injected instead of -1 */
249 };
250
251 struct inject_opts {
252 uint16_t first;
253 uint16_t last;
254 uint16_t step;
255 struct inject_data data;
256 };
257
258 # define INJECT_LAST_INF ((uint16_t) -1)
259
260 # define MAX_ERRNO_VALUE 4095
261
262 /* Trace Control Block */
263 struct tcb {
264 int flags; /* See below for TCB_ values */
265 int pid; /* If 0, this tcb is free */
266 int qual_flg; /* qual_flags[scno] or DEFAULT_QUAL_FLAGS + RAW */
267 # if SUPPORTED_PERSONALITIES > 1
268 unsigned int currpers; /* Personality at the time of scno update */
269 # endif
270 unsigned long u_error; /* Error code */
271 kernel_ulong_t scno; /* System call number */
272 kernel_ulong_t true_scno; /* Same, but without subcall decoding and shuffling */
273 kernel_ulong_t u_arg[MAX_ARGS]; /* System call arguments */
274 kernel_long_t u_rval; /* Return value */
275 int sys_func_rval; /* Syscall entry parser's return value */
276 int curcol; /* Output column for this process */
277 FILE *outf; /* Output file for this process */
278 struct staged_output_data *staged_output_data;
279
280 const char *auxstr; /* Auxiliary info from syscall (see RVAL_STR) */
281 void *_priv_data; /* Private data for syscall decoding functions */
282 void (*_free_priv_data)(void *); /* Callback for freeing priv_data */
283 const struct_sysent *s_ent; /* sysent[scno] or a stub struct for bad
284 * scno. Use tcp_sysent() macro for access.
285 */
286 const struct_sysent *s_prev_ent; /* for "resuming interrupted SYSCALL" msg */
287 struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
288 struct timespec stime; /* System time usage as of last process wait */
289 struct timespec ltime; /* System time usage as of last syscall entry */
290 struct timespec atime; /* System time right after attach */
291 struct timespec etime; /* Syscall entry time (CLOCK_MONOTONIC) */
292 struct timespec delay_expiration_time; /* When does the delay end */
293
294 /*
295 * The ID of the PID namespace of this process
296 * (inode number of /proc/<pid>/ns/pid)
297 * (0: not initialized)
298 */
299 unsigned int pid_ns;
300
301 # ifdef ENABLE_SECONTEXT
302 int last_dirfd; /* Use AT_FDCWD for 'not set' */
303 # endif
304
305 struct mmap_cache_t *mmap_cache;
306
307 /*
308 * Data that is stored during process wait traversal.
309 * We use indices as the actual data is stored in an array
310 * that is realloc'ed at runtime.
311 */
312 size_t wait_data_idx;
313 /** Wait data storage for a delayed process. */
314 struct tcb_wait_data *delayed_wait_data;
315 struct list_item wait_list;
316
317
318 # ifdef HAVE_LINUX_KVM_H
319 struct vcpu_info *vcpu_info_list;
320 # endif
321
322 # ifdef ENABLE_STACKTRACE
323 void *unwind_ctx;
324 struct unwind_queue_t *unwind_queue;
325 # endif
326
327 # define PROC_COMM_LEN 16
328 char comm[PROC_COMM_LEN];
329 };
330
331 /* TCB flags */
332 /* We have attached to this process, but did not see it stopping yet */
333 # define TCB_STARTUP 0x01
334 # define TCB_IGNORE_ONE_SIGSTOP 0x02 /* Next SIGSTOP is to be ignored */
335 /*
336 * Are we in system call entry or in syscall exit?
337 *
338 * This bit is set in syscall_entering_finish() and cleared in
339 * syscall_exiting_finish().
340 * Other stops which are possible directly after syscall entry (death, ptrace
341 * event stop) are handled without calling syscall_{entering,exiting}_*().
342 *
343 * Use entering(tcp) / exiting(tcp) to check this bit to make code more
344 * readable.
345 */
346 # define TCB_INSYSCALL 0x04
347 # define TCB_ATTACHED 0x08 /* We attached to it already */
348 # define TCB_REPRINT 0x10 /* We should reprint this syscall on exit */
349 # define TCB_FILTERED 0x20 /* This system call has been filtered out */
350 # define TCB_TAMPERED 0x40 /* A syscall has been tampered with */
351 # define TCB_HIDE_LOG 0x80 /* We should hide everything (until execve) */
352 # define TCB_CHECK_EXEC_SYSCALL 0x100 /* Check whether this execve syscall succeeded */
353 # define TCB_SKIP_DETACH_ON_FIRST_EXEC 0x200 /* -b execve should skip detach on first execve */
354 # define TCB_GRABBED 0x400 /* We grab the process and can catch it
355 * in the middle of a syscall */
356 # define TCB_RECOVERING 0x800 /* We try to recover after detecting incorrect
357 * syscall entering/exiting state */
358 # define TCB_INJECT_DELAY_EXIT 0x1000 /* Current syscall needs to be delayed
359 on exit */
360 # define TCB_INJECT_POKE_EXIT 0x2000 /* The processes memory should be tampered
361 with on exit */
362 # define TCB_DELAYED 0x4000 /* Current syscall is to be delayed */
363 # define TCB_TAMPERED_DELAYED 0x8000 /* Current syscall has been delayed */
364 # define TCB_TAMPERED_POKED 0x10000 /* Current syscall has been poked */
365 # define TCB_TAMPERED_NO_FAIL 0x20000 /* We tamper tcb with syscall
366 that should not fail. */
367 # define TCB_SECCOMP_FILTER 0x40000 /* This process has a seccomp filter
368 * attached.
369 */
370
371 /* qualifier flags */
372 # define QUAL_TRACE 0x001 /* this system call should be traced */
373 # define QUAL_ABBREV 0x002 /* abbreviate the structures of this syscall */
374 # define QUAL_VERBOSE 0x004 /* decode the structures of this syscall */
375 # define QUAL_RAW 0x008 /* print all args in hex for this syscall */
376 # define QUAL_INJECT 0x010 /* tamper with this system call on purpose */
377
378 # define DEFAULT_QUAL_FLAGS (QUAL_TRACE | QUAL_ABBREV | QUAL_VERBOSE)
379
380 # define entering(tcp) (!((tcp)->flags & TCB_INSYSCALL))
381 # define exiting(tcp) ((tcp)->flags & TCB_INSYSCALL)
382 # define syserror(tcp) ((tcp)->u_error != 0)
383 # define traced(tcp) ((tcp)->qual_flg & QUAL_TRACE)
384 # define verbose(tcp) ((tcp)->qual_flg & QUAL_VERBOSE)
385 # define abbrev(tcp) ((tcp)->qual_flg & QUAL_ABBREV)
386 # define raw(tcp) ((tcp)->qual_flg & QUAL_RAW)
387 # define inject(tcp) ((tcp)->qual_flg & QUAL_INJECT)
388 # define filtered(tcp) ((tcp)->flags & TCB_FILTERED)
389 # define hide_log(tcp) ((tcp)->flags & TCB_HIDE_LOG)
390 # define check_exec_syscall(tcp) ((tcp)->flags & TCB_CHECK_EXEC_SYSCALL)
391 # define syscall_tampered(tcp) ((tcp)->flags & TCB_TAMPERED)
392 # define recovering(tcp) ((tcp)->flags & TCB_RECOVERING)
393 # define inject_delay_exit(tcp) ((tcp)->flags & TCB_INJECT_DELAY_EXIT)
394 # define inject_poke_exit(tcp) ((tcp)->flags & TCB_INJECT_POKE_EXIT)
395 # define syscall_delayed(tcp) ((tcp)->flags & TCB_DELAYED)
396 # define syscall_tampered_delayed(tcp) ((tcp)->flags & TCB_TAMPERED_DELAYED)
397 # define syscall_tampered_poked(tcp) ((tcp)->flags & TCB_TAMPERED_POKED)
398 # define syscall_tampered_nofail(tcp) ((tcp)->flags & TCB_TAMPERED_NO_FAIL)
399 # define has_seccomp_filter(tcp) ((tcp)->flags & TCB_SECCOMP_FILTER)
400
401 extern const struct_sysent stub_sysent;
402 # define tcp_sysent(tcp) (tcp->s_ent ?: &stub_sysent)
403 # define n_args(tcp) (tcp_sysent(tcp)->nargs)
404
405 # include "xlat.h"
406
407 extern const struct xlat addrfams[];
408 extern const struct xlat arp_hardware_types[];
409 extern const struct xlat at_flags[];
410 extern const struct xlat clocknames[];
411 extern const struct xlat dirent_types[];
412 extern const struct xlat ethernet_protocols[];
413 extern const struct xlat inet_protocols[];
414 extern const struct xlat evdev_abs[];
415 extern const struct xlat audit_arch[];
416 extern const struct xlat evdev_ev[];
417 extern const struct xlat iffflags[];
418 extern const struct xlat ip_type_of_services[];
419 extern const struct xlat ipc_private[];
420 extern const struct xlat msg_flags[];
421 extern const struct xlat netlink_protocols[];
422 extern const struct xlat nl_bridge_vlan_flags[];
423 extern const struct xlat nl_netfilter_msg_types[];
424 extern const struct xlat nl_route_types[];
425 extern const struct xlat open_access_modes[];
426 extern const struct xlat open_mode_flags[];
427 extern const struct xlat perf_type_id[];
428 extern const struct xlat pollflags[];
429 extern const struct xlat ptrace_cmds[];
430 extern const struct xlat resource_flags[];
431 extern const struct xlat route_nexthop_flags[];
432 extern const struct xlat routing_protocols[];
433 extern const struct xlat routing_scopes[];
434 extern const struct xlat routing_table_ids[];
435 extern const struct xlat routing_types[];
436 extern const struct xlat rwf_flags[];
437 extern const struct xlat seccomp_filter_flags[];
438 extern const struct xlat seccomp_ret_action[];
439 extern const struct xlat setns_types[];
440 extern const struct xlat sg_io_info[];
441 extern const struct xlat socketlayers[];
442 extern const struct xlat socktypes[];
443 extern const struct xlat tcp_state_flags[];
444 extern const struct xlat tcp_states[];
445 extern const struct xlat whence_codes[];
446
447 /* Format of syscall return values */
448 # define RVAL_UDECIMAL 000 /* unsigned decimal format */
449 # define RVAL_HEX 001 /* hex format */
450 # define RVAL_OCTAL 002 /* octal format */
451 # define RVAL_FD 010 /* file descriptor */
452 # define RVAL_TID 011 /* task ID */
453 # define RVAL_SID 012 /* session ID */
454 # define RVAL_TGID 013 /* thread group ID */
455 # define RVAL_PGID 014 /* process group ID */
456 # define RVAL_MASK 017 /* mask for these values */
457
458 # define RVAL_STR 020 /* Print `auxstr' field after return val */
459 # define RVAL_NONE 040 /* Print nothing */
460
461 # define RVAL_DECODED 0100 /* syscall decoding finished */
462 # define RVAL_IOCTL_DECODED 0200 /* ioctl sub-parser successfully decoded
463 the argument */
464
465 # define IOCTL_NUMBER_UNKNOWN 0
466 # define IOCTL_NUMBER_HANDLED 1
467 # define IOCTL_NUMBER_STOP_LOOKUP 010
468
469 # define indirect_ipccall(tcp) (tcp_sysent(tcp)->sys_flags & TRACE_INDIRECT_SUBCALL)
470
471 enum pid_type {
472 PT_TID,
473 PT_TGID,
474 PT_PGID,
475 PT_SID,
476
477 PT_COUNT,
478 PT_NONE = -1
479 };
480
481 enum sock_proto {
482 SOCK_PROTO_UNKNOWN,
483 SOCK_PROTO_UNIX,
484 SOCK_PROTO_UNIX_STREAM,
485 SOCK_PROTO_TCP,
486 SOCK_PROTO_UDP,
487 SOCK_PROTO_UDPLITE,
488 SOCK_PROTO_DCCP,
489 SOCK_PROTO_SCTP,
490 SOCK_PROTO_L2TP_IP,
491 SOCK_PROTO_PING,
492 SOCK_PROTO_RAW,
493 SOCK_PROTO_TCPv6,
494 SOCK_PROTO_UDPv6,
495 SOCK_PROTO_UDPLITEv6,
496 SOCK_PROTO_DCCPv6,
497 SOCK_PROTO_L2TP_IPv6,
498 SOCK_PROTO_SCTPv6,
499 SOCK_PROTO_PINGv6,
500 SOCK_PROTO_RAWv6,
501 SOCK_PROTO_NETLINK,
502 };
503 extern enum sock_proto get_proto_by_name(const char *);
504 extern int get_family_by_proto(enum sock_proto proto);
505
506 typedef enum {
507 CFLAG_NONE = 0,
508 CFLAG_ONLY_STATS,
509 CFLAG_BOTH
510 } cflag_t;
511 extern cflag_t cflag;
512 extern bool Tflag;
513 extern int Tflag_scale;
514 extern int Tflag_width;
515 extern bool iflag;
516 extern bool count_wallclock;
517 extern bool tracing_fds;
518 extern long long syscall_limit;
519
520 struct path_set_item {
521 const char *path;
522 };
523
524 /* are we filtering traces based on paths? */
525 extern struct path_set {
526 struct path_set_item *paths_selected;
527 size_t num_selected;
528 size_t size;
529 } global_path_set;
530 # define tracing_paths (global_path_set.num_selected != 0)
531 enum xflag_opts {
532 HEXSTR_NONE,
533 HEXSTR_NON_ASCII,
534 HEXSTR_ALL,
535 /*
536 * It is here (after HEXSTR_ALL and not before HEXSTR_NON_ASCII) because
537 * none/non-ascii/all are historic options values that are supported
538 * via -x option.
539 */
540 HEXSTR_NON_ASCII_CHARS,
541
542 NUM_HEXSTR_OPTS
543 };
544 extern unsigned xflag;
545 extern bool followfork;
546 extern bool output_separately;
547 # ifdef ENABLE_STACKTRACE
548 /* if this is true do the stack trace for every system call */
549 extern bool stack_trace_enabled;
550 # else
551 # define stack_trace_enabled 0
552 # endif
553 extern unsigned ptrace_setoptions;
554 extern unsigned max_strlen;
555 extern unsigned os_release;
556 # undef KERNEL_VERSION
557 # define KERNEL_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
558
559 extern int read_int_from_file(const char *, int *);
560
561 extern void set_sortby(const char *);
562 extern int set_overhead(const char *);
563 extern void set_count_summary_columns(const char *columns);
564
565 extern bool get_instruction_pointer(struct tcb *, kernel_ulong_t *);
566 extern bool get_stack_pointer(struct tcb *, kernel_ulong_t *);
567 extern void print_instruction_pointer(struct tcb *);
568
569 extern void print_syscall_number(struct tcb *);
570
571 extern void print_syscall_resume(struct tcb *tcp);
572
573 extern int syscall_entering_decode(struct tcb *);
574 extern int syscall_entering_trace(struct tcb *, unsigned int *);
575 extern void syscall_entering_finish(struct tcb *, int);
576
577 extern int syscall_exiting_decode(struct tcb *, struct timespec *);
578 extern int syscall_exiting_trace(struct tcb *, struct timespec *, int);
579 extern void syscall_exiting_finish(struct tcb *);
580
581 extern void count_syscall(struct tcb *, const struct timespec *);
582 extern void call_summary(FILE *);
583
584 extern void clear_regs(struct tcb *tcp);
585 extern int get_scno(struct tcb *);
586 extern kernel_ulong_t get_rt_sigframe_addr(struct tcb *);
587
588 /**
589 * Convert a pair of (raw) syscall number and arch (defined by an AUDIT_ARCH_*
590 * constant) to the corresponding syscall name. So far works only for arches
591 * that are present in personalities supported by strace binary.
592 *
593 * @param nr Raw syscall number.
594 * @param arch AUDIT_ARCH_* constant that identifies the architecture.
595 * @param prefix If arch corresponds to current personality (and the argument
596 * is non-NULL), an appropriate __NR_* constant prefix
597 * is to be stored at the address pointed by the argument.
598 * If arch corresponds to a different personality, or syscall
599 * name has not been found, NULL is stored.
600 * @return String literal corresponding to the syscall number in case
601 * the latter is valid; NULL otherwise.
602 */
603 extern const char *syscall_name_arch(kernel_ulong_t nr, unsigned int arch,
604 const char **prefix);
605 /**
606 * Convert a syscall name to the corresponding (shuffled) syscall number.
607 *
608 * @param s Syscall name.
609 * @param p Personality.
610 * @param start From which position in syscall entry table resume the search.
611 * @return Shuffled syscall number (ready to use against sysent_vec)
612 * if syscall name is found; -1 otherwise.
613 */
614 extern kernel_long_t scno_by_name(const char *s, unsigned p,
615 kernel_long_t start);
616 /**
617 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
618 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
619 *
620 * @param scno Raw or shuffled syscall number.
621 * @param pers Personality to shuffle scno for.
622 * @return Shuffled or raw syscall number, respectively.
623 */
624 extern kernel_ulong_t shuffle_scno_pers(kernel_ulong_t scno, int pers);
625 /**
626 * Shorthand for shuffle_scno_pers() for the current personality.
627 *
628 * @param scno Raw or shuffled syscall number.
629 * @return Shuffled or raw syscall number, respectively.
630 */
631 static inline kernel_ulong_t
632 shuffle_scno(kernel_ulong_t scno)
633 {
634 return shuffle_scno_pers(scno, current_personality);
635 }
636 /**
637 * Print error name in accordance with current xlat style setting.
638 *
639 * @param err Error value.
640 * @param negated If set to true, negative values of the err parameter indicate
641 * error condition, otherwise positive.
642 */
643 extern void print_err(int64_t err, bool negated);
644
645 extern bool is_erestart(struct tcb *);
646 extern void temporarily_clear_syserror(struct tcb *);
647 extern void restore_cleared_syserror(struct tcb *);
648
649 extern void *get_tcb_priv_data(const struct tcb *);
650 extern int set_tcb_priv_data(struct tcb *, void *priv_data,
651 void (*free_priv_data)(void *));
652 extern void free_tcb_priv_data(struct tcb *);
653
654 static inline unsigned long get_tcb_priv_ulong(const struct tcb *tcp)
655 {
656 return (unsigned long) get_tcb_priv_data(tcp);
657 }
658
659 static inline int set_tcb_priv_ulong(struct tcb *tcp, unsigned long val)
660 {
661 return set_tcb_priv_data(tcp, (void *) val, 0);
662 }
663
664 struct finfo {
665 const char *path;
666 enum {
667 FINFO_UNSET,
668 FINFO_DEV_BLK,
669 FINFO_DEV_CHR,
670 } type;
671 bool deleted;
672 struct {
673 unsigned int major, minor;
674 } dev;
675 };
676
677 extern struct finfo *
678 get_finfo_for_dev(const char *path, struct finfo *finfo);
679
680 extern int
681 term_ioctl_decode_command_number(struct tcb *tcp,
682 const struct finfo *finfo,
683 unsigned int code);
684
685 /**
686 * @return 0 on success, -1 on error.
687 */
688 extern int
689 umoven(struct tcb *, kernel_ulong_t addr, unsigned int len, void *laddr);
690 # define umove(pid, addr, objp) \
691 umoven((pid), (addr), sizeof(*(objp)), (void *) (objp))
692
693 /**
694 * @return the number of bytes written.
695 */
696 extern unsigned int
697 upoken(struct tcb *, kernel_ulong_t addr, unsigned int len, void *laddr);
698
699 /**
700 * @return true on success, false on error.
701 */
702 extern bool
703 tfetch_mem64(struct tcb *, uint64_t addr, unsigned int len, void *laddr);
704
705 static inline bool
706 tfetch_mem(struct tcb *tcp, const kernel_ulong_t addr,
707 unsigned int len, void *laddr)
708 {
709 return tfetch_mem64(tcp, addr, len, laddr);
710 }
711 # define tfetch_obj(pid, addr, objp) \
712 tfetch_mem((pid), (addr), sizeof(*(objp)), (void *) (objp))
713
714 /**
715 * @return true on success, false on error.
716 */
717 extern bool
718 tfetch_mem64_ignore_syserror(struct tcb *, uint64_t addr,
719 unsigned int len, void *laddr);
720
721 static inline bool
722 tfetch_mem_ignore_syserror(struct tcb *tcp, const kernel_ulong_t addr,
723 unsigned int len, void *laddr)
724 {
725 return tfetch_mem64_ignore_syserror(tcp, addr, len, laddr);
726 }
727
728 /**
729 * @return 0 on success, -1 on error (and print addr).
730 */
731 extern int
732 umoven_or_printaddr64(struct tcb *, uint64_t addr,
733 unsigned int len, void *laddr);
734 # define umove_or_printaddr64(pid, addr, objp) \
735 umoven_or_printaddr64((pid), (addr), sizeof(*(objp)), (void *) (objp))
736
737 static inline int
738 umoven_or_printaddr(struct tcb *tcp, const kernel_ulong_t addr,
739 unsigned int len, void *laddr)
740 {
741 return umoven_or_printaddr64(tcp, addr, len, laddr);
742 }
743 # define umove_or_printaddr(pid, addr, objp) \
744 umoven_or_printaddr((pid), (addr), sizeof(*(objp)), (void *) (objp))
745
746 /**
747 * Retrieves up to 8 bytes (that are treated as a single unsigned integer value)
748 * and puts it into variable pointed by laddr;
749 *
750 * @return 0 on success, -1 (and print addr) on error or if len is more
751 * than size of uint64_t.
752 */
753 extern int
754 umoven_to_uint64_or_printaddr64(struct tcb *, uint64_t addr,
755 unsigned int len, uint64_t *laddr);
756
757 static inline int
758 umoven_to_uint64_or_printaddr(struct tcb *tcp, const kernel_ulong_t addr,
759 unsigned int len, uint64_t *laddr)
760 {
761 return umoven_to_uint64_or_printaddr64(tcp, addr, len, laddr);
762 }
763
764 /**
765 * @return 0 on success, -1 on error (and print addr).
766 */
767 extern int
768 umoven_or_printaddr64_ignore_syserror(struct tcb *, uint64_t addr,
769 unsigned int len, void *laddr);
770 # define umove_or_printaddr64_ignore_syserror(pid, addr, objp) \
771 umoven_or_printaddr64_ignore_syserror((pid), (addr), sizeof(*(objp)), \
772 (void *) (objp))
773
774 static inline int
775 umoven_or_printaddr_ignore_syserror(struct tcb *tcp, const kernel_ulong_t addr,
776 unsigned int len, void *laddr)
777 {
778 return umoven_or_printaddr64_ignore_syserror(tcp, addr, len, laddr);
779 }
780 # define umove_or_printaddr_ignore_syserror(pid, addr, objp) \
781 umoven_or_printaddr_ignore_syserror((pid), (addr), sizeof(*(objp)), \
782 (void *) (objp))
783
784 /**
785 * @return strlen + 1 on success, 0 on success and no NUL seen, -1 on error.
786 */
787 extern int
788 umovestr(struct tcb *, kernel_ulong_t addr, unsigned int len, char *laddr);
789
790 /* Invalidate the cache used by umove* functions. */
791 extern void invalidate_umove_cache(void);
792
793 extern int upeek(struct tcb *tcp, unsigned long, kernel_ulong_t *);
794 extern int upoke(struct tcb *tcp, unsigned long, kernel_ulong_t);
795
796 # if HAVE_ARCH_GETRVAL2
797 extern long getrval2(struct tcb *);
798 # endif
799
800 extern const char *signame(const int);
801 extern const char *sprintsigname(const int);
802
803 extern void pathtrace_select_set(const char *, struct path_set *);
804 extern bool pathtrace_match_set(struct tcb *, struct path_set *,
805 struct number_set *);
806
807 static inline void
808 pathtrace_select(const char *path)
809 {
810 return pathtrace_select_set(path, &global_path_set);
811 }
812
813 static inline bool
814 pathtrace_match(struct tcb *tcp)
815 {
816 return pathtrace_match_set(tcp, &global_path_set, trace_fd_set);
817 }
818
819 /**
820 * Resolves a path for a fd procfs PID proc_pid (the one got from
821 * get_proc_pid()).
822 *
823 * @param proc_pid PID number in /proc, obtained with get_proc_pid().
824 * @param fd FD to resolve path for.
825 * @param buf Buffer to store the resolved path in.
826 * @param bufsize The size of buf.
827 * @param deleted If non-NULL, set to true if the path associated with the FD
828 * seems to have been unlinked and to false otherwise.
829 * @return Number of bytes written including terminating '\0'.
830 */
831 extern int get_proc_pid_fd_path(int proc_pid, int fd, char *buf,
832 unsigned bufsize, bool *deleted);
833
834 extern int getfdpath_pid(pid_t pid, int fd, char *buf, unsigned bufsize,
835 bool *deleted);
836
837 static inline int
838 getfdpath(struct tcb *tcp, int fd, char *buf, unsigned bufsize)
839 {
840 return getfdpath_pid(tcp->pid, fd, buf, bufsize, NULL);
841 }
842
843 extern unsigned long getfdinode(struct tcb *, int);
844 extern enum sock_proto getfdproto(struct tcb *, int);
845
846 extern const char *xlookup(const struct xlat *, const uint64_t);
847 extern const char *xlookup_le(const struct xlat *, uint64_t *);
848
849 struct dyxlat;
850 struct dyxlat *dyxlat_alloc(size_t nmemb, enum xlat_type type);
851 void dyxlat_free(struct dyxlat *);
852 const struct xlat *dyxlat_get(const struct dyxlat *);
853 void dyxlat_add_pair(struct dyxlat *, uint64_t val, const char *str, size_t len);
854
855 const struct xlat *genl_families_xlat(struct tcb *tcp);
856
857 extern unsigned long get_pagesize(void);
858 extern int next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits);
859
860 /*
861 * Returns STR if it does not start with PREFIX,
862 * or a pointer to the first char in STR after PREFIX.
863 * The length of PREFIX is specified by PREFIX_LEN.
864 */
865 static inline const char *
866 str_strip_prefix_len(const char *str, const char *prefix, size_t prefix_len)
867 {
868 return strncmp(str, prefix, prefix_len) ? str : str + prefix_len;
869 }
870
871 # define STR_STRIP_PREFIX(str, prefix) \
872 str_strip_prefix_len((str), (prefix), sizeof(prefix) - 1)
873
874 /** String is '\0'-terminated. */
875 # define QUOTE_0_TERMINATED 0x01
876 /** Do not emit leading and ending '"' characters. */
877 # define QUOTE_OMIT_LEADING_TRAILING_QUOTES 0x02
878 /** Do not print '\0' if it is the last character. */
879 # define QUOTE_OMIT_TRAILING_0 0x08
880 /** Print ellipsis if the last character is not '\0' */
881 # define QUOTE_EXPECT_TRAILING_0 0x10
882 /** Print string in hex (using '\xHH' notation). */
883 # define QUOTE_FORCE_HEX (QUOTE_OVERWRITE_HEXSTR | QUOTE_HEXSTR_ALL)
884 /** Enclose the string in C comment syntax. */
885 # define QUOTE_EMIT_COMMENT 0x40
886 /** Override xflag setting with one of QUOTE_HEXSTR_[NA]* styles */
887 # define QUOTE_OVERWRITE_HEXSTR 0x80
888
889 /*
890 * bits 8..9 (mask 0x300) are used for supplying the respective hexstr setting
891 * if QUOTE_OVERWRITE_HEXSTR is set.
892 */
893 # define QUOTE_HEXSTR_SHIFT 8
894 /**
895 * Mask to be applied to a quoting style to extract the xflag overriding style
896 * part.
897 */
898 # define QUOTE_HEXSTR_MASK (0x3 << QUOTE_HEXSTR_SHIFT)
899 static_assert((NUM_HEXSTR_OPTS - 1) <= (QUOTE_HEXSTR_MASK >> QUOTE_HEXSTR_SHIFT),
900 "xflag options do not fit into QUOTE_HEXSTR_MASK");
901
902 # define QUOTE_HEXSTR_NONE (HEXSTR_NONE << QUOTE_HEXSTR_SHIFT)
903 # define QUOTE_HEXSTR_ALL (HEXSTR_ALL << QUOTE_HEXSTR_SHIFT)
904 # define QUOTE_HEXSTR_NON_ASCII (HEXSTR_NON_ASCII << QUOTE_HEXSTR_SHIFT)
905 # define QUOTE_HEXSTR_NON_ASCII_CHARS (HEXSTR_NON_ASCII_CHARS << QUOTE_HEXSTR_SHIFT)
906
907 extern int string_quote(const char *, char *, unsigned int, unsigned int,
908 const char *escape_chars);
909 extern int print_quoted_string_ex(const char *, unsigned int, unsigned int,
910 const char *escape_chars);
911 extern int print_quoted_string(const char *, unsigned int, unsigned int);
912 extern int print_quoted_cstring(const char *, unsigned int);
913
914 /* a refers to the lower numbered u_arg,
915 * b refers to the higher numbered u_arg
916 */
917 # ifdef WORDS_BIGENDIAN
918 # define ULONG_LONG(a, b) \
919 ((unsigned long long)(unsigned)(b) | ((unsigned long long)(a)<<32))
920 # else
921 # define ULONG_LONG(a, b) \
922 ((unsigned long long)(unsigned)(a) | ((unsigned long long)(b)<<32))
923 # endif
924 extern unsigned int getllval(struct tcb *, unsigned long long *, unsigned int);
925 extern unsigned int print_arg_lld(struct tcb *, unsigned int);
926 extern unsigned int print_arg_llu(struct tcb *, unsigned int);
927
928 extern void printaddr64(uint64_t addr);
929
930 static inline void
931 printaddr(const kernel_ulong_t addr)
932 {
933 printaddr64(addr);
934 }
935
936 # define xlat_verbose(style_) ((style_) & XLAT_STYLE_VERBOSITY_MASK)
937 # define xlat_format(style_) ((style_) & XLAT_STYLE_FORMAT_MASK)
938
939 extern enum xlat_style xlat_verbosity;
940
941 extern int printxvals_ex(uint64_t val, const char *dflt,
942 enum xlat_style, const struct xlat *, ...)
943 ATTRIBUTE_SENTINEL;
944 # define printxvals(val_, dflt_, ...) \
945 printxvals_ex((val_), (dflt_), XLAT_STYLE_DEFAULT, __VA_ARGS__)
946 # define printxval_ex(xlat_, val_, dflt_, style_) \
947 printxvals_ex((val_), (dflt_), (style_), (xlat_), NULL)
948
949 extern int sprintxval_ex(char *buf, size_t size, const struct xlat *,
950 unsigned int val, const char *dflt, enum xlat_style);
951
952 static inline int
953 sprintxval(char *buf, size_t size, const struct xlat *xlat, unsigned int val,
954 const char *dflt)
955 {
956 return sprintxval_ex(buf, size, xlat, val, dflt, XLAT_STYLE_DEFAULT);
957 }
958
959 enum xlat_style_private_flag_bits {
960 /* print_array */
961 PAF_PRINT_INDICES_BIT = XLAT_STYLE_SPEC_BITS + 1,
962 PAF_ARRAY_TRUNCATED_BIT,
963
964 /* print_xlat */
965 PXF_DEFAULT_STR_BIT,
966
967 /* sprintflags_ex */
968 SPFF_AUXSTR_MODE_BIT,
969 };
970
971 enum xlat_style_private_flags {
972 /* print_array */
973 FLAG(PAF_PRINT_INDICES),
974 FLAG(PAF_ARRAY_TRUNCATED),
975
976 /* print_xlat */
977 FLAG(PXF_DEFAULT_STR),
978
979 /* sprintflags_ex */
980 FLAG(SPFF_AUXSTR_MODE),
981 };
982
983 /**
984 * Print a value in accordance with xlat formatting settings.
985 *
986 * @param val Value itself.
987 * @param str String representation of the value. Semantics may be affected
988 * by style argument;
989 * @param style Combination of flags from enum xlat_style and PXF_* flags
990 * from enum xlat_style_private_flags:
991 * - PXF_DEFAULT_STR - interpret str argument as default
992 * (fallback) string and not as string representation of val.
993 */
994 extern void print_xlat_ex(uint64_t val, const char *str, uint32_t style);
995 # define print_xlat(val_) \
996 print_xlat_ex((val_), #val_, XLAT_STYLE_DEFAULT)
997 # define print_xlat32(val_) \
998 print_xlat_ex((uint32_t) (val_), #val_, XLAT_STYLE_DEFAULT)
999 # define print_xlat_u(val_) \
1000 print_xlat_ex((val_), #val_, XLAT_STYLE_FMT_U)
1001 # define print_xlat_d(val_) \
1002 print_xlat_ex((val_), #val_, XLAT_STYLE_FMT_D)
1003
1004 extern int printargs(struct tcb *);
1005 extern int printargs_u(struct tcb *);
1006 extern int printargs_d(struct tcb *);
1007
1008 extern int printflags_ex(uint64_t flags, const char *dflt,
1009 enum xlat_style, const struct xlat *, ...)
1010 ATTRIBUTE_SENTINEL;
1011 extern const char *sprintflags_ex(const char *prefix, const struct xlat *,
1012 uint64_t flags, char sep, enum xlat_style);
1013
1014 static inline const char *
1015 sprintflags(const char *prefix, const struct xlat *xlat, uint64_t flags)
1016 {
1017 return sprintflags_ex(prefix, xlat, flags, '\0', XLAT_STYLE_DEFAULT);
1018 }
1019
1020 extern const char *sprinttime(long long sec);
1021 extern const char *sprinttime_nsec(long long sec, unsigned long long nsec);
1022 extern const char *sprinttime_usec(long long sec, unsigned long long usec);
1023
1024 # ifndef MAX_ADDR_LEN
1025 # define MAX_ADDR_LEN 32
1026 # endif
1027
1028 extern void print_mac_addr(const char *prefix,
1029 const uint8_t addr[], size_t size);
1030
1031 extern void print_hwaddr(const char *prefix,
1032 const uint8_t addr[], size_t size, uint32_t devtype);
1033
1034 extern void print_uuid(const unsigned char *uuid);
1035
1036 extern void print_symbolic_mode_t(unsigned int);
1037 extern void print_numeric_umode_t(unsigned short);
1038 extern void print_numeric_ll_umode_t(unsigned long long);
1039 extern void print_dev_t(unsigned long long dev);
1040 extern void print_kernel_version(unsigned long version);
1041 extern void print_abnormal_hi(kernel_ulong_t);
1042 extern void print_ioprio(unsigned int ioprio);
1043
1044 extern bool print_int_array_member(struct tcb *, void *elem_buf,
1045 size_t elem_size, void *data);
1046 extern bool print_uint_array_member(struct tcb *, void *elem_buf,
1047 size_t elem_size, void *data);
1048 extern bool print_xint_array_member(struct tcb *, void *elem_buf,
1049 size_t elem_size, void *data);
1050 extern bool print_fd_array_member(struct tcb *, void *elem_buf,
1051 size_t elem_size, void *data);
1052
1053 typedef bool (*tfetch_mem_fn)(struct tcb *, kernel_ulong_t addr,
1054 unsigned int size, void *dest);
1055 typedef bool (*print_fn)(struct tcb *, void *elem_buf,
1056 size_t elem_size, void *opaque_data);
1057 typedef int (*print_obj_by_addr_fn)(struct tcb *, kernel_ulong_t);
1058 typedef const char * (*sprint_obj_by_addr_fn)(struct tcb *, kernel_ulong_t);
1059 typedef void (*print_obj_by_addr_size_fn)(struct tcb *,
1060 kernel_ulong_t addr,
1061 kernel_ulong_t size,
1062 void *opaque_data);
1063
1064
1065 /**
1066 * Array printing function with an over-engineered interface.
1067 *
1068 * @param start_addr If tfetch_mem_fn is non-NULL: an address in tracee's
1069 * memory where the start of the array is located.
1070 * If tfetch_mem_fn is NULL: ignored.
1071 * @param nmemb Number of elements in the array.
1072 * @param elem_buf If tfetch_mem_fn is non-NULL: a buffer where each
1073 * element fetched by tfetch_mem_fn is stored.
1074 * If tfetch_mem_fn is NULL: an address of the start
1075 * of the array in strace's memory.
1076 * @param elem_size Size (in bytes) of the each element in the array.
1077 * @param tfetch_mem_fn Fetching function. If it is NULL, then elem_buf
1078 * is treated as a local array of nmemb members
1079 * of elem_size each, and start_addr is ignored.
1080 * @param print_func Element printing callback.
1081 * @param opaque_data A value that is unconditionally passed to print_func
1082 * in the opaque_data argument.
1083 * @param flags Combination of xlat style settings and additional
1084 * flags from enum print_array_flags that are appled
1085 * to index printing.
1086 * @param index_xlat xlat that is used for printing indices.
1087 * @param index_dflt Default string for the values not found
1088 * in index_xlat.
1089 * @return true if at least one element has been fetched
1090 * successfully (tfetch_mem_fn returned true);
1091 * false otherwise.
1092 */
1093 extern bool
1094 print_array_ex(struct tcb *,
1095 kernel_ulong_t start_addr,
1096 size_t nmemb,
1097 void *elem_buf,
1098 size_t elem_size,
1099 tfetch_mem_fn tfetch_mem_func,
1100 print_fn print_func,
1101 void *opaque_data,
1102 unsigned int flags,
1103 const struct xlat *index_xlat,
1104 const char *index_dflt);
1105
1106 /** Print an array from tracee's memory without any index printing features. */
1107 static inline bool
1108 print_array(struct tcb *const tcp,
1109 const kernel_ulong_t start_addr,
1110 const size_t nmemb,
1111 void *const elem_buf,
1112 const size_t elem_size,
1113 tfetch_mem_fn tfetch_mem_func,
1114 print_fn print_func,
1115 void *const opaque_data)
1116 {
1117 return print_array_ex(tcp, start_addr, nmemb, elem_buf, elem_size,
1118 tfetch_mem_func, print_func, opaque_data,
1119 0, NULL, NULL);
1120 }
1121
1122 /** Shorthand for printing local arrays. */
1123 static inline bool
1124 print_local_array_ex(struct tcb *tcp,
1125 const void *start_addr,
1126 const size_t nmemb,
1127 const size_t elem_size,
1128 print_fn print_func,
1129 void *const opaque_data,
1130 unsigned int flags,
1131 const struct xlat *index_xlat,
1132 const char *index_dflt)
1133 {
1134 return print_array_ex(tcp, (uintptr_t) start_addr, nmemb,
1135 NULL, elem_size, NULL, print_func,
1136 opaque_data, flags, index_xlat, index_dflt);
1137 }
1138
1139 /** Shorthand for a shorthand for printing local arrays. */
1140 # define print_local_array(tcp_, start_addr_, print_func_) \
1141 print_local_array_ex((tcp_), (start_addr_), ARRAY_SIZE(start_addr_), \
1142 sizeof((start_addr_)[0]), (print_func_), \
1143 NULL, 0, NULL, NULL)
1144
1145 # define print_local_array_upto(tcp_, start_addr_, upto_, print_func_) \
1146 print_local_array_ex((tcp_), (start_addr_), \
1147 CLAMP((upto_), 0, \
1148 (typeof(upto_)) ARRAY_SIZE(start_addr_)), \
1149 sizeof((start_addr_)[0]), (print_func_), \
1150 NULL, 0, NULL, NULL)
1151
1152 extern kernel_ulong_t *
1153 fetch_indirect_syscall_args(struct tcb *, kernel_ulong_t addr, unsigned int n_args);
1154
1155 extern void pidns_init(void);
1156
1157 /**
1158 * Returns PID as present in /proc of the tracer (can be different from tracee
1159 * PID if /proc and the tracer process are in different PID namespaces).
1160 */
1161 extern int get_proc_pid(int pid);
1162
1163 /**
1164 * Translates a pid from tracee's namespace to our namespace.
1165 *
1166 * @param tcp The tcb of the tracee
1167 * (NULL: from_id is in strace's namespace. Useful for
1168 * getting the proc PID of from_id)
1169 * @param from_id The id to be translated
1170 * @param type The PID type of from_id
1171 * @param proc_pid_ptr If not NULL, writes the proc PID to this location
1172 * @return The translated id, or 0 if translation fails.
1173 */
1174 extern int translate_pid(struct tcb *, int dest_id, enum pid_type type,
1175 int *proc_pid_ptr);
1176
1177 extern void
1178 dumpiov_in_msghdr(struct tcb *, kernel_ulong_t addr, kernel_ulong_t data_size);
1179
1180 extern void
1181 dumpiov_in_mmsghdr(struct tcb *, kernel_ulong_t addr);
1182
1183 extern void
1184 dumpiov_upto(struct tcb *, int len, kernel_ulong_t addr, kernel_ulong_t data_size);
1185
1186 extern void
1187 dumpstr(struct tcb *, kernel_ulong_t addr, kernel_ulong_t len);
1188
1189 extern int
1190 printstr_ex(struct tcb *, kernel_ulong_t addr, kernel_ulong_t len,
1191 unsigned int user_style);
1192
1193 /**
1194 * Print a region of tracee memory only in case non-zero bytes are present
1195 * there. It almost fits into printstr_ex, but it has some pretty specific
1196 * behaviour peculiarities (like printing of ellipsis on error) to readily
1197 * integrate it there.
1198 *
1199 * Since it is expected to be used for printing tail of a structure in tracee's
1200 * memory, it accepts a combination of start_addr/start_offs/total_len and does
1201 * the relevant calculations itself.
1202 *
1203 * @param prefix_fun A function called before something is going to be printed.
1204 * @param start_addr Address of the beginning of a structure (whose tail
1205 * is supposedly to be printed) in tracee's memory.
1206 * @param start_offs Offset from the beginning of the structure where the tail
1207 * data starts.
1208 * @param total_len Total size of the tracee's memory region containing
1209 * the structure and the tail data.
1210 * Caller is responsible for imposing a sensible (usually
1211 * mandated by the kernel interface, like get_pagesize())
1212 * limit here.
1213 * @param style Passed to string_quote as "style" parameter.
1214 * @return Returns true is anything was printed, false otherwise.
1215 */
1216 extern bool print_nonzero_bytes(struct tcb *const tcp,
1217 void (*prefix_fun)(void),
1218 const kernel_ulong_t start_addr,
1219 const unsigned int start_offs,
1220 const unsigned int total_len,
1221 const unsigned int style);
1222
1223 extern int
1224 printpathn(struct tcb *, kernel_ulong_t addr, unsigned int n);
1225
1226 extern int
1227 printpath(struct tcb *, kernel_ulong_t addr);
1228
1229 # define TIMESPEC_TEXT_BUFSIZE \
1230 (sizeof(long long) * 3 * 2 + sizeof("{tv_sec=-, tv_nsec=}"))
1231
1232 /**
1233 * Returns the pid associated with pidfd of the process with ID pid_of_fd
1234 */
1235 extern pid_t pidfd_get_pid(pid_t pid_of_fd, int fd);
1236 /**
1237 * Print file descriptor fd owned by process with ID pid (from the PID NS
1238 * of the tracer).
1239 */
1240 extern void printfd_pid_with_finfo(struct tcb *tcp, pid_t pid, int fd,
1241 const struct finfo *finfo);
1242
1243 static inline void
1244 printfd_pid(struct tcb *tcp, pid_t pid, int fd)
1245 {
1246 printfd_pid_with_finfo(tcp, pid, fd, NULL);
1247 }
1248
1249 static inline void
1250 printfd(struct tcb *tcp, int fd)
1251 {
1252 printfd_pid(tcp, tcp->pid, fd);
1253 }
1254
1255 static inline void
1256 printfd_with_finfo(struct tcb *tcp, int fd, const struct finfo *finfo)
1257 {
1258 printfd_pid_with_finfo(tcp, tcp->pid, fd, finfo);
1259 }
1260
1261 /**
1262 * Helper function, converts pid to string, or to "self" for pid == 0.
1263 * Uses static buffer for operation.
1264 */
1265 extern const char *pid_to_str(pid_t pid);
1266
1267 /**
1268 * Get list of IDs present in a proc status record. IDs are placed in the array
1269 * in the order they are stored in /proc.
1270 *
1271 * @param proc_pid PID (as present in /proc) to get information for.
1272 * @param id_buf Pointer to buffer where parsed IDs are stored.
1273 * Can be NULL.
1274 * @param id_buf_size Number of items id_buf can store. If there are more IDs
1275 * than id_buf can store, the excess IDs are not stored
1276 * but still counted.
1277 * @param str (Prefix) string IDs from which are to be read.
1278 * @param str_size Size of the prefix string. Can be 0, in this case str
1279 * has to be '\0'-terminated in order to be able to obtain
1280 * its length.
1281 * @return Number of items read (even if they are not written).
1282 * 0 indicates error.
1283 */
1284 extern size_t proc_status_get_id_list(int proc_pid,
1285 int *id_buf, size_t id_buf_size,
1286 const char *str, size_t str_size);
1287
1288 /**
1289 * Print file descriptor fd owned by process with ID pid (from the PID NS
1290 * of the tracee).
1291 */
1292 extern void printfd_pid_tracee_ns(struct tcb *tcp, pid_t pid, int fd);
1293
1294 /** Prints a PID specified in the tracee's PID namespace */
1295 extern void printpid(struct tcb *, int pid, enum pid_type type);
1296
1297 /**
1298 * Prints pid as a TGID if positive, and PGID if negative
1299 * (like the first argument of kill).
1300 */
1301 extern void printpid_tgid_pgid(struct tcb *, int pid);
1302 extern void print_sockaddr(struct tcb *, const void *sa, int len);
1303 extern bool
1304 print_inet_addr(int af, const void *addr, unsigned int len, const char *var_name);
1305 extern bool
1306 decode_inet_addr(struct tcb *, kernel_ulong_t addr,
1307 unsigned int len, int family, const char *var_name);
1308 extern void print_ax25_addr(const void /* ax25_address */ *addr);
1309 extern void print_x25_addr(const void /* struct x25_address */ *addr);
1310 extern const char *get_sockaddr_by_inode(struct tcb *, int fd, unsigned long inode);
1311
1312 /**
1313 * Prints dirfd file descriptor and saves it in tcp->last_dirfd,
1314 * the latter is used when printing SELinux contexts.
1315 */
1316 extern void print_dirfd(struct tcb *, int);
1317
1318 extern int
1319 decode_sockaddr(struct tcb *, kernel_ulong_t addr, int addrlen);
1320
1321 extern void printuid(const unsigned int);
1322
1323 extern void
1324 print_sigset_addr_len(struct tcb *, kernel_ulong_t addr, kernel_ulong_t len);
1325 extern void
1326 print_sigset_addr(struct tcb *, kernel_ulong_t addr);
1327
1328 extern const char *sprintsigmask_n(const char *, const void *, unsigned int);
1329 extern void printsignal(int);
1330
1331 extern void
1332 tprint_iov_upto(struct tcb *, kernel_ulong_t len, kernel_ulong_t addr,
1333 kernel_ulong_t data_size, print_obj_by_addr_size_fn,
1334 void *opaque_data);
1335 extern void
1336 iov_decode_addr(struct tcb *, kernel_ulong_t addr, kernel_ulong_t size,
1337 void *opaque_data);
1338 extern void
1339 iov_decode_str(struct tcb *, kernel_ulong_t addr, kernel_ulong_t size,
1340 void *opaque_data);
1341
1342 extern void
1343 decode_netlink(struct tcb *, int fd, kernel_ulong_t addr, kernel_ulong_t len);
1344
1345 extern void tprint_open_modes(unsigned int);
1346 extern const char *sprint_open_modes(unsigned int);
1347
1348 extern void
1349 decode_seccomp_fprog(struct tcb *, kernel_ulong_t addr);
1350
1351 extern void
1352 print_seccomp_fprog(struct tcb *, kernel_ulong_t addr, unsigned short len);
1353
1354 extern void
1355 decode_sock_fprog(struct tcb *, kernel_ulong_t addr);
1356
1357 extern void
1358 print_sock_fprog(struct tcb *, kernel_ulong_t addr, unsigned short len);
1359
1360 struct strace_stat;
1361 extern void print_struct_stat(struct tcb *, const struct strace_stat *const st);
1362
1363 struct strace_statfs;
1364 struct strace_keyctl_kdf_params;
1365
1366 extern void
1367 print_struct_statfs(struct tcb *, kernel_ulong_t addr);
1368
1369 extern void
1370 print_struct_statfs64(struct tcb *, kernel_ulong_t addr, kernel_ulong_t size);
1371
1372 extern int
1373 fetch_perf_event_attr(struct tcb *const tcp, const kernel_ulong_t addr);
1374 extern void
1375 print_perf_event_attr(struct tcb *const tcp, const kernel_ulong_t addr);
1376
1377 extern const char *get_ifname(const unsigned int ifindex);
1378 extern void print_ifindex(unsigned int);
1379
1380 struct tcpvegas_info;
1381 extern void print_tcpvegas_info(struct tcb *tcp,
1382 const struct tcpvegas_info *vegas,
1383 unsigned int len);
1384 struct tcp_dctcp_info;
1385 extern void print_tcp_dctcp_info(struct tcb *tcp,
1386 const struct tcp_dctcp_info *dctcp,
1387 unsigned int len);
1388 struct tcp_bbr_info;
1389 extern void print_tcp_bbr_info(struct tcb *tcp, const struct tcp_bbr_info *bbr,
1390 unsigned int len);
1391
1392 extern void print_bpf_filter_code(const uint16_t code, bool extended);
1393
1394 /**
1395 * Prints a CPU set, pointed at by addr, as a list of CPU IDs. CPU set
1396 * (CPU mask, CPU affinity set, CPU affinity list) is a bit set represented
1397 * as an array of elements of unsigned long type.
1398 *
1399 * @param tcp TCB pointer.
1400 * @param addr An address of the CPU set.
1401 * @param len Length of CPU set in bytes.
1402 */
1403 extern void print_affinitylist(struct tcb *const tcp, const kernel_ulong_t addr,
1404 const unsigned int len);
1405
1406 extern void qualify(const char *);
1407 extern void qualify_trace(const char *);
1408 extern void qualify_trace_fd(const char *);
1409 extern void qualify_abbrev(const char *);
1410 extern void qualify_verbose(const char *);
1411 extern void qualify_raw(const char *);
1412 extern void qualify_signals(const char *);
1413 extern void qualify_status(const char *);
1414 extern void qualify_quiet(const char *);
1415 extern void qualify_decode_fd(const char *);
1416 extern void qualify_decode_pid(const char *);
1417 extern void qualify_read(const char *);
1418 extern void qualify_write(const char *);
1419 extern void qualify_fault(const char *);
1420 extern void qualify_inject(const char *);
1421 extern void qualify_kvm(const char *);
1422 extern unsigned int qual_flags(const unsigned int);
1423
1424 # define DECL_IOCTL(name) \
1425 extern int \
1426 name ## _ioctl(struct tcb *, unsigned int request, kernel_ulong_t arg) \
1427 /* End of DECL_IOCTL definition. */
1428
1429 DECL_IOCTL(counter);
1430 DECL_IOCTL(dm);
1431 DECL_IOCTL(evdev);
1432 DECL_IOCTL(fs_0x94);
1433 DECL_IOCTL(fs_f);
1434 DECL_IOCTL(fs_x);
1435 DECL_IOCTL(gpio);
1436 DECL_IOCTL(inotify);
1437 DECL_IOCTL(kd);
1438 DECL_IOCTL(kvm);
1439 DECL_IOCTL(lirc);
1440 DECL_IOCTL(nbd);
1441 DECL_IOCTL(nsfs);
1442 DECL_IOCTL(ptp);
1443 DECL_IOCTL(random);
1444 DECL_IOCTL(seccomp);
1445 DECL_IOCTL(scsi);
1446 DECL_IOCTL(tee);
1447 DECL_IOCTL(term);
1448 DECL_IOCTL(ubi);
1449 DECL_IOCTL(uffdio);
1450 DECL_IOCTL(watchdog);
1451 # undef DECL_IOCTL
1452
1453 extern int decode_sg_io_v4(struct tcb *, const kernel_ulong_t arg);
1454 extern void print_evdev_ff_type(const kernel_ulong_t val);
1455
1456 struct nlmsghdr;
1457
1458 typedef bool (*netlink_decoder_t)(struct tcb *, const struct nlmsghdr *,
1459 kernel_ulong_t addr, unsigned int len);
1460
1461 # define DECL_NETLINK(name) \
1462 extern bool \
1463 decode_netlink_ ## name(struct tcb *, const struct nlmsghdr *, \
1464 kernel_ulong_t addr, unsigned int len) \
1465 /* End of DECL_NETLINK definition. */
1466
1467 DECL_NETLINK(crypto);
1468 DECL_NETLINK(netfilter);
1469 DECL_NETLINK(route);
1470 DECL_NETLINK(selinux);
1471 DECL_NETLINK(sock_diag);
1472
1473 extern void
1474 decode_netlink_kobject_uevent(struct tcb *, kernel_ulong_t addr,
1475 kernel_ulong_t len);
1476
1477 enum find_xlat_flag_bits {
1478 FXL_CASE_SENSITIVE_BIT,
1479 };
1480
1481 enum find_xlat_flags {
1482 /** Whether to use strcmp instead of strcasecmp for comparison */
1483 FLAG(FXL_CASE_SENSITIVE),
1484 };
1485
1486 /**
1487 * Searches for a string-value pair in the provided array of pairs.
1488 *
1489 * @param items Array of string-value pairs to search in.
1490 * @param s String to search for.
1491 * @param num_items Item count in items array.
1492 * @param flags Bitwise-or'ed flags from enum find_xlat_flags.
1493 * @return Pointer to the first matching string-value pair inside items
1494 * or NULL if nothing has been found.
1495 */
1496 extern const struct xlat_data *find_xlat_val_ex(const struct xlat_data *items,
1497 const char *s, size_t num_items,
1498 unsigned int flags);
1499 # define find_xlat_val(items_, s_) \
1500 find_xlat_val_ex((items_), (s_), ARRAY_SIZE(items_), 0)
1501 # define find_xlat_val_case(items_, s_) \
1502 find_xlat_val_ex((items_), (s_), ARRAY_SIZE(items_), FXL_CASE_SENSITIVE)
1503
1504 /**
1505 * A find_xlat_val_ex wrapper for option arguments parsing. Provides a value
1506 * from strs array that matched the supplied arg string. If arg is NULL,
1507 * default_val is returned. If nothing has matched, not_found value
1508 * is returned.
1509 *
1510 * find_arg_val provides a wrapper for the common case of statically-defined
1511 * strs arrays.
1512 *
1513 * @param arg Argument string to parse
1514 * @param strs Array of string-value pairs to match arg against.
1515 * @param strs_size Element count in the strs array.
1516 * @param default_val Value to return if arg is NULL.
1517 * @param not_found Value to return if arg hasn't found among strs.
1518 * @return default_val is arg is NULL, value part of the matched
1519 * string-value pair, or not_found if nothing has matched.
1520 */
1521 extern uint64_t find_arg_val_(const char *arg, const struct xlat_data *strs,
1522 size_t strs_size, uint64_t default_val,
1523 uint64_t not_found);
1524 /** A find_arg_val_ wrapper that supplies strs_size to it using ARRAY_SIZE. */
1525 # define find_arg_val(arg_, strs_, dflt_, not_found_) \
1526 find_arg_val_((arg_), (strs_), ARRAY_SIZE(strs_), (dflt_), (not_found_))
1527
1528 /**
1529 * A find_arg_val wrapper for parsing time scale names.
1530 *
1531 * @param arg String to parse
1532 * @param empty_dflt Default scale for the empty arg.
1533 * @param null_dflt Default scale for the NULL arg.
1534 * @param width Width of the field required to print the second part
1535 * with the specified precision.
1536 * @return Time scale (amount of nanoseconds) if found,
1537 * empty_dflt if arg is empty, null_dflt if arg is NULL,
1538 * -1 if arg doesn't match any known time scale.
1539 */
1540 extern int str2timescale_ex(const char *arg, int empty_dflt, int null_dflt,
1541 int *width);
1542 /** str2timescale_ex wrapper for handling a separate argument. */
1543 # define str2timescale_optarg(arg_, w_) str2timescale_ex((arg_), -1, 1000, (w_))
1544 /** str2timescale_ex wrapper for handling a suffix in existing argument. */
1545 # define str2timescale_sfx(arg_, w_) str2timescale_ex((arg_), 1000, -1, (w_))
1546
1547 extern int ts_nz(const struct timespec *);
1548 extern int ts_cmp(const struct timespec *, const struct timespec *);
1549 extern double ts_float(const struct timespec *);
1550 extern void ts_add(struct timespec *, const struct timespec *, const struct timespec *);
1551 extern void ts_sub(struct timespec *, const struct timespec *, const struct timespec *);
1552 extern void ts_div(struct timespec *, const struct timespec *, uint64_t);
1553 extern const struct timespec *ts_min(const struct timespec *, const struct timespec *);
1554 extern const struct timespec *ts_max(const struct timespec *, const struct timespec *);
1555 extern int parse_ts(const char *s, struct timespec *t);
1556
1557 /** Print a time interval value specified as a number 1/freq-sized ticks. */
1558 extern void print_ticks(uint64_t val, long freq, unsigned int precision);
1559 extern void print_ticks_d(int64_t val, long freq, unsigned int precision);
1560 /** Print a clock_t value, as used by times(2) (and not clock(3)). */
1561 extern void print_clock_t(uint64_t val);
1562
1563 # ifdef ENABLE_STACKTRACE
1564 extern void unwind_init(void);
1565 extern void unwind_tcb_init(struct tcb *);
1566 extern void unwind_tcb_fin(struct tcb *);
1567 extern void unwind_tcb_print(struct tcb *);
1568 extern void unwind_tcb_capture(struct tcb *);
1569 # endif
1570
1571 # ifdef HAVE_LINUX_KVM_H
1572 extern void kvm_run_structure_decoder_init(void);
1573 extern void kvm_vcpu_info_free(struct tcb *);
1574 # endif
1575
1576 extern void maybe_load_task_comm(struct tcb *tcp);
1577 /* Print the contents of /proc/$pid/comm. */
1578 extern void print_pid_comm(int pid);
1579
1580 static inline int
1581 printstrn(struct tcb *tcp, kernel_ulong_t addr, kernel_ulong_t len)
1582 {
1583 return printstr_ex(tcp, addr, len, 0);
1584 }
1585
1586 static inline int
1587 printstr(struct tcb *tcp, kernel_ulong_t addr)
1588 {
1589 return printstr_ex(tcp, addr, -1, QUOTE_0_TERMINATED);
1590 }
1591
1592 static inline int
1593 printflags64_in(const struct xlat *x, uint64_t flags, const char *dflt)
1594 {
1595 return printflags_ex(flags, dflt, XLAT_STYLE_DEFAULT, x, NULL);
1596 }
1597
1598 static inline int
1599 printflags_in(const struct xlat *x, unsigned int flags, const char *dflt)
1600 {
1601 return printflags64_in(x, flags, dflt);
1602 }
1603
1604 static inline int
1605 printxval64(const struct xlat *x, const uint64_t val, const char *dflt)
1606 {
1607 return printxvals(val, dflt, x, NULL);
1608 }
1609
1610 static inline int
1611 printxval(const struct xlat *x, const unsigned int val, const char *dflt)
1612 {
1613 return printxvals(val, dflt, x, NULL);
1614 }
1615
1616 static inline int
1617 printxval64_u(const struct xlat *x, const uint64_t val, const char *dflt)
1618 {
1619 return printxvals_ex(val, dflt, XLAT_STYLE_FMT_U, x, NULL);
1620 }
1621
1622 static inline int
1623 printxval_u(const struct xlat *x, const unsigned int val, const char *dflt)
1624 {
1625 return printxvals_ex(val, dflt, XLAT_STYLE_FMT_U, x, NULL);
1626 }
1627
1628 static inline int
1629 printxval64_d(const struct xlat *x, const int64_t val, const char *dflt)
1630 {
1631 return printxvals_ex(val, dflt, XLAT_STYLE_FMT_D, x, NULL);
1632 }
1633
1634 static inline int
1635 printxval_d(const struct xlat *x, const int val, const char *dflt)
1636 {
1637 return printxvals_ex(val, dflt, XLAT_STYLE_FMT_D, x, NULL);
1638 }
1639
1640 static inline void
1641 tprint_iov(struct tcb *tcp, kernel_ulong_t len, kernel_ulong_t addr,
1642 print_obj_by_addr_size_fn print_func)
1643 {
1644 tprint_iov_upto(tcp, len, addr, -1, print_func, NULL);
1645 }
1646
1647 # if HAVE_ARCH_TIME32_SYSCALLS || HAVE_ARCH_TIMESPEC32
1648 extern bool print_timespec32_data_size(const void *arg, size_t size);
1649 extern bool print_timespec32_array_data_size(const void *arg,
1650 unsigned int nmemb,
1651 size_t size);
1652 # endif /* HAVE_ARCH_TIME32_SYSCALLS || HAVE_ARCH_TIMESPEC32 */
1653 # if HAVE_ARCH_TIME32_SYSCALLS
1654 extern int print_timespec32(struct tcb *, kernel_ulong_t);
1655 extern const char *sprint_timespec32(struct tcb *, kernel_ulong_t);
1656 extern int print_timespec32_utime_pair(struct tcb *, kernel_ulong_t);
1657 extern int print_itimerspec32(struct tcb *, kernel_ulong_t);
1658 extern int print_timex32(struct tcb *, kernel_ulong_t);
1659 # endif /* HAVE_ARCH_TIME32_SYSCALLS */
1660
1661 extern bool print_timespec64_data_size(const void *arg, size_t size);
1662 extern bool print_timespec64_array_data_size(const void *arg,
1663 unsigned int nmemb,
1664 size_t size);
1665 extern int print_timespec64(struct tcb *, kernel_ulong_t);
1666 extern const char *sprint_timespec64(struct tcb *, kernel_ulong_t);
1667 extern int print_timespec64_utime_pair(struct tcb *, kernel_ulong_t);
1668 extern int print_itimerspec64(struct tcb *, kernel_ulong_t);
1669
1670 extern bool print_timeval64_data_size(const void *arg, size_t size);
1671
1672 extern int print_timex64(struct tcb *, kernel_ulong_t);
1673
1674 # ifdef SPARC64
1675 extern int print_sparc64_timex(struct tcb *, kernel_ulong_t);
1676 # endif
1677
1678 # ifdef ALPHA
1679 typedef struct {
1680 int tv_sec, tv_usec;
1681 } timeval32_t;
1682
1683 extern void print_timeval32_t(const timeval32_t *);
1684 extern void printrusage32(struct tcb *, kernel_ulong_t);
1685 extern const char *sprint_timeval32(struct tcb *, kernel_ulong_t addr);
1686 extern int print_timeval32(struct tcb *, kernel_ulong_t addr);
1687 extern int print_timeval32_utimes(struct tcb *, kernel_ulong_t addr);
1688 extern int print_itimerval32(struct tcb *, kernel_ulong_t addr);
1689 # endif
1690
1691 # ifdef HAVE_STRUCT_USER_DESC
1692 /**
1693 * Filter what to print from the point of view of the get_thread_area syscall.
1694 * Kernel copies only entry_number field at first and then tries to write the
1695 * whole structure.
1696 */
1697 enum user_desc_print_filter {
1698 /* Print the "entering" part of struct user_desc - entry_number. */
1699 USER_DESC_ENTERING = 1,
1700 /* Print the "exiting" part of the structure. */
1701 USER_DESC_EXITING = 2,
1702 USER_DESC_BOTH = USER_DESC_ENTERING | USER_DESC_EXITING,
1703 };
1704
1705 extern void print_user_desc(struct tcb *, kernel_ulong_t addr,
1706 enum user_desc_print_filter filter);
1707 # endif
1708
1709 /* Strace log generation machinery.
1710 *
1711 * printing_tcp: tcb which has incomplete line being printed right now.
1712 * NULL if last line has been completed ('\n'-terminated).
1713 * printleader(tcp) examines it, finishes incomplete line if needed,
1714 * the sets it to tcp.
1715 * line_ended() clears printing_tcp and resets ->curcol = 0.
1716 * tcp->curcol == 0 check is also used to detect completeness
1717 * of last line, since in -ff mode just checking printing_tcp for NULL
1718 * is not enough.
1719 *
1720 * If you change this code, test log generation in both -f and -ff modes
1721 * using:
1722 * strace -oLOG -f[f] test/threaded_execve
1723 * strace -oLOG -f[f] test/sigkill_rain
1724 * strace -oLOG -f[f] -p "`pidof web_browser`"
1725 */
1726 extern struct tcb *printing_tcp;
1727 extern void printleader(struct tcb *);
1728 extern void line_ended(void);
1729 extern void tabto(void);
1730 extern void tprintf_string(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
1731 extern void tprints_string(const char *str);
1732 extern void tprintf_comment(const char *fmt, ...) ATTRIBUTE_FORMAT((printf, 1, 2));
1733 extern void tprints_comment(const char *str);
1734
1735 /*
1736 * Staging output for status qualifier.
1737 */
1738 extern FILE *strace_open_memstream(struct tcb *tcp);
1739 extern void strace_close_memstream(struct tcb *tcp, bool publish);
1740
1741 static inline void
1742 printaddr_comment(const kernel_ulong_t addr)
1743 {
1744 tprintf_comment("%#llx", (unsigned long long) addr);
1745 }
1746
1747 # if SIZEOF_KERNEL_LONG_T > 4 \
1748 && (SIZEOF_KERNEL_LONG_T > SIZEOF_LONG || !defined(current_wordsize))
1749 # define ANY_WORDSIZE_LESS_THAN_KERNEL_LONG 1
1750 # else
1751 # define ANY_WORDSIZE_LESS_THAN_KERNEL_LONG 0
1752 # endif
1753
1754 # if SIZEOF_KERNEL_LONG_T == SIZEOF_LONG || !defined(current_wordsize)
1755 # define ANY_WORDSIZE_EQUALS_TO_KERNEL_LONG 1
1756 # else
1757 # define ANY_WORDSIZE_EQUALS_TO_KERNEL_LONG 0
1758 # endif
1759
1760 # define DECL_PRINTNUM(name) \
1761 extern bool \
1762 printnum_ ## name(struct tcb *, kernel_ulong_t addr, const char *fmt) \
1763 ATTRIBUTE_FORMAT((printf, 3, 0)) \
1764 /* End of DECL_PRINTNUM definition. */
1765
1766 DECL_PRINTNUM(short);
1767 DECL_PRINTNUM(int);
1768 DECL_PRINTNUM(int64);
1769 # undef DECL_PRINTNUM
1770
1771 # define DECL_PRINTNUM_ADDR(name) \
1772 extern bool \
1773 printnum_addr_ ## name(struct tcb *, kernel_ulong_t addr) \
1774 /* End of DECL_PRINTNUM_ADDR definition. */
1775
1776 DECL_PRINTNUM_ADDR(int);
1777 DECL_PRINTNUM_ADDR(int64);
1778 # undef DECL_PRINTNUM_ADDR
1779
1780 extern bool
1781 printnum_fd(struct tcb *, kernel_ulong_t addr);
1782
1783 extern bool
1784 printnum_pid(struct tcb *const tcp, const kernel_ulong_t addr, enum pid_type type);
1785
1786 static inline bool
1787 printnum_slong(struct tcb *tcp, kernel_ulong_t addr)
1788 {
1789 return dispatch_wordsize(printnum_int64, printnum_int,
1790 tcp, addr, opt_wordsize("%" PRId64, "%d"));
1791 }
1792
1793 static inline bool
1794 printnum_ulong(struct tcb *tcp, kernel_ulong_t addr)
1795 {
1796 return dispatch_wordsize(printnum_int64, printnum_int,
1797 tcp, addr, opt_wordsize("%" PRIu64, "%u"));
1798 }
1799
1800 static inline bool
1801 printnum_ptr(struct tcb *tcp, kernel_ulong_t addr)
1802 {
1803 return dispatch_wordsize(printnum_addr_int64, printnum_addr_int,
1804 tcp, addr);
1805 }
1806
1807 static inline bool
1808 printnum_kptr(struct tcb *tcp, kernel_ulong_t addr)
1809 {
1810 return dispatch_klongsize(printnum_addr_int64, printnum_addr_int,
1811 tcp, addr);
1812 }
1813
1814
1815 # define DECL_PRINTPAIR(name) \
1816 extern bool \
1817 printpair_ ## name(struct tcb *, kernel_ulong_t addr, const char *fmt) \
1818 ATTRIBUTE_FORMAT((printf, 3, 0)) \
1819 /* End of DECL_PRINTPAIR definition. */
1820
1821 DECL_PRINTPAIR(int);
1822 DECL_PRINTPAIR(int64);
1823 # undef DECL_PRINTPAIR
1824
1825 static inline kernel_long_t
1826 truncate_klong_to_current_wordsize(const kernel_long_t v)
1827 {
1828 # if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
1829 if (current_wordsize < sizeof(v)) {
1830 return (int) v;
1831 } else
1832 # endif
1833 {
1834 return v;
1835 }
1836 }
1837
1838 static inline kernel_ulong_t
1839 truncate_kulong_to_current_wordsize(const kernel_ulong_t v)
1840 {
1841 # if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
1842 if (current_wordsize < sizeof(v)) {
1843 return (unsigned int) v;
1844 } else
1845 # endif
1846 {
1847 return v;
1848 }
1849 }
1850
1851 static inline kernel_long_t
1852 truncate_klong_to_current_klongsize(const kernel_long_t v)
1853 {
1854 # if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
1855 if (current_klongsize < sizeof(v)) {
1856 return (int) v;
1857 } else
1858 # endif
1859 {
1860 return v;
1861 }
1862 }
1863
1864 static inline kernel_ulong_t
1865 truncate_kulong_to_current_klongsize(const kernel_ulong_t v)
1866 {
1867 # if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
1868 if (current_klongsize < sizeof(v)) {
1869 return (unsigned int) v;
1870 } else
1871 # endif
1872 {
1873 return v;
1874 }
1875 }
1876
1877 /*
1878 * Cast a pointer or a pointer-sized integer to kernel_ulong_t.
1879 */
1880 # define ptr_to_kulong(v) ((kernel_ulong_t) (unsigned long) (v))
1881
1882 /*
1883 * Zero-extend a signed integer type to unsigned long long.
1884 */
1885 # define zero_extend_signed_to_ull(v) \
1886 (sizeof(v) == sizeof(char) ? (unsigned long long) (unsigned char) (v) : \
1887 sizeof(v) == sizeof(short) ? (unsigned long long) (unsigned short) (v) : \
1888 sizeof(v) == sizeof(int) ? (unsigned long long) (unsigned int) (v) : \
1889 sizeof(v) == sizeof(long) ? (unsigned long long) (unsigned long) (v) : \
1890 (unsigned long long) (v))
1891
1892 /*
1893 * Sign-extend an unsigned integer type to long long.
1894 */
1895 # define sign_extend_unsigned_to_ll(v) \
1896 (sizeof(v) == sizeof(char) ? (long long) (char) (v) : \
1897 sizeof(v) == sizeof(short) ? (long long) (short) (v) : \
1898 sizeof(v) == sizeof(int) ? (long long) (int) (v) : \
1899 sizeof(v) == sizeof(long) ? (long long) (long) (v) : \
1900 (long long) (v))
1901
1902 /*
1903 * Computes the popcount of a vector of 32-bit values.
1904 */
1905 static inline unsigned int
1906 popcount32(const uint32_t *a, unsigned int size)
1907 {
1908 unsigned int count = 0;
1909
1910 for (; size; ++a, --size) {
1911 uint32_t x = *a;
1912
1913 # ifdef HAVE___BUILTIN_POPCOUNT
1914 count += __builtin_popcount(x);
1915 # else
1916 for (; x; ++count)
1917 x &= x - 1;
1918 # endif
1919 }
1920
1921 return count;
1922 }
1923
1924 extern const char *const errnoent[];
1925 extern const char *const signalent[];
1926 extern const unsigned int nerrnos;
1927 extern const unsigned int nsignals;
1928
1929 extern const struct_sysent sysent0[];
1930 extern const struct_ioctlent ioctlent0[];
1931
1932 extern const char *const personality_names[];
1933
1934 # if SUPPORTED_PERSONALITIES > 1
1935 extern const struct_sysent *sysent;
1936 extern const struct_ioctlent *ioctlent;
1937 # else
1938 # define sysent sysent0
1939 # define ioctlent ioctlent0
1940 # endif
1941
1942 extern unsigned nsyscalls;
1943 extern unsigned nioctlents;
1944
1945 extern const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES];
1946 extern const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES];
1947 extern struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
1948
1949 struct audit_arch_t {
1950 unsigned int arch; /* AUDIT_ARCH_* */
1951 unsigned int flag;
1952 };
1953
1954 extern const struct audit_arch_t audit_arch_vec[SUPPORTED_PERSONALITIES];
1955
1956 # ifdef ENABLE_COVERAGE_GCOV
1957 # ifdef HAVE_GCOV_H
1958 # include <gcov.h>
1959 # else
1960 extern void __gcov_dump(void);
1961 # endif
1962 # define GCOV_DUMP __gcov_dump()
1963 # else
1964 # define GCOV_DUMP
1965 # endif
1966
1967 # ifdef IN_MPERS_BOOTSTRAP
1968 /* Transform multi-line MPERS_PRINTER_DECL statements to one-liners. */
1969 # define MPERS_PRINTER_DECL(type, name, ...) MPERS_PRINTER_DECL(type, name, __VA_ARGS__)
1970 # else /* !IN_MPERS_BOOTSTRAP */
1971 # if SUPPORTED_PERSONALITIES > 1
1972 # include "printers.h"
1973 # else
1974 # include "native_printer_decls.h"
1975 # endif
1976 # define MPERS_PRINTER_DECL(type, name, ...) type MPERS_FUNC_NAME(name)(__VA_ARGS__)
1977 # endif /* !IN_MPERS_BOOTSTRAP */
1978
1979 # ifdef IN_MPERS
1980 # define MPERS_PTR_ARG(arg_) void *
1981 # else
1982 # define MPERS_PTR_ARG(arg_) arg_
1983 # endif
1984
1985 /* Checks that sysent[scno] is not out of range. */
1986 static inline bool
1987 scno_in_range(kernel_ulong_t scno)
1988 {
1989 return scno < nsyscalls;
1990 }
1991
1992 /* Like scno_in_range but for the specified personality. */
1993 static inline bool
1994 scno_pers_in_range(kernel_ulong_t scno, unsigned int pers)
1995 {
1996 return scno < nsyscall_vec[pers];
1997 }
1998
1999 /*
2000 * Checks whether scno is not out of range,
2001 * its corresponding sysent[scno].sys_func is non-NULL,
2002 * and its sysent[scno].sys_flags has no TRACE_INDIRECT_SUBCALL flag set.
2003 */
2004 static inline bool
2005 scno_is_valid(kernel_ulong_t scno)
2006 {
2007 return scno_in_range(scno)
2008 && sysent[scno].sys_func
2009 && !(sysent[scno].sys_flags & TRACE_INDIRECT_SUBCALL);
2010 }
2011
2012 /* Like scno_is_valid but for the specified personality. */
2013 static inline bool
2014 scno_pers_is_valid(kernel_ulong_t scno, unsigned int pers)
2015 {
2016 return scno_pers_in_range(scno, pers)
2017 && sysent_vec[pers][scno].sys_func
2018 && !(sysent_vec[pers][scno].sys_flags & TRACE_INDIRECT_SUBCALL);
2019 }
2020
2021 # define MPERS_FUNC_NAME__(prefix, name) prefix ## name
2022 # define MPERS_FUNC_NAME_(prefix, name) MPERS_FUNC_NAME__(prefix, name)
2023 # define MPERS_FUNC_NAME(name) MPERS_FUNC_NAME_(MPERS_PREFIX, name)
2024
2025 # define SYS_FUNC_NAME(syscall_name) MPERS_FUNC_NAME(syscall_name)
2026
2027 # define SYS_FUNC(syscall_name) int SYS_FUNC_NAME(sys_ ## syscall_name)(struct tcb *tcp)
2028
2029 # define ILOG2_ITER_(val_, ret_, bit_) \
2030 do { \
2031 typeof(ret_) shift_ = \
2032 ((val_) > ((((typeof(val_)) 1) \
2033 << (1 << (bit_))) - 1)) << (bit_); \
2034 (val_) >>= shift_; \
2035 (ret_) |= shift_; \
2036 } while (0)
2037
2038 /**
2039 * Calculate floor(log2(val)), with the exception of val == 0, for which 0
2040 * is returned as well.
2041 *
2042 * @param val 64-bit value to calculate integer base-2 logarithm for.
2043 * @return (unsigned int) floor(log2(val)) if val > 0, 0 if val == 0.
2044 */
2045 static inline unsigned int
2046 ilog2_64(uint64_t val)
2047 {
2048 unsigned int ret = 0;
2049
2050 ILOG2_ITER_(val, ret, 5);
2051 ILOG2_ITER_(val, ret, 4);
2052 ILOG2_ITER_(val, ret, 3);
2053 ILOG2_ITER_(val, ret, 2);
2054 ILOG2_ITER_(val, ret, 1);
2055 ILOG2_ITER_(val, ret, 0);
2056
2057 return ret;
2058 }
2059
2060 /**
2061 * Calculate floor(log2(val)), with the exception of val == 0, for which 0
2062 * is returned as well.
2063 *
2064 * @param val 32-bit value to calculate integer base-2 logarithm for.
2065 * @return (unsigned int) floor(log2(val)) if val > 0, 0 if val == 0.
2066 */
2067 static inline unsigned int
2068 ilog2_32(uint32_t val)
2069 {
2070 unsigned int ret = 0;
2071
2072 ILOG2_ITER_(val, ret, 4);
2073 ILOG2_ITER_(val, ret, 3);
2074 ILOG2_ITER_(val, ret, 2);
2075 ILOG2_ITER_(val, ret, 1);
2076 ILOG2_ITER_(val, ret, 0);
2077
2078 return ret;
2079 }
2080
2081 # if SIZEOF_KERNEL_LONG_T > 4
2082 # define ilog2_klong ilog2_64
2083 # else
2084 # define ilog2_klong ilog2_32
2085 # endif
2086
2087 # undef ILOG2_ITER_
2088
2089 # include "print_fields.h"
2090
2091 static inline int
2092 printflags64(const struct xlat *x, uint64_t flags, const char *dflt)
2093 {
2094 tprint_flags_begin();
2095 int r = printflags64_in(x, flags, dflt);
2096 tprint_flags_end();
2097 return r;
2098 }
2099
2100 static inline int
2101 printflags(const struct xlat *x, unsigned int flags, const char *dflt)
2102 {
2103 return printflags64(x, flags, dflt);
2104 }
2105
2106 /*
2107 * When u64 is interpreted by the kernel as an address, there is a difference
2108 * in behaviour between 32-bit and 64-bit kernel in the way u64_to_user_ptr
2109 * works (32-bit kernel trims higher bits during conversion which may result
2110 * to a valid address). Since 32-bit strace cannot figure out what kind of
2111 * kernel the tracee is running on, it has to account for both possibilities.
2112 */
2113 # if CAN_ARCH_BE_COMPAT_ON_64BIT_KERNEL
2114
2115 /**
2116 * Print raw 64-bit value as an address if it's too big to fit in strace's
2117 * kernel_long_t.
2118 */
2119 static inline void
2120 print_big_u64_addr(const uint64_t addr)
2121 {
2122 if (sizeof(kernel_long_t) < sizeof(addr) && addr > max_kaddr()) {
2123 printaddr64(addr);
2124 tprint_alternative_value();
2125 }
2126 }
2127 # else /* !CAN_ARCH_BE_COMPAT_ON_64BIT_KERNEL */
2128 # define print_big_u64_addr(addr_) ((void) 0)
2129 # endif /* CAN_ARCH_BE_COMPAT_ON_64BIT_KERNEL */
2130
2131 # ifndef IN_MPERS_BOOTSTRAP
2132 # include "syscall.h"
2133 # endif
2134
2135 #endif /* !STRACE_DEFS_H */