break-catch-sig.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /* Everything about signal catchpoints, for GDB.
  2. Copyright (C) 2011-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 "arch-utils.h"
  16. #include <ctype.h>
  17. #include "breakpoint.h"
  18. #include "gdbcmd.h"
  19. #include "inferior.h"
  20. #include "infrun.h"
  21. #include "annotate.h"
  22. #include "valprint.h"
  23. #include "cli/cli-utils.h"
  24. #include "completer.h"
  25. #include "cli/cli-style.h"
  26. #include "cli/cli-decode.h"
  27. #include <string>
  28. #define INTERNAL_SIGNAL(x) ((x) == GDB_SIGNAL_TRAP || (x) == GDB_SIGNAL_INT)
  29. /* An instance of this type is used to represent a signal catchpoint.
  30. A breakpoint is really of this type iff its ops pointer points to
  31. SIGNAL_CATCHPOINT_OPS. */
  32. struct signal_catchpoint : public breakpoint
  33. {
  34. /* Signal numbers used for the 'catch signal' feature. If no signal
  35. has been specified for filtering, it is empty. Otherwise,
  36. it holds a list of all signals to be caught. */
  37. std::vector<gdb_signal> signals_to_be_caught;
  38. /* If SIGNALS_TO_BE_CAUGHT is empty, then all "ordinary" signals are
  39. caught. If CATCH_ALL is true, then internal signals are caught
  40. as well. If SIGNALS_TO_BE_CAUGHT is not empty, then this field
  41. is ignored. */
  42. bool catch_all;
  43. };
  44. /* The breakpoint_ops structure to be used in signal catchpoints. */
  45. static struct breakpoint_ops signal_catchpoint_ops;
  46. /* Count of each signal. */
  47. static unsigned int signal_catch_counts[GDB_SIGNAL_LAST];
  48. /* A convenience wrapper for gdb_signal_to_name that returns the
  49. integer value if the name is not known. */
  50. static const char *
  51. signal_to_name_or_int (enum gdb_signal sig)
  52. {
  53. const char *result = gdb_signal_to_name (sig);
  54. if (strcmp (result, "?") == 0)
  55. result = plongest (sig);
  56. return result;
  57. }
  58. /* Implement the "insert_location" breakpoint_ops method for signal
  59. catchpoints. */
  60. static int
  61. signal_catchpoint_insert_location (struct bp_location *bl)
  62. {
  63. struct signal_catchpoint *c = (struct signal_catchpoint *) bl->owner;
  64. if (!c->signals_to_be_caught.empty ())
  65. {
  66. for (gdb_signal iter : c->signals_to_be_caught)
  67. ++signal_catch_counts[iter];
  68. }
  69. else
  70. {
  71. for (int i = 0; i < GDB_SIGNAL_LAST; ++i)
  72. {
  73. if (c->catch_all || !INTERNAL_SIGNAL (i))
  74. ++signal_catch_counts[i];
  75. }
  76. }
  77. signal_catch_update (signal_catch_counts);
  78. return 0;
  79. }
  80. /* Implement the "remove_location" breakpoint_ops method for signal
  81. catchpoints. */
  82. static int
  83. signal_catchpoint_remove_location (struct bp_location *bl,
  84. enum remove_bp_reason reason)
  85. {
  86. struct signal_catchpoint *c = (struct signal_catchpoint *) bl->owner;
  87. if (!c->signals_to_be_caught.empty ())
  88. {
  89. for (gdb_signal iter : c->signals_to_be_caught)
  90. {
  91. gdb_assert (signal_catch_counts[iter] > 0);
  92. --signal_catch_counts[iter];
  93. }
  94. }
  95. else
  96. {
  97. for (int i = 0; i < GDB_SIGNAL_LAST; ++i)
  98. {
  99. if (c->catch_all || !INTERNAL_SIGNAL (i))
  100. {
  101. gdb_assert (signal_catch_counts[i] > 0);
  102. --signal_catch_counts[i];
  103. }
  104. }
  105. }
  106. signal_catch_update (signal_catch_counts);
  107. return 0;
  108. }
  109. /* Implement the "breakpoint_hit" breakpoint_ops method for signal
  110. catchpoints. */
  111. static int
  112. signal_catchpoint_breakpoint_hit (const struct bp_location *bl,
  113. const address_space *aspace,
  114. CORE_ADDR bp_addr,
  115. const target_waitstatus &ws)
  116. {
  117. const struct signal_catchpoint *c
  118. = (const struct signal_catchpoint *) bl->owner;
  119. gdb_signal signal_number;
  120. if (ws.kind () != TARGET_WAITKIND_STOPPED)
  121. return 0;
  122. signal_number = ws.sig ();
  123. /* If we are catching specific signals in this breakpoint, then we
  124. must guarantee that the called signal is the same signal we are
  125. catching. */
  126. if (!c->signals_to_be_caught.empty ())
  127. {
  128. for (gdb_signal iter : c->signals_to_be_caught)
  129. if (signal_number == iter)
  130. return 1;
  131. /* Not the same. */
  132. return 0;
  133. }
  134. else
  135. return c->catch_all || !INTERNAL_SIGNAL (signal_number);
  136. }
  137. /* Implement the "print_it" breakpoint_ops method for signal
  138. catchpoints. */
  139. static enum print_stop_action
  140. signal_catchpoint_print_it (bpstat *bs)
  141. {
  142. struct breakpoint *b = bs->breakpoint_at;
  143. struct target_waitstatus last;
  144. const char *signal_name;
  145. struct ui_out *uiout = current_uiout;
  146. get_last_target_status (nullptr, nullptr, &last);
  147. signal_name = signal_to_name_or_int (last.sig ());
  148. annotate_catchpoint (b->number);
  149. maybe_print_thread_hit_breakpoint (uiout);
  150. gdb_printf (_("Catchpoint %d (signal %s), "), b->number, signal_name);
  151. return PRINT_SRC_AND_LOC;
  152. }
  153. /* Implement the "print_one" breakpoint_ops method for signal
  154. catchpoints. */
  155. static void
  156. signal_catchpoint_print_one (struct breakpoint *b,
  157. struct bp_location **last_loc)
  158. {
  159. struct signal_catchpoint *c = (struct signal_catchpoint *) b;
  160. struct value_print_options opts;
  161. struct ui_out *uiout = current_uiout;
  162. get_user_print_options (&opts);
  163. /* Field 4, the address, is omitted (which makes the columns
  164. not line up too nicely with the headers, but the effect
  165. is relatively readable). */
  166. if (opts.addressprint)
  167. uiout->field_skip ("addr");
  168. annotate_field (5);
  169. if (c->signals_to_be_caught.size () > 1)
  170. uiout->text ("signals \"");
  171. else
  172. uiout->text ("signal \"");
  173. if (!c->signals_to_be_caught.empty ())
  174. {
  175. std::string text;
  176. bool first = true;
  177. for (gdb_signal iter : c->signals_to_be_caught)
  178. {
  179. const char *name = signal_to_name_or_int (iter);
  180. if (!first)
  181. text += " ";
  182. first = false;
  183. text += name;
  184. }
  185. uiout->field_string ("what", text);
  186. }
  187. else
  188. uiout->field_string ("what",
  189. c->catch_all ? "<any signal>" : "<standard signals>",
  190. metadata_style.style ());
  191. uiout->text ("\" ");
  192. if (uiout->is_mi_like_p ())
  193. uiout->field_string ("catch-type", "signal");
  194. }
  195. /* Implement the "print_mention" breakpoint_ops method for signal
  196. catchpoints. */
  197. static void
  198. signal_catchpoint_print_mention (struct breakpoint *b)
  199. {
  200. struct signal_catchpoint *c = (struct signal_catchpoint *) b;
  201. if (!c->signals_to_be_caught.empty ())
  202. {
  203. if (c->signals_to_be_caught.size () > 1)
  204. gdb_printf (_("Catchpoint %d (signals"), b->number);
  205. else
  206. gdb_printf (_("Catchpoint %d (signal"), b->number);
  207. for (gdb_signal iter : c->signals_to_be_caught)
  208. {
  209. const char *name = signal_to_name_or_int (iter);
  210. gdb_printf (" %s", name);
  211. }
  212. gdb_printf (")");
  213. }
  214. else if (c->catch_all)
  215. gdb_printf (_("Catchpoint %d (any signal)"), b->number);
  216. else
  217. gdb_printf (_("Catchpoint %d (standard signals)"), b->number);
  218. }
  219. /* Implement the "print_recreate" breakpoint_ops method for signal
  220. catchpoints. */
  221. static void
  222. signal_catchpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
  223. {
  224. struct signal_catchpoint *c = (struct signal_catchpoint *) b;
  225. gdb_printf (fp, "catch signal");
  226. if (!c->signals_to_be_caught.empty ())
  227. {
  228. for (gdb_signal iter : c->signals_to_be_caught)
  229. gdb_printf (fp, " %s", signal_to_name_or_int (iter));
  230. }
  231. else if (c->catch_all)
  232. gdb_printf (fp, " all");
  233. gdb_putc ('\n', fp);
  234. }
  235. /* Implement the "explains_signal" breakpoint_ops method for signal
  236. catchpoints. */
  237. static int
  238. signal_catchpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
  239. {
  240. return 1;
  241. }
  242. /* Create a new signal catchpoint. TEMPFLAG is true if this should be
  243. a temporary catchpoint. FILTER is the list of signals to catch; it
  244. can be empty, meaning all signals. CATCH_ALL is a flag indicating
  245. whether signals used internally by gdb should be caught; it is only
  246. valid if FILTER is NULL. If FILTER is empty and CATCH_ALL is zero,
  247. then internal signals like SIGTRAP are not caught. */
  248. static void
  249. create_signal_catchpoint (int tempflag, std::vector<gdb_signal> &&filter,
  250. bool catch_all)
  251. {
  252. struct gdbarch *gdbarch = get_current_arch ();
  253. std::unique_ptr<signal_catchpoint> c (new signal_catchpoint ());
  254. init_catchpoint (c.get (), gdbarch, tempflag, NULL, &signal_catchpoint_ops);
  255. c->signals_to_be_caught = std::move (filter);
  256. c->catch_all = catch_all;
  257. install_breakpoint (0, std::move (c), 1);
  258. }
  259. /* Splits the argument using space as delimiter. Returns a filter
  260. list, which is empty if no filtering is required. */
  261. static std::vector<gdb_signal>
  262. catch_signal_split_args (const char *arg, bool *catch_all)
  263. {
  264. std::vector<gdb_signal> result;
  265. bool first = true;
  266. while (*arg != '\0')
  267. {
  268. int num;
  269. gdb_signal signal_number;
  270. char *endptr;
  271. std::string one_arg = extract_arg (&arg);
  272. if (one_arg.empty ())
  273. break;
  274. /* Check for the special flag "all". */
  275. if (one_arg == "all")
  276. {
  277. arg = skip_spaces (arg);
  278. if (*arg != '\0' || !first)
  279. error (_("'all' cannot be caught with other signals"));
  280. *catch_all = true;
  281. gdb_assert (result.empty ());
  282. return result;
  283. }
  284. first = false;
  285. /* Check if the user provided a signal name or a number. */
  286. num = (int) strtol (one_arg.c_str (), &endptr, 0);
  287. if (*endptr == '\0')
  288. signal_number = gdb_signal_from_command (num);
  289. else
  290. {
  291. signal_number = gdb_signal_from_name (one_arg.c_str ());
  292. if (signal_number == GDB_SIGNAL_UNKNOWN)
  293. error (_("Unknown signal name '%s'."), one_arg.c_str ());
  294. }
  295. result.push_back (signal_number);
  296. }
  297. result.shrink_to_fit ();
  298. return result;
  299. }
  300. /* Implement the "catch signal" command. */
  301. static void
  302. catch_signal_command (const char *arg, int from_tty,
  303. struct cmd_list_element *command)
  304. {
  305. int tempflag;
  306. bool catch_all = false;
  307. std::vector<gdb_signal> filter;
  308. tempflag = command->context () == CATCH_TEMPORARY;
  309. arg = skip_spaces (arg);
  310. /* The allowed syntax is:
  311. catch signal
  312. catch signal <name | number> [<name | number> ... <name | number>]
  313. Let's check if there's a signal name. */
  314. if (arg != NULL)
  315. filter = catch_signal_split_args (arg, &catch_all);
  316. create_signal_catchpoint (tempflag, std::move (filter), catch_all);
  317. }
  318. static void
  319. initialize_signal_catchpoint_ops (void)
  320. {
  321. struct breakpoint_ops *ops;
  322. initialize_breakpoint_ops ();
  323. ops = &signal_catchpoint_ops;
  324. *ops = base_breakpoint_ops;
  325. ops->insert_location = signal_catchpoint_insert_location;
  326. ops->remove_location = signal_catchpoint_remove_location;
  327. ops->breakpoint_hit = signal_catchpoint_breakpoint_hit;
  328. ops->print_it = signal_catchpoint_print_it;
  329. ops->print_one = signal_catchpoint_print_one;
  330. ops->print_mention = signal_catchpoint_print_mention;
  331. ops->print_recreate = signal_catchpoint_print_recreate;
  332. ops->explains_signal = signal_catchpoint_explains_signal;
  333. }
  334. void _initialize_break_catch_sig ();
  335. void
  336. _initialize_break_catch_sig ()
  337. {
  338. initialize_signal_catchpoint_ops ();
  339. add_catch_command ("signal", _("\
  340. Catch signals by their names and/or numbers.\n\
  341. Usage: catch signal [[NAME|NUMBER] [NAME|NUMBER]...|all]\n\
  342. Arguments say which signals to catch. If no arguments\n\
  343. are given, every \"normal\" signal will be caught.\n\
  344. The argument \"all\" means to also catch signals used by GDB.\n\
  345. Arguments, if given, should be one or more signal names\n\
  346. (if your system supports that), or signal numbers."),
  347. catch_signal_command,
  348. signal_completer,
  349. CATCH_PERMANENT,
  350. CATCH_TEMPORARY);
  351. }