py-breakpoint.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  1. /* Python interface to breakpoints
  2. Copyright (C) 2008-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 "value.h"
  16. #include "python-internal.h"
  17. #include "python.h"
  18. #include "charset.h"
  19. #include "breakpoint.h"
  20. #include "gdbcmd.h"
  21. #include "gdbthread.h"
  22. #include "observable.h"
  23. #include "cli/cli-script.h"
  24. #include "ada-lang.h"
  25. #include "arch-utils.h"
  26. #include "language.h"
  27. #include "location.h"
  28. #include "py-event.h"
  29. #include "linespec.h"
  30. /* Debugging of Python breakpoints. */
  31. static bool pybp_debug;
  32. /* Implementation of "show debug py-breakpoint". */
  33. static void
  34. show_pybp_debug (struct ui_file *file, int from_tty,
  35. struct cmd_list_element *c, const char *value)
  36. {
  37. gdb_printf (file, _("Python breakpoint debugging is %s.\n"), value);
  38. }
  39. /* Print a "py-breakpoint" debug statement. */
  40. #define pybp_debug_printf(fmt, ...) \
  41. debug_prefixed_printf_cond (pybp_debug, "py-breakpoint", fmt, ##__VA_ARGS__)
  42. /* Print a "py-breakpoint" enter/exit debug statements. */
  43. #define PYBP_SCOPED_DEBUG_ENTER_EXIT \
  44. scoped_debug_enter_exit (pybp_debug, "py-breakpoint")
  45. /* Number of live breakpoints. */
  46. static int bppy_live;
  47. /* Variables used to pass information between the Breakpoint
  48. constructor and the breakpoint-created hook function. */
  49. gdbpy_breakpoint_object *bppy_pending_object;
  50. /* Function that is called when a Python condition is evaluated. */
  51. static const char stop_func[] = "stop";
  52. /* This is used to initialize various gdb.bp_* constants. */
  53. struct pybp_code
  54. {
  55. /* The name. */
  56. const char *name;
  57. /* The code. */
  58. int code;
  59. };
  60. /* Entries related to the type of user set breakpoints. */
  61. static struct pybp_code pybp_codes[] =
  62. {
  63. { "BP_NONE", bp_none},
  64. { "BP_BREAKPOINT", bp_breakpoint},
  65. { "BP_HARDWARE_BREAKPOINT", bp_hardware_breakpoint},
  66. { "BP_WATCHPOINT", bp_watchpoint},
  67. { "BP_HARDWARE_WATCHPOINT", bp_hardware_watchpoint},
  68. { "BP_READ_WATCHPOINT", bp_read_watchpoint},
  69. { "BP_ACCESS_WATCHPOINT", bp_access_watchpoint},
  70. { "BP_CATCHPOINT", bp_catchpoint},
  71. {NULL} /* Sentinel. */
  72. };
  73. /* Entries related to the type of watchpoint. */
  74. static struct pybp_code pybp_watch_types[] =
  75. {
  76. { "WP_READ", hw_read},
  77. { "WP_WRITE", hw_write},
  78. { "WP_ACCESS", hw_access},
  79. {NULL} /* Sentinel. */
  80. };
  81. /* Python function which checks the validity of a breakpoint object. */
  82. static PyObject *
  83. bppy_is_valid (PyObject *self, PyObject *args)
  84. {
  85. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  86. if (self_bp->bp)
  87. Py_RETURN_TRUE;
  88. Py_RETURN_FALSE;
  89. }
  90. /* Python function to test whether or not the breakpoint is enabled. */
  91. static PyObject *
  92. bppy_get_enabled (PyObject *self, void *closure)
  93. {
  94. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  95. BPPY_REQUIRE_VALID (self_bp);
  96. if (! self_bp->bp)
  97. Py_RETURN_FALSE;
  98. if (self_bp->bp->enable_state == bp_enabled)
  99. Py_RETURN_TRUE;
  100. Py_RETURN_FALSE;
  101. }
  102. /* Python function to test whether or not the breakpoint is silent. */
  103. static PyObject *
  104. bppy_get_silent (PyObject *self, void *closure)
  105. {
  106. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  107. BPPY_REQUIRE_VALID (self_bp);
  108. if (self_bp->bp->silent)
  109. Py_RETURN_TRUE;
  110. Py_RETURN_FALSE;
  111. }
  112. /* Python function to set the enabled state of a breakpoint. */
  113. static int
  114. bppy_set_enabled (PyObject *self, PyObject *newvalue, void *closure)
  115. {
  116. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  117. int cmp;
  118. BPPY_SET_REQUIRE_VALID (self_bp);
  119. if (newvalue == NULL)
  120. {
  121. PyErr_SetString (PyExc_TypeError,
  122. _("Cannot delete `enabled' attribute."));
  123. return -1;
  124. }
  125. else if (! PyBool_Check (newvalue))
  126. {
  127. PyErr_SetString (PyExc_TypeError,
  128. _("The value of `enabled' must be a boolean."));
  129. return -1;
  130. }
  131. cmp = PyObject_IsTrue (newvalue);
  132. if (cmp < 0)
  133. return -1;
  134. try
  135. {
  136. if (cmp == 1)
  137. enable_breakpoint (self_bp->bp);
  138. else
  139. disable_breakpoint (self_bp->bp);
  140. }
  141. catch (const gdb_exception &except)
  142. {
  143. GDB_PY_SET_HANDLE_EXCEPTION (except);
  144. }
  145. return 0;
  146. }
  147. /* Python function to set the 'silent' state of a breakpoint. */
  148. static int
  149. bppy_set_silent (PyObject *self, PyObject *newvalue, void *closure)
  150. {
  151. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  152. int cmp;
  153. BPPY_SET_REQUIRE_VALID (self_bp);
  154. if (newvalue == NULL)
  155. {
  156. PyErr_SetString (PyExc_TypeError,
  157. _("Cannot delete `silent' attribute."));
  158. return -1;
  159. }
  160. else if (! PyBool_Check (newvalue))
  161. {
  162. PyErr_SetString (PyExc_TypeError,
  163. _("The value of `silent' must be a boolean."));
  164. return -1;
  165. }
  166. cmp = PyObject_IsTrue (newvalue);
  167. if (cmp < 0)
  168. return -1;
  169. else
  170. breakpoint_set_silent (self_bp->bp, cmp);
  171. return 0;
  172. }
  173. /* Python function to set the thread of a breakpoint. */
  174. static int
  175. bppy_set_thread (PyObject *self, PyObject *newvalue, void *closure)
  176. {
  177. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  178. long id;
  179. BPPY_SET_REQUIRE_VALID (self_bp);
  180. if (newvalue == NULL)
  181. {
  182. PyErr_SetString (PyExc_TypeError,
  183. _("Cannot delete `thread' attribute."));
  184. return -1;
  185. }
  186. else if (PyLong_Check (newvalue))
  187. {
  188. if (! gdb_py_int_as_long (newvalue, &id))
  189. return -1;
  190. if (!valid_global_thread_id (id))
  191. {
  192. PyErr_SetString (PyExc_RuntimeError,
  193. _("Invalid thread ID."));
  194. return -1;
  195. }
  196. }
  197. else if (newvalue == Py_None)
  198. id = -1;
  199. else
  200. {
  201. PyErr_SetString (PyExc_TypeError,
  202. _("The value of `thread' must be an integer or None."));
  203. return -1;
  204. }
  205. breakpoint_set_thread (self_bp->bp, id);
  206. return 0;
  207. }
  208. /* Python function to set the (Ada) task of a breakpoint. */
  209. static int
  210. bppy_set_task (PyObject *self, PyObject *newvalue, void *closure)
  211. {
  212. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  213. long id;
  214. int valid_id = 0;
  215. BPPY_SET_REQUIRE_VALID (self_bp);
  216. if (newvalue == NULL)
  217. {
  218. PyErr_SetString (PyExc_TypeError,
  219. _("Cannot delete `task' attribute."));
  220. return -1;
  221. }
  222. else if (PyLong_Check (newvalue))
  223. {
  224. if (! gdb_py_int_as_long (newvalue, &id))
  225. return -1;
  226. try
  227. {
  228. valid_id = valid_task_id (id);
  229. }
  230. catch (const gdb_exception &except)
  231. {
  232. GDB_PY_SET_HANDLE_EXCEPTION (except);
  233. }
  234. if (! valid_id)
  235. {
  236. PyErr_SetString (PyExc_RuntimeError,
  237. _("Invalid task ID."));
  238. return -1;
  239. }
  240. }
  241. else if (newvalue == Py_None)
  242. id = 0;
  243. else
  244. {
  245. PyErr_SetString (PyExc_TypeError,
  246. _("The value of `task' must be an integer or None."));
  247. return -1;
  248. }
  249. breakpoint_set_task (self_bp->bp, id);
  250. return 0;
  251. }
  252. /* Python function which deletes the underlying GDB breakpoint. This
  253. triggers the breakpoint_deleted observer which will call
  254. gdbpy_breakpoint_deleted; that function cleans up the Python
  255. sections. */
  256. static PyObject *
  257. bppy_delete_breakpoint (PyObject *self, PyObject *args)
  258. {
  259. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  260. BPPY_REQUIRE_VALID (self_bp);
  261. try
  262. {
  263. delete_breakpoint (self_bp->bp);
  264. }
  265. catch (const gdb_exception &except)
  266. {
  267. GDB_PY_HANDLE_EXCEPTION (except);
  268. }
  269. Py_RETURN_NONE;
  270. }
  271. /* Python function to set the ignore count of a breakpoint. */
  272. static int
  273. bppy_set_ignore_count (PyObject *self, PyObject *newvalue, void *closure)
  274. {
  275. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  276. long value;
  277. BPPY_SET_REQUIRE_VALID (self_bp);
  278. if (newvalue == NULL)
  279. {
  280. PyErr_SetString (PyExc_TypeError,
  281. _("Cannot delete `ignore_count' attribute."));
  282. return -1;
  283. }
  284. else if (!PyLong_Check (newvalue))
  285. {
  286. PyErr_SetString (PyExc_TypeError,
  287. _("The value of `ignore_count' must be an integer."));
  288. return -1;
  289. }
  290. if (! gdb_py_int_as_long (newvalue, &value))
  291. return -1;
  292. if (value < 0)
  293. value = 0;
  294. try
  295. {
  296. set_ignore_count (self_bp->number, (int) value, 0);
  297. }
  298. catch (const gdb_exception &except)
  299. {
  300. GDB_PY_SET_HANDLE_EXCEPTION (except);
  301. }
  302. return 0;
  303. }
  304. /* Python function to set the hit count of a breakpoint. */
  305. static int
  306. bppy_set_hit_count (PyObject *self, PyObject *newvalue, void *closure)
  307. {
  308. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  309. BPPY_SET_REQUIRE_VALID (self_bp);
  310. if (newvalue == NULL)
  311. {
  312. PyErr_SetString (PyExc_TypeError,
  313. _("Cannot delete `hit_count' attribute."));
  314. return -1;
  315. }
  316. else
  317. {
  318. long value;
  319. if (! gdb_py_int_as_long (newvalue, &value))
  320. return -1;
  321. if (value != 0)
  322. {
  323. PyErr_SetString (PyExc_AttributeError,
  324. _("The value of `hit_count' must be zero."));
  325. return -1;
  326. }
  327. }
  328. self_bp->bp->hit_count = 0;
  329. return 0;
  330. }
  331. /* Python function to get the location of a breakpoint. */
  332. static PyObject *
  333. bppy_get_location (PyObject *self, void *closure)
  334. {
  335. gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
  336. BPPY_REQUIRE_VALID (obj);
  337. if (obj->bp->type != bp_breakpoint
  338. && obj->bp->type != bp_hardware_breakpoint)
  339. Py_RETURN_NONE;
  340. const char *str = event_location_to_string (obj->bp->location.get ());
  341. if (! str)
  342. str = "";
  343. return host_string_to_python_string (str).release ();
  344. }
  345. /* Python function to get the breakpoint expression. */
  346. static PyObject *
  347. bppy_get_expression (PyObject *self, void *closure)
  348. {
  349. const char *str;
  350. gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
  351. struct watchpoint *wp;
  352. BPPY_REQUIRE_VALID (obj);
  353. if (!is_watchpoint (obj->bp))
  354. Py_RETURN_NONE;
  355. wp = (struct watchpoint *) obj->bp;
  356. str = wp->exp_string.get ();
  357. if (! str)
  358. str = "";
  359. return host_string_to_python_string (str).release ();
  360. }
  361. /* Python function to get the condition expression of a breakpoint. */
  362. static PyObject *
  363. bppy_get_condition (PyObject *self, void *closure)
  364. {
  365. char *str;
  366. gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
  367. BPPY_REQUIRE_VALID (obj);
  368. str = obj->bp->cond_string.get ();
  369. if (! str)
  370. Py_RETURN_NONE;
  371. return host_string_to_python_string (str).release ();
  372. }
  373. /* Returns 0 on success. Returns -1 on error, with a python exception set.
  374. */
  375. static int
  376. bppy_set_condition (PyObject *self, PyObject *newvalue, void *closure)
  377. {
  378. gdb::unique_xmalloc_ptr<char> exp_holder;
  379. const char *exp = NULL;
  380. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  381. struct gdb_exception except;
  382. BPPY_SET_REQUIRE_VALID (self_bp);
  383. if (newvalue == NULL)
  384. {
  385. PyErr_SetString (PyExc_TypeError,
  386. _("Cannot delete `condition' attribute."));
  387. return -1;
  388. }
  389. else if (newvalue == Py_None)
  390. exp = "";
  391. else
  392. {
  393. exp_holder = python_string_to_host_string (newvalue);
  394. if (exp_holder == NULL)
  395. return -1;
  396. exp = exp_holder.get ();
  397. }
  398. try
  399. {
  400. set_breakpoint_condition (self_bp->bp, exp, 0, false);
  401. }
  402. catch (gdb_exception &ex)
  403. {
  404. except = std::move (ex);
  405. }
  406. GDB_PY_SET_HANDLE_EXCEPTION (except);
  407. return 0;
  408. }
  409. /* Python function to get the commands attached to a breakpoint. */
  410. static PyObject *
  411. bppy_get_commands (PyObject *self, void *closure)
  412. {
  413. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  414. struct breakpoint *bp = self_bp->bp;
  415. BPPY_REQUIRE_VALID (self_bp);
  416. if (! self_bp->bp->commands)
  417. Py_RETURN_NONE;
  418. string_file stb;
  419. current_uiout->redirect (&stb);
  420. try
  421. {
  422. print_command_lines (current_uiout, breakpoint_commands (bp), 0);
  423. }
  424. catch (const gdb_exception &except)
  425. {
  426. current_uiout->redirect (NULL);
  427. gdbpy_convert_exception (except);
  428. return NULL;
  429. }
  430. current_uiout->redirect (NULL);
  431. return host_string_to_python_string (stb.c_str ()).release ();
  432. }
  433. /* Set the commands attached to a breakpoint. Returns 0 on success.
  434. Returns -1 on error, with a python exception set. */
  435. static int
  436. bppy_set_commands (PyObject *self, PyObject *newvalue, void *closure)
  437. {
  438. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  439. struct gdb_exception except;
  440. BPPY_SET_REQUIRE_VALID (self_bp);
  441. gdb::unique_xmalloc_ptr<char> commands
  442. (python_string_to_host_string (newvalue));
  443. if (commands == nullptr)
  444. return -1;
  445. try
  446. {
  447. bool first = true;
  448. char *save_ptr = nullptr;
  449. auto reader
  450. = [&] ()
  451. {
  452. const char *result = strtok_r (first ? commands.get () : nullptr,
  453. "\n", &save_ptr);
  454. first = false;
  455. return result;
  456. };
  457. counted_command_line lines = read_command_lines_1 (reader, 1, nullptr);
  458. breakpoint_set_commands (self_bp->bp, std::move (lines));
  459. }
  460. catch (gdb_exception &ex)
  461. {
  462. except = std::move (ex);
  463. }
  464. GDB_PY_SET_HANDLE_EXCEPTION (except);
  465. return 0;
  466. }
  467. /* Python function to get the breakpoint type. */
  468. static PyObject *
  469. bppy_get_type (PyObject *self, void *closure)
  470. {
  471. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  472. BPPY_REQUIRE_VALID (self_bp);
  473. return gdb_py_object_from_longest (self_bp->bp->type).release ();
  474. }
  475. /* Python function to get the visibility of the breakpoint. */
  476. static PyObject *
  477. bppy_get_visibility (PyObject *self, void *closure)
  478. {
  479. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  480. BPPY_REQUIRE_VALID (self_bp);
  481. if (user_breakpoint_p (self_bp->bp))
  482. Py_RETURN_TRUE;
  483. Py_RETURN_FALSE;
  484. }
  485. /* Python function to determine if the breakpoint is a temporary
  486. breakpoint. */
  487. static PyObject *
  488. bppy_get_temporary (PyObject *self, void *closure)
  489. {
  490. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  491. BPPY_REQUIRE_VALID (self_bp);
  492. if (self_bp->bp->disposition == disp_del
  493. || self_bp->bp->disposition == disp_del_at_next_stop)
  494. Py_RETURN_TRUE;
  495. Py_RETURN_FALSE;
  496. }
  497. /* Python function to determine if the breakpoint is a pending
  498. breakpoint. */
  499. static PyObject *
  500. bppy_get_pending (PyObject *self, void *closure)
  501. {
  502. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  503. BPPY_REQUIRE_VALID (self_bp);
  504. if (is_watchpoint (self_bp->bp))
  505. Py_RETURN_FALSE;
  506. if (pending_breakpoint_p (self_bp->bp))
  507. Py_RETURN_TRUE;
  508. Py_RETURN_FALSE;
  509. }
  510. /* Python function to get the breakpoint's number. */
  511. static PyObject *
  512. bppy_get_number (PyObject *self, void *closure)
  513. {
  514. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  515. BPPY_REQUIRE_VALID (self_bp);
  516. return gdb_py_object_from_longest (self_bp->number).release ();
  517. }
  518. /* Python function to get the breakpoint's thread ID. */
  519. static PyObject *
  520. bppy_get_thread (PyObject *self, void *closure)
  521. {
  522. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  523. BPPY_REQUIRE_VALID (self_bp);
  524. if (self_bp->bp->thread == -1)
  525. Py_RETURN_NONE;
  526. return gdb_py_object_from_longest (self_bp->bp->thread).release ();
  527. }
  528. /* Python function to get the breakpoint's task ID (in Ada). */
  529. static PyObject *
  530. bppy_get_task (PyObject *self, void *closure)
  531. {
  532. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  533. BPPY_REQUIRE_VALID (self_bp);
  534. if (self_bp->bp->task == 0)
  535. Py_RETURN_NONE;
  536. return gdb_py_object_from_longest (self_bp->bp->task).release ();
  537. }
  538. /* Python function to get the breakpoint's hit count. */
  539. static PyObject *
  540. bppy_get_hit_count (PyObject *self, void *closure)
  541. {
  542. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  543. BPPY_REQUIRE_VALID (self_bp);
  544. return gdb_py_object_from_longest (self_bp->bp->hit_count).release ();
  545. }
  546. /* Python function to get the breakpoint's ignore count. */
  547. static PyObject *
  548. bppy_get_ignore_count (PyObject *self, void *closure)
  549. {
  550. gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  551. BPPY_REQUIRE_VALID (self_bp);
  552. return gdb_py_object_from_longest (self_bp->bp->ignore_count).release ();
  553. }
  554. /* Internal function to validate the Python parameters/keywords
  555. provided to bppy_init. */
  556. static int
  557. bppy_init_validate_args (const char *spec, char *source,
  558. char *function, char *label,
  559. char *line, enum bptype type)
  560. {
  561. /* If spec is defined, ensure that none of the explicit location
  562. keywords are also defined. */
  563. if (spec != NULL)
  564. {
  565. if (source != NULL || function != NULL || label != NULL || line != NULL)
  566. {
  567. PyErr_SetString (PyExc_RuntimeError,
  568. _("Breakpoints specified with spec cannot "
  569. "have source, function, label or line defined."));
  570. return -1;
  571. }
  572. }
  573. else
  574. {
  575. /* If spec isn't defined, ensure that the user is not trying to
  576. define a watchpoint with an explicit location. */
  577. if (type == bp_watchpoint)
  578. {
  579. PyErr_SetString (PyExc_RuntimeError,
  580. _("Watchpoints cannot be set by explicit "
  581. "location parameters."));
  582. return -1;
  583. }
  584. else
  585. {
  586. /* Otherwise, ensure some explicit locations are defined. */
  587. if (source == NULL && function == NULL && label == NULL
  588. && line == NULL)
  589. {
  590. PyErr_SetString (PyExc_RuntimeError,
  591. _("Neither spec nor explicit location set."));
  592. return -1;
  593. }
  594. /* Finally, if source is specified, ensure that line, label
  595. or function are specified too. */
  596. if (source != NULL && function == NULL && label == NULL
  597. && line == NULL)
  598. {
  599. PyErr_SetString (PyExc_RuntimeError,
  600. _("Specifying a source must also include a "
  601. "line, label or function."));
  602. return -1;
  603. }
  604. }
  605. }
  606. return 1;
  607. }
  608. /* Python function to create a new breakpoint. */
  609. static int
  610. bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
  611. {
  612. static const char *keywords[] = { "spec", "type", "wp_class", "internal",
  613. "temporary","source", "function",
  614. "label", "line", "qualified", NULL };
  615. const char *spec = NULL;
  616. enum bptype type = bp_breakpoint;
  617. int access_type = hw_write;
  618. PyObject *internal = NULL;
  619. PyObject *temporary = NULL;
  620. PyObject *lineobj = NULL;;
  621. int internal_bp = 0;
  622. int temporary_bp = 0;
  623. gdb::unique_xmalloc_ptr<char> line;
  624. char *label = NULL;
  625. char *source = NULL;
  626. char *function = NULL;
  627. PyObject * qualified = NULL;
  628. if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "|siiOOsssOO", keywords,
  629. &spec, &type, &access_type,
  630. &internal,
  631. &temporary, &source,
  632. &function, &label, &lineobj,
  633. &qualified))
  634. return -1;
  635. if (lineobj != NULL)
  636. {
  637. if (PyLong_Check (lineobj))
  638. line = xstrprintf ("%ld", PyLong_AsLong (lineobj));
  639. else if (PyUnicode_Check (lineobj))
  640. line = python_string_to_host_string (lineobj);
  641. else
  642. {
  643. PyErr_SetString (PyExc_RuntimeError,
  644. _("Line keyword should be an integer or a string. "));
  645. return -1;
  646. }
  647. }
  648. if (internal)
  649. {
  650. internal_bp = PyObject_IsTrue (internal);
  651. if (internal_bp == -1)
  652. return -1;
  653. }
  654. if (temporary != NULL)
  655. {
  656. temporary_bp = PyObject_IsTrue (temporary);
  657. if (temporary_bp == -1)
  658. return -1;
  659. }
  660. if (bppy_init_validate_args (spec, source, function, label, line.get (),
  661. type) == -1)
  662. return -1;
  663. bppy_pending_object = (gdbpy_breakpoint_object *) self;
  664. bppy_pending_object->number = -1;
  665. bppy_pending_object->bp = NULL;
  666. try
  667. {
  668. switch (type)
  669. {
  670. case bp_breakpoint:
  671. case bp_hardware_breakpoint:
  672. {
  673. event_location_up location;
  674. symbol_name_match_type func_name_match_type
  675. = (qualified != NULL && PyObject_IsTrue (qualified)
  676. ? symbol_name_match_type::FULL
  677. : symbol_name_match_type::WILD);
  678. if (spec != NULL)
  679. {
  680. gdb::unique_xmalloc_ptr<char>
  681. copy_holder (xstrdup (skip_spaces (spec)));
  682. const char *copy = copy_holder.get ();
  683. location = string_to_event_location (&copy,
  684. current_language,
  685. func_name_match_type);
  686. }
  687. else
  688. {
  689. struct explicit_location explicit_loc;
  690. initialize_explicit_location (&explicit_loc);
  691. explicit_loc.source_filename = source;
  692. explicit_loc.function_name = function;
  693. explicit_loc.label_name = label;
  694. if (line != NULL)
  695. explicit_loc.line_offset =
  696. linespec_parse_line_offset (line.get ());
  697. explicit_loc.func_name_match_type = func_name_match_type;
  698. location = new_explicit_location (&explicit_loc);
  699. }
  700. const struct breakpoint_ops *ops =
  701. breakpoint_ops_for_event_location (location.get (), false);
  702. create_breakpoint (gdbpy_enter::get_gdbarch (),
  703. location.get (), NULL, -1, NULL, false,
  704. 0,
  705. temporary_bp, type,
  706. 0,
  707. AUTO_BOOLEAN_TRUE,
  708. ops,
  709. 0, 1, internal_bp, 0);
  710. break;
  711. }
  712. case bp_watchpoint:
  713. {
  714. gdb::unique_xmalloc_ptr<char>
  715. copy_holder (xstrdup (skip_spaces (spec)));
  716. char *copy = copy_holder.get ();
  717. if (access_type == hw_write)
  718. watch_command_wrapper (copy, 0, internal_bp);
  719. else if (access_type == hw_access)
  720. awatch_command_wrapper (copy, 0, internal_bp);
  721. else if (access_type == hw_read)
  722. rwatch_command_wrapper (copy, 0, internal_bp);
  723. else
  724. error(_("Cannot understand watchpoint access type."));
  725. break;
  726. }
  727. case bp_catchpoint:
  728. error (_("BP_CATCHPOINT not supported"));
  729. default:
  730. error(_("Do not understand breakpoint type to set."));
  731. }
  732. }
  733. catch (const gdb_exception &except)
  734. {
  735. bppy_pending_object = NULL;
  736. gdbpy_convert_exception (except);
  737. return -1;
  738. }
  739. BPPY_SET_REQUIRE_VALID ((gdbpy_breakpoint_object *) self);
  740. return 0;
  741. }
  742. /* Append to LIST the breakpoint Python object associated to B.
  743. Return true on success. Return false on failure, with the Python error
  744. indicator set. */
  745. static bool
  746. build_bp_list (struct breakpoint *b, PyObject *list)
  747. {
  748. PyObject *bp = (PyObject *) b->py_bp_object;
  749. /* Not all breakpoints will have a companion Python object.
  750. Only breakpoints that were created via bppy_new, or
  751. breakpoints that were created externally and are tracked by
  752. the Python Scripting API. */
  753. if (bp == nullptr)
  754. return true;
  755. return PyList_Append (list, bp) == 0;
  756. }
  757. /* Static function to return a tuple holding all breakpoints. */
  758. PyObject *
  759. gdbpy_breakpoints (PyObject *self, PyObject *args)
  760. {
  761. if (bppy_live == 0)
  762. return PyTuple_New (0);
  763. gdbpy_ref<> list (PyList_New (0));
  764. if (list == NULL)
  765. return NULL;
  766. /* If build_bp_list returns false, it signals an error condition. In that
  767. case abandon building the list and return nullptr. */
  768. for (breakpoint *bp : all_breakpoints ())
  769. if (!build_bp_list (bp, list.get ()))
  770. return nullptr;
  771. return PyList_AsTuple (list.get ());
  772. }
  773. /* Call the "stop" method (if implemented) in the breakpoint
  774. class. If the method returns True, the inferior will be
  775. stopped at the breakpoint. Otherwise the inferior will be
  776. allowed to continue. */
  777. enum ext_lang_bp_stop
  778. gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn *extlang,
  779. struct breakpoint *b)
  780. {
  781. int stop;
  782. struct gdbpy_breakpoint_object *bp_obj = b->py_bp_object;
  783. PyObject *py_bp = (PyObject *) bp_obj;
  784. if (bp_obj == NULL)
  785. return EXT_LANG_BP_STOP_UNSET;
  786. stop = -1;
  787. gdbpy_enter enter_py (b->gdbarch);
  788. if (bp_obj->is_finish_bp)
  789. bpfinishpy_pre_stop_hook (bp_obj);
  790. if (PyObject_HasAttrString (py_bp, stop_func))
  791. {
  792. gdbpy_ref<> result (PyObject_CallMethod (py_bp, stop_func, NULL));
  793. stop = 1;
  794. if (result != NULL)
  795. {
  796. int evaluate = PyObject_IsTrue (result.get ());
  797. if (evaluate == -1)
  798. gdbpy_print_stack ();
  799. /* If the "stop" function returns False that means
  800. the Python breakpoint wants GDB to continue. */
  801. if (! evaluate)
  802. stop = 0;
  803. }
  804. else
  805. gdbpy_print_stack ();
  806. }
  807. if (bp_obj->is_finish_bp)
  808. bpfinishpy_post_stop_hook (bp_obj);
  809. if (stop < 0)
  810. return EXT_LANG_BP_STOP_UNSET;
  811. return stop ? EXT_LANG_BP_STOP_YES : EXT_LANG_BP_STOP_NO;
  812. }
  813. /* Checks if the "stop" method exists in this breakpoint.
  814. Used by condition_command to ensure mutual exclusion of breakpoint
  815. conditions. */
  816. int
  817. gdbpy_breakpoint_has_cond (const struct extension_language_defn *extlang,
  818. struct breakpoint *b)
  819. {
  820. PyObject *py_bp;
  821. if (b->py_bp_object == NULL)
  822. return 0;
  823. py_bp = (PyObject *) b->py_bp_object;
  824. gdbpy_enter enter_py (b->gdbarch);
  825. return PyObject_HasAttrString (py_bp, stop_func);
  826. }
  827. /* Event callback functions. */
  828. /* Callback that is used when a breakpoint is created. This function
  829. will create a new Python breakpoint object. */
  830. static void
  831. gdbpy_breakpoint_created (struct breakpoint *bp)
  832. {
  833. PYBP_SCOPED_DEBUG_ENTER_EXIT;
  834. gdbpy_breakpoint_object *newbp;
  835. if (!user_breakpoint_p (bp) && bppy_pending_object == NULL)
  836. {
  837. pybp_debug_printf ("not attaching python object to this breakpoint");
  838. return;
  839. }
  840. if (bp->type != bp_breakpoint
  841. && bp->type != bp_hardware_breakpoint
  842. && bp->type != bp_watchpoint
  843. && bp->type != bp_hardware_watchpoint
  844. && bp->type != bp_read_watchpoint
  845. && bp->type != bp_access_watchpoint
  846. && bp->type != bp_catchpoint)
  847. {
  848. pybp_debug_printf ("is not a breakpoint or watchpoint");
  849. return;
  850. }
  851. gdbpy_enter enter_py (bp->gdbarch);
  852. if (bppy_pending_object)
  853. {
  854. newbp = bppy_pending_object;
  855. Py_INCREF (newbp);
  856. bppy_pending_object = NULL;
  857. pybp_debug_printf ("attaching existing breakpoint object");
  858. }
  859. else
  860. {
  861. newbp = PyObject_New (gdbpy_breakpoint_object, &breakpoint_object_type);
  862. pybp_debug_printf ("attaching new breakpoint object");
  863. }
  864. if (newbp)
  865. {
  866. newbp->number = bp->number;
  867. newbp->bp = bp;
  868. newbp->bp->py_bp_object = newbp;
  869. newbp->is_finish_bp = 0;
  870. ++bppy_live;
  871. }
  872. else
  873. {
  874. PyErr_SetString (PyExc_RuntimeError,
  875. _("Error while creating breakpoint from GDB."));
  876. gdbpy_print_stack ();
  877. }
  878. if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_created))
  879. {
  880. if (evpy_emit_event ((PyObject *) newbp,
  881. gdb_py_events.breakpoint_created) < 0)
  882. gdbpy_print_stack ();
  883. }
  884. }
  885. /* Callback that is used when a breakpoint is deleted. This will
  886. invalidate the corresponding Python object. */
  887. static void
  888. gdbpy_breakpoint_deleted (struct breakpoint *b)
  889. {
  890. PYBP_SCOPED_DEBUG_ENTER_EXIT;
  891. int num = b->number;
  892. struct breakpoint *bp = NULL;
  893. bp = get_breakpoint (num);
  894. if (bp)
  895. {
  896. gdbpy_enter enter_py (b->gdbarch);
  897. gdbpy_ref<gdbpy_breakpoint_object> bp_obj (bp->py_bp_object);
  898. if (bp_obj != NULL)
  899. {
  900. if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted))
  901. {
  902. if (evpy_emit_event ((PyObject *) bp_obj.get (),
  903. gdb_py_events.breakpoint_deleted) < 0)
  904. gdbpy_print_stack ();
  905. }
  906. bp_obj->bp = NULL;
  907. --bppy_live;
  908. }
  909. }
  910. }
  911. /* Callback that is used when a breakpoint is modified. */
  912. static void
  913. gdbpy_breakpoint_modified (struct breakpoint *b)
  914. {
  915. PYBP_SCOPED_DEBUG_ENTER_EXIT;
  916. int num = b->number;
  917. struct breakpoint *bp = NULL;
  918. bp = get_breakpoint (num);
  919. if (bp)
  920. {
  921. gdbpy_enter enter_py (b->gdbarch);
  922. PyObject *bp_obj = (PyObject *) bp->py_bp_object;
  923. if (bp_obj)
  924. {
  925. if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_modified))
  926. {
  927. if (evpy_emit_event (bp_obj,
  928. gdb_py_events.breakpoint_modified) < 0)
  929. gdbpy_print_stack ();
  930. }
  931. }
  932. }
  933. }
  934. /* Initialize the Python breakpoint code. */
  935. int
  936. gdbpy_initialize_breakpoints (void)
  937. {
  938. int i;
  939. breakpoint_object_type.tp_new = PyType_GenericNew;
  940. if (PyType_Ready (&breakpoint_object_type) < 0)
  941. return -1;
  942. if (gdb_pymodule_addobject (gdb_module, "Breakpoint",
  943. (PyObject *) &breakpoint_object_type) < 0)
  944. return -1;
  945. gdb::observers::breakpoint_created.attach (gdbpy_breakpoint_created,
  946. "py-breakpoint");
  947. gdb::observers::breakpoint_deleted.attach (gdbpy_breakpoint_deleted,
  948. "py-breakpoint");
  949. gdb::observers::breakpoint_modified.attach (gdbpy_breakpoint_modified,
  950. "py-breakpoint");
  951. /* Add breakpoint types constants. */
  952. for (i = 0; pybp_codes[i].name; ++i)
  953. {
  954. if (PyModule_AddIntConstant (gdb_module, pybp_codes[i].name,
  955. pybp_codes[i].code) < 0)
  956. return -1;
  957. }
  958. /* Add watchpoint types constants. */
  959. for (i = 0; pybp_watch_types[i].name; ++i)
  960. {
  961. if (PyModule_AddIntConstant (gdb_module, pybp_watch_types[i].name,
  962. pybp_watch_types[i].code) < 0)
  963. return -1;
  964. }
  965. return 0;
  966. }
  967. /* Helper function that overrides this Python object's
  968. PyObject_GenericSetAttr to allow extra validation of the attribute
  969. being set. */
  970. static int
  971. local_setattro (PyObject *self, PyObject *name, PyObject *v)
  972. {
  973. gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
  974. gdb::unique_xmalloc_ptr<char> attr (python_string_to_host_string (name));
  975. if (attr == NULL)
  976. return -1;
  977. /* If the attribute trying to be set is the "stop" method,
  978. but we already have a condition set in the CLI or other extension
  979. language, disallow this operation. */
  980. if (strcmp (attr.get (), stop_func) == 0)
  981. {
  982. const struct extension_language_defn *extlang = NULL;
  983. if (obj->bp->cond_string != NULL)
  984. extlang = get_ext_lang_defn (EXT_LANG_GDB);
  985. if (extlang == NULL)
  986. extlang = get_breakpoint_cond_ext_lang (obj->bp, EXT_LANG_PYTHON);
  987. if (extlang != NULL)
  988. {
  989. std::string error_text
  990. = string_printf (_("Only one stop condition allowed. There is"
  991. " currently a %s stop condition defined for"
  992. " this breakpoint."),
  993. ext_lang_capitalized_name (extlang));
  994. PyErr_SetString (PyExc_RuntimeError, error_text.c_str ());
  995. return -1;
  996. }
  997. }
  998. return PyObject_GenericSetAttr (self, name, v);
  999. }
  1000. static gdb_PyGetSetDef breakpoint_object_getset[] = {
  1001. { "enabled", bppy_get_enabled, bppy_set_enabled,
  1002. "Boolean telling whether the breakpoint is enabled.", NULL },
  1003. { "silent", bppy_get_silent, bppy_set_silent,
  1004. "Boolean telling whether the breakpoint is silent.", NULL },
  1005. { "thread", bppy_get_thread, bppy_set_thread,
  1006. "Thread ID for the breakpoint.\n\
  1007. If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
  1008. If the value is None, then this breakpoint is not thread-specific.\n\
  1009. No other type of value can be used.", NULL },
  1010. { "task", bppy_get_task, bppy_set_task,
  1011. "Thread ID for the breakpoint.\n\
  1012. If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
  1013. If the value is None, then this breakpoint is not task-specific.\n\
  1014. No other type of value can be used.", NULL },
  1015. { "ignore_count", bppy_get_ignore_count, bppy_set_ignore_count,
  1016. "Number of times this breakpoint should be automatically continued.",
  1017. NULL },
  1018. { "number", bppy_get_number, NULL,
  1019. "Breakpoint's number assigned by GDB.", NULL },
  1020. { "hit_count", bppy_get_hit_count, bppy_set_hit_count,
  1021. "Number of times the breakpoint has been hit.\n\
  1022. Can be set to zero to clear the count. No other value is valid\n\
  1023. when setting this property.", NULL },
  1024. { "location", bppy_get_location, NULL,
  1025. "Location of the breakpoint, as specified by the user.", NULL},
  1026. { "expression", bppy_get_expression, NULL,
  1027. "Expression of the breakpoint, as specified by the user.", NULL},
  1028. { "condition", bppy_get_condition, bppy_set_condition,
  1029. "Condition of the breakpoint, as specified by the user,\
  1030. or None if no condition set."},
  1031. { "commands", bppy_get_commands, bppy_set_commands,
  1032. "Commands of the breakpoint, as specified by the user."},
  1033. { "type", bppy_get_type, NULL,
  1034. "Type of breakpoint."},
  1035. { "visible", bppy_get_visibility, NULL,
  1036. "Whether the breakpoint is visible to the user."},
  1037. { "temporary", bppy_get_temporary, NULL,
  1038. "Whether this breakpoint is a temporary breakpoint."},
  1039. { "pending", bppy_get_pending, NULL,
  1040. "Whether this breakpoint is a pending breakpoint."},
  1041. { NULL } /* Sentinel. */
  1042. };
  1043. static PyMethodDef breakpoint_object_methods[] =
  1044. {
  1045. { "is_valid", bppy_is_valid, METH_NOARGS,
  1046. "Return true if this breakpoint is valid, false if not." },
  1047. { "delete", bppy_delete_breakpoint, METH_NOARGS,
  1048. "Delete the underlying GDB breakpoint." },
  1049. { NULL } /* Sentinel. */
  1050. };
  1051. PyTypeObject breakpoint_object_type =
  1052. {
  1053. PyVarObject_HEAD_INIT (NULL, 0)
  1054. "gdb.Breakpoint", /*tp_name*/
  1055. sizeof (gdbpy_breakpoint_object), /*tp_basicsize*/
  1056. 0, /*tp_itemsize*/
  1057. 0, /*tp_dealloc*/
  1058. 0, /*tp_print*/
  1059. 0, /*tp_getattr*/
  1060. 0, /*tp_setattr*/
  1061. 0, /*tp_compare*/
  1062. 0, /*tp_repr*/
  1063. 0, /*tp_as_number*/
  1064. 0, /*tp_as_sequence*/
  1065. 0, /*tp_as_mapping*/
  1066. 0, /*tp_hash */
  1067. 0, /*tp_call*/
  1068. 0, /*tp_str*/
  1069. 0, /*tp_getattro*/
  1070. (setattrofunc)local_setattro, /*tp_setattro */
  1071. 0, /*tp_as_buffer*/
  1072. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
  1073. "GDB breakpoint object", /* tp_doc */
  1074. 0, /* tp_traverse */
  1075. 0, /* tp_clear */
  1076. 0, /* tp_richcompare */
  1077. 0, /* tp_weaklistoffset */
  1078. 0, /* tp_iter */
  1079. 0, /* tp_iternext */
  1080. breakpoint_object_methods, /* tp_methods */
  1081. 0, /* tp_members */
  1082. breakpoint_object_getset, /* tp_getset */
  1083. 0, /* tp_base */
  1084. 0, /* tp_dict */
  1085. 0, /* tp_descr_get */
  1086. 0, /* tp_descr_set */
  1087. 0, /* tp_dictoffset */
  1088. bppy_init, /* tp_init */
  1089. 0, /* tp_alloc */
  1090. };
  1091. void _initialize_py_breakpoint ();
  1092. void
  1093. _initialize_py_breakpoint ()
  1094. {
  1095. add_setshow_boolean_cmd
  1096. ("py-breakpoint", class_maintenance, &pybp_debug,
  1097. _("Set Python breakpoint debugging."),
  1098. _("Show Python breakpoint debugging."),
  1099. _("When on, Python breakpoint debugging is enabled."),
  1100. NULL,
  1101. show_pybp_debug,
  1102. &setdebuglist, &showdebuglist);
  1103. }