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