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.

syscall.c 35KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  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) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
  6. * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
  7. * Linux for s390 port by D.J. Barrow
  8. * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
  9. * Copyright (c) 1999-2020 The strace developers.
  10. * All rights reserved.
  11. *
  12. * SPDX-License-Identifier: LGPL-2.1-or-later
  13. */
  14. #include "defs.h"
  15. #include "get_personality.h"
  16. #include "mmap_notify.h"
  17. #include "native_defs.h"
  18. #include "ptrace.h"
  19. #include "ptrace_syscall_info.h"
  20. #include "nsig.h"
  21. #include "number_set.h"
  22. #include "delay.h"
  23. #include "retval.h"
  24. #include <limits.h>
  25. /* for struct iovec */
  26. #include <sys/uio.h>
  27. /* for __X32_SYSCALL_BIT */
  28. #include "scno.h"
  29. #include "regs.h"
  30. #if defined(SPARC64)
  31. # undef PTRACE_GETREGS
  32. # define PTRACE_GETREGS PTRACE_GETREGS64
  33. # undef PTRACE_SETREGS
  34. # define PTRACE_SETREGS PTRACE_SETREGS64
  35. #endif
  36. #include "syscall.h"
  37. #include "xstring.h"
  38. #include "syscallent_base_nr.h"
  39. /* Define these shorthand notations to simplify the syscallent files. */
  40. #include "sysent_shorthand_defs.h"
  41. #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
  42. const struct_sysent sysent0[] = {
  43. #include "syscallent.h"
  44. };
  45. #if SUPPORTED_PERSONALITIES > 1
  46. # include PERSONALITY1_INCLUDE_FUNCS
  47. static const struct_sysent sysent1[] = {
  48. # include "syscallent1.h"
  49. };
  50. #endif
  51. #if SUPPORTED_PERSONALITIES > 2
  52. # include PERSONALITY2_INCLUDE_FUNCS
  53. static const struct_sysent sysent2[] = {
  54. # include "syscallent2.h"
  55. };
  56. #endif
  57. /* Now undef them since short defines cause wicked namespace pollution. */
  58. #include "sysent_shorthand_undefs.h"
  59. const char *const errnoent[] = {
  60. #include "errnoent.h"
  61. };
  62. const char *const signalent[] = {
  63. #include "signalent.h"
  64. };
  65. /*
  66. * `ioctlent[012].h' files are automatically generated by the auxiliary
  67. * program `ioctlsort', such that the list is sorted by the `code' field.
  68. * This has the side-effect of resolving the _IO.. macros into
  69. * plain integers, eliminating the need to include here everything
  70. * in "/usr/include".
  71. */
  72. const struct_ioctlent ioctlent0[] = {
  73. #include "ioctlent0.h"
  74. };
  75. #if SUPPORTED_PERSONALITIES > 1
  76. static const struct_ioctlent ioctlent1[] = {
  77. # include "ioctlent1.h"
  78. };
  79. # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
  80. static const struct_printers printers0 = {
  81. # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
  82. };
  83. # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
  84. static const struct_printers printers1 = {
  85. # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
  86. };
  87. #endif
  88. #if SUPPORTED_PERSONALITIES > 2
  89. static const struct_ioctlent ioctlent2[] = {
  90. # include "ioctlent2.h"
  91. };
  92. # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
  93. static const struct_printers printers2 = {
  94. # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
  95. };
  96. #endif
  97. enum {
  98. nsyscalls0 = ARRAY_SIZE(sysent0)
  99. #if SUPPORTED_PERSONALITIES > 1
  100. , nsyscalls1 = ARRAY_SIZE(sysent1)
  101. # if SUPPORTED_PERSONALITIES > 2
  102. , nsyscalls2 = ARRAY_SIZE(sysent2)
  103. # endif
  104. #endif
  105. };
  106. enum {
  107. nioctlents0 = ARRAY_SIZE(ioctlent0)
  108. #if SUPPORTED_PERSONALITIES > 1
  109. , nioctlents1 = ARRAY_SIZE(ioctlent1)
  110. # if SUPPORTED_PERSONALITIES > 2
  111. , nioctlents2 = ARRAY_SIZE(ioctlent2)
  112. # endif
  113. #endif
  114. };
  115. #if SUPPORTED_PERSONALITIES > 1
  116. const struct_sysent *sysent = sysent0;
  117. const struct_ioctlent *ioctlent = ioctlent0;
  118. const struct_printers *printers = &printers0;
  119. #endif
  120. const unsigned int nerrnos = ARRAY_SIZE(errnoent);
  121. const unsigned int nsignals = ARRAY_SIZE(signalent);
  122. unsigned nsyscalls = nsyscalls0;
  123. unsigned nioctlents = nioctlents0;
  124. const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES] = {
  125. nsyscalls0,
  126. #if SUPPORTED_PERSONALITIES > 1
  127. nsyscalls1,
  128. #endif
  129. #if SUPPORTED_PERSONALITIES > 2
  130. nsyscalls2,
  131. #endif
  132. };
  133. const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
  134. sysent0,
  135. #if SUPPORTED_PERSONALITIES > 1
  136. sysent1,
  137. #endif
  138. #if SUPPORTED_PERSONALITIES > 2
  139. sysent2,
  140. #endif
  141. };
  142. const char *const personality_names[] =
  143. #if defined X86_64
  144. {"64 bit", "32 bit", "x32"}
  145. #elif defined X32
  146. {"x32", "32 bit"}
  147. #elif SUPPORTED_PERSONALITIES == 2
  148. {"64 bit", "32 bit"}
  149. #else
  150. {STRINGIFY_VAL(__WORDSIZE) " bit"}
  151. #endif
  152. ;
  153. const char *const personality_designators[] =
  154. #if defined X86_64
  155. { "64", "32", "x32" }
  156. #elif defined X32
  157. { "x32", "32" }
  158. #elif SUPPORTED_PERSONALITIES == 2
  159. { "64", "32" }
  160. #else
  161. { STRINGIFY_VAL(__WORDSIZE) }
  162. #endif
  163. ;
  164. #if SUPPORTED_PERSONALITIES > 1
  165. unsigned current_personality;
  166. # ifndef current_wordsize
  167. unsigned current_wordsize = PERSONALITY0_WORDSIZE;
  168. static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
  169. PERSONALITY0_WORDSIZE,
  170. PERSONALITY1_WORDSIZE,
  171. # if SUPPORTED_PERSONALITIES > 2
  172. PERSONALITY2_WORDSIZE,
  173. # endif
  174. };
  175. # endif
  176. # ifndef current_klongsize
  177. unsigned current_klongsize = PERSONALITY0_KLONGSIZE;
  178. static const int personality_klongsize[SUPPORTED_PERSONALITIES] = {
  179. PERSONALITY0_KLONGSIZE,
  180. PERSONALITY1_KLONGSIZE,
  181. # if SUPPORTED_PERSONALITIES > 2
  182. PERSONALITY2_KLONGSIZE,
  183. # endif
  184. };
  185. # endif
  186. void
  187. set_personality(unsigned int personality)
  188. {
  189. if (personality == current_personality)
  190. return;
  191. if (personality >= SUPPORTED_PERSONALITIES)
  192. error_msg_and_die("Requested switch to unsupported personality "
  193. "%u", personality);
  194. nsyscalls = nsyscall_vec[personality];
  195. sysent = sysent_vec[personality];
  196. switch (personality) {
  197. case 0:
  198. ioctlent = ioctlent0;
  199. nioctlents = nioctlents0;
  200. printers = &printers0;
  201. break;
  202. case 1:
  203. ioctlent = ioctlent1;
  204. nioctlents = nioctlents1;
  205. printers = &printers1;
  206. break;
  207. # if SUPPORTED_PERSONALITIES > 2
  208. case 2:
  209. ioctlent = ioctlent2;
  210. nioctlents = nioctlents2;
  211. printers = &printers2;
  212. break;
  213. # endif
  214. }
  215. current_personality = personality;
  216. # ifndef current_wordsize
  217. current_wordsize = personality_wordsize[personality];
  218. # endif
  219. # ifndef current_klongsize
  220. current_klongsize = personality_klongsize[personality];
  221. # endif
  222. }
  223. static void
  224. update_personality(struct tcb *tcp, unsigned int personality)
  225. {
  226. static bool need_mpers_warning[] =
  227. { false, !HAVE_PERSONALITY_1_MPERS, !HAVE_PERSONALITY_2_MPERS };
  228. set_personality(personality);
  229. if (personality == tcp->currpers)
  230. return;
  231. tcp->currpers = personality;
  232. if (!is_number_in_set(QUIET_PERSONALITY, quiet_set)) {
  233. printleader(tcp);
  234. tprintf("[ Process PID=%d runs in %s mode. ]\n",
  235. tcp->pid, personality_names[personality]);
  236. line_ended();
  237. }
  238. if (need_mpers_warning[personality]) {
  239. error_msg("WARNING: Proper structure decoding for this "
  240. "personality is not supported, please consider "
  241. "building strace with mpers support enabled.");
  242. need_mpers_warning[personality] = false;
  243. }
  244. }
  245. #endif
  246. #ifdef SYS_socket_subcall
  247. static void
  248. decode_socket_subcall(struct tcb *tcp)
  249. {
  250. const int call = tcp->u_arg[0];
  251. if (call < 1 || call >= SYS_socket_nsubcalls)
  252. return;
  253. const kernel_ulong_t scno = SYS_socket_subcall + call;
  254. const unsigned int nargs = sysent[scno].nargs;
  255. uint64_t buf[nargs];
  256. if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
  257. return;
  258. tcp->scno = scno;
  259. tcp->qual_flg = qual_flags(scno);
  260. tcp->s_ent = &sysent[scno];
  261. unsigned int i;
  262. for (i = 0; i < nargs; ++i)
  263. tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
  264. ? ((uint32_t *) (void *) buf)[i] : buf[i];
  265. }
  266. #endif /* SYS_socket_subcall */
  267. #ifdef SYS_ipc_subcall
  268. static void
  269. decode_ipc_subcall(struct tcb *tcp)
  270. {
  271. unsigned int call = tcp->u_arg[0];
  272. const unsigned int version = call >> 16;
  273. if (version) {
  274. # if defined S390 || defined S390X
  275. return;
  276. # else
  277. # ifdef SPARC64
  278. if (current_wordsize == 8)
  279. return;
  280. # endif
  281. set_tcb_priv_ulong(tcp, version);
  282. call &= 0xffff;
  283. # endif
  284. }
  285. switch (call) {
  286. case 1: case 2: case 3: case 4:
  287. case 11: case 12: case 13: case 14:
  288. case 21: case 22: case 23: case 24:
  289. break;
  290. default:
  291. return;
  292. }
  293. tcp->scno = SYS_ipc_subcall + call;
  294. tcp->qual_flg = qual_flags(tcp->scno);
  295. tcp->s_ent = &sysent[tcp->scno];
  296. const unsigned int n = n_args(tcp);
  297. unsigned int i;
  298. for (i = 0; i < n; i++)
  299. tcp->u_arg[i] = tcp->u_arg[i + 1];
  300. }
  301. #endif /* SYS_ipc_subcall */
  302. #ifdef SYS_syscall_subcall
  303. /* The implementation is architecture specific. */
  304. static void decode_syscall_subcall(struct tcb *);
  305. #endif /* SYS_syscall_subcall */
  306. static void
  307. dumpio(struct tcb *tcp)
  308. {
  309. int fd = tcp->u_arg[0];
  310. if (fd < 0)
  311. return;
  312. if (is_number_in_set(fd, write_set)) {
  313. switch (tcp_sysent(tcp)->sen) {
  314. case SEN_write:
  315. case SEN_pwrite:
  316. case SEN_send:
  317. case SEN_sendto:
  318. case SEN_mq_timedsend_time32:
  319. case SEN_mq_timedsend_time64:
  320. dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
  321. break;
  322. case SEN_writev:
  323. case SEN_pwritev:
  324. case SEN_pwritev2:
  325. case SEN_vmsplice:
  326. dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1);
  327. break;
  328. case SEN_sendmsg:
  329. dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1);
  330. break;
  331. case SEN_sendmmsg:
  332. dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
  333. break;
  334. }
  335. }
  336. if (syserror(tcp))
  337. return;
  338. if (is_number_in_set(fd, read_set)) {
  339. switch (tcp_sysent(tcp)->sen) {
  340. case SEN_read:
  341. case SEN_pread:
  342. case SEN_recv:
  343. case SEN_recvfrom:
  344. case SEN_mq_timedreceive_time32:
  345. case SEN_mq_timedreceive_time64:
  346. dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
  347. return;
  348. case SEN_readv:
  349. case SEN_preadv:
  350. case SEN_preadv2:
  351. dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
  352. tcp->u_rval);
  353. return;
  354. case SEN_recvmsg:
  355. dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
  356. return;
  357. case SEN_recvmmsg:
  358. case SEN_recvmmsg_time32:
  359. case SEN_recvmmsg_time64:
  360. dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
  361. return;
  362. }
  363. }
  364. }
  365. static const char *
  366. err_name(uint64_t err)
  367. {
  368. return err < nerrnos ? errnoent[err] : NULL;
  369. }
  370. void
  371. print_err(int64_t err, bool negated)
  372. {
  373. const char *str = err_name(negated ? -err : err);
  374. if (!str || xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
  375. tprintf(negated ? "%" PRId64 : "%" PRIu64, err);
  376. if (!str || xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
  377. return;
  378. (xlat_verbose(xlat_verbosity) == XLAT_STYLE_ABBREV
  379. ? tprintf : tprintf_comment)("%s%s",
  380. negated ? "-" : "", str);
  381. }
  382. static void
  383. print_err_ret(kernel_ulong_t ret, unsigned long u_error)
  384. {
  385. const char *u_error_str = err_name(u_error);
  386. if (u_error_str)
  387. tprintf("= %" PRI_kld " %s (%s)",
  388. ret, u_error_str, strerror(u_error));
  389. else
  390. tprintf("= %" PRI_kld " (errno %lu)", ret, u_error);
  391. }
  392. static long get_regs(struct tcb *);
  393. static int get_syscall_args(struct tcb *);
  394. static int get_syscall_result(struct tcb *);
  395. static void get_error(struct tcb *, bool);
  396. static void set_error(struct tcb *, unsigned long);
  397. static void set_success(struct tcb *, kernel_long_t);
  398. static int arch_get_scno(struct tcb *);
  399. static int arch_check_scno(struct tcb *);
  400. static int arch_set_scno(struct tcb *, kernel_ulong_t);
  401. static int arch_get_syscall_args(struct tcb *);
  402. static void arch_get_error(struct tcb *, bool);
  403. static int arch_set_error(struct tcb *);
  404. static int arch_set_success(struct tcb *);
  405. #if MAX_ARGS > 6
  406. static void arch_get_syscall_args_extra(struct tcb *, unsigned int);
  407. #endif
  408. struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
  409. static struct inject_opts *
  410. tcb_inject_opts(struct tcb *tcp)
  411. {
  412. return (scno_in_range(tcp->scno) && tcp->inject_vec[current_personality])
  413. ? &tcp->inject_vec[current_personality][tcp->scno] : NULL;
  414. }
  415. static long
  416. tamper_with_syscall_entering(struct tcb *tcp, unsigned int *signo)
  417. {
  418. if (!tcp->inject_vec[current_personality]) {
  419. tcp->inject_vec[current_personality] =
  420. xcalloc(nsyscalls, sizeof(**inject_vec));
  421. memcpy(tcp->inject_vec[current_personality],
  422. inject_vec[current_personality],
  423. nsyscalls * sizeof(**inject_vec));
  424. }
  425. struct inject_opts *opts = tcb_inject_opts(tcp);
  426. if (!opts || opts->first == 0)
  427. return 0;
  428. --opts->first;
  429. if (opts->first != 0)
  430. return 0;
  431. opts->first = opts->step;
  432. if (!recovering(tcp)) {
  433. if (opts->data.flags & INJECT_F_SIGNAL)
  434. *signo = opts->data.signo;
  435. if (opts->data.flags & (INJECT_F_ERROR | INJECT_F_RETVAL)) {
  436. kernel_long_t scno =
  437. (opts->data.flags & INJECT_F_SYSCALL)
  438. ? (kernel_long_t) shuffle_scno(opts->data.scno)
  439. : -1;
  440. if (!arch_set_scno(tcp, scno)) {
  441. tcp->flags |= TCB_TAMPERED;
  442. if (scno != -1)
  443. tcp->flags |= TCB_TAMPERED_NO_FAIL;
  444. #if ARCH_NEEDS_SET_ERROR_FOR_SCNO_TAMPERING
  445. /*
  446. * So far it's just a workaround for hppa,
  447. * but let's pretend it could be used elsewhere.
  448. */
  449. else {
  450. kernel_long_t rval =
  451. (opts->data.flags & INJECT_F_RETVAL) ?
  452. ENOSYS : retval_get(opts->data.rval_idx);
  453. tcp->u_error = 0; /* force reset */
  454. set_error(tcp, rval);
  455. }
  456. #endif
  457. }
  458. }
  459. if (opts->data.flags & INJECT_F_DELAY_ENTER)
  460. delay_tcb(tcp, opts->data.delay_idx, true);
  461. if (opts->data.flags & INJECT_F_DELAY_EXIT)
  462. tcp->flags |= TCB_INJECT_DELAY_EXIT;
  463. }
  464. return 0;
  465. }
  466. static long
  467. tamper_with_syscall_exiting(struct tcb *tcp)
  468. {
  469. struct inject_opts *opts = tcb_inject_opts(tcp);
  470. if (!opts)
  471. return 0;
  472. if (inject_delay_exit(tcp))
  473. delay_tcb(tcp, opts->data.delay_idx, false);
  474. if (!syscall_tampered(tcp))
  475. return 0;
  476. if (!syserror(tcp) ^ !!syscall_tampered_nofail(tcp)) {
  477. error_msg("Failed to tamper with process %d: unexpectedly got"
  478. " %serror (return value %#" PRI_klx ", error %lu)",
  479. tcp->pid, syscall_tampered_nofail(tcp) ? "" : "no ",
  480. tcp->u_rval, tcp->u_error);
  481. return 1;
  482. }
  483. if (opts->data.flags & INJECT_F_RETVAL)
  484. set_success(tcp, retval_get(opts->data.rval_idx));
  485. else
  486. set_error(tcp, retval_get(opts->data.rval_idx));
  487. return 0;
  488. }
  489. /*
  490. * Returns:
  491. * 0: "ignore this ptrace stop", bail out silently.
  492. * 1: ok, decoded; call
  493. * syscall_entering_finish(tcp, syscall_entering_trace(tcp, ...)).
  494. * other: error; call syscall_entering_finish(tcp, res), where res is the value
  495. * returned.
  496. */
  497. int
  498. syscall_entering_decode(struct tcb *tcp)
  499. {
  500. int res = get_scno(tcp);
  501. if (res == 0)
  502. return res;
  503. if (res != 1 || (res = get_syscall_args(tcp)) != 1) {
  504. printleader(tcp);
  505. tprintf("%s(", tcp_sysent(tcp)->sys_name);
  506. /*
  507. * " <unavailable>" will be added later by the code which
  508. * detects ptrace errors.
  509. */
  510. return res;
  511. }
  512. #ifdef SYS_syscall_subcall
  513. if (tcp_sysent(tcp)->sen == SEN_syscall)
  514. decode_syscall_subcall(tcp);
  515. #endif
  516. #if defined SYS_ipc_subcall \
  517. || defined SYS_socket_subcall
  518. switch (tcp_sysent(tcp)->sen) {
  519. # ifdef SYS_ipc_subcall
  520. case SEN_ipc:
  521. decode_ipc_subcall(tcp);
  522. break;
  523. # endif
  524. # ifdef SYS_socket_subcall
  525. case SEN_socketcall:
  526. decode_socket_subcall(tcp);
  527. break;
  528. # endif
  529. }
  530. #endif
  531. return 1;
  532. }
  533. int
  534. syscall_entering_trace(struct tcb *tcp, unsigned int *sig)
  535. {
  536. if (hide_log(tcp)) {
  537. /*
  538. * Restrain from fault injection
  539. * while the tracee executes strace code.
  540. */
  541. tcp->qual_flg &= ~QUAL_INJECT;
  542. switch (tcp_sysent(tcp)->sen) {
  543. case SEN_execve:
  544. case SEN_execveat:
  545. case SEN_execv:
  546. /*
  547. * First exec* syscall makes the log visible.
  548. */
  549. tcp->flags &= ~TCB_HIDE_LOG;
  550. /*
  551. * Check whether this exec* syscall succeeds.
  552. */
  553. tcp->flags |= TCB_CHECK_EXEC_SYSCALL;
  554. break;
  555. }
  556. }
  557. if (hide_log(tcp) || !traced(tcp) || (tracing_paths && !pathtrace_match(tcp))) {
  558. tcp->flags |= TCB_FILTERED;
  559. return 0;
  560. }
  561. tcp->flags &= ~TCB_FILTERED;
  562. if (inject(tcp))
  563. tamper_with_syscall_entering(tcp, sig);
  564. if (cflag == CFLAG_ONLY_STATS) {
  565. return 0;
  566. }
  567. #ifdef ENABLE_STACKTRACE
  568. if (stack_trace_enabled &&
  569. !check_exec_syscall(tcp) &&
  570. tcp_sysent(tcp)->sys_flags & STACKTRACE_CAPTURE_ON_ENTER) {
  571. unwind_tcb_capture(tcp);
  572. }
  573. #endif
  574. if (!is_complete_set(status_set, NUMBER_OF_STATUSES))
  575. strace_open_memstream(tcp);
  576. printleader(tcp);
  577. tprintf("%s(", tcp_sysent(tcp)->sys_name);
  578. int res = raw(tcp) ? printargs(tcp) : tcp_sysent(tcp)->sys_func(tcp);
  579. fflush(tcp->outf);
  580. return res;
  581. }
  582. void
  583. syscall_entering_finish(struct tcb *tcp, int res)
  584. {
  585. tcp->flags |= TCB_INSYSCALL;
  586. tcp->sys_func_rval = res;
  587. /* Measure the entrance time as late as possible to avoid errors. */
  588. if ((Tflag || cflag) && !filtered(tcp))
  589. clock_gettime(CLOCK_MONOTONIC, &tcp->etime);
  590. /* Start tracking system time */
  591. if (cflag) {
  592. if (debug_flag) {
  593. struct timespec dt;
  594. ts_sub(&dt, &tcp->stime, &tcp->ltime);
  595. if (ts_nz(&dt))
  596. debug_func_msg("pid %d: %.9f seconds of system "
  597. "time spent since the last "
  598. "syscall exit",
  599. tcp->pid, ts_float(&dt));
  600. }
  601. tcp->ltime = tcp->stime;
  602. }
  603. }
  604. /* Returns:
  605. * 0: "bail out".
  606. * 1: ok.
  607. * -1: error in one of ptrace ops.
  608. *
  609. * If not 0, call syscall_exiting_trace(tcp, res), where res is the return
  610. * value. Anyway, call syscall_exiting_finish(tcp) then.
  611. */
  612. int
  613. syscall_exiting_decode(struct tcb *tcp, struct timespec *pts)
  614. {
  615. /* Measure the exit time as early as possible to avoid errors. */
  616. if ((Tflag || cflag) && !filtered(tcp))
  617. clock_gettime(CLOCK_MONOTONIC, pts);
  618. if (tcp_sysent(tcp)->sys_flags & MEMORY_MAPPING_CHANGE)
  619. mmap_notify_report(tcp);
  620. if (filtered(tcp))
  621. return 0;
  622. if (check_exec_syscall(tcp)) {
  623. /* The check failed, hide the log. */
  624. tcp->flags |= TCB_HIDE_LOG;
  625. }
  626. #if SUPPORTED_PERSONALITIES > 1
  627. update_personality(tcp, tcp->currpers);
  628. #endif
  629. return get_syscall_result(tcp);
  630. }
  631. void
  632. print_syscall_resume(struct tcb *tcp)
  633. {
  634. /* If not in -ff mode, and printing_tcp != tcp,
  635. * then the log currently does not end with output
  636. * of _our syscall entry_, but with something else.
  637. * We need to say which syscall's return is this.
  638. *
  639. * Forced reprinting via TCB_REPRINT is used only by
  640. * "strace -ff -oLOG test/threaded_execve" corner case.
  641. * It's the only case when -ff mode needs reprinting.
  642. */
  643. if ((!output_separately && printing_tcp != tcp && !tcp->staged_output_data)
  644. || (tcp->flags & TCB_REPRINT)) {
  645. tcp->flags &= ~TCB_REPRINT;
  646. printleader(tcp);
  647. tprintf("<... %s resumed>", tcp_sysent(tcp)->sys_name);
  648. }
  649. }
  650. int
  651. syscall_exiting_trace(struct tcb *tcp, struct timespec *ts, int res)
  652. {
  653. if (syscall_tampered(tcp) || inject_delay_exit(tcp))
  654. tamper_with_syscall_exiting(tcp);
  655. if (cflag) {
  656. count_syscall(tcp, ts);
  657. if (cflag == CFLAG_ONLY_STATS) {
  658. return 0;
  659. }
  660. }
  661. print_syscall_resume(tcp);
  662. printing_tcp = tcp;
  663. tcp->s_prev_ent = NULL;
  664. if (res != 1) {
  665. /* There was error in one of prior ptrace ops */
  666. tprints(") ");
  667. tabto();
  668. tprints("= ? <unavailable>\n");
  669. if (!is_complete_set(status_set, NUMBER_OF_STATUSES)) {
  670. bool publish = is_number_in_set(STATUS_UNAVAILABLE,
  671. status_set);
  672. strace_close_memstream(tcp, publish);
  673. }
  674. line_ended();
  675. return res;
  676. }
  677. tcp->s_prev_ent = tcp->s_ent;
  678. int sys_res = 0;
  679. if (raw(tcp)) {
  680. /* sys_res = printargs(tcp); - but it's nop on sysexit */
  681. } else {
  682. if (tcp->sys_func_rval & RVAL_DECODED)
  683. sys_res = tcp->sys_func_rval;
  684. else
  685. sys_res = tcp_sysent(tcp)->sys_func(tcp);
  686. }
  687. if (!is_complete_set(status_set, NUMBER_OF_STATUSES)) {
  688. bool publish = syserror(tcp)
  689. && is_number_in_set(STATUS_FAILED, status_set);
  690. publish |= !syserror(tcp)
  691. && is_number_in_set(STATUS_SUCCESSFUL, status_set);
  692. strace_close_memstream(tcp, publish);
  693. if (!publish) {
  694. line_ended();
  695. return 0;
  696. }
  697. }
  698. tprints(") ");
  699. tabto();
  700. if (raw(tcp)) {
  701. if (tcp->u_error)
  702. print_err_ret(tcp->u_rval, tcp->u_error);
  703. else
  704. tprintf("= %#" PRI_klx, tcp->u_rval);
  705. if (syscall_tampered(tcp))
  706. tprints(" (INJECTED)");
  707. } else if (!(sys_res & RVAL_NONE) && tcp->u_error) {
  708. switch (tcp->u_error) {
  709. /* Blocked signals do not interrupt any syscalls.
  710. * In this case syscalls don't return ERESTARTfoo codes.
  711. *
  712. * Deadly signals set to SIG_DFL interrupt syscalls
  713. * and kill the process regardless of which of the codes below
  714. * is returned by the interrupted syscall.
  715. * In some cases, kernel forces a kernel-generated deadly
  716. * signal to be unblocked and set to SIG_DFL (and thus cause
  717. * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
  718. * or SIGILL. (The alternative is to leave process spinning
  719. * forever on the faulty instruction - not useful).
  720. *
  721. * SIG_IGNed signals and non-deadly signals set to SIG_DFL
  722. * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
  723. * but kernel will always restart them.
  724. */
  725. case ERESTARTSYS:
  726. /* Most common type of signal-interrupted syscall exit code.
  727. * The system call will be restarted with the same arguments
  728. * if SA_RESTART is set; otherwise, it will fail with EINTR.
  729. */
  730. tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
  731. break;
  732. case ERESTARTNOINTR:
  733. /* Rare. For example, fork() returns this if interrupted.
  734. * SA_RESTART is ignored (assumed set): the restart is unconditional.
  735. */
  736. tprints("= ? ERESTARTNOINTR (To be restarted)");
  737. break;
  738. case ERESTARTNOHAND:
  739. /* pause(), rt_sigsuspend() etc use this code.
  740. * SA_RESTART is ignored (assumed not set):
  741. * syscall won't restart (will return EINTR instead)
  742. * even after signal with SA_RESTART set. However,
  743. * after SIG_IGN or SIG_DFL signal it will restart
  744. * (thus the name "restart only if has no handler").
  745. */
  746. tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
  747. break;
  748. case ERESTART_RESTARTBLOCK:
  749. /* Syscalls like nanosleep(), poll() which can't be
  750. * restarted with their original arguments use this
  751. * code. Kernel will execute restart_syscall() instead,
  752. * which changes arguments before restarting syscall.
  753. * SA_RESTART is ignored (assumed not set) similarly
  754. * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
  755. * since restart data is saved in "restart block"
  756. * in task struct, and if signal handler uses a syscall
  757. * which in turn saves another such restart block,
  758. * old data is lost and restart becomes impossible)
  759. */
  760. tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
  761. break;
  762. default:
  763. print_err_ret(tcp->u_rval, tcp->u_error);
  764. break;
  765. }
  766. if (syscall_tampered(tcp))
  767. tprints(" (INJECTED)");
  768. if ((sys_res & RVAL_STR) && tcp->auxstr)
  769. tprintf(" (%s)", tcp->auxstr);
  770. } else {
  771. if (sys_res & RVAL_NONE)
  772. tprints("= ?");
  773. else {
  774. switch (sys_res & RVAL_MASK) {
  775. case RVAL_HEX:
  776. #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
  777. if (current_klongsize < sizeof(tcp->u_rval)) {
  778. tprintf("= %#x",
  779. (unsigned int) tcp->u_rval);
  780. } else
  781. #endif
  782. {
  783. tprintf("= %#" PRI_klx, tcp->u_rval);
  784. }
  785. break;
  786. case RVAL_OCTAL:
  787. tprints("= ");
  788. print_numeric_long_umask(tcp->u_rval);
  789. break;
  790. case RVAL_UDECIMAL:
  791. #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
  792. if (current_klongsize < sizeof(tcp->u_rval)) {
  793. tprintf("= %u",
  794. (unsigned int) tcp->u_rval);
  795. } else
  796. #endif
  797. {
  798. tprintf("= %" PRI_klu, tcp->u_rval);
  799. }
  800. break;
  801. case RVAL_FD:
  802. /*
  803. * printfd accepts int as fd and it makes
  804. * little sense to pass negative fds to it.
  805. */
  806. if ((current_klongsize < sizeof(tcp->u_rval)) ||
  807. ((kernel_ulong_t) tcp->u_rval <= INT_MAX)) {
  808. tprints("= ");
  809. printfd(tcp, tcp->u_rval);
  810. } else {
  811. tprintf("= %" PRI_kld, tcp->u_rval);
  812. }
  813. break;
  814. default:
  815. error_msg("invalid rval format");
  816. break;
  817. }
  818. }
  819. if ((sys_res & RVAL_STR) && tcp->auxstr)
  820. tprintf(" (%s)", tcp->auxstr);
  821. if (syscall_tampered(tcp))
  822. tprints(" (INJECTED)");
  823. }
  824. if (Tflag) {
  825. ts_sub(ts, ts, &tcp->etime);
  826. tprintf(" <%ld", (long) ts->tv_sec);
  827. if (Tflag_width) {
  828. tprintf(".%0*ld",
  829. Tflag_width, (long) ts->tv_nsec / Tflag_scale);
  830. }
  831. tprints(">");
  832. }
  833. tprints("\n");
  834. dumpio(tcp);
  835. line_ended();
  836. #ifdef ENABLE_STACKTRACE
  837. if (stack_trace_enabled)
  838. unwind_tcb_print(tcp);
  839. #endif
  840. return 0;
  841. }
  842. void
  843. syscall_exiting_finish(struct tcb *tcp)
  844. {
  845. tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED | TCB_INJECT_DELAY_EXIT);
  846. tcp->sys_func_rval = 0;
  847. free_tcb_priv_data(tcp);
  848. if (cflag)
  849. tcp->ltime = tcp->stime;
  850. }
  851. bool
  852. is_erestart(struct tcb *tcp)
  853. {
  854. switch (tcp->u_error) {
  855. case ERESTARTSYS:
  856. case ERESTARTNOINTR:
  857. case ERESTARTNOHAND:
  858. case ERESTART_RESTARTBLOCK:
  859. return true;
  860. default:
  861. return false;
  862. }
  863. }
  864. static unsigned long saved_u_error;
  865. void
  866. temporarily_clear_syserror(struct tcb *tcp)
  867. {
  868. saved_u_error = tcp->u_error;
  869. tcp->u_error = 0;
  870. }
  871. void
  872. restore_cleared_syserror(struct tcb *tcp)
  873. {
  874. tcp->u_error = saved_u_error;
  875. }
  876. static struct_ptrace_syscall_info ptrace_sci;
  877. static bool
  878. ptrace_syscall_info_is_valid(void)
  879. {
  880. return ptrace_get_syscall_info_supported &&
  881. ptrace_sci.op <= PTRACE_SYSCALL_INFO_SECCOMP;
  882. }
  883. #define XLAT_MACROS_ONLY
  884. #include "xlat/nt_descriptor_types.h"
  885. #undef XLAT_MACROS_ONLY
  886. #define ARCH_MIGHT_USE_SET_REGS 1
  887. #include "arch_regs.c"
  888. #if HAVE_ARCH_GETRVAL2
  889. # include "arch_getrval2.c"
  890. #endif
  891. #include "getregs_old.h"
  892. #ifdef HAVE_GETREGS_OLD
  893. /* Either getregs_old() or set_regs() */
  894. # undef ARCH_MIGHT_USE_SET_REGS
  895. # define ARCH_MIGHT_USE_SET_REGS 0
  896. #endif
  897. #undef ptrace_getregset_or_getregs
  898. #undef ptrace_setregset_or_setregs
  899. #ifdef ARCH_REGS_FOR_GETREGSET
  900. # define ptrace_getregset_or_getregs ptrace_getregset
  901. static long
  902. ptrace_getregset(pid_t pid)
  903. {
  904. # ifdef ARCH_IOVEC_FOR_GETREGSET
  905. /* variable iovec */
  906. ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
  907. return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
  908. &ARCH_IOVEC_FOR_GETREGSET);
  909. # else
  910. /* constant iovec */
  911. static struct iovec io = {
  912. .iov_base = &ARCH_REGS_FOR_GETREGSET,
  913. .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
  914. };
  915. return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
  916. # endif
  917. }
  918. # if ARCH_MIGHT_USE_SET_REGS
  919. # define ptrace_setregset_or_setregs ptrace_setregset
  920. static int
  921. ptrace_setregset(pid_t pid)
  922. {
  923. # ifdef ARCH_IOVEC_FOR_GETREGSET
  924. /* variable iovec */
  925. return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
  926. &ARCH_IOVEC_FOR_GETREGSET);
  927. # else
  928. /* constant iovec */
  929. static struct iovec io = {
  930. .iov_base = &ARCH_REGS_FOR_GETREGSET,
  931. .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
  932. };
  933. return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
  934. # endif
  935. }
  936. # endif /* ARCH_MIGHT_USE_SET_REGS */
  937. #elif defined ARCH_REGS_FOR_GETREGS
  938. # define ptrace_getregset_or_getregs ptrace_getregs
  939. static long
  940. ptrace_getregs(pid_t pid)
  941. {
  942. # if defined SPARC || defined SPARC64
  943. /* SPARC systems have the meaning of data and addr reversed */
  944. return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
  945. # else
  946. return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
  947. # endif
  948. }
  949. # if ARCH_MIGHT_USE_SET_REGS
  950. # define ptrace_setregset_or_setregs ptrace_setregs
  951. static int
  952. ptrace_setregs(pid_t pid)
  953. {
  954. # if defined SPARC || defined SPARC64
  955. /* SPARC systems have the meaning of data and addr reversed */
  956. return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
  957. # else
  958. return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
  959. # endif
  960. }
  961. # endif /* ARCH_MIGHT_USE_SET_REGS */
  962. #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
  963. static long get_regs_error = -1;
  964. void
  965. clear_regs(struct tcb *tcp)
  966. {
  967. ptrace_sci.op = 0xff;
  968. get_regs_error = -1;
  969. }
  970. static long
  971. get_regs(struct tcb *const tcp)
  972. {
  973. #ifdef ptrace_getregset_or_getregs
  974. if (get_regs_error != -1)
  975. return get_regs_error;
  976. # ifdef HAVE_GETREGS_OLD
  977. /*
  978. * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
  979. * fallback to getregs_old.
  980. */
  981. static int use_getregs_old;
  982. if (use_getregs_old < 0) {
  983. return get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
  984. } else if (use_getregs_old == 0) {
  985. get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
  986. if (get_regs_error >= 0) {
  987. use_getregs_old = -1;
  988. return get_regs_error;
  989. }
  990. if (errno == EPERM || errno == ESRCH)
  991. return get_regs_error;
  992. use_getregs_old = 1;
  993. }
  994. return get_regs_error = getregs_old(tcp);
  995. # else /* !HAVE_GETREGS_OLD */
  996. /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
  997. get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
  998. # if defined ARCH_PERSONALITY_0_IOV_SIZE
  999. if (get_regs_error)
  1000. return get_regs_error;
  1001. switch (ARCH_IOVEC_FOR_GETREGSET.iov_len) {
  1002. case ARCH_PERSONALITY_0_IOV_SIZE:
  1003. update_personality(tcp, 0);
  1004. break;
  1005. case ARCH_PERSONALITY_1_IOV_SIZE:
  1006. update_personality(tcp, 1);
  1007. break;
  1008. default: {
  1009. static bool printed = false;
  1010. if (!printed) {
  1011. error_msg("Unsupported regset size returned by "
  1012. "PTRACE_GETREGSET: %zu",
  1013. ARCH_IOVEC_FOR_GETREGSET.iov_len);
  1014. printed = true;
  1015. }
  1016. update_personality(tcp, 0);
  1017. }
  1018. }
  1019. # endif /* ARCH_PERSONALITY_0_IOV_SIZE */
  1020. return get_regs_error;
  1021. # endif /* !HAVE_GETREGS_OLD */
  1022. #else /* !ptrace_getregset_or_getregs */
  1023. # warning get_regs is not implemented for this architecture yet
  1024. return 0;
  1025. #endif /* !ptrace_getregset_or_getregs */
  1026. }
  1027. #ifdef ptrace_setregset_or_setregs
  1028. static int
  1029. set_regs(pid_t pid)
  1030. {
  1031. return ptrace_setregset_or_setregs(pid);
  1032. }
  1033. #endif /* ptrace_setregset_or_setregs */
  1034. struct sysent_buf {
  1035. struct tcb *tcp;
  1036. struct_sysent ent;
  1037. char buf[sizeof("syscall_0x") + sizeof(kernel_ulong_t) * 2];
  1038. };
  1039. static void
  1040. free_sysent_buf(void *ptr)
  1041. {
  1042. struct sysent_buf *s = ptr;
  1043. s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
  1044. free(ptr);
  1045. }
  1046. static bool
  1047. strace_get_syscall_info(struct tcb *tcp)
  1048. {
  1049. /*
  1050. * ptrace_get_syscall_info_supported should have been checked
  1051. * by the caller.
  1052. */
  1053. if (ptrace_sci.op == 0xff) {
  1054. const size_t size = sizeof(ptrace_sci);
  1055. if (ptrace(PTRACE_GET_SYSCALL_INFO, tcp->pid,
  1056. (void *) size, &ptrace_sci) < 0) {
  1057. get_regs_error = -2;
  1058. return false;
  1059. }
  1060. #if SUPPORTED_PERSONALITIES > 1
  1061. int newpers = get_personality_from_syscall_info(&ptrace_sci);
  1062. if (newpers >= 0)
  1063. update_personality(tcp, newpers);
  1064. #endif
  1065. }
  1066. if (entering(tcp)) {
  1067. if (ptrace_sci.op == PTRACE_SYSCALL_INFO_EXIT) {
  1068. error_msg("pid %d: entering"
  1069. ", ptrace_syscall_info.op == %u",
  1070. tcp->pid, ptrace_sci.op);
  1071. /* TODO: handle this. */
  1072. }
  1073. } else {
  1074. if (ptrace_sci.op == PTRACE_SYSCALL_INFO_ENTRY) {
  1075. error_msg("pid %d: exiting"
  1076. ", ptrace_syscall_info.op == %u",
  1077. tcp->pid, ptrace_sci.op);
  1078. /* TODO: handle this. */
  1079. }
  1080. }
  1081. return true;
  1082. }
  1083. bool
  1084. get_instruction_pointer(struct tcb *tcp, kernel_ulong_t *ip)
  1085. {
  1086. if (get_regs_error < -1)
  1087. return false;
  1088. if (ptrace_get_syscall_info_supported) {
  1089. if (!strace_get_syscall_info(tcp))
  1090. return false;
  1091. *ip = (kernel_ulong_t) ptrace_sci.instruction_pointer;
  1092. return true;
  1093. }
  1094. #if defined ARCH_PC_REG
  1095. if (get_regs(tcp) < 0)
  1096. return false;
  1097. *ip = (kernel_ulong_t) ARCH_PC_REG;
  1098. return true;
  1099. #elif defined ARCH_PC_PEEK_ADDR
  1100. if (upeek(tcp, ARCH_PC_PEEK_ADDR, ip) < 0)
  1101. return false;
  1102. return true;
  1103. #else
  1104. # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
  1105. #endif
  1106. }
  1107. bool
  1108. get_stack_pointer(struct tcb *tcp, kernel_ulong_t *sp)
  1109. {
  1110. if (get_regs_error < -1)
  1111. return false;
  1112. if (ptrace_get_syscall_info_supported) {
  1113. if (!strace_get_syscall_info(tcp))
  1114. return false;
  1115. *sp = (kernel_ulong_t) ptrace_sci.stack_pointer;
  1116. return true;
  1117. }
  1118. #if defined ARCH_SP_REG
  1119. if (get_regs(tcp) < 0)
  1120. return false;
  1121. *sp = (kernel_ulong_t) ARCH_SP_REG;
  1122. return true;
  1123. #elif defined ARCH_SP_PEEK_ADDR
  1124. if (upeek(tcp, ARCH_SP_PEEK_ADDR, sp) < 0)
  1125. return false;
  1126. return true;
  1127. #else
  1128. return false;
  1129. #endif
  1130. }
  1131. static int
  1132. get_syscall_regs(struct tcb *tcp)
  1133. {
  1134. if (get_regs_error != -1)
  1135. return get_regs_error;
  1136. if (ptrace_get_syscall_info_supported)
  1137. return strace_get_syscall_info(tcp) ? 0 : get_regs_error;
  1138. return get_regs(tcp);
  1139. }
  1140. const struct_sysent stub_sysent = {
  1141. .nargs = MAX_ARGS,
  1142. .sys_flags = MEMORY_MAPPING_CHANGE,
  1143. .sen = SEN_printargs,
  1144. .sys_func = printargs,
  1145. .sys_name = "????",
  1146. };
  1147. /*
  1148. * Returns:
  1149. * 0: "ignore this ptrace stop", syscall_entering_decode() should return a "bail
  1150. * out silently" code.
  1151. * 1: ok, continue in syscall_entering_decode().
  1152. * other: error, syscall_entering_decode() should print error indicator
  1153. * ("????" etc) and return an appropriate code.
  1154. */
  1155. int
  1156. get_scno(struct tcb *tcp)
  1157. {
  1158. tcp->scno = -1;
  1159. tcp->s_ent = NULL;
  1160. tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
  1161. if (get_syscall_regs(tcp) < 0)
  1162. return -1;
  1163. if (ptrace_syscall_info_is_valid()) {
  1164. /* Apply arch-specific workarounds. */
  1165. int rc = arch_check_scno(tcp);
  1166. if (rc != 1)
  1167. return rc;
  1168. tcp->scno = ptrace_sci.entry.nr;
  1169. } else {
  1170. int rc = arch_get_scno(tcp);
  1171. if (rc != 1)
  1172. return rc;
  1173. }
  1174. tcp->scno = shuffle_scno(tcp->scno);
  1175. if (scno_is_valid(tcp->scno)) {
  1176. tcp->s_ent = &sysent[tcp->scno];
  1177. tcp->qual_flg = qual_flags(tcp->scno);
  1178. } else {
  1179. struct sysent_buf *s = xzalloc(sizeof(*s));
  1180. s->tcp = tcp;
  1181. s->ent = stub_sysent;
  1182. s->ent.sys_name = s->buf;
  1183. xsprintf(s->buf, "syscall_%#" PRI_klx, shuffle_scno(tcp->scno));
  1184. tcp->s_ent = &s->ent;
  1185. set_tcb_priv_data(tcp, s, free_sysent_buf);
  1186. debug_msg("pid %d invalid syscall %#" PRI_klx,
  1187. tcp->pid, shuffle_scno(tcp->scno));
  1188. }
  1189. /*
  1190. * We refrain from argument decoding during recovering
  1191. * as tracee memory mappings has changed and the registers
  1192. * are very likely pointing to garbage already.
  1193. */
  1194. if (recovering(tcp))
  1195. tcp->qual_flg |= QUAL_RAW;
  1196. return 1;
  1197. }
  1198. static int
  1199. get_syscall_args(struct tcb *tcp)
  1200. {
  1201. if (ptrace_syscall_info_is_valid()) {
  1202. const unsigned int n =
  1203. MIN(ARRAY_SIZE(tcp->u_arg),
  1204. ARRAY_SIZE(ptrace_sci.entry.args));
  1205. for (unsigned int i = 0; i < n; ++i)
  1206. tcp->u_arg[i] = ptrace_sci.entry.args[i];
  1207. #if SUPPORTED_PERSONALITIES > 1
  1208. if (tcp_sysent(tcp)->sys_flags & COMPAT_SYSCALL_TYPES) {
  1209. for (unsigned int i = 0; i < n; ++i)
  1210. tcp->u_arg[i] = (uint32_t) tcp->u_arg[i];
  1211. }
  1212. #endif
  1213. /*
  1214. * So far it's just a workaround for mips o32,
  1215. * but let's pretend it could be used elsewhere.
  1216. */
  1217. #if MAX_ARGS > 6
  1218. arch_get_syscall_args_extra(tcp, n);
  1219. #endif
  1220. return 1;
  1221. }
  1222. return arch_get_syscall_args(tcp);
  1223. }
  1224. #ifdef ptrace_getregset_or_getregs
  1225. # define get_syscall_result_regs get_syscall_regs
  1226. #else
  1227. static int get_syscall_result_regs(struct tcb *);
  1228. #endif
  1229. /* Returns:
  1230. * 1: ok, continue in syscall_exiting_trace().
  1231. * -1: error, syscall_exiting_trace() should print error indicator
  1232. * ("????" etc) and bail out.
  1233. */
  1234. static int
  1235. get_syscall_result(struct tcb *tcp)
  1236. {
  1237. if (get_syscall_result_regs(tcp) < 0)
  1238. return -1;
  1239. get_error(tcp,
  1240. (!(tcp_sysent(tcp)->sys_flags & SYSCALL_NEVER_FAILS)
  1241. || syscall_tampered(tcp))
  1242. && !syscall_tampered_nofail(tcp));
  1243. return 1;
  1244. }
  1245. static void
  1246. get_error(struct tcb *tcp, const bool check_errno)
  1247. {
  1248. if (ptrace_syscall_info_is_valid()) {
  1249. if (ptrace_sci.exit.is_error) {
  1250. tcp->u_rval = -1;
  1251. tcp->u_error = -ptrace_sci.exit.rval;
  1252. } else {
  1253. tcp->u_error = 0;
  1254. tcp->u_rval = ptrace_sci.exit.rval;
  1255. }
  1256. } else {
  1257. tcp->u_error = 0;
  1258. arch_get_error(tcp, check_errno);
  1259. }
  1260. }
  1261. static void
  1262. set_error(struct tcb *tcp, unsigned long new_error)
  1263. {
  1264. const unsigned long old_error = tcp->u_error;
  1265. if (new_error == old_error || new_error > MAX_ERRNO_VALUE)
  1266. return;
  1267. #ifdef ptrace_setregset_or_setregs
  1268. /* if we are going to invoke set_regs, call get_regs first */
  1269. if (get_regs(tcp) < 0)
  1270. return;
  1271. #endif
  1272. tcp->u_error = new_error;
  1273. if (arch_set_error(tcp)) {
  1274. tcp->u_error = old_error;
  1275. /* arch_set_error does not update u_rval */
  1276. } else {
  1277. if (ptrace_syscall_info_is_valid())
  1278. tcp->u_rval = -1;
  1279. else
  1280. get_error(tcp, !(tcp_sysent(tcp)->sys_flags &
  1281. SYSCALL_NEVER_FAILS));
  1282. }
  1283. }
  1284. static void
  1285. set_success(struct tcb *tcp, kernel_long_t new_rval)
  1286. {
  1287. const kernel_long_t old_rval = tcp->u_rval;
  1288. #ifdef ptrace_setregset_or_setregs
  1289. /* if we are going to invoke set_regs, call get_regs first */
  1290. if (get_regs(tcp) < 0)
  1291. return;
  1292. #endif
  1293. tcp->u_rval = new_rval;
  1294. if (arch_set_success(tcp)) {
  1295. tcp->u_rval = old_rval;
  1296. /* arch_set_success does not update u_error */
  1297. } else {
  1298. if (ptrace_syscall_info_is_valid())
  1299. tcp->u_error = 0;
  1300. else
  1301. get_error(tcp, !(tcp_sysent(tcp)->sys_flags &
  1302. SYSCALL_NEVER_FAILS));
  1303. }
  1304. }
  1305. #include "get_scno.c"
  1306. #include "check_scno.c"
  1307. #include "set_scno.c"
  1308. #include "get_syscall_args.c"
  1309. #ifndef ptrace_getregset_or_getregs
  1310. # include "get_syscall_result.c"
  1311. #endif
  1312. #include "get_error.c"
  1313. #include "set_error.c"
  1314. #ifdef HAVE_GETREGS_OLD
  1315. # include "getregs_old.c"
  1316. #endif
  1317. #include "shuffle_scno.c"
  1318. const char *
  1319. syscall_name(kernel_ulong_t scno)
  1320. {
  1321. return scno_is_valid(scno) ? sysent[scno].sys_name : NULL;
  1322. }