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