1194262Sjhb/* Python interface to breakpoints 2282067Sngie 3194262Sjhb Copyright (C) 2008, 2009, 2010, 2011 Free Software Foundation, Inc. 4194262Sjhb 5194262Sjhb This file is part of GDB. 6194262Sjhb 7194262Sjhb This program is free software; you can redistribute it and/or modify 8194262Sjhb it under the terms of the GNU General Public License as published by 9194262Sjhb the Free Software Foundation; either version 3 of the License, or 10194262Sjhb (at your option) any later version. 11194262Sjhb 12194262Sjhb This program is distributed in the hope that it will be useful, 13194262Sjhb but WITHOUT ANY WARRANTY; without even the implied warranty of 14194262Sjhb MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15194262Sjhb GNU General Public License for more details. 16194262Sjhb 17194262Sjhb You should have received a copy of the GNU General Public License 18194262Sjhb along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19194262Sjhb 20194262Sjhb#include "defs.h" 21194262Sjhb#include "value.h" 22194262Sjhb#include "exceptions.h" 23194262Sjhb#include "python-internal.h" 24194262Sjhb#include "charset.h" 25194262Sjhb#include "breakpoint.h" 26194262Sjhb#include "gdbcmd.h" 27194262Sjhb#include "gdbthread.h" 28194262Sjhb#include "observer.h" 29194262Sjhb#include "cli/cli-script.h" 30194262Sjhb#include "ada-lang.h" 31194262Sjhb#include "arch-utils.h" 32194262Sjhb#include "language.h" 33194262Sjhb 34194262Sjhbstatic PyTypeObject breakpoint_object_type; 35194262Sjhb 36194262Sjhb/* Number of live breakpoints. */ 37194262Sjhbstatic int bppy_live; 38194262Sjhb 39194262Sjhb/* Variables used to pass information between the Breakpoint 40194262Sjhb constructor and the breakpoint-created hook function. */ 41194262Sjhbstatic breakpoint_object *bppy_pending_object; 42281432Sngie 43194262Sjhb/* Function that is called when a Python condition is evaluated. */ 44194262Sjhbstatic char * const stop_func = "stop"; 45194262Sjhb 46194262Sjhbstruct breakpoint_object 47194262Sjhb{ 48194262Sjhb PyObject_HEAD 49194262Sjhb 50194262Sjhb /* The breakpoint number according to gdb. */ 51194262Sjhb int number; 52194262Sjhb 53194262Sjhb /* The gdb breakpoint object, or NULL if the breakpoint has been 54194262Sjhb deleted. */ 55194262Sjhb struct breakpoint *bp; 56194262Sjhb}; 57194262Sjhb 58194262Sjhb/* Require that BREAKPOINT be a valid breakpoint ID; throw a Python 59194262Sjhb exception if it is invalid. */ 60194262Sjhb#define BPPY_REQUIRE_VALID(Breakpoint) \ 61194262Sjhb do { \ 62194262Sjhb if ((Breakpoint)->bp == NULL) \ 63194262Sjhb return PyErr_Format (PyExc_RuntimeError, \ 64194262Sjhb _("Breakpoint %d is invalid."), \ 65194262Sjhb (Breakpoint)->number); \ 66194262Sjhb } while (0) 67194262Sjhb 68194262Sjhb/* Require that BREAKPOINT be a valid breakpoint ID; throw a Python 69194262Sjhb exception if it is invalid. This macro is for use in setter functions. */ 70194262Sjhb#define BPPY_SET_REQUIRE_VALID(Breakpoint) \ 71194262Sjhb do { \ 72194262Sjhb if ((Breakpoint)->bp == NULL) \ 73194262Sjhb { \ 74194262Sjhb PyErr_Format (PyExc_RuntimeError, _("Breakpoint %d is invalid."), \ 75194262Sjhb (Breakpoint)->number); \ 76194262Sjhb return -1; \ 77194262Sjhb } \ 78194262Sjhb } while (0) 79194262Sjhb 80194262Sjhb/* This is used to initialize various gdb.bp_* constants. */ 81194262Sjhbstruct pybp_code 82194262Sjhb{ 83194262Sjhb /* The name. */ 84194262Sjhb const char *name; 85194262Sjhb /* The code. */ 86194262Sjhb enum type_code code; 87194262Sjhb}; 88194262Sjhb 89194262Sjhb/* Entries related to the type of user set breakpoints. */ 90194262Sjhbstatic struct pybp_code pybp_codes[] = 91194262Sjhb{ 92194262Sjhb { "BP_NONE", bp_none}, 93194262Sjhb { "BP_BREAKPOINT", bp_breakpoint}, 94194262Sjhb { "BP_WATCHPOINT", bp_watchpoint}, 95194262Sjhb { "BP_HARDWARE_WATCHPOINT", bp_hardware_watchpoint}, 96194262Sjhb { "BP_READ_WATCHPOINT", bp_read_watchpoint}, 97194262Sjhb { "BP_ACCESS_WATCHPOINT", bp_access_watchpoint}, 98194262Sjhb {NULL} /* Sentinel. */ 99194262Sjhb}; 100194262Sjhb 101194262Sjhb/* Entries related to the type of watchpoint. */ 102194262Sjhbstatic struct pybp_code pybp_watch_types[] = 103194262Sjhb{ 104194262Sjhb { "WP_READ", hw_read}, 105194262Sjhb { "WP_WRITE", hw_write}, 106194262Sjhb { "WP_ACCESS", hw_access}, 107194262Sjhb {NULL} /* Sentinel. */ 108194262Sjhb}; 109194262Sjhb 110194262Sjhb/* Python function which checks the validity of a breakpoint object. */ 111194262Sjhbstatic PyObject * 112194262Sjhbbppy_is_valid (PyObject *self, PyObject *args) 113194262Sjhb{ 114194262Sjhb breakpoint_object *self_bp = (breakpoint_object *) self; 115194262Sjhb 116194262Sjhb if (self_bp->bp) 117194262Sjhb Py_RETURN_TRUE; 118194262Sjhb Py_RETURN_FALSE; 119194262Sjhb} 120194262Sjhb 121194262Sjhb/* Python function to test whether or not the breakpoint is enabled. */ 122194262Sjhbstatic PyObject * 123194262Sjhbbppy_get_enabled (PyObject *self, void *closure) 124194262Sjhb{ 125194262Sjhb breakpoint_object *self_bp = (breakpoint_object *) self; 126194262Sjhb 127194262Sjhb BPPY_REQUIRE_VALID (self_bp); 128194262Sjhb if (! self_bp->bp) 129194262Sjhb Py_RETURN_FALSE; 130194262Sjhb if (self_bp->bp->enable_state == bp_enabled) 131194262Sjhb Py_RETURN_TRUE; 132194262Sjhb Py_RETURN_FALSE; 133194262Sjhb} 134194262Sjhb 135194262Sjhb/* Python function to test whether or not the breakpoint is silent. */ 136281432Sngiestatic PyObject * 137194262Sjhbbppy_get_silent (PyObject *self, void *closure) 138281432Sngie{ 139194262Sjhb breakpoint_object *self_bp = (breakpoint_object *) self; 140194262Sjhb 141194262Sjhb BPPY_REQUIRE_VALID (self_bp); 142194262Sjhb if (self_bp->bp->silent) 143281432Sngie Py_RETURN_TRUE; 144194262Sjhb Py_RETURN_FALSE; 145194262Sjhb} 146194262Sjhb 147281432Sngie/* Python function to set the enabled state of a breakpoint. */ 148194262Sjhbstatic int 149194262Sjhbbppy_set_enabled (PyObject *self, PyObject *newvalue, void *closure) 150194262Sjhb{ 151194262Sjhb breakpoint_object *self_bp = (breakpoint_object *) self; 152281432Sngie int cmp; 153281432Sngie 154281432Sngie BPPY_SET_REQUIRE_VALID (self_bp); 155194262Sjhb 156194262Sjhb if (newvalue == NULL) 157194262Sjhb { 158194262Sjhb PyErr_SetString (PyExc_TypeError, 159281432Sngie _("Cannot delete `enabled' attribute.")); 160281432Sngie 161194262Sjhb return -1; 162194262Sjhb } 163194262Sjhb else if (! PyBool_Check (newvalue)) 164281432Sngie { 165194262Sjhb PyErr_SetString (PyExc_TypeError, 166281432Sngie _("The value of `enabled' must be a boolean.")); 167194262Sjhb return -1; 168194262Sjhb } 169194262Sjhb 170194262Sjhb cmp = PyObject_IsTrue (newvalue); 171194262Sjhb if (cmp < 0) 172194262Sjhb return -1; 173194262Sjhb else if (cmp == 1) 174281432Sngie enable_breakpoint (self_bp->bp); 175194262Sjhb else 176194262Sjhb disable_breakpoint (self_bp->bp); 177194262Sjhb return 0; 178194262Sjhb} 179194262Sjhb 180194262Sjhb/* Python function to set the 'silent' state of a breakpoint. */ 181194262Sjhbstatic int 182194262Sjhbbppy_set_silent (PyObject *self, PyObject *newvalue, void *closure) 183194262Sjhb{ 184194262Sjhb breakpoint_object *self_bp = (breakpoint_object *) self; 185194262Sjhb int cmp; 186194262Sjhb 187194262Sjhb BPPY_SET_REQUIRE_VALID (self_bp); 188194262Sjhb 189194262Sjhb if (newvalue == NULL) 190194262Sjhb { 191194262Sjhb PyErr_SetString (PyExc_TypeError, 192194262Sjhb _("Cannot delete `silent' attribute.")); 193194262Sjhb return -1; 194194262Sjhb } 195194262Sjhb else if (! PyBool_Check (newvalue)) 196194262Sjhb { 197194262Sjhb PyErr_SetString (PyExc_TypeError, 198194262Sjhb _("The value of `silent' must be a boolean.")); 199194262Sjhb return -1; 200194262Sjhb } 201194262Sjhb 202194262Sjhb cmp = PyObject_IsTrue (newvalue); 203194262Sjhb if (cmp < 0) 204194262Sjhb return -1; 205194262Sjhb else 206194262Sjhb breakpoint_set_silent (self_bp->bp, cmp); 207194262Sjhb 208194262Sjhb return 0; 209194262Sjhb} 210194262Sjhb 211194262Sjhb/* Python function to set the thread of a breakpoint. */ 212194262Sjhbstatic int 213194262Sjhbbppy_set_thread (PyObject *self, PyObject *newvalue, void *closure) 214194262Sjhb{ 215194262Sjhb breakpoint_object *self_bp = (breakpoint_object *) self; 216194262Sjhb long id; 217194262Sjhb 218194262Sjhb BPPY_SET_REQUIRE_VALID (self_bp); 219194262Sjhb 220194262Sjhb if (newvalue == NULL) 221194262Sjhb { 222194262Sjhb PyErr_SetString (PyExc_TypeError, 223194262Sjhb _("Cannot delete `thread' attribute.")); 224194262Sjhb return -1; 225194262Sjhb } 226194262Sjhb else if (PyInt_Check (newvalue)) 227194262Sjhb { 228194262Sjhb if (! gdb_py_int_as_long (newvalue, &id)) 229194262Sjhb return -1; 230194262Sjhb 231194262Sjhb if (! valid_thread_id (id)) 232194262Sjhb { 233194262Sjhb PyErr_SetString (PyExc_RuntimeError, 234194262Sjhb _("Invalid thread ID.")); 235194262Sjhb return -1; 236194262Sjhb } 237194262Sjhb } 238194262Sjhb else if (newvalue == Py_None) 239194262Sjhb id = -1; 240194262Sjhb else 241194262Sjhb { 242194262Sjhb PyErr_SetString (PyExc_TypeError, 243194262Sjhb _("The value of `thread' must be an integer or None.")); 244194262Sjhb return -1; 245194262Sjhb } 246194262Sjhb 247194262Sjhb breakpoint_set_thread (self_bp->bp, id); 248194262Sjhb 249194262Sjhb return 0; 250194262Sjhb} 251194262Sjhb 252194262Sjhb/* Python function to set the (Ada) task of a breakpoint. */ 253194262Sjhbstatic int 254194262Sjhbbppy_set_task (PyObject *self, PyObject *newvalue, void *closure) 255194262Sjhb{ 256194262Sjhb breakpoint_object *self_bp = (breakpoint_object *) self; 257194262Sjhb long id; 258194262Sjhb 259194262Sjhb BPPY_SET_REQUIRE_VALID (self_bp); 260194262Sjhb 261194262Sjhb if (newvalue == NULL) 262194262Sjhb { 263194262Sjhb PyErr_SetString (PyExc_TypeError, 264194262Sjhb _("Cannot delete `task' attribute.")); 265194262Sjhb return -1; 266194262Sjhb } 267194262Sjhb else if (PyInt_Check (newvalue)) 268194262Sjhb { 269194262Sjhb if (! gdb_py_int_as_long (newvalue, &id)) 270194262Sjhb return -1; 271194262Sjhb 272194262Sjhb if (! valid_task_id (id)) 273281432Sngie { 274194262Sjhb PyErr_SetString (PyExc_RuntimeError, 275194262Sjhb _("Invalid task ID.")); 276 return -1; 277 } 278 } 279 else if (newvalue == Py_None) 280 id = 0; 281 else 282 { 283 PyErr_SetString (PyExc_TypeError, 284 _("The value of `task' must be an integer or None.")); 285 return -1; 286 } 287 288 breakpoint_set_task (self_bp->bp, id); 289 290 return 0; 291} 292 293/* Python function which deletes the underlying GDB breakpoint. This 294 triggers the breakpoint_deleted observer which will call 295 gdbpy_breakpoint_deleted; that function cleans up the Python 296 sections. */ 297 298static PyObject * 299bppy_delete_breakpoint (PyObject *self, PyObject *args) 300{ 301 breakpoint_object *self_bp = (breakpoint_object *) self; 302 303 BPPY_REQUIRE_VALID (self_bp); 304 305 delete_breakpoint (self_bp->bp); 306 307 Py_RETURN_NONE; 308} 309 310 311/* Python function to set the ignore count of a breakpoint. */ 312static int 313bppy_set_ignore_count (PyObject *self, PyObject *newvalue, void *closure) 314{ 315 breakpoint_object *self_bp = (breakpoint_object *) self; 316 long value; 317 318 BPPY_SET_REQUIRE_VALID (self_bp); 319 320 if (newvalue == NULL) 321 { 322 PyErr_SetString (PyExc_TypeError, 323 _("Cannot delete `ignore_count' attribute.")); 324 return -1; 325 } 326 else if (! PyInt_Check (newvalue)) 327 { 328 PyErr_SetString (PyExc_TypeError, 329 _("The value of `ignore_count' must be an integer.")); 330 return -1; 331 } 332 333 if (! gdb_py_int_as_long (newvalue, &value)) 334 return -1; 335 336 if (value < 0) 337 value = 0; 338 set_ignore_count (self_bp->number, (int) value, 0); 339 340 return 0; 341} 342 343/* Python function to set the hit count of a breakpoint. */ 344static int 345bppy_set_hit_count (PyObject *self, PyObject *newvalue, void *closure) 346{ 347 breakpoint_object *self_bp = (breakpoint_object *) self; 348 349 BPPY_SET_REQUIRE_VALID (self_bp); 350 351 if (newvalue == NULL) 352 { 353 PyErr_SetString (PyExc_TypeError, 354 _("Cannot delete `hit_count' attribute.")); 355 return -1; 356 } 357 else 358 { 359 long value; 360 361 if (! gdb_py_int_as_long (newvalue, &value)) 362 return -1; 363 364 if (value != 0) 365 { 366 PyErr_SetString (PyExc_AttributeError, 367 _("The value of `hit_count' must be zero.")); 368 return -1; 369 } 370 } 371 372 self_bp->bp->hit_count = 0; 373 374 return 0; 375} 376 377/* Python function to get the location of a breakpoint. */ 378static PyObject * 379bppy_get_location (PyObject *self, void *closure) 380{ 381 char *str; 382 breakpoint_object *obj = (breakpoint_object *) self; 383 384 BPPY_REQUIRE_VALID (obj); 385 386 if (obj->bp->type != bp_breakpoint) 387 Py_RETURN_NONE; 388 389 str = obj->bp->addr_string; 390 391 if (! str) 392 str = ""; 393 return PyString_Decode (str, strlen (str), host_charset (), NULL); 394} 395 396/* Python function to get the breakpoint expression. */ 397static PyObject * 398bppy_get_expression (PyObject *self, void *closure) 399{ 400 char *str; 401 breakpoint_object *obj = (breakpoint_object *) self; 402 403 BPPY_REQUIRE_VALID (obj); 404 405 if (obj->bp->type != bp_watchpoint 406 && obj->bp->type != bp_hardware_watchpoint 407 && obj->bp->type != bp_read_watchpoint 408 && obj->bp->type != bp_access_watchpoint) 409 Py_RETURN_NONE; 410 411 str = obj->bp->exp_string; 412 if (! str) 413 str = ""; 414 415 return PyString_Decode (str, strlen (str), host_charset (), NULL); 416} 417 418/* Python function to get the condition expression of a breakpoint. */ 419static PyObject * 420bppy_get_condition (PyObject *self, void *closure) 421{ 422 char *str; 423 breakpoint_object *obj = (breakpoint_object *) self; 424 425 BPPY_REQUIRE_VALID (obj); 426 427 str = obj->bp->cond_string; 428 if (! str) 429 Py_RETURN_NONE; 430 431 return PyString_Decode (str, strlen (str), host_charset (), NULL); 432} 433 434/* Returns 0 on success. Returns -1 on error, with a python exception set. 435 */ 436 437static int 438bppy_set_condition (PyObject *self, PyObject *newvalue, void *closure) 439{ 440 char *exp; 441 breakpoint_object *self_bp = (breakpoint_object *) self; 442 volatile struct gdb_exception except; 443 444 BPPY_SET_REQUIRE_VALID (self_bp); 445 446 if (newvalue == NULL) 447 { 448 PyErr_SetString (PyExc_TypeError, 449 _("Cannot delete `condition' attribute.")); 450 return -1; 451 } 452 else if (newvalue == Py_None) 453 exp = ""; 454 else 455 { 456 exp = python_string_to_host_string (newvalue); 457 if (exp == NULL) 458 return -1; 459 } 460 461 TRY_CATCH (except, RETURN_MASK_ALL) 462 { 463 set_breakpoint_condition (self_bp->bp, exp, 0); 464 } 465 466 if (newvalue != Py_None) 467 xfree (exp); 468 469 GDB_PY_SET_HANDLE_EXCEPTION (except); 470 471 return 0; 472} 473 474/* Python function to get the commands attached to a breakpoint. */ 475static PyObject * 476bppy_get_commands (PyObject *self, void *closure) 477{ 478 breakpoint_object *self_bp = (breakpoint_object *) self; 479 struct breakpoint *bp = self_bp->bp; 480 long length; 481 volatile struct gdb_exception except; 482 struct ui_file *string_file; 483 struct cleanup *chain; 484 PyObject *result; 485 char *cmdstr; 486 487 BPPY_REQUIRE_VALID (self_bp); 488 489 if (! self_bp->bp->commands) 490 Py_RETURN_NONE; 491 492 string_file = mem_fileopen (); 493 chain = make_cleanup_ui_file_delete (string_file); 494 495 ui_out_redirect (uiout, string_file); 496 TRY_CATCH (except, RETURN_MASK_ALL) 497 { 498 print_command_lines (uiout, breakpoint_commands (bp), 0); 499 } 500 ui_out_redirect (uiout, NULL); 501 GDB_PY_HANDLE_EXCEPTION (except); 502 503 cmdstr = ui_file_xstrdup (string_file, &length); 504 make_cleanup (xfree, cmdstr); 505 result = PyString_Decode (cmdstr, strlen (cmdstr), host_charset (), NULL); 506 do_cleanups (chain); 507 return result; 508} 509 510/* Python function to get the breakpoint type. */ 511static PyObject * 512bppy_get_type (PyObject *self, void *closure) 513{ 514 breakpoint_object *self_bp = (breakpoint_object *) self; 515 516 BPPY_REQUIRE_VALID (self_bp); 517 518 return PyInt_FromLong (self_bp->bp->type); 519} 520 521/* Python function to get the visibility of the breakpoint. */ 522 523static PyObject * 524bppy_get_visibility (PyObject *self, void *closure) 525{ 526 breakpoint_object *self_bp = (breakpoint_object *) self; 527 528 BPPY_REQUIRE_VALID (self_bp); 529 530 if (self_bp->bp->number < 0) 531 Py_RETURN_FALSE; 532 533 Py_RETURN_TRUE; 534} 535 536/* Python function to get the breakpoint's number. */ 537static PyObject * 538bppy_get_number (PyObject *self, void *closure) 539{ 540 breakpoint_object *self_bp = (breakpoint_object *) self; 541 542 BPPY_REQUIRE_VALID (self_bp); 543 544 return PyInt_FromLong (self_bp->number); 545} 546 547/* Python function to get the breakpoint's thread ID. */ 548static PyObject * 549bppy_get_thread (PyObject *self, void *closure) 550{ 551 breakpoint_object *self_bp = (breakpoint_object *) self; 552 553 BPPY_REQUIRE_VALID (self_bp); 554 555 if (self_bp->bp->thread == -1) 556 Py_RETURN_NONE; 557 558 return PyInt_FromLong (self_bp->bp->thread); 559} 560 561/* Python function to get the breakpoint's task ID (in Ada). */ 562static PyObject * 563bppy_get_task (PyObject *self, void *closure) 564{ 565 breakpoint_object *self_bp = (breakpoint_object *) self; 566 567 BPPY_REQUIRE_VALID (self_bp); 568 569 if (self_bp->bp->task == 0) 570 Py_RETURN_NONE; 571 572 return PyInt_FromLong (self_bp->bp->task); 573} 574 575/* Python function to get the breakpoint's hit count. */ 576static PyObject * 577bppy_get_hit_count (PyObject *self, void *closure) 578{ 579 breakpoint_object *self_bp = (breakpoint_object *) self; 580 581 BPPY_REQUIRE_VALID (self_bp); 582 583 return PyInt_FromLong (self_bp->bp->hit_count); 584} 585 586/* Python function to get the breakpoint's ignore count. */ 587static PyObject * 588bppy_get_ignore_count (PyObject *self, void *closure) 589{ 590 breakpoint_object *self_bp = (breakpoint_object *) self; 591 592 BPPY_REQUIRE_VALID (self_bp); 593 594 return PyInt_FromLong (self_bp->bp->ignore_count); 595} 596 597/* Python function to create a new breakpoint. */ 598static int 599bppy_init (PyObject *self, PyObject *args, PyObject *kwargs) 600{ 601 static char *keywords[] = { "spec", "type", "wp_class", "internal", NULL }; 602 char *spec; 603 int type = bp_breakpoint; 604 int access_type = hw_write; 605 PyObject *internal = NULL; 606 int internal_bp = 0; 607 volatile struct gdb_exception except; 608 609 if (! PyArg_ParseTupleAndKeywords (args, kwargs, "s|iiO", keywords, 610 &spec, &type, &access_type, &internal)) 611 return -1; 612 613 if (internal) 614 { 615 internal_bp = PyObject_IsTrue (internal); 616 if (internal_bp == -1) 617 return -1; 618 } 619 620 bppy_pending_object = (breakpoint_object *) self; 621 bppy_pending_object->number = -1; 622 bppy_pending_object->bp = NULL; 623 624 TRY_CATCH (except, RETURN_MASK_ALL) 625 { 626 switch (type) 627 { 628 case bp_breakpoint: 629 { 630 create_breakpoint (python_gdbarch, 631 spec, NULL, -1, 632 0, 633 0, bp_breakpoint, 634 0, 635 AUTO_BOOLEAN_TRUE, 636 NULL, 0, 1, internal_bp); 637 break; 638 } 639 case bp_watchpoint: 640 { 641 if (access_type == hw_write) 642 watch_command_wrapper (spec, 0, internal_bp); 643 else if (access_type == hw_access) 644 awatch_command_wrapper (spec, 0, internal_bp); 645 else if (access_type == hw_read) 646 rwatch_command_wrapper (spec, 0, internal_bp); 647 else 648 error(_("Cannot understand watchpoint access type.")); 649 break; 650 } 651 default: 652 error(_("Do not understand breakpoint type to set.")); 653 } 654 } 655 if (except.reason < 0) 656 { 657 PyErr_Format (except.reason == RETURN_QUIT 658 ? PyExc_KeyboardInterrupt : PyExc_RuntimeError, 659 "%s", except.message); 660 return -1; 661 } 662 663 BPPY_SET_REQUIRE_VALID ((breakpoint_object *) self); 664 return 0; 665} 666 667 668 669static int 670build_bp_list (struct breakpoint *b, void *arg) 671{ 672 PyObject *list = arg; 673 PyObject *bp = (PyObject *) b->py_bp_object; 674 int iserr = 0; 675 676 /* Not all breakpoints will have a companion Python object. 677 Only breakpoints that were created via bppy_new, or 678 breakpoints that were created externally and are tracked by 679 the Python Scripting API. */ 680 if (bp) 681 iserr = PyList_Append (list, bp); 682 683 if (iserr == -1) 684 return 1; 685 686 return 0; 687} 688 689/* Static function to return a tuple holding all breakpoints. */ 690 691PyObject * 692gdbpy_breakpoints (PyObject *self, PyObject *args) 693{ 694 PyObject *list; 695 696 if (bppy_live == 0) 697 Py_RETURN_NONE; 698 699 list = PyList_New (0); 700 if (!list) 701 return NULL; 702 703 /* If iteratre_over_breakpoints returns non NULL it signals an error 704 condition. In that case abandon building the list and return 705 NULL. */ 706 if (iterate_over_breakpoints (build_bp_list, list) != NULL) 707 { 708 Py_DECREF (list); 709 return NULL; 710 } 711 712 return PyList_AsTuple (list); 713} 714 715/* Call the "stop" method (if implemented) in the breakpoint 716 class. If the method returns True, the inferior will be 717 stopped at the breakpoint. Otherwise the inferior will be 718 allowed to continue. */ 719 720int 721gdbpy_should_stop (struct breakpoint_object *bp_obj) 722{ 723 int stop = 1; 724 725 PyObject *py_bp = (PyObject *) bp_obj; 726 struct breakpoint *b = bp_obj->bp; 727 struct gdbarch *garch = b->gdbarch ? b->gdbarch : get_current_arch (); 728 struct cleanup *cleanup = ensure_python_env (garch, current_language); 729 730 if (PyObject_HasAttrString (py_bp, stop_func)) 731 { 732 PyObject *result = PyObject_CallMethod (py_bp, stop_func, NULL); 733 734 if (result) 735 { 736 int evaluate = PyObject_IsTrue (result); 737 738 if (evaluate == -1) 739 gdbpy_print_stack (); 740 741 /* If the "stop" function returns False that means 742 the Python breakpoint wants GDB to continue. */ 743 if (! evaluate) 744 stop = 0; 745 746 Py_DECREF (result); 747 } 748 else 749 gdbpy_print_stack (); 750 } 751 do_cleanups (cleanup); 752 753 return stop; 754} 755 756/* Checks if the "stop" method exists in this breakpoint. 757 Used by condition_command to ensure mutual exclusion of breakpoint 758 conditions. */ 759 760int 761gdbpy_breakpoint_has_py_cond (struct breakpoint_object *bp_obj) 762{ 763 int has_func = 0; 764 PyObject *py_bp = (PyObject *) bp_obj; 765 struct gdbarch *garch = bp_obj->bp->gdbarch ? bp_obj->bp->gdbarch : 766 get_current_arch (); 767 struct cleanup *cleanup = ensure_python_env (garch, current_language); 768 769 if (py_bp == NULL) 770 return 0; 771 772 has_func = PyObject_HasAttrString (py_bp, stop_func); 773 do_cleanups (cleanup); 774 775 return has_func; 776} 777 778 779 780/* Event callback functions. */ 781 782/* Callback that is used when a breakpoint is created. This function 783 will create a new Python breakpoint object. */ 784static void 785gdbpy_breakpoint_created (int num) 786{ 787 breakpoint_object *newbp; 788 struct breakpoint *bp = NULL; 789 PyGILState_STATE state; 790 791 bp = get_breakpoint (num); 792 if (! bp) 793 return; 794 795 if (num < 0 && bppy_pending_object == NULL) 796 return; 797 798 if (bp->type != bp_breakpoint 799 && bp->type != bp_watchpoint 800 && bp->type != bp_hardware_watchpoint 801 && bp->type != bp_read_watchpoint 802 && bp->type != bp_access_watchpoint) 803 return; 804 805 state = PyGILState_Ensure (); 806 807 if (bppy_pending_object) 808 { 809 newbp = bppy_pending_object; 810 bppy_pending_object = NULL; 811 } 812 else 813 newbp = PyObject_New (breakpoint_object, &breakpoint_object_type); 814 if (newbp) 815 { 816 newbp->number = num; 817 newbp->bp = bp; 818 newbp->bp->py_bp_object = newbp; 819 Py_INCREF (newbp); 820 ++bppy_live; 821 } 822 else 823 { 824 PyErr_SetString (PyExc_RuntimeError, 825 _("Error while creating breakpoint from GDB.")); 826 gdbpy_print_stack (); 827 } 828 829 PyGILState_Release (state); 830} 831 832/* Callback that is used when a breakpoint is deleted. This will 833 invalidate the corresponding Python object. */ 834static void 835gdbpy_breakpoint_deleted (int num) 836{ 837 PyGILState_STATE state; 838 struct breakpoint *bp = NULL; 839 breakpoint_object *bp_obj; 840 841 state = PyGILState_Ensure (); 842 bp = get_breakpoint (num); 843 if (! bp) 844 return; 845 846 bp_obj = bp->py_bp_object; 847 if (bp_obj) 848 { 849 bp_obj->bp = NULL; 850 --bppy_live; 851 Py_DECREF (bp_obj); 852 } 853 PyGILState_Release (state); 854} 855 856 857 858/* Initialize the Python breakpoint code. */ 859void 860gdbpy_initialize_breakpoints (void) 861{ 862 int i; 863 864 if (PyType_Ready (&breakpoint_object_type) < 0) 865 return; 866 867 Py_INCREF (&breakpoint_object_type); 868 PyModule_AddObject (gdb_module, "Breakpoint", 869 (PyObject *) &breakpoint_object_type); 870 871 observer_attach_breakpoint_created (gdbpy_breakpoint_created); 872 observer_attach_breakpoint_deleted (gdbpy_breakpoint_deleted); 873 874 /* Add breakpoint types constants. */ 875 for (i = 0; pybp_codes[i].name; ++i) 876 { 877 if (PyModule_AddIntConstant (gdb_module, 878 /* Cast needed for Python 2.4. */ 879 (char *) pybp_codes[i].name, 880 pybp_codes[i].code) < 0) 881 return; 882 } 883 884 /* Add watchpoint types constants. */ 885 for (i = 0; pybp_watch_types[i].name; ++i) 886 { 887 if (PyModule_AddIntConstant (gdb_module, 888 /* Cast needed for Python 2.4. */ 889 (char *) pybp_watch_types[i].name, 890 pybp_watch_types[i].code) < 0) 891 return; 892 } 893 894} 895 896 897 898/* Helper function that overrides this Python object's 899 PyObject_GenericSetAttr to allow extra validation of the attribute 900 being set. */ 901 902static int 903local_setattro (PyObject *self, PyObject *name, PyObject *v) 904{ 905 breakpoint_object *obj = (breakpoint_object *) self; 906 char *attr = python_string_to_host_string (name); 907 908 if (attr == NULL) 909 return -1; 910 911 /* If the attribute trying to be set is the "stop" method, 912 but we already have a condition set in the CLI, disallow this 913 operation. */ 914 if (strcmp (attr, stop_func) == 0 && obj->bp->cond_string) 915 { 916 xfree (attr); 917 PyErr_SetString (PyExc_RuntimeError, 918 _("Cannot set 'stop' method. There is an " \ 919 "existing GDB condition attached to the " \ 920 "breakpoint.")); 921 return -1; 922 } 923 924 xfree (attr); 925 926 return PyObject_GenericSetAttr ((PyObject *)self, name, v); 927} 928 929static PyGetSetDef breakpoint_object_getset[] = { 930 { "enabled", bppy_get_enabled, bppy_set_enabled, 931 "Boolean telling whether the breakpoint is enabled.", NULL }, 932 { "silent", bppy_get_silent, bppy_set_silent, 933 "Boolean telling whether the breakpoint is silent.", NULL }, 934 { "thread", bppy_get_thread, bppy_set_thread, 935 "Thread ID for the breakpoint.\n\ 936If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\ 937If the value is None, then this breakpoint is not thread-specific.\n\ 938No other type of value can be used.", NULL }, 939 { "task", bppy_get_task, bppy_set_task, 940 "Thread ID for the breakpoint.\n\ 941If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\ 942If the value is None, then this breakpoint is not task-specific.\n\ 943No other type of value can be used.", NULL }, 944 { "ignore_count", bppy_get_ignore_count, bppy_set_ignore_count, 945 "Number of times this breakpoint should be automatically continued.", 946 NULL }, 947 { "number", bppy_get_number, NULL, 948 "Breakpoint's number assigned by GDB.", NULL }, 949 { "hit_count", bppy_get_hit_count, bppy_set_hit_count, 950 "Number of times the breakpoint has been hit.\n\ 951Can be set to zero to clear the count. No other value is valid\n\ 952when setting this property.", NULL }, 953 { "location", bppy_get_location, NULL, 954 "Location of the breakpoint, as specified by the user.", NULL}, 955 { "expression", bppy_get_expression, NULL, 956 "Expression of the breakpoint, as specified by the user.", NULL}, 957 { "condition", bppy_get_condition, bppy_set_condition, 958 "Condition of the breakpoint, as specified by the user,\ 959or None if no condition set."}, 960 { "commands", bppy_get_commands, NULL, 961 "Commands of the breakpoint, as specified by the user."}, 962 { "type", bppy_get_type, NULL, 963 "Type of breakpoint."}, 964 { "visible", bppy_get_visibility, NULL, 965 "Whether the breakpoint is visible to the user."}, 966 { NULL } /* Sentinel. */ 967}; 968 969static PyMethodDef breakpoint_object_methods[] = 970{ 971 { "is_valid", bppy_is_valid, METH_NOARGS, 972 "Return true if this breakpoint is valid, false if not." }, 973 { "delete", bppy_delete_breakpoint, METH_NOARGS, 974 "Delete the underlying GDB breakpoint." }, 975 { NULL } /* Sentinel. */ 976}; 977 978static PyTypeObject breakpoint_object_type = 979{ 980 PyObject_HEAD_INIT (NULL) 981 0, /*ob_size*/ 982 "gdb.Breakpoint", /*tp_name*/ 983 sizeof (breakpoint_object), /*tp_basicsize*/ 984 0, /*tp_itemsize*/ 985 0, /*tp_dealloc*/ 986 0, /*tp_print*/ 987 0, /*tp_getattr*/ 988 0, /*tp_setattr*/ 989 0, /*tp_compare*/ 990 0, /*tp_repr*/ 991 0, /*tp_as_number*/ 992 0, /*tp_as_sequence*/ 993 0, /*tp_as_mapping*/ 994 0, /*tp_hash */ 995 0, /*tp_call*/ 996 0, /*tp_str*/ 997 0, /*tp_getattro*/ 998 (setattrofunc)local_setattro, /*tp_setattro */ 999 0, /*tp_as_buffer*/ 1000 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ 1001 "GDB breakpoint object", /* tp_doc */ 1002 0, /* tp_traverse */ 1003 0, /* tp_clear */ 1004 0, /* tp_richcompare */ 1005 0, /* tp_weaklistoffset */ 1006 0, /* tp_iter */ 1007 0, /* tp_iternext */ 1008 breakpoint_object_methods, /* tp_methods */ 1009 0, /* tp_members */ 1010 breakpoint_object_getset, /* tp_getset */ 1011 0, /* tp_base */ 1012 0, /* tp_dict */ 1013 0, /* tp_descr_get */ 1014 0, /* tp_descr_set */ 1015 0, /* tp_dictoffset */ 1016 bppy_init, /* tp_init */ 1017 0, /* tp_alloc */ 1018 PyType_GenericNew /* tp_new */ 1019}; 1020