break-catch-syscall.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /* Everything about syscall catchpoints, for GDB.
  2. Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include <ctype.h>
  16. #include "breakpoint.h"
  17. #include "gdbcmd.h"
  18. #include "inferior.h"
  19. #include "cli/cli-utils.h"
  20. #include "annotate.h"
  21. #include "mi/mi-common.h"
  22. #include "valprint.h"
  23. #include "arch-utils.h"
  24. #include "observable.h"
  25. #include "xml-syscall.h"
  26. #include "cli/cli-style.h"
  27. #include "cli/cli-decode.h"
  28. /* An instance of this type is used to represent a syscall catchpoint.
  29. A breakpoint is really of this type iff its ops pointer points to
  30. CATCH_SYSCALL_BREAKPOINT_OPS. */
  31. struct syscall_catchpoint : public breakpoint
  32. {
  33. /* Syscall numbers used for the 'catch syscall' feature. If no
  34. syscall has been specified for filtering, it is empty.
  35. Otherwise, it holds a list of all syscalls to be caught. */
  36. std::vector<int> syscalls_to_be_caught;
  37. };
  38. struct catch_syscall_inferior_data
  39. {
  40. /* We keep a count of the number of times the user has requested a
  41. particular syscall to be tracked, and pass this information to the
  42. target. This lets capable targets implement filtering directly. */
  43. /* Number of times that "any" syscall is requested. */
  44. int any_syscall_count;
  45. /* Count of each system call. */
  46. std::vector<int> syscalls_counts;
  47. /* This counts all syscall catch requests, so we can readily determine
  48. if any catching is necessary. */
  49. int total_syscalls_count;
  50. };
  51. static const struct inferior_key<struct catch_syscall_inferior_data>
  52. catch_syscall_inferior_data;
  53. static struct catch_syscall_inferior_data *
  54. get_catch_syscall_inferior_data (struct inferior *inf)
  55. {
  56. struct catch_syscall_inferior_data *inf_data;
  57. inf_data = catch_syscall_inferior_data.get (inf);
  58. if (inf_data == NULL)
  59. inf_data = catch_syscall_inferior_data.emplace (inf);
  60. return inf_data;
  61. }
  62. /* Implement the "insert" breakpoint_ops method for syscall
  63. catchpoints. */
  64. static int
  65. insert_catch_syscall (struct bp_location *bl)
  66. {
  67. struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
  68. struct inferior *inf = current_inferior ();
  69. struct catch_syscall_inferior_data *inf_data
  70. = get_catch_syscall_inferior_data (inf);
  71. ++inf_data->total_syscalls_count;
  72. if (c->syscalls_to_be_caught.empty ())
  73. ++inf_data->any_syscall_count;
  74. else
  75. {
  76. for (int iter : c->syscalls_to_be_caught)
  77. {
  78. if (iter >= inf_data->syscalls_counts.size ())
  79. inf_data->syscalls_counts.resize (iter + 1);
  80. ++inf_data->syscalls_counts[iter];
  81. }
  82. }
  83. return target_set_syscall_catchpoint (inferior_ptid.pid (),
  84. inf_data->total_syscalls_count != 0,
  85. inf_data->any_syscall_count,
  86. inf_data->syscalls_counts);
  87. }
  88. /* Implement the "remove" breakpoint_ops method for syscall
  89. catchpoints. */
  90. static int
  91. remove_catch_syscall (struct bp_location *bl, enum remove_bp_reason reason)
  92. {
  93. struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
  94. struct inferior *inf = current_inferior ();
  95. struct catch_syscall_inferior_data *inf_data
  96. = get_catch_syscall_inferior_data (inf);
  97. --inf_data->total_syscalls_count;
  98. if (c->syscalls_to_be_caught.empty ())
  99. --inf_data->any_syscall_count;
  100. else
  101. {
  102. for (int iter : c->syscalls_to_be_caught)
  103. {
  104. if (iter >= inf_data->syscalls_counts.size ())
  105. /* Shouldn't happen. */
  106. continue;
  107. --inf_data->syscalls_counts[iter];
  108. }
  109. }
  110. return target_set_syscall_catchpoint (inferior_ptid.pid (),
  111. inf_data->total_syscalls_count != 0,
  112. inf_data->any_syscall_count,
  113. inf_data->syscalls_counts);
  114. }
  115. /* Implement the "breakpoint_hit" breakpoint_ops method for syscall
  116. catchpoints. */
  117. static int
  118. breakpoint_hit_catch_syscall (const struct bp_location *bl,
  119. const address_space *aspace, CORE_ADDR bp_addr,
  120. const target_waitstatus &ws)
  121. {
  122. /* We must check if we are catching specific syscalls in this
  123. breakpoint. If we are, then we must guarantee that the called
  124. syscall is the same syscall we are catching. */
  125. int syscall_number = 0;
  126. const struct syscall_catchpoint *c
  127. = (const struct syscall_catchpoint *) bl->owner;
  128. if (ws.kind () != TARGET_WAITKIND_SYSCALL_ENTRY
  129. && ws.kind () != TARGET_WAITKIND_SYSCALL_RETURN)
  130. return 0;
  131. syscall_number = ws.syscall_number ();
  132. /* Now, checking if the syscall is the same. */
  133. if (!c->syscalls_to_be_caught.empty ())
  134. {
  135. for (int iter : c->syscalls_to_be_caught)
  136. if (syscall_number == iter)
  137. return 1;
  138. return 0;
  139. }
  140. return 1;
  141. }
  142. /* Implement the "print_it" breakpoint_ops method for syscall
  143. catchpoints. */
  144. static enum print_stop_action
  145. print_it_catch_syscall (bpstat *bs)
  146. {
  147. struct ui_out *uiout = current_uiout;
  148. struct breakpoint *b = bs->breakpoint_at;
  149. /* These are needed because we want to know in which state a
  150. syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
  151. or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
  152. must print "called syscall" or "returned from syscall". */
  153. struct target_waitstatus last;
  154. struct syscall s;
  155. struct gdbarch *gdbarch = bs->bp_location_at->gdbarch;
  156. get_last_target_status (nullptr, nullptr, &last);
  157. get_syscall_by_number (gdbarch, last.syscall_number (), &s);
  158. annotate_catchpoint (b->number);
  159. maybe_print_thread_hit_breakpoint (uiout);
  160. if (b->disposition == disp_del)
  161. uiout->text ("Temporary catchpoint ");
  162. else
  163. uiout->text ("Catchpoint ");
  164. if (uiout->is_mi_like_p ())
  165. {
  166. uiout->field_string ("reason",
  167. async_reason_lookup (last.kind () == TARGET_WAITKIND_SYSCALL_ENTRY
  168. ? EXEC_ASYNC_SYSCALL_ENTRY
  169. : EXEC_ASYNC_SYSCALL_RETURN));
  170. uiout->field_string ("disp", bpdisp_text (b->disposition));
  171. }
  172. uiout->field_signed ("bkptno", b->number);
  173. if (last.kind () == TARGET_WAITKIND_SYSCALL_ENTRY)
  174. uiout->text (" (call to syscall ");
  175. else
  176. uiout->text (" (returned from syscall ");
  177. if (s.name == NULL || uiout->is_mi_like_p ())
  178. uiout->field_signed ("syscall-number", last.syscall_number ());
  179. if (s.name != NULL)
  180. uiout->field_string ("syscall-name", s.name);
  181. uiout->text ("), ");
  182. return PRINT_SRC_AND_LOC;
  183. }
  184. /* Implement the "print_one" breakpoint_ops method for syscall
  185. catchpoints. */
  186. static void
  187. print_one_catch_syscall (struct breakpoint *b,
  188. struct bp_location **last_loc)
  189. {
  190. struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
  191. struct value_print_options opts;
  192. struct ui_out *uiout = current_uiout;
  193. struct gdbarch *gdbarch = b->loc->gdbarch;
  194. get_user_print_options (&opts);
  195. /* Field 4, the address, is omitted (which makes the columns not
  196. line up too nicely with the headers, but the effect is relatively
  197. readable). */
  198. if (opts.addressprint)
  199. uiout->field_skip ("addr");
  200. annotate_field (5);
  201. if (c->syscalls_to_be_caught.size () > 1)
  202. uiout->text ("syscalls \"");
  203. else
  204. uiout->text ("syscall \"");
  205. if (!c->syscalls_to_be_caught.empty ())
  206. {
  207. std::string text;
  208. bool first = true;
  209. for (int iter : c->syscalls_to_be_caught)
  210. {
  211. struct syscall s;
  212. get_syscall_by_number (gdbarch, iter, &s);
  213. if (!first)
  214. text += ", ";
  215. first = false;
  216. if (s.name != NULL)
  217. text += s.name;
  218. else
  219. text += std::to_string (iter);
  220. }
  221. uiout->field_string ("what", text.c_str ());
  222. }
  223. else
  224. uiout->field_string ("what", "<any syscall>", metadata_style.style ());
  225. uiout->text ("\" ");
  226. if (uiout->is_mi_like_p ())
  227. uiout->field_string ("catch-type", "syscall");
  228. }
  229. /* Implement the "print_mention" breakpoint_ops method for syscall
  230. catchpoints. */
  231. static void
  232. print_mention_catch_syscall (struct breakpoint *b)
  233. {
  234. struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
  235. struct gdbarch *gdbarch = b->loc->gdbarch;
  236. if (!c->syscalls_to_be_caught.empty ())
  237. {
  238. if (c->syscalls_to_be_caught.size () > 1)
  239. gdb_printf (_("Catchpoint %d (syscalls"), b->number);
  240. else
  241. gdb_printf (_("Catchpoint %d (syscall"), b->number);
  242. for (int iter : c->syscalls_to_be_caught)
  243. {
  244. struct syscall s;
  245. get_syscall_by_number (gdbarch, iter, &s);
  246. if (s.name != NULL)
  247. gdb_printf (" '%s' [%d]", s.name, s.number);
  248. else
  249. gdb_printf (" %d", s.number);
  250. }
  251. gdb_printf (")");
  252. }
  253. else
  254. gdb_printf (_("Catchpoint %d (any syscall)"),
  255. b->number);
  256. }
  257. /* Implement the "print_recreate" breakpoint_ops method for syscall
  258. catchpoints. */
  259. static void
  260. print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
  261. {
  262. struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
  263. struct gdbarch *gdbarch = b->loc->gdbarch;
  264. gdb_printf (fp, "catch syscall");
  265. for (int iter : c->syscalls_to_be_caught)
  266. {
  267. struct syscall s;
  268. get_syscall_by_number (gdbarch, iter, &s);
  269. if (s.name != NULL)
  270. gdb_printf (fp, " %s", s.name);
  271. else
  272. gdb_printf (fp, " %d", s.number);
  273. }
  274. print_recreate_thread (b, fp);
  275. }
  276. /* The breakpoint_ops structure to be used in syscall catchpoints. */
  277. static struct breakpoint_ops catch_syscall_breakpoint_ops;
  278. /* Returns non-zero if 'b' is a syscall catchpoint. */
  279. static int
  280. syscall_catchpoint_p (struct breakpoint *b)
  281. {
  282. return (b->ops == &catch_syscall_breakpoint_ops);
  283. }
  284. static void
  285. create_syscall_event_catchpoint (int tempflag, std::vector<int> &&filter,
  286. const struct breakpoint_ops *ops)
  287. {
  288. struct gdbarch *gdbarch = get_current_arch ();
  289. std::unique_ptr<syscall_catchpoint> c (new syscall_catchpoint ());
  290. init_catchpoint (c.get (), gdbarch, tempflag, NULL, ops);
  291. c->syscalls_to_be_caught = std::move (filter);
  292. install_breakpoint (0, std::move (c), 1);
  293. }
  294. /* Splits the argument using space as delimiter. */
  295. static std::vector<int>
  296. catch_syscall_split_args (const char *arg)
  297. {
  298. std::vector<int> result;
  299. struct gdbarch *gdbarch = target_gdbarch ();
  300. while (*arg != '\0')
  301. {
  302. int i, syscall_number;
  303. char *endptr;
  304. char cur_name[128];
  305. struct syscall s;
  306. /* Skip whitespace. */
  307. arg = skip_spaces (arg);
  308. for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
  309. cur_name[i] = arg[i];
  310. cur_name[i] = '\0';
  311. arg += i;
  312. /* Check if the user provided a syscall name, group, or a number. */
  313. syscall_number = (int) strtol (cur_name, &endptr, 0);
  314. if (*endptr == '\0')
  315. {
  316. if (syscall_number < 0)
  317. error (_("Unknown syscall number '%d'."), syscall_number);
  318. get_syscall_by_number (gdbarch, syscall_number, &s);
  319. result.push_back (s.number);
  320. }
  321. else if (startswith (cur_name, "g:")
  322. || startswith (cur_name, "group:"))
  323. {
  324. /* We have a syscall group. Let's expand it into a syscall
  325. list before inserting. */
  326. const char *group_name;
  327. /* Skip over "g:" and "group:" prefix strings. */
  328. group_name = strchr (cur_name, ':') + 1;
  329. if (!get_syscalls_by_group (gdbarch, group_name, &result))
  330. error (_("Unknown syscall group '%s'."), group_name);
  331. }
  332. else
  333. {
  334. /* We have a name. Let's check if it's valid and fetch a
  335. list of matching numbers. */
  336. if (!get_syscalls_by_name (gdbarch, cur_name, &result))
  337. /* Here we have to issue an error instead of a warning,
  338. because GDB cannot do anything useful if there's no
  339. syscall number to be caught. */
  340. error (_("Unknown syscall name '%s'."), cur_name);
  341. }
  342. }
  343. return result;
  344. }
  345. /* Implement the "catch syscall" command. */
  346. static void
  347. catch_syscall_command_1 (const char *arg, int from_tty,
  348. struct cmd_list_element *command)
  349. {
  350. int tempflag;
  351. std::vector<int> filter;
  352. struct syscall s;
  353. struct gdbarch *gdbarch = get_current_arch ();
  354. /* Checking if the feature if supported. */
  355. if (gdbarch_get_syscall_number_p (gdbarch) == 0)
  356. error (_("The feature 'catch syscall' is not supported on \
  357. this architecture yet."));
  358. tempflag = command->context () == CATCH_TEMPORARY;
  359. arg = skip_spaces (arg);
  360. /* We need to do this first "dummy" translation in order
  361. to get the syscall XML file loaded or, most important,
  362. to display a warning to the user if there's no XML file
  363. for his/her architecture. */
  364. get_syscall_by_number (gdbarch, 0, &s);
  365. /* The allowed syntax is:
  366. catch syscall
  367. catch syscall <name | number> [<name | number> ... <name | number>]
  368. Let's check if there's a syscall name. */
  369. if (arg != NULL)
  370. filter = catch_syscall_split_args (arg);
  371. create_syscall_event_catchpoint (tempflag, std::move (filter),
  372. &catch_syscall_breakpoint_ops);
  373. }
  374. /* Returns 0 if 'bp' is NOT a syscall catchpoint,
  375. non-zero otherwise. */
  376. static int
  377. is_syscall_catchpoint_enabled (struct breakpoint *bp)
  378. {
  379. if (syscall_catchpoint_p (bp)
  380. && bp->enable_state != bp_disabled
  381. && bp->enable_state != bp_call_disabled)
  382. return 1;
  383. else
  384. return 0;
  385. }
  386. int
  387. catch_syscall_enabled (void)
  388. {
  389. struct catch_syscall_inferior_data *inf_data
  390. = get_catch_syscall_inferior_data (current_inferior ());
  391. return inf_data->total_syscalls_count != 0;
  392. }
  393. /* Helper function for catching_syscall_number. return true if B is a syscall
  394. catchpoint for SYSCALL_NUMBER, else false. */
  395. static bool
  396. catching_syscall_number_1 (struct breakpoint *b, int syscall_number)
  397. {
  398. if (is_syscall_catchpoint_enabled (b))
  399. {
  400. struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
  401. if (!c->syscalls_to_be_caught.empty ())
  402. {
  403. for (int iter : c->syscalls_to_be_caught)
  404. if (syscall_number == iter)
  405. return true;
  406. }
  407. else
  408. return true;
  409. }
  410. return false;
  411. }
  412. bool
  413. catching_syscall_number (int syscall_number)
  414. {
  415. for (breakpoint *b : all_breakpoints ())
  416. if (catching_syscall_number_1 (b, syscall_number))
  417. return true;
  418. return false;
  419. }
  420. /* Complete syscall names. Used by "catch syscall". */
  421. static void
  422. catch_syscall_completer (struct cmd_list_element *cmd,
  423. completion_tracker &tracker,
  424. const char *text, const char *word)
  425. {
  426. struct gdbarch *gdbarch = get_current_arch ();
  427. gdb::unique_xmalloc_ptr<const char *> group_list;
  428. const char *prefix;
  429. /* Completion considers ':' to be a word separator, so we use this to
  430. verify whether the previous word was a group prefix. If so, we
  431. build the completion list using group names only. */
  432. for (prefix = word; prefix != text && prefix[-1] != ' '; prefix--)
  433. ;
  434. if (startswith (prefix, "g:") || startswith (prefix, "group:"))
  435. {
  436. /* Perform completion inside 'group:' namespace only. */
  437. group_list.reset (get_syscall_group_names (gdbarch));
  438. if (group_list != NULL)
  439. complete_on_enum (tracker, group_list.get (), word, word);
  440. }
  441. else
  442. {
  443. /* Complete with both, syscall names and groups. */
  444. gdb::unique_xmalloc_ptr<const char *> syscall_list
  445. (get_syscall_names (gdbarch));
  446. group_list.reset (get_syscall_group_names (gdbarch));
  447. const char **group_ptr = group_list.get ();
  448. /* Hold on to strings while we're using them. */
  449. std::vector<std::string> holders;
  450. /* Append "group:" prefix to syscall groups. */
  451. for (int i = 0; group_ptr[i] != NULL; i++)
  452. holders.push_back (string_printf ("group:%s", group_ptr[i]));
  453. for (int i = 0; group_ptr[i] != NULL; i++)
  454. group_ptr[i] = holders[i].c_str ();
  455. if (syscall_list != NULL)
  456. complete_on_enum (tracker, syscall_list.get (), word, word);
  457. if (group_list != NULL)
  458. complete_on_enum (tracker, group_ptr, word, word);
  459. }
  460. }
  461. static void
  462. clear_syscall_counts (struct inferior *inf)
  463. {
  464. struct catch_syscall_inferior_data *inf_data
  465. = get_catch_syscall_inferior_data (inf);
  466. inf_data->total_syscalls_count = 0;
  467. inf_data->any_syscall_count = 0;
  468. inf_data->syscalls_counts.clear ();
  469. }
  470. static void
  471. initialize_syscall_catchpoint_ops (void)
  472. {
  473. struct breakpoint_ops *ops;
  474. initialize_breakpoint_ops ();
  475. /* Syscall catchpoints. */
  476. ops = &catch_syscall_breakpoint_ops;
  477. *ops = base_breakpoint_ops;
  478. ops->insert_location = insert_catch_syscall;
  479. ops->remove_location = remove_catch_syscall;
  480. ops->breakpoint_hit = breakpoint_hit_catch_syscall;
  481. ops->print_it = print_it_catch_syscall;
  482. ops->print_one = print_one_catch_syscall;
  483. ops->print_mention = print_mention_catch_syscall;
  484. ops->print_recreate = print_recreate_catch_syscall;
  485. }
  486. void _initialize_break_catch_syscall ();
  487. void
  488. _initialize_break_catch_syscall ()
  489. {
  490. initialize_syscall_catchpoint_ops ();
  491. gdb::observers::inferior_exit.attach (clear_syscall_counts,
  492. "break-catch-syscall");
  493. add_catch_command ("syscall", _("\
  494. Catch system calls by their names, groups and/or numbers.\n\
  495. Arguments say which system calls to catch. If no arguments are given,\n\
  496. every system call will be caught. Arguments, if given, should be one\n\
  497. or more system call names (if your system supports that), system call\n\
  498. groups or system call numbers."),
  499. catch_syscall_command_1,
  500. catch_syscall_completer,
  501. CATCH_PERMANENT,
  502. CATCH_TEMPORARY);
  503. }