1 /* Python interface to breakpoints 2 3 Copyright (C) 2008-2023 Free Software Foundation, Inc. 4 5 This file is part of GDB. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 20 #include "defs.h" 21 #include "value.h" 22 #include "python-internal.h" 23 #include "python.h" 24 #include "charset.h" 25 #include "breakpoint.h" 26 #include "gdbcmd.h" 27 #include "gdbthread.h" 28 #include "observable.h" 29 #include "cli/cli-script.h" 30 #include "ada-lang.h" 31 #include "arch-utils.h" 32 #include "language.h" 33 #include "location.h" 34 #include "py-event.h" 35 #include "linespec.h" 36 37 extern PyTypeObject breakpoint_location_object_type 38 CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("breakpoint_location_object"); 39 40 struct gdbpy_breakpoint_location_object 41 { 42 PyObject_HEAD 43 44 /* An owning reference to the gdb breakpoint location object. */ 45 bp_location *bp_loc; 46 47 /* An owning reference to the location's breakpoint owner. */ 48 gdbpy_breakpoint_object *owner; 49 }; 50 51 /* Require that BREAKPOINT and LOCATION->OWNER are the same; throw a Python 52 exception if they are not. */ 53 #define BPLOCPY_REQUIRE_VALID(Breakpoint, Location) \ 54 do { \ 55 if ((Breakpoint)->bp != (Location)->bp_loc->owner) \ 56 return PyErr_Format (PyExc_RuntimeError, \ 57 _("Breakpoint location is invalid.")); \ 58 } while (0) 59 60 /* Require that BREAKPOINT and LOCATION->OWNER are the same; throw a Python 61 exception if they are not. This macro is for use in setter functions. */ 62 #define BPLOCPY_SET_REQUIRE_VALID(Breakpoint, Location) \ 63 do { \ 64 if ((Breakpoint)->bp != (Location)->bp_loc->owner) \ 65 { \ 66 PyErr_Format (PyExc_RuntimeError, \ 67 _("Breakpoint location is invalid.")); \ 68 return -1; \ 69 } \ 70 } while (0) 71 72 /* Debugging of Python breakpoints. */ 73 74 static bool pybp_debug; 75 76 /* Implementation of "show debug py-breakpoint". */ 77 78 static void 79 show_pybp_debug (struct ui_file *file, int from_tty, 80 struct cmd_list_element *c, const char *value) 81 { 82 gdb_printf (file, _("Python breakpoint debugging is %s.\n"), value); 83 } 84 85 /* Print a "py-breakpoint" debug statement. */ 86 87 #define pybp_debug_printf(fmt, ...) \ 88 debug_prefixed_printf_cond (pybp_debug, "py-breakpoint", fmt, ##__VA_ARGS__) 89 90 /* Print a "py-breakpoint" enter/exit debug statements. */ 91 92 #define PYBP_SCOPED_DEBUG_ENTER_EXIT \ 93 scoped_debug_enter_exit (pybp_debug, "py-breakpoint") 94 95 /* Number of live breakpoints. */ 96 static int bppy_live; 97 98 /* Variables used to pass information between the Breakpoint 99 constructor and the breakpoint-created hook function. */ 100 gdbpy_breakpoint_object *bppy_pending_object; 101 102 /* Function that is called when a Python condition is evaluated. */ 103 static const char stop_func[] = "stop"; 104 105 /* This is used to initialize various gdb.bp_* constants. */ 106 struct pybp_code 107 { 108 /* The name. */ 109 const char *name; 110 /* The code. */ 111 int code; 112 }; 113 114 /* Entries related to the type of user set breakpoints. */ 115 static struct pybp_code pybp_codes[] = 116 { 117 { "BP_NONE", bp_none}, 118 { "BP_BREAKPOINT", bp_breakpoint}, 119 { "BP_HARDWARE_BREAKPOINT", bp_hardware_breakpoint}, 120 { "BP_WATCHPOINT", bp_watchpoint}, 121 { "BP_HARDWARE_WATCHPOINT", bp_hardware_watchpoint}, 122 { "BP_READ_WATCHPOINT", bp_read_watchpoint}, 123 { "BP_ACCESS_WATCHPOINT", bp_access_watchpoint}, 124 { "BP_CATCHPOINT", bp_catchpoint}, 125 {NULL} /* Sentinel. */ 126 }; 127 128 /* Entries related to the type of watchpoint. */ 129 static struct pybp_code pybp_watch_types[] = 130 { 131 { "WP_READ", hw_read}, 132 { "WP_WRITE", hw_write}, 133 { "WP_ACCESS", hw_access}, 134 {NULL} /* Sentinel. */ 135 }; 136 137 /* Python function which checks the validity of a breakpoint object. */ 138 static PyObject * 139 bppy_is_valid (PyObject *self, PyObject *args) 140 { 141 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 142 143 if (self_bp->bp) 144 Py_RETURN_TRUE; 145 Py_RETURN_FALSE; 146 } 147 148 /* Python function to test whether or not the breakpoint is enabled. */ 149 static PyObject * 150 bppy_get_enabled (PyObject *self, void *closure) 151 { 152 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 153 154 BPPY_REQUIRE_VALID (self_bp); 155 if (! self_bp->bp) 156 Py_RETURN_FALSE; 157 if (self_bp->bp->enable_state == bp_enabled) 158 Py_RETURN_TRUE; 159 Py_RETURN_FALSE; 160 } 161 162 /* Python function to test whether or not the breakpoint is silent. */ 163 static PyObject * 164 bppy_get_silent (PyObject *self, void *closure) 165 { 166 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 167 168 BPPY_REQUIRE_VALID (self_bp); 169 if (self_bp->bp->silent) 170 Py_RETURN_TRUE; 171 Py_RETURN_FALSE; 172 } 173 174 /* Python function to set the enabled state of a breakpoint. */ 175 static int 176 bppy_set_enabled (PyObject *self, PyObject *newvalue, void *closure) 177 { 178 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 179 int cmp; 180 181 BPPY_SET_REQUIRE_VALID (self_bp); 182 183 if (newvalue == NULL) 184 { 185 PyErr_SetString (PyExc_TypeError, 186 _("Cannot delete `enabled' attribute.")); 187 188 return -1; 189 } 190 else if (! PyBool_Check (newvalue)) 191 { 192 PyErr_SetString (PyExc_TypeError, 193 _("The value of `enabled' must be a boolean.")); 194 return -1; 195 } 196 197 cmp = PyObject_IsTrue (newvalue); 198 if (cmp < 0) 199 return -1; 200 201 try 202 { 203 if (cmp == 1) 204 enable_breakpoint (self_bp->bp); 205 else 206 disable_breakpoint (self_bp->bp); 207 } 208 catch (const gdb_exception &except) 209 { 210 GDB_PY_SET_HANDLE_EXCEPTION (except); 211 } 212 213 return 0; 214 } 215 216 /* Python function to set the 'silent' state of a breakpoint. */ 217 static int 218 bppy_set_silent (PyObject *self, PyObject *newvalue, void *closure) 219 { 220 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 221 int cmp; 222 223 BPPY_SET_REQUIRE_VALID (self_bp); 224 225 if (newvalue == NULL) 226 { 227 PyErr_SetString (PyExc_TypeError, 228 _("Cannot delete `silent' attribute.")); 229 return -1; 230 } 231 else if (! PyBool_Check (newvalue)) 232 { 233 PyErr_SetString (PyExc_TypeError, 234 _("The value of `silent' must be a boolean.")); 235 return -1; 236 } 237 238 cmp = PyObject_IsTrue (newvalue); 239 if (cmp < 0) 240 return -1; 241 else 242 breakpoint_set_silent (self_bp->bp, cmp); 243 244 return 0; 245 } 246 247 /* Python function to set the thread of a breakpoint. */ 248 static int 249 bppy_set_thread (PyObject *self, PyObject *newvalue, void *closure) 250 { 251 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 252 long id; 253 254 BPPY_SET_REQUIRE_VALID (self_bp); 255 256 if (newvalue == NULL) 257 { 258 PyErr_SetString (PyExc_TypeError, 259 _("Cannot delete `thread' attribute.")); 260 return -1; 261 } 262 else if (PyLong_Check (newvalue)) 263 { 264 if (! gdb_py_int_as_long (newvalue, &id)) 265 return -1; 266 267 if (!valid_global_thread_id (id)) 268 { 269 PyErr_SetString (PyExc_RuntimeError, 270 _("Invalid thread ID.")); 271 return -1; 272 } 273 } 274 else if (newvalue == Py_None) 275 id = -1; 276 else 277 { 278 PyErr_SetString (PyExc_TypeError, 279 _("The value of `thread' must be an integer or None.")); 280 return -1; 281 } 282 283 breakpoint_set_thread (self_bp->bp, id); 284 285 return 0; 286 } 287 288 /* Python function to set the (Ada) task of a breakpoint. */ 289 static int 290 bppy_set_task (PyObject *self, PyObject *newvalue, void *closure) 291 { 292 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 293 long id; 294 int valid_id = 0; 295 296 BPPY_SET_REQUIRE_VALID (self_bp); 297 298 if (newvalue == NULL) 299 { 300 PyErr_SetString (PyExc_TypeError, 301 _("Cannot delete `task' attribute.")); 302 return -1; 303 } 304 else if (PyLong_Check (newvalue)) 305 { 306 if (! gdb_py_int_as_long (newvalue, &id)) 307 return -1; 308 309 try 310 { 311 valid_id = valid_task_id (id); 312 } 313 catch (const gdb_exception &except) 314 { 315 GDB_PY_SET_HANDLE_EXCEPTION (except); 316 } 317 318 if (! valid_id) 319 { 320 PyErr_SetString (PyExc_RuntimeError, 321 _("Invalid task ID.")); 322 return -1; 323 } 324 } 325 else if (newvalue == Py_None) 326 id = 0; 327 else 328 { 329 PyErr_SetString (PyExc_TypeError, 330 _("The value of `task' must be an integer or None.")); 331 return -1; 332 } 333 334 breakpoint_set_task (self_bp->bp, id); 335 336 return 0; 337 } 338 339 /* Python function which deletes the underlying GDB breakpoint. This 340 triggers the breakpoint_deleted observer which will call 341 gdbpy_breakpoint_deleted; that function cleans up the Python 342 sections. */ 343 344 static PyObject * 345 bppy_delete_breakpoint (PyObject *self, PyObject *args) 346 { 347 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 348 349 BPPY_REQUIRE_VALID (self_bp); 350 351 try 352 { 353 delete_breakpoint (self_bp->bp); 354 } 355 catch (const gdb_exception &except) 356 { 357 GDB_PY_HANDLE_EXCEPTION (except); 358 } 359 360 Py_RETURN_NONE; 361 } 362 363 364 /* Python function to set the ignore count of a breakpoint. */ 365 static int 366 bppy_set_ignore_count (PyObject *self, PyObject *newvalue, void *closure) 367 { 368 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 369 long value; 370 371 BPPY_SET_REQUIRE_VALID (self_bp); 372 373 if (newvalue == NULL) 374 { 375 PyErr_SetString (PyExc_TypeError, 376 _("Cannot delete `ignore_count' attribute.")); 377 return -1; 378 } 379 else if (!PyLong_Check (newvalue)) 380 { 381 PyErr_SetString (PyExc_TypeError, 382 _("The value of `ignore_count' must be an integer.")); 383 return -1; 384 } 385 386 if (! gdb_py_int_as_long (newvalue, &value)) 387 return -1; 388 389 if (value < 0) 390 value = 0; 391 392 try 393 { 394 set_ignore_count (self_bp->number, (int) value, 0); 395 } 396 catch (const gdb_exception &except) 397 { 398 GDB_PY_SET_HANDLE_EXCEPTION (except); 399 } 400 401 return 0; 402 } 403 404 /* Python function to set the hit count of a breakpoint. */ 405 static int 406 bppy_set_hit_count (PyObject *self, PyObject *newvalue, void *closure) 407 { 408 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 409 410 BPPY_SET_REQUIRE_VALID (self_bp); 411 412 if (newvalue == NULL) 413 { 414 PyErr_SetString (PyExc_TypeError, 415 _("Cannot delete `hit_count' attribute.")); 416 return -1; 417 } 418 else 419 { 420 long value; 421 422 if (! gdb_py_int_as_long (newvalue, &value)) 423 return -1; 424 425 if (value != 0) 426 { 427 PyErr_SetString (PyExc_AttributeError, 428 _("The value of `hit_count' must be zero.")); 429 return -1; 430 } 431 } 432 433 self_bp->bp->hit_count = 0; 434 435 return 0; 436 } 437 438 /* Python function to get the location of a breakpoint. */ 439 static PyObject * 440 bppy_get_location (PyObject *self, void *closure) 441 { 442 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self; 443 444 BPPY_REQUIRE_VALID (obj); 445 446 if (obj->bp->type != bp_breakpoint 447 && obj->bp->type != bp_hardware_breakpoint) 448 Py_RETURN_NONE; 449 450 const char *str = obj->bp->locspec->to_string (); 451 if (str == nullptr) 452 str = ""; 453 return host_string_to_python_string (str).release (); 454 } 455 456 /* Python function to get the breakpoint expression. */ 457 static PyObject * 458 bppy_get_expression (PyObject *self, void *closure) 459 { 460 const char *str; 461 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self; 462 struct watchpoint *wp; 463 464 BPPY_REQUIRE_VALID (obj); 465 466 if (!is_watchpoint (obj->bp)) 467 Py_RETURN_NONE; 468 469 wp = (struct watchpoint *) obj->bp; 470 471 str = wp->exp_string.get (); 472 if (! str) 473 str = ""; 474 475 return host_string_to_python_string (str).release (); 476 } 477 478 /* Python function to get the condition expression of a breakpoint. */ 479 static PyObject * 480 bppy_get_condition (PyObject *self, void *closure) 481 { 482 char *str; 483 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self; 484 485 BPPY_REQUIRE_VALID (obj); 486 487 str = obj->bp->cond_string.get (); 488 if (! str) 489 Py_RETURN_NONE; 490 491 return host_string_to_python_string (str).release (); 492 } 493 494 /* Returns 0 on success. Returns -1 on error, with a python exception set. 495 */ 496 497 static int 498 bppy_set_condition (PyObject *self, PyObject *newvalue, void *closure) 499 { 500 gdb::unique_xmalloc_ptr<char> exp_holder; 501 const char *exp = NULL; 502 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 503 struct gdb_exception except; 504 505 BPPY_SET_REQUIRE_VALID (self_bp); 506 507 if (newvalue == NULL) 508 { 509 PyErr_SetString (PyExc_TypeError, 510 _("Cannot delete `condition' attribute.")); 511 return -1; 512 } 513 else if (newvalue == Py_None) 514 exp = ""; 515 else 516 { 517 exp_holder = python_string_to_host_string (newvalue); 518 if (exp_holder == NULL) 519 return -1; 520 exp = exp_holder.get (); 521 } 522 523 try 524 { 525 set_breakpoint_condition (self_bp->bp, exp, 0, false); 526 } 527 catch (gdb_exception &ex) 528 { 529 except = std::move (ex); 530 } 531 532 GDB_PY_SET_HANDLE_EXCEPTION (except); 533 534 return 0; 535 } 536 537 /* Python function to get the commands attached to a breakpoint. */ 538 static PyObject * 539 bppy_get_commands (PyObject *self, void *closure) 540 { 541 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 542 struct breakpoint *bp = self_bp->bp; 543 544 BPPY_REQUIRE_VALID (self_bp); 545 546 if (! self_bp->bp->commands) 547 Py_RETURN_NONE; 548 549 string_file stb; 550 551 try 552 { 553 ui_out_redirect_pop redir (current_uiout, &stb); 554 print_command_lines (current_uiout, breakpoint_commands (bp), 0); 555 } 556 catch (const gdb_exception &except) 557 { 558 gdbpy_convert_exception (except); 559 return NULL; 560 } 561 562 return host_string_to_python_string (stb.c_str ()).release (); 563 } 564 565 /* Set the commands attached to a breakpoint. Returns 0 on success. 566 Returns -1 on error, with a python exception set. */ 567 static int 568 bppy_set_commands (PyObject *self, PyObject *newvalue, void *closure) 569 { 570 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 571 struct gdb_exception except; 572 573 BPPY_SET_REQUIRE_VALID (self_bp); 574 575 gdb::unique_xmalloc_ptr<char> commands 576 (python_string_to_host_string (newvalue)); 577 if (commands == nullptr) 578 return -1; 579 580 try 581 { 582 bool first = true; 583 char *save_ptr = nullptr; 584 auto reader 585 = [&] (std::string &buffer) 586 { 587 const char *result = strtok_r (first ? commands.get () : nullptr, 588 "\n", &save_ptr); 589 first = false; 590 return result; 591 }; 592 593 counted_command_line lines = read_command_lines_1 (reader, 1, nullptr); 594 breakpoint_set_commands (self_bp->bp, std::move (lines)); 595 } 596 catch (gdb_exception &ex) 597 { 598 except = std::move (ex); 599 } 600 601 GDB_PY_SET_HANDLE_EXCEPTION (except); 602 603 return 0; 604 } 605 606 /* Python function to get the breakpoint type. */ 607 static PyObject * 608 bppy_get_type (PyObject *self, void *closure) 609 { 610 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 611 612 BPPY_REQUIRE_VALID (self_bp); 613 614 return gdb_py_object_from_longest (self_bp->bp->type).release (); 615 } 616 617 /* Python function to get the visibility of the breakpoint. */ 618 619 static PyObject * 620 bppy_get_visibility (PyObject *self, void *closure) 621 { 622 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 623 624 BPPY_REQUIRE_VALID (self_bp); 625 626 if (user_breakpoint_p (self_bp->bp)) 627 Py_RETURN_TRUE; 628 629 Py_RETURN_FALSE; 630 } 631 632 /* Python function to determine if the breakpoint is a temporary 633 breakpoint. */ 634 635 static PyObject * 636 bppy_get_temporary (PyObject *self, void *closure) 637 { 638 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 639 640 BPPY_REQUIRE_VALID (self_bp); 641 642 if (self_bp->bp->disposition == disp_del 643 || self_bp->bp->disposition == disp_del_at_next_stop) 644 Py_RETURN_TRUE; 645 646 Py_RETURN_FALSE; 647 } 648 649 /* Python function to determine if the breakpoint is a pending 650 breakpoint. */ 651 652 static PyObject * 653 bppy_get_pending (PyObject *self, void *closure) 654 { 655 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 656 657 BPPY_REQUIRE_VALID (self_bp); 658 659 if (is_watchpoint (self_bp->bp)) 660 Py_RETURN_FALSE; 661 if (pending_breakpoint_p (self_bp->bp)) 662 Py_RETURN_TRUE; 663 664 Py_RETURN_FALSE; 665 } 666 667 /* Python function to get the breakpoint's number. */ 668 static PyObject * 669 bppy_get_number (PyObject *self, void *closure) 670 { 671 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 672 673 BPPY_REQUIRE_VALID (self_bp); 674 675 return gdb_py_object_from_longest (self_bp->number).release (); 676 } 677 678 /* Python function to get the breakpoint's thread ID. */ 679 static PyObject * 680 bppy_get_thread (PyObject *self, void *closure) 681 { 682 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 683 684 BPPY_REQUIRE_VALID (self_bp); 685 686 if (self_bp->bp->thread == -1) 687 Py_RETURN_NONE; 688 689 return gdb_py_object_from_longest (self_bp->bp->thread).release (); 690 } 691 692 /* Python function to get the breakpoint's task ID (in Ada). */ 693 static PyObject * 694 bppy_get_task (PyObject *self, void *closure) 695 { 696 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 697 698 BPPY_REQUIRE_VALID (self_bp); 699 700 if (self_bp->bp->task == 0) 701 Py_RETURN_NONE; 702 703 return gdb_py_object_from_longest (self_bp->bp->task).release (); 704 } 705 706 /* Python function to get the breakpoint's hit count. */ 707 static PyObject * 708 bppy_get_hit_count (PyObject *self, void *closure) 709 { 710 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 711 712 BPPY_REQUIRE_VALID (self_bp); 713 714 return gdb_py_object_from_longest (self_bp->bp->hit_count).release (); 715 } 716 717 /* Python function to get the breakpoint's ignore count. */ 718 static PyObject * 719 bppy_get_ignore_count (PyObject *self, void *closure) 720 { 721 gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self; 722 723 BPPY_REQUIRE_VALID (self_bp); 724 725 return gdb_py_object_from_longest (self_bp->bp->ignore_count).release (); 726 } 727 728 /* Python function to get the breakpoint locations of an owner breakpoint. */ 729 730 static PyObject * 731 bppy_get_locations (PyObject *self, void *closure) 732 { 733 using py_bploc_t = gdbpy_breakpoint_location_object; 734 auto *self_bp = (gdbpy_breakpoint_object *) self; 735 BPPY_REQUIRE_VALID (self_bp); 736 737 gdbpy_ref<> list (PyList_New (0)); 738 if (list == nullptr) 739 return nullptr; 740 741 for (bp_location *loc : self_bp->bp->locations ()) 742 { 743 gdbpy_ref<py_bploc_t> py_bploc 744 (PyObject_New (py_bploc_t, &breakpoint_location_object_type)); 745 if (py_bploc == nullptr) 746 return nullptr; 747 748 bp_location_ref_ptr ref = bp_location_ref_ptr::new_reference (loc); 749 /* The location takes a reference to the owner breakpoint. 750 Decrements when they are de-allocated in bplocpy_dealloc */ 751 Py_INCREF (self); 752 py_bploc->owner = self_bp; 753 py_bploc->bp_loc = ref.release (); 754 if (PyList_Append (list.get (), (PyObject *) py_bploc.get ()) != 0) 755 return nullptr; 756 } 757 return list.release (); 758 } 759 760 /* Internal function to validate the Python parameters/keywords 761 provided to bppy_init. */ 762 763 static int 764 bppy_init_validate_args (const char *spec, char *source, 765 char *function, char *label, 766 char *line, enum bptype type) 767 { 768 /* If spec is defined, ensure that none of the explicit location 769 keywords are also defined. */ 770 if (spec != NULL) 771 { 772 if (source != NULL || function != NULL || label != NULL || line != NULL) 773 { 774 PyErr_SetString (PyExc_RuntimeError, 775 _("Breakpoints specified with spec cannot " 776 "have source, function, label or line defined.")); 777 return -1; 778 } 779 } 780 else 781 { 782 /* If spec isn't defined, ensure that the user is not trying to 783 define a watchpoint with an explicit location. */ 784 if (type == bp_watchpoint) 785 { 786 PyErr_SetString (PyExc_RuntimeError, 787 _("Watchpoints cannot be set by explicit " 788 "location parameters.")); 789 return -1; 790 } 791 else 792 { 793 /* Otherwise, ensure some explicit locations are defined. */ 794 if (source == NULL && function == NULL && label == NULL 795 && line == NULL) 796 { 797 PyErr_SetString (PyExc_RuntimeError, 798 _("Neither spec nor explicit location set.")); 799 return -1; 800 } 801 /* Finally, if source is specified, ensure that line, label 802 or function are specified too. */ 803 if (source != NULL && function == NULL && label == NULL 804 && line == NULL) 805 { 806 PyErr_SetString (PyExc_RuntimeError, 807 _("Specifying a source must also include a " 808 "line, label or function.")); 809 return -1; 810 } 811 } 812 } 813 return 1; 814 } 815 816 /* Python function to create a new breakpoint. */ 817 static int 818 bppy_init (PyObject *self, PyObject *args, PyObject *kwargs) 819 { 820 static const char *keywords[] = { "spec", "type", "wp_class", "internal", 821 "temporary","source", "function", 822 "label", "line", "qualified", NULL }; 823 const char *spec = NULL; 824 enum bptype type = bp_breakpoint; 825 int access_type = hw_write; 826 PyObject *internal = NULL; 827 PyObject *temporary = NULL; 828 PyObject *lineobj = NULL;; 829 int internal_bp = 0; 830 int temporary_bp = 0; 831 gdb::unique_xmalloc_ptr<char> line; 832 char *label = NULL; 833 char *source = NULL; 834 char *function = NULL; 835 PyObject * qualified = NULL; 836 837 if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "|siiOOsssOO", keywords, 838 &spec, &type, &access_type, 839 &internal, 840 &temporary, &source, 841 &function, &label, &lineobj, 842 &qualified)) 843 return -1; 844 845 846 if (lineobj != NULL) 847 { 848 if (PyLong_Check (lineobj)) 849 line = xstrprintf ("%ld", PyLong_AsLong (lineobj)); 850 else if (PyUnicode_Check (lineobj)) 851 line = python_string_to_host_string (lineobj); 852 else 853 { 854 PyErr_SetString (PyExc_RuntimeError, 855 _("Line keyword should be an integer or a string. ")); 856 return -1; 857 } 858 } 859 860 if (internal) 861 { 862 internal_bp = PyObject_IsTrue (internal); 863 if (internal_bp == -1) 864 return -1; 865 } 866 867 if (temporary != NULL) 868 { 869 temporary_bp = PyObject_IsTrue (temporary); 870 if (temporary_bp == -1) 871 return -1; 872 } 873 874 if (bppy_init_validate_args (spec, source, function, label, line.get (), 875 type) == -1) 876 return -1; 877 878 bppy_pending_object = (gdbpy_breakpoint_object *) self; 879 bppy_pending_object->number = -1; 880 bppy_pending_object->bp = NULL; 881 882 try 883 { 884 switch (type) 885 { 886 case bp_breakpoint: 887 case bp_hardware_breakpoint: 888 { 889 location_spec_up locspec; 890 symbol_name_match_type func_name_match_type 891 = (qualified != NULL && PyObject_IsTrue (qualified) 892 ? symbol_name_match_type::FULL 893 : symbol_name_match_type::WILD); 894 895 if (spec != NULL) 896 { 897 gdb::unique_xmalloc_ptr<char> 898 copy_holder (xstrdup (skip_spaces (spec))); 899 const char *copy = copy_holder.get (); 900 901 locspec = string_to_location_spec (©, 902 current_language, 903 func_name_match_type); 904 } 905 else 906 { 907 std::unique_ptr<explicit_location_spec> explicit_loc 908 (new explicit_location_spec ()); 909 910 explicit_loc->source_filename 911 = source != nullptr ? xstrdup (source) : nullptr; 912 explicit_loc->function_name 913 = function != nullptr ? xstrdup (function) : nullptr; 914 explicit_loc->label_name 915 = label != nullptr ? xstrdup (label) : nullptr; 916 917 if (line != NULL) 918 explicit_loc->line_offset 919 = linespec_parse_line_offset (line.get ()); 920 921 explicit_loc->func_name_match_type = func_name_match_type; 922 923 locspec.reset (explicit_loc.release ()); 924 } 925 926 const struct breakpoint_ops *ops 927 = breakpoint_ops_for_location_spec (locspec.get (), false); 928 929 create_breakpoint (gdbpy_enter::get_gdbarch (), 930 locspec.get (), NULL, -1, NULL, false, 931 0, 932 temporary_bp, type, 933 0, 934 AUTO_BOOLEAN_TRUE, 935 ops, 936 0, 1, internal_bp, 0); 937 break; 938 } 939 case bp_watchpoint: 940 { 941 spec = skip_spaces (spec); 942 943 if (access_type == hw_write) 944 watch_command_wrapper (spec, 0, internal_bp); 945 else if (access_type == hw_access) 946 awatch_command_wrapper (spec, 0, internal_bp); 947 else if (access_type == hw_read) 948 rwatch_command_wrapper (spec, 0, internal_bp); 949 else 950 error(_("Cannot understand watchpoint access type.")); 951 break; 952 } 953 case bp_catchpoint: 954 error (_("BP_CATCHPOINT not supported")); 955 default: 956 error(_("Do not understand breakpoint type to set.")); 957 } 958 } 959 catch (const gdb_exception &except) 960 { 961 bppy_pending_object = NULL; 962 gdbpy_convert_exception (except); 963 return -1; 964 } 965 966 BPPY_SET_REQUIRE_VALID ((gdbpy_breakpoint_object *) self); 967 return 0; 968 } 969 970 /* Append to LIST the breakpoint Python object associated to B. 971 972 Return true on success. Return false on failure, with the Python error 973 indicator set. */ 974 975 static bool 976 build_bp_list (struct breakpoint *b, PyObject *list) 977 { 978 PyObject *bp = (PyObject *) b->py_bp_object; 979 980 /* Not all breakpoints will have a companion Python object. 981 Only breakpoints that were created via bppy_new, or 982 breakpoints that were created externally and are tracked by 983 the Python Scripting API. */ 984 if (bp == nullptr) 985 return true; 986 987 return PyList_Append (list, bp) == 0; 988 } 989 990 /* See python-internal.h. */ 991 992 bool 993 gdbpy_breakpoint_init_breakpoint_type () 994 { 995 if (breakpoint_object_type.tp_new == nullptr) 996 { 997 breakpoint_object_type.tp_new = PyType_GenericNew; 998 if (PyType_Ready (&breakpoint_object_type) < 0) 999 { 1000 /* Reset tp_new back to nullptr so future calls to this function 1001 will try calling PyType_Ready again. */ 1002 breakpoint_object_type.tp_new = nullptr; 1003 return false; 1004 } 1005 } 1006 1007 return true; 1008 } 1009 1010 /* Static function to return a tuple holding all breakpoints. */ 1011 1012 PyObject * 1013 gdbpy_breakpoints (PyObject *self, PyObject *args) 1014 { 1015 if (bppy_live == 0) 1016 return PyTuple_New (0); 1017 1018 gdbpy_ref<> list (PyList_New (0)); 1019 if (list == NULL) 1020 return NULL; 1021 1022 /* If build_bp_list returns false, it signals an error condition. In that 1023 case abandon building the list and return nullptr. */ 1024 for (breakpoint *bp : all_breakpoints ()) 1025 if (!build_bp_list (bp, list.get ())) 1026 return nullptr; 1027 1028 return PyList_AsTuple (list.get ()); 1029 } 1030 1031 /* Call the "stop" method (if implemented) in the breakpoint 1032 class. If the method returns True, the inferior will be 1033 stopped at the breakpoint. Otherwise the inferior will be 1034 allowed to continue. */ 1035 1036 enum ext_lang_bp_stop 1037 gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn *extlang, 1038 struct breakpoint *b) 1039 { 1040 int stop; 1041 struct gdbpy_breakpoint_object *bp_obj = b->py_bp_object; 1042 PyObject *py_bp = (PyObject *) bp_obj; 1043 1044 if (bp_obj == NULL) 1045 return EXT_LANG_BP_STOP_UNSET; 1046 1047 stop = -1; 1048 1049 gdbpy_enter enter_py (b->gdbarch); 1050 1051 if (bp_obj->is_finish_bp) 1052 bpfinishpy_pre_stop_hook (bp_obj); 1053 1054 if (PyObject_HasAttrString (py_bp, stop_func)) 1055 { 1056 gdbpy_ref<> result (PyObject_CallMethod (py_bp, stop_func, NULL)); 1057 1058 stop = 1; 1059 if (result != NULL) 1060 { 1061 int evaluate = PyObject_IsTrue (result.get ()); 1062 1063 if (evaluate == -1) 1064 gdbpy_print_stack (); 1065 1066 /* If the "stop" function returns False that means 1067 the Python breakpoint wants GDB to continue. */ 1068 if (! evaluate) 1069 stop = 0; 1070 } 1071 else 1072 gdbpy_print_stack (); 1073 } 1074 1075 if (bp_obj->is_finish_bp) 1076 bpfinishpy_post_stop_hook (bp_obj); 1077 1078 if (stop < 0) 1079 return EXT_LANG_BP_STOP_UNSET; 1080 return stop ? EXT_LANG_BP_STOP_YES : EXT_LANG_BP_STOP_NO; 1081 } 1082 1083 /* Checks if the "stop" method exists in this breakpoint. 1084 Used by condition_command to ensure mutual exclusion of breakpoint 1085 conditions. */ 1086 1087 int 1088 gdbpy_breakpoint_has_cond (const struct extension_language_defn *extlang, 1089 struct breakpoint *b) 1090 { 1091 PyObject *py_bp; 1092 1093 if (b->py_bp_object == NULL) 1094 return 0; 1095 1096 py_bp = (PyObject *) b->py_bp_object; 1097 1098 gdbpy_enter enter_py (b->gdbarch); 1099 return PyObject_HasAttrString (py_bp, stop_func); 1100 } 1101 1102 1103 1104 /* Event callback functions. */ 1105 1106 /* Callback that is used when a breakpoint is created. This function 1107 will create a new Python breakpoint object. */ 1108 static void 1109 gdbpy_breakpoint_created (struct breakpoint *bp) 1110 { 1111 PYBP_SCOPED_DEBUG_ENTER_EXIT; 1112 1113 gdbpy_breakpoint_object *newbp; 1114 1115 if (!user_breakpoint_p (bp) && bppy_pending_object == NULL) 1116 { 1117 pybp_debug_printf ("not attaching python object to this breakpoint"); 1118 return; 1119 } 1120 1121 if (bp->type != bp_breakpoint 1122 && bp->type != bp_hardware_breakpoint 1123 && bp->type != bp_watchpoint 1124 && bp->type != bp_hardware_watchpoint 1125 && bp->type != bp_read_watchpoint 1126 && bp->type != bp_access_watchpoint 1127 && bp->type != bp_catchpoint) 1128 { 1129 pybp_debug_printf ("is not a breakpoint or watchpoint"); 1130 return; 1131 } 1132 1133 gdbpy_enter enter_py (bp->gdbarch); 1134 1135 if (bppy_pending_object) 1136 { 1137 newbp = bppy_pending_object; 1138 Py_INCREF (newbp); 1139 bppy_pending_object = NULL; 1140 pybp_debug_printf ("attaching existing breakpoint object"); 1141 } 1142 else 1143 { 1144 newbp = PyObject_New (gdbpy_breakpoint_object, &breakpoint_object_type); 1145 pybp_debug_printf ("attaching new breakpoint object"); 1146 } 1147 if (newbp) 1148 { 1149 newbp->number = bp->number; 1150 newbp->bp = bp; 1151 newbp->bp->py_bp_object = newbp; 1152 newbp->is_finish_bp = 0; 1153 ++bppy_live; 1154 } 1155 else 1156 { 1157 PyErr_SetString (PyExc_RuntimeError, 1158 _("Error while creating breakpoint from GDB.")); 1159 gdbpy_print_stack (); 1160 } 1161 1162 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_created)) 1163 { 1164 if (evpy_emit_event ((PyObject *) newbp, 1165 gdb_py_events.breakpoint_created) < 0) 1166 gdbpy_print_stack (); 1167 } 1168 } 1169 1170 /* Callback that is used when a breakpoint is deleted. This will 1171 invalidate the corresponding Python object. */ 1172 static void 1173 gdbpy_breakpoint_deleted (struct breakpoint *b) 1174 { 1175 PYBP_SCOPED_DEBUG_ENTER_EXIT; 1176 1177 int num = b->number; 1178 struct breakpoint *bp = NULL; 1179 1180 bp = get_breakpoint (num); 1181 if (bp) 1182 { 1183 gdbpy_enter enter_py (b->gdbarch); 1184 1185 gdbpy_ref<gdbpy_breakpoint_object> bp_obj (bp->py_bp_object); 1186 if (bp_obj != NULL) 1187 { 1188 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted)) 1189 { 1190 if (evpy_emit_event ((PyObject *) bp_obj.get (), 1191 gdb_py_events.breakpoint_deleted) < 0) 1192 gdbpy_print_stack (); 1193 } 1194 1195 bp_obj->bp = NULL; 1196 --bppy_live; 1197 } 1198 } 1199 } 1200 1201 /* Callback that is used when a breakpoint is modified. */ 1202 1203 static void 1204 gdbpy_breakpoint_modified (struct breakpoint *b) 1205 { 1206 PYBP_SCOPED_DEBUG_ENTER_EXIT; 1207 1208 int num = b->number; 1209 struct breakpoint *bp = NULL; 1210 1211 bp = get_breakpoint (num); 1212 if (bp) 1213 { 1214 gdbpy_enter enter_py (b->gdbarch); 1215 1216 PyObject *bp_obj = (PyObject *) bp->py_bp_object; 1217 if (bp_obj) 1218 { 1219 if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_modified)) 1220 { 1221 if (evpy_emit_event (bp_obj, 1222 gdb_py_events.breakpoint_modified) < 0) 1223 gdbpy_print_stack (); 1224 } 1225 } 1226 } 1227 } 1228 1229 1230 1231 /* Initialize the Python breakpoint code. */ 1232 int 1233 gdbpy_initialize_breakpoints (void) 1234 { 1235 int i; 1236 1237 if (!gdbpy_breakpoint_init_breakpoint_type ()) 1238 return -1; 1239 1240 if (gdb_pymodule_addobject (gdb_module, "Breakpoint", 1241 (PyObject *) &breakpoint_object_type) < 0) 1242 return -1; 1243 1244 gdb::observers::breakpoint_created.attach (gdbpy_breakpoint_created, 1245 "py-breakpoint"); 1246 gdb::observers::breakpoint_deleted.attach (gdbpy_breakpoint_deleted, 1247 "py-breakpoint"); 1248 gdb::observers::breakpoint_modified.attach (gdbpy_breakpoint_modified, 1249 "py-breakpoint"); 1250 1251 /* Add breakpoint types constants. */ 1252 for (i = 0; pybp_codes[i].name; ++i) 1253 { 1254 if (PyModule_AddIntConstant (gdb_module, pybp_codes[i].name, 1255 pybp_codes[i].code) < 0) 1256 return -1; 1257 } 1258 1259 /* Add watchpoint types constants. */ 1260 for (i = 0; pybp_watch_types[i].name; ++i) 1261 { 1262 if (PyModule_AddIntConstant (gdb_module, pybp_watch_types[i].name, 1263 pybp_watch_types[i].code) < 0) 1264 return -1; 1265 } 1266 1267 return 0; 1268 } 1269 1270 /* Initialize the Python BreakpointLocation code. */ 1271 1272 int 1273 gdbpy_initialize_breakpoint_locations () 1274 { 1275 if (PyType_Ready (&breakpoint_location_object_type) < 0) 1276 return -1; 1277 1278 if (gdb_pymodule_addobject (gdb_module, "BreakpointLocation", 1279 (PyObject *) &breakpoint_location_object_type) 1280 < 0) 1281 return -1; 1282 return 0; 1283 } 1284 1285 1286 1287 /* Helper function that overrides this Python object's 1288 PyObject_GenericSetAttr to allow extra validation of the attribute 1289 being set. */ 1290 1291 static int 1292 local_setattro (PyObject *self, PyObject *name, PyObject *v) 1293 { 1294 gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self; 1295 gdb::unique_xmalloc_ptr<char> attr (python_string_to_host_string (name)); 1296 1297 if (attr == NULL) 1298 return -1; 1299 1300 /* If the attribute trying to be set is the "stop" method, 1301 but we already have a condition set in the CLI or other extension 1302 language, disallow this operation. */ 1303 if (strcmp (attr.get (), stop_func) == 0) 1304 { 1305 const struct extension_language_defn *extlang = NULL; 1306 1307 if (obj->bp->cond_string != NULL) 1308 extlang = get_ext_lang_defn (EXT_LANG_GDB); 1309 if (extlang == NULL) 1310 extlang = get_breakpoint_cond_ext_lang (obj->bp, EXT_LANG_PYTHON); 1311 if (extlang != NULL) 1312 { 1313 std::string error_text 1314 = string_printf (_("Only one stop condition allowed. There is" 1315 " currently a %s stop condition defined for" 1316 " this breakpoint."), 1317 ext_lang_capitalized_name (extlang)); 1318 PyErr_SetString (PyExc_RuntimeError, error_text.c_str ()); 1319 return -1; 1320 } 1321 } 1322 1323 return PyObject_GenericSetAttr (self, name, v); 1324 } 1325 1326 static gdb_PyGetSetDef breakpoint_object_getset[] = { 1327 { "enabled", bppy_get_enabled, bppy_set_enabled, 1328 "Boolean telling whether the breakpoint is enabled.", NULL }, 1329 { "silent", bppy_get_silent, bppy_set_silent, 1330 "Boolean telling whether the breakpoint is silent.", NULL }, 1331 { "thread", bppy_get_thread, bppy_set_thread, 1332 "Thread ID for the breakpoint.\n\ 1333 If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\ 1334 If the value is None, then this breakpoint is not thread-specific.\n\ 1335 No other type of value can be used.", NULL }, 1336 { "task", bppy_get_task, bppy_set_task, 1337 "Thread ID for the breakpoint.\n\ 1338 If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\ 1339 If the value is None, then this breakpoint is not task-specific.\n\ 1340 No other type of value can be used.", NULL }, 1341 { "ignore_count", bppy_get_ignore_count, bppy_set_ignore_count, 1342 "Number of times this breakpoint should be automatically continued.", 1343 NULL }, 1344 { "number", bppy_get_number, NULL, 1345 "Breakpoint's number assigned by GDB.", NULL }, 1346 { "hit_count", bppy_get_hit_count, bppy_set_hit_count, 1347 "Number of times the breakpoint has been hit.\n\ 1348 Can be set to zero to clear the count. No other value is valid\n\ 1349 when setting this property.", NULL }, 1350 { "location", bppy_get_location, NULL, 1351 "Location of the breakpoint, as specified by the user.", NULL}, 1352 { "expression", bppy_get_expression, NULL, 1353 "Expression of the breakpoint, as specified by the user.", NULL}, 1354 { "condition", bppy_get_condition, bppy_set_condition, 1355 "Condition of the breakpoint, as specified by the user,\ 1356 or None if no condition set."}, 1357 { "commands", bppy_get_commands, bppy_set_commands, 1358 "Commands of the breakpoint, as specified by the user."}, 1359 { "type", bppy_get_type, NULL, 1360 "Type of breakpoint."}, 1361 { "visible", bppy_get_visibility, NULL, 1362 "Whether the breakpoint is visible to the user."}, 1363 { "temporary", bppy_get_temporary, NULL, 1364 "Whether this breakpoint is a temporary breakpoint."}, 1365 { "pending", bppy_get_pending, NULL, 1366 "Whether this breakpoint is a pending breakpoint."}, 1367 { "locations", bppy_get_locations, NULL, 1368 "Get locations where this breakpoint was set"}, 1369 { NULL } /* Sentinel. */ 1370 }; 1371 1372 static PyMethodDef breakpoint_object_methods[] = 1373 { 1374 { "is_valid", bppy_is_valid, METH_NOARGS, 1375 "Return true if this breakpoint is valid, false if not." }, 1376 { "delete", bppy_delete_breakpoint, METH_NOARGS, 1377 "Delete the underlying GDB breakpoint." }, 1378 { NULL } /* Sentinel. */ 1379 }; 1380 1381 PyTypeObject breakpoint_object_type = 1382 { 1383 PyVarObject_HEAD_INIT (NULL, 0) 1384 "gdb.Breakpoint", /*tp_name*/ 1385 sizeof (gdbpy_breakpoint_object), /*tp_basicsize*/ 1386 0, /*tp_itemsize*/ 1387 0, /*tp_dealloc*/ 1388 0, /*tp_print*/ 1389 0, /*tp_getattr*/ 1390 0, /*tp_setattr*/ 1391 0, /*tp_compare*/ 1392 0, /*tp_repr*/ 1393 0, /*tp_as_number*/ 1394 0, /*tp_as_sequence*/ 1395 0, /*tp_as_mapping*/ 1396 0, /*tp_hash */ 1397 0, /*tp_call*/ 1398 0, /*tp_str*/ 1399 0, /*tp_getattro*/ 1400 (setattrofunc)local_setattro, /*tp_setattro */ 1401 0, /*tp_as_buffer*/ 1402 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ 1403 "GDB breakpoint object", /* tp_doc */ 1404 0, /* tp_traverse */ 1405 0, /* tp_clear */ 1406 0, /* tp_richcompare */ 1407 0, /* tp_weaklistoffset */ 1408 0, /* tp_iter */ 1409 0, /* tp_iternext */ 1410 breakpoint_object_methods, /* tp_methods */ 1411 0, /* tp_members */ 1412 breakpoint_object_getset, /* tp_getset */ 1413 0, /* tp_base */ 1414 0, /* tp_dict */ 1415 0, /* tp_descr_get */ 1416 0, /* tp_descr_set */ 1417 0, /* tp_dictoffset */ 1418 bppy_init, /* tp_init */ 1419 0, /* tp_alloc */ 1420 }; 1421 1422 void _initialize_py_breakpoint (); 1423 void 1424 _initialize_py_breakpoint () 1425 { 1426 add_setshow_boolean_cmd 1427 ("py-breakpoint", class_maintenance, &pybp_debug, 1428 _("Set Python breakpoint debugging."), 1429 _("Show Python breakpoint debugging."), 1430 _("When on, Python breakpoint debugging is enabled."), 1431 NULL, 1432 show_pybp_debug, 1433 &setdebuglist, &showdebuglist); 1434 } 1435 1436 /* Python function to set the enabled state of a breakpoint location. */ 1437 1438 static int 1439 bplocpy_set_enabled (PyObject *py_self, PyObject *newvalue, void *closure) 1440 { 1441 auto *self = (gdbpy_breakpoint_location_object *) py_self; 1442 BPPY_SET_REQUIRE_VALID (self->owner); 1443 BPLOCPY_SET_REQUIRE_VALID (self->owner, self); 1444 1445 if (newvalue == nullptr) 1446 { 1447 PyErr_SetString (PyExc_TypeError, 1448 _("Cannot delete 'enabled' attribute.")); 1449 return -1; 1450 } 1451 else if (!PyBool_Check (newvalue)) 1452 { 1453 PyErr_SetString (PyExc_TypeError, 1454 _("The value of 'enabled' must be a boolean.")); 1455 return -1; 1456 } 1457 1458 int cmp = PyObject_IsTrue (newvalue); 1459 if (cmp < 0) 1460 return -1; 1461 1462 try 1463 { 1464 enable_disable_bp_location (self->bp_loc, cmp == 1); 1465 } 1466 catch (const gdb_exception &except) 1467 { 1468 GDB_PY_SET_HANDLE_EXCEPTION (except); 1469 } 1470 return 0; 1471 } 1472 1473 /* Python function to test whether or not the breakpoint location is enabled. */ 1474 1475 static PyObject * 1476 bplocpy_get_enabled (PyObject *py_self, void *closure) 1477 { 1478 auto *self = (gdbpy_breakpoint_location_object *) py_self; 1479 BPPY_REQUIRE_VALID (self->owner); 1480 BPLOCPY_REQUIRE_VALID (self->owner, self); 1481 1482 if (self->bp_loc->enabled) 1483 Py_RETURN_TRUE; 1484 else 1485 Py_RETURN_FALSE; 1486 } 1487 1488 /* Python function to get address of breakpoint location. */ 1489 1490 static PyObject * 1491 bplocpy_get_address (PyObject *py_self, void *closure) 1492 { 1493 auto *self = (gdbpy_breakpoint_location_object *) py_self; 1494 BPPY_REQUIRE_VALID (self->owner); 1495 BPLOCPY_REQUIRE_VALID (self->owner, self); 1496 return gdb_py_object_from_ulongest (self->bp_loc->address).release (); 1497 } 1498 1499 /* Python function to get owner of breakpoint location, which 1500 is of type gdb.Breakpoint. */ 1501 1502 static PyObject * 1503 bplocpy_get_owner (PyObject *py_self, void *closure) 1504 { 1505 auto *self = (gdbpy_breakpoint_location_object *) py_self; 1506 BPPY_REQUIRE_VALID (self->owner); 1507 BPLOCPY_REQUIRE_VALID (self->owner, self); 1508 Py_INCREF (self->owner); 1509 return (PyObject *) self->owner; 1510 } 1511 1512 /* Python function to get the source file name path and line number 1513 where this breakpoint location was set. */ 1514 1515 static PyObject * 1516 bplocpy_get_source_location (PyObject *py_self, void *closure) 1517 { 1518 auto *self = (gdbpy_breakpoint_location_object *) py_self; 1519 BPPY_REQUIRE_VALID (self->owner); 1520 BPLOCPY_REQUIRE_VALID (self->owner, self); 1521 if (self->bp_loc->symtab) 1522 { 1523 gdbpy_ref<> tup (PyTuple_New (2)); 1524 if (tup == nullptr) 1525 return nullptr; 1526 /* symtab->filename is never NULL. */ 1527 gdbpy_ref<> filename 1528 = host_string_to_python_string (self->bp_loc->symtab->filename); 1529 if (filename == nullptr) 1530 return nullptr; 1531 auto line = gdb_py_object_from_ulongest (self->bp_loc->line_number); 1532 if (line == nullptr) 1533 return nullptr; 1534 if (PyTuple_SetItem (tup.get (), 0, filename.release ()) == -1 1535 || PyTuple_SetItem (tup.get (), 1, line.release ()) == -1) 1536 return nullptr; 1537 return tup.release (); 1538 } 1539 else 1540 Py_RETURN_NONE; 1541 } 1542 1543 /* Python function to get the function name of where this location was set. */ 1544 1545 static PyObject * 1546 bplocpy_get_function (PyObject *py_self, void *closure) 1547 { 1548 auto *self = (gdbpy_breakpoint_location_object *) py_self; 1549 BPPY_REQUIRE_VALID (self->owner); 1550 BPLOCPY_REQUIRE_VALID (self->owner, self); 1551 const auto fn_name = self->bp_loc->function_name.get (); 1552 if (fn_name != nullptr) 1553 return host_string_to_python_string (fn_name).release (); 1554 Py_RETURN_NONE; 1555 } 1556 1557 static PyObject * 1558 bplocpy_get_thread_groups (PyObject *py_self, void *closure) 1559 { 1560 auto *self = (gdbpy_breakpoint_location_object *) py_self; 1561 BPPY_REQUIRE_VALID (self->owner); 1562 BPLOCPY_REQUIRE_VALID (self->owner, self); 1563 gdbpy_ref<> list (PyList_New (0)); 1564 if (list == nullptr) 1565 return nullptr; 1566 for (inferior *inf : all_inferiors ()) 1567 { 1568 if (inf->pspace == self->bp_loc->pspace) 1569 { 1570 gdbpy_ref<> num = gdb_py_object_from_ulongest (inf->num); 1571 if (num == nullptr) 1572 return nullptr; 1573 if (PyList_Append (list.get (), num.release ()) != 0) 1574 return nullptr; 1575 } 1576 } 1577 return list.release (); 1578 } 1579 1580 static PyObject * 1581 bplocpy_get_fullname (PyObject *py_self, void *closure) 1582 { 1583 auto *self = (gdbpy_breakpoint_location_object *) py_self; 1584 BPPY_REQUIRE_VALID (self->owner); 1585 BPLOCPY_REQUIRE_VALID (self->owner, self); 1586 const auto symtab = self->bp_loc->symtab; 1587 if (symtab != nullptr && symtab->fullname != nullptr) 1588 { 1589 gdbpy_ref<> fullname 1590 = host_string_to_python_string (symtab->fullname); 1591 return fullname.release (); 1592 } 1593 Py_RETURN_NONE; 1594 } 1595 1596 /* De-allocation function to be called for the Python object. */ 1597 1598 static void 1599 bplocpy_dealloc (PyObject *py_self) 1600 { 1601 auto *self = (gdbpy_breakpoint_location_object *) py_self; 1602 bp_location_ref_ptr decrementing_ref {self->bp_loc}; 1603 Py_XDECREF (self->owner); 1604 Py_TYPE (py_self)->tp_free (py_self); 1605 } 1606 1607 /* Attribute get/set Python definitions. */ 1608 1609 static gdb_PyGetSetDef bp_location_object_getset[] = { 1610 { "enabled", bplocpy_get_enabled, bplocpy_set_enabled, 1611 "Boolean telling whether the breakpoint is enabled.", NULL }, 1612 { "owner", bplocpy_get_owner, NULL, 1613 "Get the breakpoint owner object", NULL }, 1614 { "address", bplocpy_get_address, NULL, 1615 "Get address of where this location was set", NULL}, 1616 { "source", bplocpy_get_source_location, NULL, 1617 "Get file and line number of where this location was set", NULL}, 1618 { "function", bplocpy_get_function, NULL, 1619 "Get function of where this location was set", NULL }, 1620 { "fullname", bplocpy_get_fullname, NULL, 1621 "Get fullname of where this location was set", NULL }, 1622 { "thread_groups", bplocpy_get_thread_groups, NULL, 1623 "Get thread groups where this location is in", NULL }, 1624 { NULL } /* Sentinel. */ 1625 }; 1626 1627 PyTypeObject breakpoint_location_object_type = 1628 { 1629 PyVarObject_HEAD_INIT (NULL, 0) 1630 "gdb.BreakpointLocation", /*tp_name*/ 1631 sizeof (gdbpy_breakpoint_location_object), /*tp_basicsize*/ 1632 0, /*tp_itemsize*/ 1633 bplocpy_dealloc, /*tp_dealloc*/ 1634 0, /*tp_print*/ 1635 0, /*tp_getattr*/ 1636 0, /*tp_setattr*/ 1637 0, /*tp_compare*/ 1638 0, /*tp_repr*/ 1639 0, /*tp_as_number*/ 1640 0, /*tp_as_sequence*/ 1641 0, /*tp_as_mapping*/ 1642 0, /*tp_hash */ 1643 0, /*tp_call*/ 1644 0, /*tp_str*/ 1645 0, /*tp_getattro*/ 1646 0, /*tp_setattro */ 1647 0, /*tp_as_buffer*/ 1648 Py_TPFLAGS_DEFAULT, /*tp_flags*/ 1649 "GDB breakpoint location object", /* tp_doc */ 1650 0, /* tp_traverse */ 1651 0, /* tp_clear */ 1652 0, /* tp_richcompare */ 1653 0, /* tp_weaklistoffset */ 1654 0, /* tp_iter */ 1655 0, /* tp_iternext */ 1656 0, /* tp_methods */ 1657 0, /* tp_members */ 1658 bp_location_object_getset, /* tp_getset */ 1659 0, /* tp_base */ 1660 0, /* tp_dict */ 1661 0, /* tp_descr_get */ 1662 0, /* tp_descr_set */ 1663 0, /* tp_dictoffset */ 1664 0, /* tp_init */ 1665 0, /* tp_alloc */ 1666 }; 1667