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 48KB

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