gdb/python/py-breakpoint.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Python interface to breakpoints

  2.    Copyright (C) 2008-2015 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 "observer.h"
  23. #include "cli/cli-script.h"
  24. #include "ada-lang.h"
  25. #include "arch-utils.h"
  26. #include "language.h"

  27. /* Number of live breakpoints.  */
  28. static int bppy_live;

  29. /* Variables used to pass information between the Breakpoint
  30.    constructor and the breakpoint-created hook function.  */
  31. gdbpy_breakpoint_object *bppy_pending_object;

  32. /* Function that is called when a Python condition is evaluated.  */
  33. static char * const stop_func = "stop";

  34. /* This is used to initialize various gdb.bp_* constants.  */
  35. struct pybp_code
  36. {
  37.   /* The name.  */
  38.   const char *name;
  39.   /* The code.  */
  40.   int code;
  41. };

  42. /* Entries related to the type of user set breakpoints.  */
  43. static struct pybp_code pybp_codes[] =
  44. {
  45.   { "BP_NONE", bp_none},
  46.   { "BP_BREAKPOINT", bp_breakpoint},
  47.   { "BP_WATCHPOINT", bp_watchpoint},
  48.   { "BP_HARDWARE_WATCHPOINT", bp_hardware_watchpoint},
  49.   { "BP_READ_WATCHPOINT", bp_read_watchpoint},
  50.   { "BP_ACCESS_WATCHPOINT", bp_access_watchpoint},
  51.   {NULL} /* Sentinel.  */
  52. };

  53. /* Entries related to the type of watchpoint.  */
  54. static struct pybp_code pybp_watch_types[] =
  55. {
  56.   { "WP_READ", hw_read},
  57.   { "WP_WRITE", hw_write},
  58.   { "WP_ACCESS", hw_access},
  59.   {NULL} /* Sentinel.  */
  60. };

  61. /* Python function which checks the validity of a breakpoint object.  */
  62. static PyObject *
  63. bppy_is_valid (PyObject *self, PyObject *args)
  64. {
  65.   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;

  66.   if (self_bp->bp)
  67.     Py_RETURN_TRUE;
  68.   Py_RETURN_FALSE;
  69. }

  70. /* Python function to test whether or not the breakpoint is enabled.  */
  71. static PyObject *
  72. bppy_get_enabled (PyObject *self, void *closure)
  73. {
  74.   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;

  75.   BPPY_REQUIRE_VALID (self_bp);
  76.   if (! self_bp->bp)
  77.     Py_RETURN_FALSE;
  78.   if (self_bp->bp->enable_state == bp_enabled)
  79.     Py_RETURN_TRUE;
  80.   Py_RETURN_FALSE;
  81. }

  82. /* Python function to test whether or not the breakpoint is silent.  */
  83. static PyObject *
  84. bppy_get_silent (PyObject *self, void *closure)
  85. {
  86.   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;

  87.   BPPY_REQUIRE_VALID (self_bp);
  88.   if (self_bp->bp->silent)
  89.     Py_RETURN_TRUE;
  90.   Py_RETURN_FALSE;
  91. }

  92. /* Python function to set the enabled state of a breakpoint.  */
  93. static int
  94. bppy_set_enabled (PyObject *self, PyObject *newvalue, void *closure)
  95. {
  96.   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  97.   int cmp;
  98.   volatile struct gdb_exception except;

  99.   BPPY_SET_REQUIRE_VALID (self_bp);

  100.   if (newvalue == NULL)
  101.     {
  102.       PyErr_SetString (PyExc_TypeError,
  103.                        _("Cannot delete `enabled' attribute."));

  104.       return -1;
  105.     }
  106.   else if (! PyBool_Check (newvalue))
  107.     {
  108.       PyErr_SetString (PyExc_TypeError,
  109.                        _("The value of `enabled' must be a boolean."));
  110.       return -1;
  111.     }

  112.   cmp = PyObject_IsTrue (newvalue);
  113.   if (cmp < 0)
  114.     return -1;

  115.   TRY_CATCH (except, RETURN_MASK_ALL)
  116.     {
  117.       if (cmp == 1)
  118.         enable_breakpoint (self_bp->bp);
  119.       else
  120.         disable_breakpoint (self_bp->bp);
  121.     }
  122.   GDB_PY_SET_HANDLE_EXCEPTION (except);

  123.   return 0;
  124. }

  125. /* Python function to set the 'silent' state of a breakpoint.  */
  126. static int
  127. bppy_set_silent (PyObject *self, PyObject *newvalue, void *closure)
  128. {
  129.   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  130.   int cmp;

  131.   BPPY_SET_REQUIRE_VALID (self_bp);

  132.   if (newvalue == NULL)
  133.     {
  134.       PyErr_SetString (PyExc_TypeError,
  135.                        _("Cannot delete `silent' attribute."));
  136.       return -1;
  137.     }
  138.   else if (! PyBool_Check (newvalue))
  139.     {
  140.       PyErr_SetString (PyExc_TypeError,
  141.                        _("The value of `silent' must be a boolean."));
  142.       return -1;
  143.     }

  144.   cmp = PyObject_IsTrue (newvalue);
  145.   if (cmp < 0)
  146.     return -1;
  147.   else
  148.     breakpoint_set_silent (self_bp->bp, cmp);

  149.   return 0;
  150. }

  151. /* Python function to set the thread of a breakpoint.  */
  152. static int
  153. bppy_set_thread (PyObject *self, PyObject *newvalue, void *closure)
  154. {
  155.   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  156.   long id;

  157.   BPPY_SET_REQUIRE_VALID (self_bp);

  158.   if (newvalue == NULL)
  159.     {
  160.       PyErr_SetString (PyExc_TypeError,
  161.                        _("Cannot delete `thread' attribute."));
  162.       return -1;
  163.     }
  164.   else if (PyInt_Check (newvalue))
  165.     {
  166.       if (! gdb_py_int_as_long (newvalue, &id))
  167.         return -1;

  168.       if (! valid_thread_id (id))
  169.         {
  170.           PyErr_SetString (PyExc_RuntimeError,
  171.                            _("Invalid thread ID."));
  172.           return -1;
  173.         }
  174.     }
  175.   else if (newvalue == Py_None)
  176.     id = -1;
  177.   else
  178.     {
  179.       PyErr_SetString (PyExc_TypeError,
  180.                        _("The value of `thread' must be an integer or None."));
  181.       return -1;
  182.     }

  183.   breakpoint_set_thread (self_bp->bp, id);

  184.   return 0;
  185. }

  186. /* Python function to set the (Ada) task of a breakpoint.  */
  187. static int
  188. bppy_set_task (PyObject *self, PyObject *newvalue, void *closure)
  189. {
  190.   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  191.   long id;
  192.   int valid_id = 0;
  193.   volatile struct gdb_exception except;

  194.   BPPY_SET_REQUIRE_VALID (self_bp);

  195.   if (newvalue == NULL)
  196.     {
  197.       PyErr_SetString (PyExc_TypeError,
  198.                        _("Cannot delete `task' attribute."));
  199.       return -1;
  200.     }
  201.   else if (PyInt_Check (newvalue))
  202.     {
  203.       if (! gdb_py_int_as_long (newvalue, &id))
  204.         return -1;

  205.       TRY_CATCH (except, RETURN_MASK_ALL)
  206.         {
  207.           valid_id = valid_task_id (id);
  208.         }
  209.       GDB_PY_SET_HANDLE_EXCEPTION (except);

  210.       if (! valid_id)
  211.         {
  212.           PyErr_SetString (PyExc_RuntimeError,
  213.                            _("Invalid task ID."));
  214.           return -1;
  215.         }
  216.     }
  217.   else if (newvalue == Py_None)
  218.     id = 0;
  219.   else
  220.     {
  221.       PyErr_SetString (PyExc_TypeError,
  222.                        _("The value of `task' must be an integer or None."));
  223.       return -1;
  224.     }

  225.   breakpoint_set_task (self_bp->bp, id);

  226.   return 0;
  227. }

  228. /* Python function which deletes the underlying GDB breakpoint.  This
  229.    triggers the breakpoint_deleted observer which will call
  230.    gdbpy_breakpoint_deleted; that function cleans up the Python
  231.    sections.  */

  232. static PyObject *
  233. bppy_delete_breakpoint (PyObject *self, PyObject *args)
  234. {
  235.   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  236.   volatile struct gdb_exception except;

  237.   BPPY_REQUIRE_VALID (self_bp);

  238.   TRY_CATCH (except, RETURN_MASK_ALL)
  239.     {
  240.       delete_breakpoint (self_bp->bp);
  241.     }
  242.   GDB_PY_HANDLE_EXCEPTION (except);

  243.   Py_RETURN_NONE;
  244. }


  245. /* Python function to set the ignore count of a breakpoint.  */
  246. static int
  247. bppy_set_ignore_count (PyObject *self, PyObject *newvalue, void *closure)
  248. {
  249.   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  250.   long value;
  251.   volatile struct gdb_exception except;

  252.   BPPY_SET_REQUIRE_VALID (self_bp);

  253.   if (newvalue == NULL)
  254.     {
  255.       PyErr_SetString (PyExc_TypeError,
  256.                        _("Cannot delete `ignore_count' attribute."));
  257.       return -1;
  258.     }
  259.   else if (! PyInt_Check (newvalue))
  260.     {
  261.       PyErr_SetString (PyExc_TypeError,
  262.                        _("The value of `ignore_count' must be an integer."));
  263.       return -1;
  264.     }

  265.   if (! gdb_py_int_as_long (newvalue, &value))
  266.     return -1;

  267.   if (value < 0)
  268.     value = 0;

  269.   TRY_CATCH (except, RETURN_MASK_ALL)
  270.     {
  271.       set_ignore_count (self_bp->number, (int) value, 0);
  272.     }
  273.   GDB_PY_SET_HANDLE_EXCEPTION (except);

  274.   return 0;
  275. }

  276. /* Python function to set the hit count of a breakpoint.  */
  277. static int
  278. bppy_set_hit_count (PyObject *self, PyObject *newvalue, void *closure)
  279. {
  280.   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;

  281.   BPPY_SET_REQUIRE_VALID (self_bp);

  282.   if (newvalue == NULL)
  283.     {
  284.       PyErr_SetString (PyExc_TypeError,
  285.                        _("Cannot delete `hit_count' attribute."));
  286.       return -1;
  287.     }
  288.   else
  289.     {
  290.       long value;

  291.       if (! gdb_py_int_as_long (newvalue, &value))
  292.         return -1;

  293.       if (value != 0)
  294.         {
  295.           PyErr_SetString (PyExc_AttributeError,
  296.                            _("The value of `hit_count' must be zero."));
  297.           return -1;
  298.         }
  299.     }

  300.   self_bp->bp->hit_count = 0;

  301.   return 0;
  302. }

  303. /* Python function to get the location of a breakpoint.  */
  304. static PyObject *
  305. bppy_get_location (PyObject *self, void *closure)
  306. {
  307.   char *str;
  308.   gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;

  309.   BPPY_REQUIRE_VALID (obj);

  310.   if (obj->bp->type != bp_breakpoint)
  311.     Py_RETURN_NONE;

  312.   str = obj->bp->addr_string;

  313.   if (! str)
  314.     str = "";
  315.   return PyString_Decode (str, strlen (str), host_charset (), NULL);
  316. }

  317. /* Python function to get the breakpoint expression.  */
  318. static PyObject *
  319. bppy_get_expression (PyObject *self, void *closure)
  320. {
  321.   char *str;
  322.   gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
  323.   struct watchpoint *wp;

  324.   BPPY_REQUIRE_VALID (obj);

  325.   if (!is_watchpoint (obj->bp))
  326.     Py_RETURN_NONE;

  327.   wp = (struct watchpoint *) obj->bp;

  328.   str = wp->exp_string;
  329.   if (! str)
  330.     str = "";

  331.   return PyString_Decode (str, strlen (str), host_charset (), NULL);
  332. }

  333. /* Python function to get the condition expression of a breakpoint.  */
  334. static PyObject *
  335. bppy_get_condition (PyObject *self, void *closure)
  336. {
  337.   char *str;
  338.   gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;

  339.   BPPY_REQUIRE_VALID (obj);

  340.   str = obj->bp->cond_string;
  341.   if (! str)
  342.     Py_RETURN_NONE;

  343.   return PyString_Decode (str, strlen (str), host_charset (), NULL);
  344. }

  345. /* Returns 0 on success.  Returns -1 on error, with a python exception set.
  346.    */

  347. static int
  348. bppy_set_condition (PyObject *self, PyObject *newvalue, void *closure)
  349. {
  350.   char *exp;
  351.   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  352.   volatile struct gdb_exception except;

  353.   BPPY_SET_REQUIRE_VALID (self_bp);

  354.   if (newvalue == NULL)
  355.     {
  356.       PyErr_SetString (PyExc_TypeError,
  357.                        _("Cannot delete `condition' attribute."));
  358.       return -1;
  359.     }
  360.   else if (newvalue == Py_None)
  361.     exp = "";
  362.   else
  363.     {
  364.       exp = python_string_to_host_string (newvalue);
  365.       if (exp == NULL)
  366.         return -1;
  367.     }

  368.   TRY_CATCH (except, RETURN_MASK_ALL)
  369.     {
  370.       set_breakpoint_condition (self_bp->bp, exp, 0);
  371.     }

  372.   if (newvalue != Py_None)
  373.     xfree (exp);

  374.   GDB_PY_SET_HANDLE_EXCEPTION (except);

  375.   return 0;
  376. }

  377. /* Python function to get the commands attached to a breakpoint.  */
  378. static PyObject *
  379. bppy_get_commands (PyObject *self, void *closure)
  380. {
  381.   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
  382.   struct breakpoint *bp = self_bp->bp;
  383.   long length;
  384.   volatile struct gdb_exception except;
  385.   struct ui_file *string_file;
  386.   struct cleanup *chain;
  387.   PyObject *result;
  388.   char *cmdstr;

  389.   BPPY_REQUIRE_VALID (self_bp);

  390.   if (! self_bp->bp->commands)
  391.     Py_RETURN_NONE;

  392.   string_file = mem_fileopen ();
  393.   chain = make_cleanup_ui_file_delete (string_file);

  394.   ui_out_redirect (current_uiout, string_file);
  395.   TRY_CATCH (except, RETURN_MASK_ALL)
  396.     {
  397.       print_command_lines (current_uiout, breakpoint_commands (bp), 0);
  398.     }
  399.   ui_out_redirect (current_uiout, NULL);
  400.   if (except.reason < 0)
  401.     {
  402.       do_cleanups (chain);
  403.       gdbpy_convert_exception (except);
  404.       return NULL;
  405.     }

  406.   cmdstr = ui_file_xstrdup (string_file, &length);
  407.   make_cleanup (xfree, cmdstr);
  408.   result = PyString_Decode (cmdstr, strlen (cmdstr), host_charset (), NULL);
  409.   do_cleanups (chain);
  410.   return result;
  411. }

  412. /* Python function to get the breakpoint type.  */
  413. static PyObject *
  414. bppy_get_type (PyObject *self, void *closure)
  415. {
  416.   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;

  417.   BPPY_REQUIRE_VALID (self_bp);

  418.   return PyInt_FromLong (self_bp->bp->type);
  419. }

  420. /* Python function to get the visibility of the breakpoint.  */

  421. static PyObject *
  422. bppy_get_visibility (PyObject *self, void *closure)
  423. {
  424.   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;

  425.   BPPY_REQUIRE_VALID (self_bp);

  426.   if (self_bp->bp->number < 0)
  427.     Py_RETURN_FALSE;

  428.   Py_RETURN_TRUE;
  429. }

  430. /* Python function to determine if the breakpoint is a temporary
  431.    breakpoint.  */

  432. static PyObject *
  433. bppy_get_temporary (PyObject *self, void *closure)
  434. {
  435.   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;

  436.   BPPY_REQUIRE_VALID (self_bp);

  437.   if (self_bp->bp->disposition == disp_del
  438.       || self_bp->bp->disposition == disp_del_at_next_stop)
  439.     Py_RETURN_TRUE;

  440.   Py_RETURN_FALSE;
  441. }

  442. /* Python function to get the breakpoint's number.  */
  443. static PyObject *
  444. bppy_get_number (PyObject *self, void *closure)
  445. {
  446.   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;

  447.   BPPY_REQUIRE_VALID (self_bp);

  448.   return PyInt_FromLong (self_bp->number);
  449. }

  450. /* Python function to get the breakpoint's thread ID.  */
  451. static PyObject *
  452. bppy_get_thread (PyObject *self, void *closure)
  453. {
  454.   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;

  455.   BPPY_REQUIRE_VALID (self_bp);

  456.   if (self_bp->bp->thread == -1)
  457.     Py_RETURN_NONE;

  458.   return PyInt_FromLong (self_bp->bp->thread);
  459. }

  460. /* Python function to get the breakpoint's task ID (in Ada).  */
  461. static PyObject *
  462. bppy_get_task (PyObject *self, void *closure)
  463. {
  464.   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;

  465.   BPPY_REQUIRE_VALID (self_bp);

  466.   if (self_bp->bp->task == 0)
  467.     Py_RETURN_NONE;

  468.   return PyInt_FromLong (self_bp->bp->task);
  469. }

  470. /* Python function to get the breakpoint's hit count.  */
  471. static PyObject *
  472. bppy_get_hit_count (PyObject *self, void *closure)
  473. {
  474.   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;

  475.   BPPY_REQUIRE_VALID (self_bp);

  476.   return PyInt_FromLong (self_bp->bp->hit_count);
  477. }

  478. /* Python function to get the breakpoint's ignore count.  */
  479. static PyObject *
  480. bppy_get_ignore_count (PyObject *self, void *closure)
  481. {
  482.   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;

  483.   BPPY_REQUIRE_VALID (self_bp);

  484.   return PyInt_FromLong (self_bp->bp->ignore_count);
  485. }

  486. /* Python function to create a new breakpoint.  */
  487. static int
  488. bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
  489. {
  490.   static char *keywords[] = { "spec", "type", "wp_class", "internal",
  491.                               "temporary", NULL };
  492.   const char *spec;
  493.   int type = bp_breakpoint;
  494.   int access_type = hw_write;
  495.   PyObject *internal = NULL;
  496.   PyObject *temporary = NULL;
  497.   int internal_bp = 0;
  498.   int temporary_bp = 0;
  499.   volatile struct gdb_exception except;

  500.   if (! PyArg_ParseTupleAndKeywords (args, kwargs, "s|iiOO", keywords,
  501.                                      &spec, &type, &access_type,
  502.                                      &internal, &temporary))
  503.     return -1;

  504.   if (internal)
  505.     {
  506.       internal_bp = PyObject_IsTrue (internal);
  507.       if (internal_bp == -1)
  508.         return -1;
  509.     }

  510.   if (temporary != NULL)
  511.     {
  512.       temporary_bp = PyObject_IsTrue (temporary);
  513.       if (temporary_bp == -1)
  514.         return -1;
  515.     }

  516.   bppy_pending_object = (gdbpy_breakpoint_object *) self;
  517.   bppy_pending_object->number = -1;
  518.   bppy_pending_object->bp = NULL;

  519.   TRY_CATCH (except, RETURN_MASK_ALL)
  520.     {
  521.       char *copy = xstrdup (spec);
  522.       struct cleanup *cleanup = make_cleanup (xfree, copy);

  523.       switch (type)
  524.         {
  525.         case bp_breakpoint:
  526.           {
  527.             create_breakpoint (python_gdbarch,
  528.                                copy, NULL, -1, NULL,
  529.                                0,
  530.                                temporary_bp, bp_breakpoint,
  531.                                0,
  532.                                AUTO_BOOLEAN_TRUE,
  533.                                &bkpt_breakpoint_ops,
  534.                                0, 1, internal_bp, 0);
  535.             break;
  536.           }
  537.         case bp_watchpoint:
  538.           {
  539.             if (access_type == hw_write)
  540.               watch_command_wrapper (copy, 0, internal_bp);
  541.             else if (access_type == hw_access)
  542.               awatch_command_wrapper (copy, 0, internal_bp);
  543.             else if (access_type == hw_read)
  544.               rwatch_command_wrapper (copy, 0, internal_bp);
  545.             else
  546.               error(_("Cannot understand watchpoint access type."));
  547.             break;
  548.           }
  549.         default:
  550.           error(_("Do not understand breakpoint type to set."));
  551.         }

  552.       do_cleanups (cleanup);
  553.     }
  554.   if (except.reason < 0)
  555.     {
  556.       PyErr_Format (except.reason == RETURN_QUIT
  557.                     ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
  558.                     "%s", except.message);
  559.       return -1;
  560.     }

  561.   BPPY_SET_REQUIRE_VALID ((gdbpy_breakpoint_object *) self);
  562.   return 0;
  563. }



  564. static int
  565. build_bp_list (struct breakpoint *b, void *arg)
  566. {
  567.   PyObject *list = arg;
  568.   PyObject *bp = (PyObject *) b->py_bp_object;
  569.   int iserr = 0;

  570.   /* Not all breakpoints will have a companion Python object.
  571.      Only breakpoints that were created via bppy_new, or
  572.      breakpoints that were created externally and are tracked by
  573.      the Python Scripting API.  */
  574.   if (bp)
  575.     iserr = PyList_Append (list, bp);

  576.   if (iserr == -1)
  577.     return 1;

  578.   return 0;
  579. }

  580. /* Static function to return a tuple holding all breakpoints.  */

  581. PyObject *
  582. gdbpy_breakpoints (PyObject *self, PyObject *args)
  583. {
  584.   PyObject *list, *tuple;

  585.   if (bppy_live == 0)
  586.     Py_RETURN_NONE;

  587.   list = PyList_New (0);
  588.   if (!list)
  589.     return NULL;

  590.   /* If iteratre_over_breakpoints returns non NULL it signals an error
  591.      condition.  In that case abandon building the list and return
  592.      NULL.  */
  593.   if (iterate_over_breakpoints (build_bp_list, list) != NULL)
  594.     {
  595.       Py_DECREF (list);
  596.       return NULL;
  597.     }

  598.   tuple = PyList_AsTuple (list);
  599.   Py_DECREF (list);

  600.   return tuple;
  601. }

  602. /* Call the "stop" method (if implemented) in the breakpoint
  603.    class.  If the method returns True, the inferior  will be
  604.    stopped at the breakpoint.  Otherwise the inferior will be
  605.    allowed to continue.  */

  606. enum ext_lang_bp_stop
  607. gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn *extlang,
  608.                                  struct breakpoint *b)
  609. {
  610.   int stop;
  611.   struct gdbpy_breakpoint_object *bp_obj = b->py_bp_object;
  612.   PyObject *py_bp = (PyObject *) bp_obj;
  613.   struct gdbarch *garch;
  614.   struct cleanup *cleanup;

  615.   if (bp_obj == NULL)
  616.     return EXT_LANG_BP_STOP_UNSET;

  617.   stop = -1;
  618.   garch = b->gdbarch ? b->gdbarch : get_current_arch ();
  619.   cleanup = ensure_python_env (garch, current_language);

  620.   if (bp_obj->is_finish_bp)
  621.     bpfinishpy_pre_stop_hook (bp_obj);

  622.   if (PyObject_HasAttrString (py_bp, stop_func))
  623.     {
  624.       PyObject *result = PyObject_CallMethod (py_bp, stop_func, NULL);

  625.       stop = 1;
  626.       if (result)
  627.         {
  628.           int evaluate = PyObject_IsTrue (result);

  629.           if (evaluate == -1)
  630.             gdbpy_print_stack ();

  631.           /* If the "stop" function returns False that means
  632.              the Python breakpoint wants GDB to continue.  */
  633.           if (! evaluate)
  634.             stop = 0;

  635.           Py_DECREF (result);
  636.         }
  637.       else
  638.         gdbpy_print_stack ();
  639.     }

  640.   if (bp_obj->is_finish_bp)
  641.     bpfinishpy_post_stop_hook (bp_obj);

  642.   do_cleanups (cleanup);

  643.   if (stop < 0)
  644.     return EXT_LANG_BP_STOP_UNSET;
  645.   return stop ? EXT_LANG_BP_STOP_YES : EXT_LANG_BP_STOP_NO;
  646. }

  647. /* Checks if the  "stop" method exists in this breakpoint.
  648.    Used by condition_command to ensure mutual exclusion of breakpoint
  649.    conditions.  */

  650. int
  651. gdbpy_breakpoint_has_cond (const struct extension_language_defn *extlang,
  652.                            struct breakpoint *b)
  653. {
  654.   int has_func;
  655.   PyObject *py_bp;
  656.   struct gdbarch *garch;
  657.   struct cleanup *cleanup;

  658.   if (b->py_bp_object == NULL)
  659.     return 0;

  660.   py_bp = (PyObject *) b->py_bp_object;
  661.   garch = b->gdbarch ? b->gdbarch : get_current_arch ();
  662.   cleanup = ensure_python_env (garch, current_language);
  663.   has_func = PyObject_HasAttrString (py_bp, stop_func);
  664.   do_cleanups (cleanup);

  665.   return has_func;
  666. }



  667. /* Event callback functions.  */

  668. /* Callback that is used when a breakpoint is created.  This function
  669.    will create a new Python breakpoint object.  */
  670. static void
  671. gdbpy_breakpoint_created (struct breakpoint *bp)
  672. {
  673.   gdbpy_breakpoint_object *newbp;
  674.   PyGILState_STATE state;

  675.   if (bp->number < 0 && bppy_pending_object == NULL)
  676.     return;

  677.   if (bp->type != bp_breakpoint
  678.       && bp->type != bp_watchpoint
  679.       && bp->type != bp_hardware_watchpoint
  680.       && bp->type != bp_read_watchpoint
  681.       && bp->type != bp_access_watchpoint)
  682.     return;

  683.   state = PyGILState_Ensure ();

  684.   if (bppy_pending_object)
  685.     {
  686.       newbp = bppy_pending_object;
  687.       bppy_pending_object = NULL;
  688.     }
  689.   else
  690.     newbp = PyObject_New (gdbpy_breakpoint_object, &breakpoint_object_type);
  691.   if (newbp)
  692.     {
  693.       newbp->number = bp->number;
  694.       newbp->bp = bp;
  695.       newbp->bp->py_bp_object = newbp;
  696.       newbp->is_finish_bp = 0;
  697.       Py_INCREF (newbp);
  698.       ++bppy_live;
  699.     }
  700.   else
  701.     {
  702.       PyErr_SetString (PyExc_RuntimeError,
  703.                        _("Error while creating breakpoint from GDB."));
  704.       gdbpy_print_stack ();
  705.     }

  706.   PyGILState_Release (state);
  707. }

  708. /* Callback that is used when a breakpoint is deleted.  This will
  709.    invalidate the corresponding Python object.  */
  710. static void
  711. gdbpy_breakpoint_deleted (struct breakpoint *b)
  712. {
  713.   int num = b->number;
  714.   PyGILState_STATE state;
  715.   struct breakpoint *bp = NULL;
  716.   gdbpy_breakpoint_object *bp_obj;

  717.   state = PyGILState_Ensure ();
  718.   bp = get_breakpoint (num);
  719.   if (bp)
  720.     {
  721.       bp_obj = bp->py_bp_object;
  722.       if (bp_obj)
  723.         {
  724.           bp_obj->bp = NULL;
  725.           --bppy_live;
  726.           Py_DECREF (bp_obj);
  727.         }
  728.     }
  729.   PyGILState_Release (state);
  730. }



  731. /* Initialize the Python breakpoint code.  */
  732. int
  733. gdbpy_initialize_breakpoints (void)
  734. {
  735.   int i;

  736.   breakpoint_object_type.tp_new = PyType_GenericNew;
  737.   if (PyType_Ready (&breakpoint_object_type) < 0)
  738.     return -1;

  739.   if (gdb_pymodule_addobject (gdb_module, "Breakpoint",
  740.                               (PyObject *) &breakpoint_object_type) < 0)
  741.     return -1;

  742.   observer_attach_breakpoint_created (gdbpy_breakpoint_created);
  743.   observer_attach_breakpoint_deleted (gdbpy_breakpoint_deleted);

  744.   /* Add breakpoint types constants.  */
  745.   for (i = 0; pybp_codes[i].name; ++i)
  746.     {
  747.       if (PyModule_AddIntConstant (gdb_module,
  748.                                    /* Cast needed for Python 2.4.  */
  749.                                    (char *) pybp_codes[i].name,
  750.                                    pybp_codes[i].code) < 0)
  751.         return -1;
  752.     }

  753.   /* Add watchpoint types constants.  */
  754.   for (i = 0; pybp_watch_types[i].name; ++i)
  755.     {
  756.       if (PyModule_AddIntConstant (gdb_module,
  757.                                    /* Cast needed for Python 2.4.  */
  758.                                    (char *) pybp_watch_types[i].name,
  759.                                    pybp_watch_types[i].code) < 0)
  760.         return -1;
  761.     }

  762.   return 0;
  763. }



  764. /* Helper function that overrides this Python object's
  765.    PyObject_GenericSetAttr to allow extra validation of the attribute
  766.    being set.  */

  767. static int
  768. local_setattro (PyObject *self, PyObject *name, PyObject *v)
  769. {
  770.   gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
  771.   char *attr = python_string_to_host_string (name);

  772.   if (attr == NULL)
  773.     return -1;

  774.   /* If the attribute trying to be set is the "stop" method,
  775.      but we already have a condition set in the CLI or other extension
  776.      language, disallow this operation.  */
  777.   if (strcmp (attr, stop_func) == 0)
  778.     {
  779.       const struct extension_language_defn *extlang = NULL;

  780.       if (obj->bp->cond_string != NULL)
  781.         extlang = get_ext_lang_defn (EXT_LANG_GDB);
  782.       if (extlang == NULL)
  783.         extlang = get_breakpoint_cond_ext_lang (obj->bp, EXT_LANG_PYTHON);
  784.       if (extlang != NULL)
  785.         {
  786.           char *error_text;

  787.           xfree (attr);
  788.           error_text
  789.             = xstrprintf (_("Only one stop condition allowed.  There is"
  790.                             " currently a %s stop condition defined for"
  791.                             " this breakpoint."),
  792.                           ext_lang_capitalized_name (extlang));
  793.           PyErr_SetString (PyExc_RuntimeError, error_text);
  794.           xfree (error_text);
  795.           return -1;
  796.         }
  797.     }

  798.   xfree (attr);

  799.   return PyObject_GenericSetAttr ((PyObject *)self, name, v);
  800. }

  801. static PyGetSetDef breakpoint_object_getset[] = {
  802.   { "enabled", bppy_get_enabled, bppy_set_enabled,
  803.     "Boolean telling whether the breakpoint is enabled.", NULL },
  804.   { "silent", bppy_get_silent, bppy_set_silent,
  805.     "Boolean telling whether the breakpoint is silent.", NULL },
  806.   { "thread", bppy_get_thread, bppy_set_thread,
  807.     "Thread ID for the breakpoint.\n\
  808. If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
  809. If the value is None, then this breakpoint is not thread-specific.\n\
  810. No other type of value can be used.", NULL },
  811.   { "task", bppy_get_task, bppy_set_task,
  812.     "Thread ID for the breakpoint.\n\
  813. If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
  814. If the value is None, then this breakpoint is not task-specific.\n\
  815. No other type of value can be used.", NULL },
  816.   { "ignore_count", bppy_get_ignore_count, bppy_set_ignore_count,
  817.     "Number of times this breakpoint should be automatically continued.",
  818.     NULL },
  819.   { "number", bppy_get_number, NULL,
  820.     "Breakpoint's number assigned by GDB.", NULL },
  821.   { "hit_count", bppy_get_hit_count, bppy_set_hit_count,
  822.     "Number of times the breakpoint has been hit.\n\
  823. Can be set to zero to clear the count. No other value is valid\n\
  824. when setting this property.", NULL },
  825.   { "location", bppy_get_location, NULL,
  826.     "Location of the breakpoint, as specified by the user.", NULL},
  827.   { "expression", bppy_get_expression, NULL,
  828.     "Expression of the breakpoint, as specified by the user.", NULL},
  829.   { "condition", bppy_get_condition, bppy_set_condition,
  830.     "Condition of the breakpoint, as specified by the user,\
  831. or None if no condition set."},
  832.   { "commands", bppy_get_commands, NULL,
  833.     "Commands of the breakpoint, as specified by the user."},
  834.   { "type", bppy_get_type, NULL,
  835.     "Type of breakpoint."},
  836.   { "visible", bppy_get_visibility, NULL,
  837.     "Whether the breakpoint is visible to the user."},
  838.   { "temporary", bppy_get_temporary, NULL,
  839.     "Whether this breakpoint is a temporary breakpoint."},
  840.   { NULL/* Sentinel.  */
  841. };

  842. static PyMethodDef breakpoint_object_methods[] =
  843. {
  844.   { "is_valid", bppy_is_valid, METH_NOARGS,
  845.     "Return true if this breakpoint is valid, false if not." },
  846.   { "delete", bppy_delete_breakpoint, METH_NOARGS,
  847.     "Delete the underlying GDB breakpoint." },
  848.   { NULL } /* Sentinel.  */
  849. };

  850. PyTypeObject breakpoint_object_type =
  851. {
  852.   PyVarObject_HEAD_INIT (NULL, 0)
  853.   "gdb.Breakpoint",                  /*tp_name*/
  854.   sizeof (gdbpy_breakpoint_object), /*tp_basicsize*/
  855.   0,                                  /*tp_itemsize*/
  856.   0,                                  /*tp_dealloc*/
  857.   0,                                  /*tp_print*/
  858.   0,                                  /*tp_getattr*/
  859.   0,                                  /*tp_setattr*/
  860.   0,                                  /*tp_compare*/
  861.   0,                                  /*tp_repr*/
  862.   0,                                  /*tp_as_number*/
  863.   0,                                  /*tp_as_sequence*/
  864.   0,                                  /*tp_as_mapping*/
  865.   0,                                  /*tp_hash */
  866.   0,                                  /*tp_call*/
  867.   0,                                  /*tp_str*/
  868.   0,                                  /*tp_getattro*/
  869.   (setattrofunc)local_setattro,   /*tp_setattro */
  870.   0,                                  /*tp_as_buffer*/
  871.   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
  872.   "GDB breakpoint object",          /* tp_doc */
  873.   0,                                  /* tp_traverse */
  874.   0,                                  /* tp_clear */
  875.   0,                                  /* tp_richcompare */
  876.   0,                                  /* tp_weaklistoffset */
  877.   0,                                  /* tp_iter */
  878.   0,                                  /* tp_iternext */
  879.   breakpoint_object_methods,          /* tp_methods */
  880.   0,                                  /* tp_members */
  881.   breakpoint_object_getset,          /* tp_getset */
  882.   0,                                  /* tp_base */
  883.   0,                                  /* tp_dict */
  884.   0,                                  /* tp_descr_get */
  885.   0,                                  /* tp_descr_set */
  886.   0,                                  /* tp_dictoffset */
  887.   bppy_init,                          /* tp_init */
  888.   0,                                  /* tp_alloc */
  889. };