GDB (API)
/home/stan/gdb/src/gdb/python/py-breakpoint.c
Go to the documentation of this file.
00001 /* Python interface to breakpoints
00002 
00003    Copyright (C) 2008-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "value.h"
00022 #include "exceptions.h"
00023 #include "python-internal.h"
00024 #include "python.h"
00025 #include "charset.h"
00026 #include "breakpoint.h"
00027 #include "gdbcmd.h"
00028 #include "gdbthread.h"
00029 #include "observer.h"
00030 #include "cli/cli-script.h"
00031 #include "ada-lang.h"
00032 #include "arch-utils.h"
00033 #include "language.h"
00034 
00035 /* Number of live breakpoints.  */
00036 static int bppy_live;
00037 
00038 /* Variables used to pass information between the Breakpoint
00039    constructor and the breakpoint-created hook function.  */
00040 breakpoint_object *bppy_pending_object;
00041 
00042 /* Function that is called when a Python condition is evaluated.  */
00043 static char * const stop_func = "stop";
00044 
00045 /* This is used to initialize various gdb.bp_* constants.  */
00046 struct pybp_code
00047 {
00048   /* The name.  */
00049   const char *name;
00050   /* The code.  */
00051   int code;
00052 };
00053 
00054 /* Entries related to the type of user set breakpoints.  */
00055 static struct pybp_code pybp_codes[] =
00056 {
00057   { "BP_NONE", bp_none},
00058   { "BP_BREAKPOINT", bp_breakpoint},
00059   { "BP_WATCHPOINT", bp_watchpoint},
00060   { "BP_HARDWARE_WATCHPOINT", bp_hardware_watchpoint},
00061   { "BP_READ_WATCHPOINT", bp_read_watchpoint},
00062   { "BP_ACCESS_WATCHPOINT", bp_access_watchpoint},
00063   {NULL} /* Sentinel.  */
00064 };
00065 
00066 /* Entries related to the type of watchpoint.  */
00067 static struct pybp_code pybp_watch_types[] =
00068 {
00069   { "WP_READ", hw_read},
00070   { "WP_WRITE", hw_write},
00071   { "WP_ACCESS", hw_access},
00072   {NULL} /* Sentinel.  */
00073 };
00074 
00075 /* Python function which checks the validity of a breakpoint object.  */
00076 static PyObject *
00077 bppy_is_valid (PyObject *self, PyObject *args)
00078 {
00079   breakpoint_object *self_bp = (breakpoint_object *) self;
00080 
00081   if (self_bp->bp)
00082     Py_RETURN_TRUE;
00083   Py_RETURN_FALSE;
00084 }
00085 
00086 /* Python function to test whether or not the breakpoint is enabled.  */
00087 static PyObject *
00088 bppy_get_enabled (PyObject *self, void *closure)
00089 {
00090   breakpoint_object *self_bp = (breakpoint_object *) self;
00091 
00092   BPPY_REQUIRE_VALID (self_bp);
00093   if (! self_bp->bp)
00094     Py_RETURN_FALSE;
00095   if (self_bp->bp->enable_state == bp_enabled)
00096     Py_RETURN_TRUE;
00097   Py_RETURN_FALSE;
00098 }
00099 
00100 /* Python function to test whether or not the breakpoint is silent.  */
00101 static PyObject *
00102 bppy_get_silent (PyObject *self, void *closure)
00103 {
00104   breakpoint_object *self_bp = (breakpoint_object *) self;
00105 
00106   BPPY_REQUIRE_VALID (self_bp);
00107   if (self_bp->bp->silent)
00108     Py_RETURN_TRUE;
00109   Py_RETURN_FALSE;
00110 }
00111 
00112 /* Python function to set the enabled state of a breakpoint.  */
00113 static int
00114 bppy_set_enabled (PyObject *self, PyObject *newvalue, void *closure)
00115 {
00116   breakpoint_object *self_bp = (breakpoint_object *) self;
00117   int cmp;
00118   volatile struct gdb_exception except;
00119 
00120   BPPY_SET_REQUIRE_VALID (self_bp);
00121 
00122   if (newvalue == NULL)
00123     {
00124       PyErr_SetString (PyExc_TypeError, 
00125                        _("Cannot delete `enabled' attribute."));
00126 
00127       return -1;
00128     }
00129   else if (! PyBool_Check (newvalue))
00130     {
00131       PyErr_SetString (PyExc_TypeError,
00132                        _("The value of `enabled' must be a boolean."));
00133       return -1;
00134     }
00135 
00136   cmp = PyObject_IsTrue (newvalue);
00137   if (cmp < 0)
00138     return -1;
00139 
00140   TRY_CATCH (except, RETURN_MASK_ALL)
00141     {
00142       if (cmp == 1)
00143         enable_breakpoint (self_bp->bp);
00144       else
00145         disable_breakpoint (self_bp->bp);
00146     }
00147   GDB_PY_SET_HANDLE_EXCEPTION (except);
00148 
00149   return 0;
00150 }
00151 
00152 /* Python function to set the 'silent' state of a breakpoint.  */
00153 static int
00154 bppy_set_silent (PyObject *self, PyObject *newvalue, void *closure)
00155 {
00156   breakpoint_object *self_bp = (breakpoint_object *) self;
00157   int cmp;
00158 
00159   BPPY_SET_REQUIRE_VALID (self_bp);
00160 
00161   if (newvalue == NULL)
00162     {
00163       PyErr_SetString (PyExc_TypeError, 
00164                        _("Cannot delete `silent' attribute."));
00165       return -1;
00166     }
00167   else if (! PyBool_Check (newvalue))
00168     {
00169       PyErr_SetString (PyExc_TypeError,
00170                        _("The value of `silent' must be a boolean."));
00171       return -1;
00172     }
00173 
00174   cmp = PyObject_IsTrue (newvalue);
00175   if (cmp < 0)
00176     return -1;
00177   else
00178     breakpoint_set_silent (self_bp->bp, cmp);
00179 
00180   return 0;
00181 }
00182 
00183 /* Python function to set the thread of a breakpoint.  */
00184 static int
00185 bppy_set_thread (PyObject *self, PyObject *newvalue, void *closure)
00186 {
00187   breakpoint_object *self_bp = (breakpoint_object *) self;
00188   long id;
00189 
00190   BPPY_SET_REQUIRE_VALID (self_bp);
00191 
00192   if (newvalue == NULL)
00193     {
00194       PyErr_SetString (PyExc_TypeError, 
00195                        _("Cannot delete `thread' attribute."));
00196       return -1;
00197     }
00198   else if (PyInt_Check (newvalue))
00199     {
00200       if (! gdb_py_int_as_long (newvalue, &id))
00201         return -1;
00202 
00203       if (! valid_thread_id (id))
00204         {
00205           PyErr_SetString (PyExc_RuntimeError, 
00206                            _("Invalid thread ID."));
00207           return -1;
00208         }
00209     }
00210   else if (newvalue == Py_None)
00211     id = -1;
00212   else
00213     {
00214       PyErr_SetString (PyExc_TypeError,
00215                        _("The value of `thread' must be an integer or None."));
00216       return -1;
00217     }
00218 
00219   breakpoint_set_thread (self_bp->bp, id);
00220 
00221   return 0;
00222 }
00223 
00224 /* Python function to set the (Ada) task of a breakpoint.  */
00225 static int
00226 bppy_set_task (PyObject *self, PyObject *newvalue, void *closure)
00227 {
00228   breakpoint_object *self_bp = (breakpoint_object *) self;
00229   long id;
00230   int valid_id = 0;
00231   volatile struct gdb_exception except;
00232 
00233   BPPY_SET_REQUIRE_VALID (self_bp);
00234 
00235   if (newvalue == NULL)
00236     {
00237       PyErr_SetString (PyExc_TypeError, 
00238                        _("Cannot delete `task' attribute."));
00239       return -1;
00240     }
00241   else if (PyInt_Check (newvalue))
00242     {
00243       if (! gdb_py_int_as_long (newvalue, &id))
00244         return -1;
00245 
00246       TRY_CATCH (except, RETURN_MASK_ALL)
00247         {
00248           valid_id = valid_task_id (id);
00249         }
00250       GDB_PY_SET_HANDLE_EXCEPTION (except);
00251 
00252       if (! valid_id)
00253         {
00254           PyErr_SetString (PyExc_RuntimeError, 
00255                            _("Invalid task ID."));
00256           return -1;
00257         }
00258     }
00259   else if (newvalue == Py_None)
00260     id = 0;
00261   else
00262     {
00263       PyErr_SetString (PyExc_TypeError,
00264                        _("The value of `task' must be an integer or None."));
00265       return -1;
00266     }
00267 
00268   breakpoint_set_task (self_bp->bp, id);
00269 
00270   return 0;
00271 }
00272 
00273 /* Python function which deletes the underlying GDB breakpoint.  This
00274    triggers the breakpoint_deleted observer which will call
00275    gdbpy_breakpoint_deleted; that function cleans up the Python
00276    sections.  */
00277 
00278 static PyObject *
00279 bppy_delete_breakpoint (PyObject *self, PyObject *args)
00280 {
00281   breakpoint_object *self_bp = (breakpoint_object *) self;
00282   volatile struct gdb_exception except;
00283 
00284   BPPY_REQUIRE_VALID (self_bp);
00285 
00286   TRY_CATCH (except, RETURN_MASK_ALL)
00287     {
00288       delete_breakpoint (self_bp->bp);
00289     }
00290   GDB_PY_HANDLE_EXCEPTION (except);
00291 
00292   Py_RETURN_NONE;
00293 }
00294 
00295 
00296 /* Python function to set the ignore count of a breakpoint.  */
00297 static int
00298 bppy_set_ignore_count (PyObject *self, PyObject *newvalue, void *closure)
00299 {
00300   breakpoint_object *self_bp = (breakpoint_object *) self;
00301   long value;
00302   volatile struct gdb_exception except;
00303 
00304   BPPY_SET_REQUIRE_VALID (self_bp);
00305 
00306   if (newvalue == NULL)
00307     {
00308       PyErr_SetString (PyExc_TypeError,
00309                        _("Cannot delete `ignore_count' attribute."));
00310       return -1;
00311     }
00312   else if (! PyInt_Check (newvalue))
00313     {
00314       PyErr_SetString (PyExc_TypeError,
00315                        _("The value of `ignore_count' must be an integer."));
00316       return -1;
00317     }
00318 
00319   if (! gdb_py_int_as_long (newvalue, &value))
00320     return -1;
00321 
00322   if (value < 0)
00323     value = 0;
00324 
00325   TRY_CATCH (except, RETURN_MASK_ALL)
00326     {
00327       set_ignore_count (self_bp->number, (int) value, 0);
00328     }
00329   GDB_PY_SET_HANDLE_EXCEPTION (except);
00330 
00331   return 0;
00332 }
00333 
00334 /* Python function to set the hit count of a breakpoint.  */
00335 static int
00336 bppy_set_hit_count (PyObject *self, PyObject *newvalue, void *closure)
00337 {
00338   breakpoint_object *self_bp = (breakpoint_object *) self;
00339 
00340   BPPY_SET_REQUIRE_VALID (self_bp);
00341 
00342   if (newvalue == NULL)
00343     {
00344       PyErr_SetString (PyExc_TypeError, 
00345                        _("Cannot delete `hit_count' attribute."));
00346       return -1;
00347     }
00348   else
00349     {
00350       long value;
00351 
00352       if (! gdb_py_int_as_long (newvalue, &value))
00353         return -1;
00354 
00355       if (value != 0)
00356         {
00357           PyErr_SetString (PyExc_AttributeError,
00358                            _("The value of `hit_count' must be zero."));
00359           return -1;
00360         }
00361     }
00362 
00363   self_bp->bp->hit_count = 0;
00364 
00365   return 0;
00366 }
00367 
00368 /* Python function to get the location of a breakpoint.  */
00369 static PyObject *
00370 bppy_get_location (PyObject *self, void *closure)
00371 {
00372   char *str;
00373   breakpoint_object *obj = (breakpoint_object *) self;
00374 
00375   BPPY_REQUIRE_VALID (obj);
00376 
00377   if (obj->bp->type != bp_breakpoint)
00378     Py_RETURN_NONE;
00379 
00380   str = obj->bp->addr_string;
00381 
00382   if (! str)
00383     str = "";
00384   return PyString_Decode (str, strlen (str), host_charset (), NULL);
00385 }
00386 
00387 /* Python function to get the breakpoint expression.  */
00388 static PyObject *
00389 bppy_get_expression (PyObject *self, void *closure)
00390 {
00391   char *str;
00392   breakpoint_object *obj = (breakpoint_object *) self;
00393   struct watchpoint *wp;
00394 
00395   BPPY_REQUIRE_VALID (obj);
00396 
00397   if (!is_watchpoint (obj->bp))
00398     Py_RETURN_NONE;
00399 
00400   wp = (struct watchpoint *) obj->bp;
00401 
00402   str = wp->exp_string;
00403   if (! str)
00404     str = "";
00405 
00406   return PyString_Decode (str, strlen (str), host_charset (), NULL);
00407 }
00408 
00409 /* Python function to get the condition expression of a breakpoint.  */
00410 static PyObject *
00411 bppy_get_condition (PyObject *self, void *closure)
00412 {
00413   char *str;
00414   breakpoint_object *obj = (breakpoint_object *) self;
00415 
00416   BPPY_REQUIRE_VALID (obj);
00417 
00418   str = obj->bp->cond_string;
00419   if (! str)
00420     Py_RETURN_NONE;
00421 
00422   return PyString_Decode (str, strlen (str), host_charset (), NULL);
00423 }
00424 
00425 /* Returns 0 on success.  Returns -1 on error, with a python exception set.
00426    */
00427 
00428 static int
00429 bppy_set_condition (PyObject *self, PyObject *newvalue, void *closure)
00430 {
00431   char *exp;
00432   breakpoint_object *self_bp = (breakpoint_object *) self;
00433   volatile struct gdb_exception except;
00434 
00435   BPPY_SET_REQUIRE_VALID (self_bp);
00436 
00437   if (newvalue == NULL)
00438     {
00439       PyErr_SetString (PyExc_TypeError, 
00440                        _("Cannot delete `condition' attribute."));
00441       return -1;
00442     }
00443   else if (newvalue == Py_None)
00444     exp = "";
00445   else
00446     {
00447       exp = python_string_to_host_string (newvalue);
00448       if (exp == NULL)
00449         return -1;
00450     }
00451 
00452   TRY_CATCH (except, RETURN_MASK_ALL)
00453     {
00454       set_breakpoint_condition (self_bp->bp, exp, 0);
00455     }
00456 
00457   if (newvalue != Py_None)
00458     xfree (exp);
00459 
00460   GDB_PY_SET_HANDLE_EXCEPTION (except);
00461 
00462   return 0;
00463 }
00464 
00465 /* Python function to get the commands attached to a breakpoint.  */
00466 static PyObject *
00467 bppy_get_commands (PyObject *self, void *closure)
00468 {
00469   breakpoint_object *self_bp = (breakpoint_object *) self;
00470   struct breakpoint *bp = self_bp->bp;
00471   long length;
00472   volatile struct gdb_exception except;
00473   struct ui_file *string_file;
00474   struct cleanup *chain;
00475   PyObject *result;
00476   char *cmdstr;
00477 
00478   BPPY_REQUIRE_VALID (self_bp);
00479 
00480   if (! self_bp->bp->commands)
00481     Py_RETURN_NONE;
00482 
00483   string_file = mem_fileopen ();
00484   chain = make_cleanup_ui_file_delete (string_file);
00485 
00486   ui_out_redirect (current_uiout, string_file);
00487   TRY_CATCH (except, RETURN_MASK_ALL)
00488     {
00489       print_command_lines (current_uiout, breakpoint_commands (bp), 0);
00490     }
00491   ui_out_redirect (current_uiout, NULL);
00492   if (except.reason < 0)
00493     {
00494       do_cleanups (chain);
00495       gdbpy_convert_exception (except);
00496       return NULL;
00497     }
00498 
00499   cmdstr = ui_file_xstrdup (string_file, &length);
00500   make_cleanup (xfree, cmdstr);
00501   result = PyString_Decode (cmdstr, strlen (cmdstr), host_charset (), NULL);
00502   do_cleanups (chain);
00503   return result;
00504 }
00505 
00506 /* Python function to get the breakpoint type.  */
00507 static PyObject *
00508 bppy_get_type (PyObject *self, void *closure)
00509 {
00510   breakpoint_object *self_bp = (breakpoint_object *) self;
00511 
00512   BPPY_REQUIRE_VALID (self_bp);
00513 
00514   return PyInt_FromLong (self_bp->bp->type);
00515 }
00516 
00517 /* Python function to get the visibility of the breakpoint.  */
00518 
00519 static PyObject *
00520 bppy_get_visibility (PyObject *self, void *closure)
00521 {
00522   breakpoint_object *self_bp = (breakpoint_object *) self;
00523 
00524   BPPY_REQUIRE_VALID (self_bp);
00525 
00526   if (self_bp->bp->number < 0)
00527     Py_RETURN_FALSE;
00528 
00529   Py_RETURN_TRUE;
00530 }
00531 
00532 /* Python function to get the breakpoint's number.  */
00533 static PyObject *
00534 bppy_get_number (PyObject *self, void *closure)
00535 {
00536   breakpoint_object *self_bp = (breakpoint_object *) self;
00537 
00538   BPPY_REQUIRE_VALID (self_bp);
00539 
00540   return PyInt_FromLong (self_bp->number);
00541 }
00542 
00543 /* Python function to get the breakpoint's thread ID.  */
00544 static PyObject *
00545 bppy_get_thread (PyObject *self, void *closure)
00546 {
00547   breakpoint_object *self_bp = (breakpoint_object *) self;
00548 
00549   BPPY_REQUIRE_VALID (self_bp);
00550 
00551   if (self_bp->bp->thread == -1)
00552     Py_RETURN_NONE;
00553 
00554   return PyInt_FromLong (self_bp->bp->thread);
00555 }
00556 
00557 /* Python function to get the breakpoint's task ID (in Ada).  */
00558 static PyObject *
00559 bppy_get_task (PyObject *self, void *closure)
00560 {
00561   breakpoint_object *self_bp = (breakpoint_object *) self;
00562 
00563   BPPY_REQUIRE_VALID (self_bp);
00564 
00565   if (self_bp->bp->task == 0)
00566     Py_RETURN_NONE;
00567 
00568   return PyInt_FromLong (self_bp->bp->task);
00569 }
00570 
00571 /* Python function to get the breakpoint's hit count.  */
00572 static PyObject *
00573 bppy_get_hit_count (PyObject *self, void *closure)
00574 {
00575   breakpoint_object *self_bp = (breakpoint_object *) self;
00576 
00577   BPPY_REQUIRE_VALID (self_bp);
00578 
00579   return PyInt_FromLong (self_bp->bp->hit_count);
00580 }
00581 
00582 /* Python function to get the breakpoint's ignore count.  */
00583 static PyObject *
00584 bppy_get_ignore_count (PyObject *self, void *closure)
00585 {
00586   breakpoint_object *self_bp = (breakpoint_object *) self;
00587 
00588   BPPY_REQUIRE_VALID (self_bp);
00589 
00590   return PyInt_FromLong (self_bp->bp->ignore_count);
00591 }
00592 
00593 /* Python function to create a new breakpoint.  */
00594 static int
00595 bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
00596 {
00597   static char *keywords[] = { "spec", "type", "wp_class", "internal", NULL };
00598   const char *spec;
00599   int type = bp_breakpoint;
00600   int access_type = hw_write;
00601   PyObject *internal = NULL;
00602   int internal_bp = 0;
00603   volatile struct gdb_exception except;
00604 
00605   if (! PyArg_ParseTupleAndKeywords (args, kwargs, "s|iiO", keywords,
00606                                      &spec, &type, &access_type, &internal))
00607     return -1;
00608 
00609   if (internal)
00610     {
00611       internal_bp = PyObject_IsTrue (internal);
00612       if (internal_bp == -1)
00613         return -1;
00614     }
00615 
00616   bppy_pending_object = (breakpoint_object *) self;
00617   bppy_pending_object->number = -1;
00618   bppy_pending_object->bp = NULL;
00619   
00620   TRY_CATCH (except, RETURN_MASK_ALL)
00621     {
00622       char *copy = xstrdup (spec);
00623       struct cleanup *cleanup = make_cleanup (xfree, copy);
00624 
00625       switch (type)
00626         {
00627         case bp_breakpoint:
00628           {
00629             create_breakpoint (python_gdbarch,
00630                                copy, NULL, -1, NULL,
00631                                0,
00632                                0, bp_breakpoint,
00633                                0,
00634                                AUTO_BOOLEAN_TRUE,
00635                                &bkpt_breakpoint_ops,
00636                                0, 1, internal_bp, 0);
00637             break;
00638           }
00639         case bp_watchpoint:
00640           {
00641             if (access_type == hw_write)
00642               watch_command_wrapper (copy, 0, internal_bp);
00643             else if (access_type == hw_access)
00644               awatch_command_wrapper (copy, 0, internal_bp);
00645             else if (access_type == hw_read)
00646               rwatch_command_wrapper (copy, 0, internal_bp);
00647             else
00648               error(_("Cannot understand watchpoint access type."));
00649             break;
00650           }
00651         default:
00652           error(_("Do not understand breakpoint type to set."));
00653         }
00654 
00655       do_cleanups (cleanup);
00656     }
00657   if (except.reason < 0)
00658     {
00659       PyErr_Format (except.reason == RETURN_QUIT
00660                     ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
00661                     "%s", except.message);
00662       return -1;
00663     }
00664 
00665   BPPY_SET_REQUIRE_VALID ((breakpoint_object *) self);
00666   return 0;
00667 }
00668 
00669 
00670 
00671 static int
00672 build_bp_list (struct breakpoint *b, void *arg)
00673 {
00674   PyObject *list = arg;
00675   PyObject *bp = (PyObject *) b->py_bp_object;
00676   int iserr = 0;
00677 
00678   /* Not all breakpoints will have a companion Python object.
00679      Only breakpoints that were created via bppy_new, or
00680      breakpoints that were created externally and are tracked by
00681      the Python Scripting API.  */
00682   if (bp)
00683     iserr = PyList_Append (list, bp);
00684 
00685   if (iserr == -1)
00686     return 1;
00687 
00688   return 0;
00689 }
00690 
00691 /* Static function to return a tuple holding all breakpoints.  */
00692 
00693 PyObject *
00694 gdbpy_breakpoints (PyObject *self, PyObject *args)
00695 {
00696   PyObject *list, *tuple;
00697 
00698   if (bppy_live == 0)
00699     Py_RETURN_NONE;
00700 
00701   list = PyList_New (0);
00702   if (!list)
00703     return NULL;
00704 
00705   /* If iteratre_over_breakpoints returns non NULL it signals an error
00706      condition.  In that case abandon building the list and return
00707      NULL.  */
00708   if (iterate_over_breakpoints (build_bp_list, list) != NULL)
00709     {
00710       Py_DECREF (list);
00711       return NULL;
00712     }
00713 
00714   tuple = PyList_AsTuple (list);
00715   Py_DECREF (list);
00716 
00717   return tuple;
00718 }
00719 
00720 /* Call the "stop" method (if implemented) in the breakpoint
00721    class.  If the method returns True, the inferior  will be
00722    stopped at the breakpoint.  Otherwise the inferior will be
00723    allowed to continue.  */
00724 
00725 int
00726 gdbpy_should_stop (struct breakpoint_object *bp_obj)
00727 {
00728   int stop = 1;
00729 
00730   PyObject *py_bp = (PyObject *) bp_obj;
00731   struct breakpoint *b = bp_obj->bp;
00732   struct gdbarch *garch = b->gdbarch ? b->gdbarch : get_current_arch ();
00733   struct cleanup *cleanup = ensure_python_env (garch, current_language);
00734 
00735   if (bp_obj->is_finish_bp)
00736     bpfinishpy_pre_stop_hook (bp_obj);
00737 
00738   if (PyObject_HasAttrString (py_bp, stop_func))
00739     {
00740       PyObject *result = PyObject_CallMethod (py_bp, stop_func, NULL);
00741 
00742       if (result)
00743         {
00744           int evaluate = PyObject_IsTrue (result);
00745 
00746           if (evaluate == -1)
00747             gdbpy_print_stack ();
00748 
00749           /* If the "stop" function returns False that means
00750              the Python breakpoint wants GDB to continue.  */
00751           if (! evaluate)
00752             stop = 0;
00753 
00754           Py_DECREF (result);
00755         }
00756       else
00757         gdbpy_print_stack ();
00758     }
00759 
00760   if (bp_obj->is_finish_bp)
00761     bpfinishpy_post_stop_hook (bp_obj);
00762 
00763   do_cleanups (cleanup);
00764 
00765   return stop;
00766 }
00767 
00768 /* Checks if the  "stop" method exists in this breakpoint.
00769    Used by condition_command to ensure mutual exclusion of breakpoint
00770    conditions.  */
00771 
00772 int
00773 gdbpy_breakpoint_has_py_cond (struct breakpoint_object *bp_obj)
00774 {
00775   int has_func = 0;
00776   PyObject *py_bp = (PyObject *) bp_obj;
00777   struct gdbarch *garch = bp_obj->bp->gdbarch ? bp_obj->bp->gdbarch :
00778     get_current_arch ();
00779   struct cleanup *cleanup = ensure_python_env (garch, current_language);
00780   
00781   if (py_bp != NULL)
00782     has_func = PyObject_HasAttrString (py_bp, stop_func);
00783 
00784   do_cleanups (cleanup);
00785 
00786   return has_func;
00787 }
00788 
00789 
00790 
00791 /* Event callback functions.  */
00792 
00793 /* Callback that is used when a breakpoint is created.  This function
00794    will create a new Python breakpoint object.  */
00795 static void
00796 gdbpy_breakpoint_created (struct breakpoint *bp)
00797 {
00798   breakpoint_object *newbp;
00799   PyGILState_STATE state;
00800 
00801   if (bp->number < 0 && bppy_pending_object == NULL)
00802     return;
00803 
00804   if (bp->type != bp_breakpoint 
00805       && bp->type != bp_watchpoint
00806       && bp->type != bp_hardware_watchpoint  
00807       && bp->type != bp_read_watchpoint
00808       && bp->type != bp_access_watchpoint)
00809     return;
00810 
00811   state = PyGILState_Ensure ();
00812 
00813   if (bppy_pending_object)
00814     {
00815       newbp = bppy_pending_object;
00816       bppy_pending_object = NULL;
00817     }
00818   else
00819     newbp = PyObject_New (breakpoint_object, &breakpoint_object_type);
00820   if (newbp)
00821     {
00822       newbp->number = bp->number;
00823       newbp->bp = bp;
00824       newbp->bp->py_bp_object = newbp;
00825       newbp->is_finish_bp = 0;
00826       Py_INCREF (newbp);
00827       ++bppy_live;
00828     }
00829   else
00830     {
00831       PyErr_SetString (PyExc_RuntimeError,
00832                        _("Error while creating breakpoint from GDB."));
00833       gdbpy_print_stack ();
00834     }
00835 
00836   PyGILState_Release (state);
00837 }
00838 
00839 /* Callback that is used when a breakpoint is deleted.  This will
00840    invalidate the corresponding Python object.  */
00841 static void
00842 gdbpy_breakpoint_deleted (struct breakpoint *b)
00843 {
00844   int num = b->number;
00845   PyGILState_STATE state;
00846   struct breakpoint *bp = NULL;
00847   breakpoint_object *bp_obj;
00848 
00849   state = PyGILState_Ensure ();
00850   bp = get_breakpoint (num);
00851   if (bp)
00852     {
00853       bp_obj = bp->py_bp_object;
00854       if (bp_obj)
00855         {
00856           bp_obj->bp = NULL;
00857           --bppy_live;
00858           Py_DECREF (bp_obj);
00859         }
00860     }
00861   PyGILState_Release (state);
00862 }
00863 
00864 
00865 
00866 /* Initialize the Python breakpoint code.  */
00867 int
00868 gdbpy_initialize_breakpoints (void)
00869 {
00870   int i;
00871 
00872   breakpoint_object_type.tp_new = PyType_GenericNew;
00873   if (PyType_Ready (&breakpoint_object_type) < 0)
00874     return -1;
00875 
00876   if (gdb_pymodule_addobject (gdb_module, "Breakpoint",
00877                               (PyObject *) &breakpoint_object_type) < 0)
00878     return -1;
00879 
00880   observer_attach_breakpoint_created (gdbpy_breakpoint_created);
00881   observer_attach_breakpoint_deleted (gdbpy_breakpoint_deleted);
00882 
00883   /* Add breakpoint types constants.  */
00884   for (i = 0; pybp_codes[i].name; ++i)
00885     {
00886       if (PyModule_AddIntConstant (gdb_module,
00887                                    /* Cast needed for Python 2.4.  */
00888                                    (char *) pybp_codes[i].name,
00889                                    pybp_codes[i].code) < 0)
00890         return -1;
00891     }
00892 
00893   /* Add watchpoint types constants.  */
00894   for (i = 0; pybp_watch_types[i].name; ++i)
00895     {
00896       if (PyModule_AddIntConstant (gdb_module,
00897                                    /* Cast needed for Python 2.4.  */
00898                                    (char *) pybp_watch_types[i].name,
00899                                    pybp_watch_types[i].code) < 0)
00900         return -1;
00901     }
00902 
00903   return 0;
00904 }
00905 
00906 
00907 
00908 /* Helper function that overrides this Python object's
00909    PyObject_GenericSetAttr to allow extra validation of the attribute
00910    being set.  */
00911 
00912 static int 
00913 local_setattro (PyObject *self, PyObject *name, PyObject *v)
00914 {
00915   breakpoint_object *obj = (breakpoint_object *) self;  
00916   char *attr = python_string_to_host_string (name);
00917   
00918   if (attr == NULL)
00919     return -1;
00920   
00921   /* If the attribute trying to be set is the "stop" method,
00922      but we already have a condition set in the CLI, disallow this
00923      operation.  */
00924   if (strcmp (attr, stop_func) == 0 && obj->bp->cond_string)
00925     {
00926       xfree (attr);
00927       PyErr_SetString (PyExc_RuntimeError, 
00928                        _("Cannot set 'stop' method.  There is an " \
00929                          "existing GDB condition attached to the " \
00930                          "breakpoint."));
00931       return -1;
00932     }
00933   
00934   xfree (attr);
00935   
00936   return PyObject_GenericSetAttr ((PyObject *)self, name, v);  
00937 }
00938 
00939 static PyGetSetDef breakpoint_object_getset[] = {
00940   { "enabled", bppy_get_enabled, bppy_set_enabled,
00941     "Boolean telling whether the breakpoint is enabled.", NULL },
00942   { "silent", bppy_get_silent, bppy_set_silent,
00943     "Boolean telling whether the breakpoint is silent.", NULL },
00944   { "thread", bppy_get_thread, bppy_set_thread,
00945     "Thread ID for the breakpoint.\n\
00946 If the value is a thread ID (integer), then this is a thread-specific breakpoint.\n\
00947 If the value is None, then this breakpoint is not thread-specific.\n\
00948 No other type of value can be used.", NULL },
00949   { "task", bppy_get_task, bppy_set_task,
00950     "Thread ID for the breakpoint.\n\
00951 If the value is a task ID (integer), then this is an Ada task-specific breakpoint.\n\
00952 If the value is None, then this breakpoint is not task-specific.\n\
00953 No other type of value can be used.", NULL },
00954   { "ignore_count", bppy_get_ignore_count, bppy_set_ignore_count,
00955     "Number of times this breakpoint should be automatically continued.",
00956     NULL },
00957   { "number", bppy_get_number, NULL,
00958     "Breakpoint's number assigned by GDB.", NULL },
00959   { "hit_count", bppy_get_hit_count, bppy_set_hit_count,
00960     "Number of times the breakpoint has been hit.\n\
00961 Can be set to zero to clear the count. No other value is valid\n\
00962 when setting this property.", NULL },
00963   { "location", bppy_get_location, NULL,
00964     "Location of the breakpoint, as specified by the user.", NULL},
00965   { "expression", bppy_get_expression, NULL,
00966     "Expression of the breakpoint, as specified by the user.", NULL},
00967   { "condition", bppy_get_condition, bppy_set_condition,
00968     "Condition of the breakpoint, as specified by the user,\
00969 or None if no condition set."},
00970   { "commands", bppy_get_commands, NULL,
00971     "Commands of the breakpoint, as specified by the user."},
00972   { "type", bppy_get_type, NULL,
00973     "Type of breakpoint."},
00974   { "visible", bppy_get_visibility, NULL,
00975     "Whether the breakpoint is visible to the user."},
00976   { NULL }  /* Sentinel.  */
00977 };
00978 
00979 static PyMethodDef breakpoint_object_methods[] =
00980 {
00981   { "is_valid", bppy_is_valid, METH_NOARGS,
00982     "Return true if this breakpoint is valid, false if not." },
00983   { "delete", bppy_delete_breakpoint, METH_NOARGS,
00984     "Delete the underlying GDB breakpoint." },
00985   { NULL } /* Sentinel.  */
00986 };
00987 
00988 PyTypeObject breakpoint_object_type =
00989 {
00990   PyVarObject_HEAD_INIT (NULL, 0)
00991   "gdb.Breakpoint",               /*tp_name*/
00992   sizeof (breakpoint_object),     /*tp_basicsize*/
00993   0,                              /*tp_itemsize*/
00994   0,                              /*tp_dealloc*/
00995   0,                              /*tp_print*/
00996   0,                              /*tp_getattr*/
00997   0,                              /*tp_setattr*/
00998   0,                              /*tp_compare*/
00999   0,                              /*tp_repr*/
01000   0,                              /*tp_as_number*/
01001   0,                              /*tp_as_sequence*/
01002   0,                              /*tp_as_mapping*/
01003   0,                              /*tp_hash */
01004   0,                              /*tp_call*/
01005   0,                              /*tp_str*/
01006   0,                              /*tp_getattro*/
01007   (setattrofunc)local_setattro,   /*tp_setattro */
01008   0,                              /*tp_as_buffer*/
01009   Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,  /*tp_flags*/
01010   "GDB breakpoint object",        /* tp_doc */
01011   0,                              /* tp_traverse */
01012   0,                              /* tp_clear */
01013   0,                              /* tp_richcompare */
01014   0,                              /* tp_weaklistoffset */
01015   0,                              /* tp_iter */
01016   0,                              /* tp_iternext */
01017   breakpoint_object_methods,      /* tp_methods */
01018   0,                              /* tp_members */
01019   breakpoint_object_getset,       /* tp_getset */
01020   0,                              /* tp_base */
01021   0,                              /* tp_dict */
01022   0,                              /* tp_descr_get */
01023   0,                              /* tp_descr_set */
01024   0,                              /* tp_dictoffset */
01025   bppy_init,                      /* tp_init */
01026   0,                              /* tp_alloc */
01027 };
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines