Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

defs.h 45KB

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