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