Class-ify ui_out
[deliverable/binutils-gdb.git] / gdb / python / py-breakpoint.c
index aa404b7dec5f906ac56ce49962df8fbe805f4b31..4813a432847083d75bdbf4e523d307e5346047bf 100644 (file)
@@ -1,6 +1,6 @@
 /* Python interface to breakpoints
 
-   Copyright (C) 2008-2014 Free Software Foundation, Inc.
+   Copyright (C) 2008-2016 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -19,7 +19,6 @@
 
 #include "defs.h"
 #include "value.h"
-#include "exceptions.h"
 #include "python-internal.h"
 #include "python.h"
 #include "charset.h"
@@ -31,6 +30,8 @@
 #include "ada-lang.h"
 #include "arch-utils.h"
 #include "language.h"
+#include "location.h"
+#include "py-event.h"
 
 /* Number of live breakpoints.  */
 static int bppy_live;
@@ -115,7 +116,6 @@ bppy_set_enabled (PyObject *self, PyObject *newvalue, void *closure)
 {
   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
   int cmp;
-  volatile struct gdb_exception except;
 
   BPPY_SET_REQUIRE_VALID (self_bp);
 
@@ -137,14 +137,18 @@ bppy_set_enabled (PyObject *self, PyObject *newvalue, void *closure)
   if (cmp < 0)
     return -1;
 
-  TRY_CATCH (except, RETURN_MASK_ALL)
+  TRY
     {
       if (cmp == 1)
        enable_breakpoint (self_bp->bp);
       else
        disable_breakpoint (self_bp->bp);
     }
-  GDB_PY_SET_HANDLE_EXCEPTION (except);
+  CATCH (except, RETURN_MASK_ALL)
+    {
+      GDB_PY_SET_HANDLE_EXCEPTION (except);
+    }
+  END_CATCH
 
   return 0;
 }
@@ -200,7 +204,7 @@ bppy_set_thread (PyObject *self, PyObject *newvalue, void *closure)
       if (! gdb_py_int_as_long (newvalue, &id))
        return -1;
 
-      if (! valid_thread_id (id))
+      if (!valid_global_thread_id (id))
        {
          PyErr_SetString (PyExc_RuntimeError,
                           _("Invalid thread ID."));
@@ -228,7 +232,6 @@ bppy_set_task (PyObject *self, PyObject *newvalue, void *closure)
   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
   long id;
   int valid_id = 0;
-  volatile struct gdb_exception except;
 
   BPPY_SET_REQUIRE_VALID (self_bp);
 
@@ -243,11 +246,15 @@ bppy_set_task (PyObject *self, PyObject *newvalue, void *closure)
       if (! gdb_py_int_as_long (newvalue, &id))
        return -1;
 
-      TRY_CATCH (except, RETURN_MASK_ALL)
+      TRY
        {
          valid_id = valid_task_id (id);
        }
-      GDB_PY_SET_HANDLE_EXCEPTION (except);
+      CATCH (except, RETURN_MASK_ALL)
+       {
+         GDB_PY_SET_HANDLE_EXCEPTION (except);
+       }
+      END_CATCH
 
       if (! valid_id)
        {
@@ -279,15 +286,18 @@ static PyObject *
 bppy_delete_breakpoint (PyObject *self, PyObject *args)
 {
   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
-  volatile struct gdb_exception except;
 
   BPPY_REQUIRE_VALID (self_bp);
 
-  TRY_CATCH (except, RETURN_MASK_ALL)
+  TRY
     {
       delete_breakpoint (self_bp->bp);
     }
-  GDB_PY_HANDLE_EXCEPTION (except);
+  CATCH (except, RETURN_MASK_ALL)
+    {
+      GDB_PY_HANDLE_EXCEPTION (except);
+    }
+  END_CATCH
 
   Py_RETURN_NONE;
 }
@@ -299,7 +309,6 @@ bppy_set_ignore_count (PyObject *self, PyObject *newvalue, void *closure)
 {
   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
   long value;
-  volatile struct gdb_exception except;
 
   BPPY_SET_REQUIRE_VALID (self_bp);
 
@@ -322,11 +331,15 @@ bppy_set_ignore_count (PyObject *self, PyObject *newvalue, void *closure)
   if (value < 0)
     value = 0;
 
-  TRY_CATCH (except, RETURN_MASK_ALL)
+  TRY
     {
       set_ignore_count (self_bp->number, (int) value, 0);
     }
-  GDB_PY_SET_HANDLE_EXCEPTION (except);
+  CATCH (except, RETURN_MASK_ALL)
+    {
+      GDB_PY_SET_HANDLE_EXCEPTION (except);
+    }
+  END_CATCH
 
   return 0;
 }
@@ -369,7 +382,7 @@ bppy_set_hit_count (PyObject *self, PyObject *newvalue, void *closure)
 static PyObject *
 bppy_get_location (PyObject *self, void *closure)
 {
-  char *str;
+  const char *str;
   gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
 
   BPPY_REQUIRE_VALID (obj);
@@ -377,11 +390,10 @@ bppy_get_location (PyObject *self, void *closure)
   if (obj->bp->type != bp_breakpoint)
     Py_RETURN_NONE;
 
-  str = obj->bp->addr_string;
-
+  str = event_location_to_string (obj->bp->location);
   if (! str)
     str = "";
-  return PyString_Decode (str, strlen (str), host_charset (), NULL);
+  return host_string_to_python_string (str);
 }
 
 /* Python function to get the breakpoint expression.  */
@@ -403,7 +415,7 @@ bppy_get_expression (PyObject *self, void *closure)
   if (! str)
     str = "";
 
-  return PyString_Decode (str, strlen (str), host_charset (), NULL);
+  return host_string_to_python_string (str);
 }
 
 /* Python function to get the condition expression of a breakpoint.  */
@@ -419,7 +431,7 @@ bppy_get_condition (PyObject *self, void *closure)
   if (! str)
     Py_RETURN_NONE;
 
-  return PyString_Decode (str, strlen (str), host_charset (), NULL);
+  return host_string_to_python_string (str);
 }
 
 /* Returns 0 on success.  Returns -1 on error, with a python exception set.
@@ -428,9 +440,10 @@ bppy_get_condition (PyObject *self, void *closure)
 static int
 bppy_set_condition (PyObject *self, PyObject *newvalue, void *closure)
 {
-  char *exp;
+  gdb::unique_xmalloc_ptr<char> exp_holder;
+  const char *exp = NULL;
   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
-  volatile struct gdb_exception except;
+  struct gdb_exception except = exception_none;
 
   BPPY_SET_REQUIRE_VALID (self_bp);
 
@@ -444,18 +457,21 @@ bppy_set_condition (PyObject *self, PyObject *newvalue, void *closure)
     exp = "";
   else
     {
-      exp = python_string_to_host_string (newvalue);
-      if (exp == NULL)
+      exp_holder = python_string_to_host_string (newvalue);
+      if (exp_holder == NULL)
        return -1;
+      exp = exp_holder.get ();
     }
 
-  TRY_CATCH (except, RETURN_MASK_ALL)
+  TRY
     {
       set_breakpoint_condition (self_bp->bp, exp, 0);
     }
-
-  if (newvalue != Py_None)
-    xfree (exp);
+  CATCH (ex, RETURN_MASK_ALL)
+    {
+      except = ex;
+    }
+  END_CATCH
 
   GDB_PY_SET_HANDLE_EXCEPTION (except);
 
@@ -469,11 +485,9 @@ bppy_get_commands (PyObject *self, void *closure)
   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
   struct breakpoint *bp = self_bp->bp;
   long length;
-  volatile struct gdb_exception except;
   struct ui_file *string_file;
-  struct cleanup *chain;
   PyObject *result;
-  char *cmdstr;
+  struct cleanup *chain;
 
   BPPY_REQUIRE_VALID (self_bp);
 
@@ -483,22 +497,23 @@ bppy_get_commands (PyObject *self, void *closure)
   string_file = mem_fileopen ();
   chain = make_cleanup_ui_file_delete (string_file);
 
-  ui_out_redirect (current_uiout, string_file);
-  TRY_CATCH (except, RETURN_MASK_ALL)
+  current_uiout->redirect (string_file);
+  TRY
     {
       print_command_lines (current_uiout, breakpoint_commands (bp), 0);
     }
-  ui_out_redirect (current_uiout, NULL);
-  if (except.reason < 0)
+  CATCH (except, RETURN_MASK_ALL)
     {
+      current_uiout->redirect (NULL);
       do_cleanups (chain);
       gdbpy_convert_exception (except);
       return NULL;
     }
+  END_CATCH
 
-  cmdstr = ui_file_xstrdup (string_file, &length);
-  make_cleanup (xfree, cmdstr);
-  result = PyString_Decode (cmdstr, strlen (cmdstr), host_charset (), NULL);
+  current_uiout->redirect (NULL);
+  std::string cmdstr = ui_file_as_string (string_file);
+  result = host_string_to_python_string (cmdstr.c_str ());
   do_cleanups (chain);
   return result;
 }
@@ -523,10 +538,10 @@ bppy_get_visibility (PyObject *self, void *closure)
 
   BPPY_REQUIRE_VALID (self_bp);
 
-  if (self_bp->bp->number < 0)
-    Py_RETURN_FALSE;
+  if (user_breakpoint_p (self_bp->bp))
+    Py_RETURN_TRUE;
 
-  Py_RETURN_TRUE;
+  Py_RETURN_FALSE;
 }
 
 /* Python function to determine if the breakpoint is a temporary
@@ -546,6 +561,24 @@ bppy_get_temporary (PyObject *self, void *closure)
   Py_RETURN_FALSE;
 }
 
+/* Python function to determine if the breakpoint is a pending
+   breakpoint.  */
+
+static PyObject *
+bppy_get_pending (PyObject *self, void *closure)
+{
+  gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
+
+  BPPY_REQUIRE_VALID (self_bp);
+
+  if (is_watchpoint (self_bp->bp))
+    Py_RETURN_FALSE;
+  if (pending_breakpoint_p (self_bp->bp))
+    Py_RETURN_TRUE;
+
+  Py_RETURN_FALSE;
+}
+
 /* Python function to get the breakpoint's number.  */
 static PyObject *
 bppy_get_number (PyObject *self, void *closure)
@@ -620,7 +653,6 @@ bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
   PyObject *temporary = NULL;
   int internal_bp = 0;
   int temporary_bp = 0;
-  volatile struct gdb_exception except;
 
   if (! PyArg_ParseTupleAndKeywords (args, kwargs, "s|iiOO", keywords,
                                     &spec, &type, &access_type,
@@ -645,23 +677,32 @@ bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
   bppy_pending_object->number = -1;
   bppy_pending_object->bp = NULL;
 
-  TRY_CATCH (except, RETURN_MASK_ALL)
+  TRY
     {
-      char *copy = xstrdup (spec);
-      struct cleanup *cleanup = make_cleanup (xfree, copy);
+      gdb::unique_xmalloc_ptr<char>
+       copy_holder (xstrdup (skip_spaces_const (spec)));
+      char *copy = copy_holder.get ();
 
       switch (type)
        {
        case bp_breakpoint:
          {
+           struct event_location *location;
+           struct cleanup *cleanup;
+
+           location
+             = string_to_event_location_basic (&copy, current_language);
+           cleanup = make_cleanup_delete_event_location (location);
            create_breakpoint (python_gdbarch,
-                              copy, NULL, -1, NULL,
+                              location, NULL, -1, NULL,
                               0,
                               temporary_bp, bp_breakpoint,
                               0,
                               AUTO_BOOLEAN_TRUE,
                               &bkpt_breakpoint_ops,
                               0, 1, internal_bp, 0);
+
+           do_cleanups (cleanup);
            break;
          }
         case bp_watchpoint:
@@ -679,16 +720,16 @@ bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
        default:
          error(_("Do not understand breakpoint type to set."));
        }
-
-      do_cleanups (cleanup);
     }
-  if (except.reason < 0)
+  CATCH (except, RETURN_MASK_ALL)
     {
+      bppy_pending_object = NULL;
       PyErr_Format (except.reason == RETURN_QUIT
                    ? PyExc_KeyboardInterrupt : PyExc_RuntimeError,
                    "%s", except.message);
       return -1;
     }
+  END_CATCH
 
   BPPY_SET_REQUIRE_VALID ((gdbpy_breakpoint_object *) self);
   return 0;
@@ -699,7 +740,7 @@ bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
 static int
 build_bp_list (struct breakpoint *b, void *arg)
 {
-  PyObject *list = arg;
+  PyObject *list = (PyObject *) arg;
   PyObject *bp = (PyObject *) b->py_bp_object;
   int iserr = 0;
 
@@ -724,13 +765,13 @@ gdbpy_breakpoints (PyObject *self, PyObject *args)
   PyObject *list, *tuple;
 
   if (bppy_live == 0)
-    Py_RETURN_NONE;
+    return PyTuple_New (0);
 
   list = PyList_New (0);
   if (!list)
     return NULL;
 
-  /* If iteratre_over_breakpoints returns non NULL it signals an error
+  /* If iterate_over_breakpoints returns non NULL it signals an error
      condition.  In that case abandon building the list and return
      NULL.  */
   if (iterate_over_breakpoints (build_bp_list, list) != NULL)
@@ -750,15 +791,22 @@ gdbpy_breakpoints (PyObject *self, PyObject *args)
    stopped at the breakpoint.  Otherwise the inferior will be
    allowed to continue.  */
 
-int
-gdbpy_should_stop (struct gdbpy_breakpoint_object *bp_obj)
+enum ext_lang_bp_stop
+gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn *extlang,
+                                struct breakpoint *b)
 {
-  int stop = 1;
-
+  int stop;
+  struct gdbpy_breakpoint_object *bp_obj = b->py_bp_object;
   PyObject *py_bp = (PyObject *) bp_obj;
-  struct breakpoint *b = bp_obj->bp;
-  struct gdbarch *garch = b->gdbarch ? b->gdbarch : get_current_arch ();
-  struct cleanup *cleanup = ensure_python_env (garch, current_language);
+  struct gdbarch *garch;
+  struct cleanup *cleanup;
+
+  if (bp_obj == NULL)
+    return EXT_LANG_BP_STOP_UNSET;
+
+  stop = -1;
+  garch = b->gdbarch ? b->gdbarch : get_current_arch ();
+  cleanup = ensure_python_env (garch, current_language);
 
   if (bp_obj->is_finish_bp)
     bpfinishpy_pre_stop_hook (bp_obj);
@@ -767,6 +815,7 @@ gdbpy_should_stop (struct gdbpy_breakpoint_object *bp_obj)
     {
       PyObject *result = PyObject_CallMethod (py_bp, stop_func, NULL);
 
+      stop = 1;
       if (result)
        {
          int evaluate = PyObject_IsTrue (result);
@@ -790,7 +839,9 @@ gdbpy_should_stop (struct gdbpy_breakpoint_object *bp_obj)
 
   do_cleanups (cleanup);
 
-  return stop;
+  if (stop < 0)
+    return EXT_LANG_BP_STOP_UNSET;
+  return stop ? EXT_LANG_BP_STOP_YES : EXT_LANG_BP_STOP_NO;
 }
 
 /* Checks if the  "stop" method exists in this breakpoint.
@@ -798,17 +849,21 @@ gdbpy_should_stop (struct gdbpy_breakpoint_object *bp_obj)
    conditions.  */
 
 int
-gdbpy_breakpoint_has_py_cond (struct gdbpy_breakpoint_object *bp_obj)
+gdbpy_breakpoint_has_cond (const struct extension_language_defn *extlang,
+                          struct breakpoint *b)
 {
-  int has_func = 0;
-  PyObject *py_bp = (PyObject *) bp_obj;
-  struct gdbarch *garch = bp_obj->bp->gdbarch ? bp_obj->bp->gdbarch :
-    get_current_arch ();
-  struct cleanup *cleanup = ensure_python_env (garch, current_language);
-
-  if (py_bp != NULL)
-    has_func = PyObject_HasAttrString (py_bp, stop_func);
-
+  int has_func;
+  PyObject *py_bp;
+  struct gdbarch *garch;
+  struct cleanup *cleanup;
+
+  if (b->py_bp_object == NULL)
+    return 0;
+
+  py_bp = (PyObject *) b->py_bp_object;
+  garch = b->gdbarch ? b->gdbarch : get_current_arch ();
+  cleanup = ensure_python_env (garch, current_language);
+  has_func = PyObject_HasAttrString (py_bp, stop_func);
   do_cleanups (cleanup);
 
   return has_func;
@@ -826,7 +881,7 @@ gdbpy_breakpoint_created (struct breakpoint *bp)
   gdbpy_breakpoint_object *newbp;
   PyGILState_STATE state;
 
-  if (bp->number < 0 && bppy_pending_object == NULL)
+  if (!user_breakpoint_p (bp) && bppy_pending_object == NULL)
     return;
 
   if (bp->type != bp_breakpoint
@@ -861,6 +916,14 @@ gdbpy_breakpoint_created (struct breakpoint *bp)
       gdbpy_print_stack ();
     }
 
+  if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_created))
+    {
+      Py_INCREF (newbp);
+      if (evpy_emit_event ((PyObject *) newbp,
+                          gdb_py_events.breakpoint_created) < 0)
+       gdbpy_print_stack ();
+    }
+
   PyGILState_Release (state);
 }
 
@@ -881,6 +944,16 @@ gdbpy_breakpoint_deleted (struct breakpoint *b)
       bp_obj = bp->py_bp_object;
       if (bp_obj)
        {
+         if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted))
+           {
+             PyObject *bp_obj_alias = (PyObject *) bp_obj;
+
+             Py_INCREF (bp_obj_alias);
+             if (evpy_emit_event (bp_obj_alias,
+                                  gdb_py_events.breakpoint_deleted) < 0)
+               gdbpy_print_stack ();
+           }
+
          bp_obj->bp = NULL;
          --bppy_live;
          Py_DECREF (bp_obj);
@@ -889,6 +962,35 @@ gdbpy_breakpoint_deleted (struct breakpoint *b)
   PyGILState_Release (state);
 }
 
+/* Callback that is used when a breakpoint is modified.  */
+
+static void
+gdbpy_breakpoint_modified (struct breakpoint *b)
+{
+  int num = b->number;
+  PyGILState_STATE state;
+  struct breakpoint *bp = NULL;
+  gdbpy_breakpoint_object *bp_obj;
+
+  state = PyGILState_Ensure ();
+  bp = get_breakpoint (num);
+  if (bp)
+    {
+      PyObject *bp_obj = (PyObject *) bp->py_bp_object;
+      if (bp_obj)
+       {
+         if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_modified))
+           {
+             Py_INCREF (bp_obj);
+             if (evpy_emit_event (bp_obj,
+                                  gdb_py_events.breakpoint_modified) < 0)
+               gdbpy_print_stack ();
+           }
+       }
+    }
+  PyGILState_Release (state);
+}
+
 \f
 
 /* Initialize the Python breakpoint code.  */
@@ -907,6 +1009,7 @@ gdbpy_initialize_breakpoints (void)
 
   observer_attach_breakpoint_created (gdbpy_breakpoint_created);
   observer_attach_breakpoint_deleted (gdbpy_breakpoint_deleted);
+  observer_attach_breakpoint_modified (gdbpy_breakpoint_modified);
 
   /* Add breakpoint types constants.  */
   for (i = 0; pybp_codes[i].name; ++i)
@@ -941,25 +1044,36 @@ static int
 local_setattro (PyObject *self, PyObject *name, PyObject *v)
 {
   gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
-  char *attr = python_string_to_host_string (name);
+  gdb::unique_xmalloc_ptr<char> attr (python_string_to_host_string (name));
 
   if (attr == NULL)
     return -1;
 
   /* If the attribute trying to be set is the "stop" method,
-     but we already have a condition set in the CLI, disallow this
-     operation.  */
-  if (strcmp (attr, stop_func) == 0 && obj->bp->cond_string)
+     but we already have a condition set in the CLI or other extension
+     language, disallow this operation.  */
+  if (strcmp (attr.get (), stop_func) == 0)
     {
-      xfree (attr);
-      PyErr_SetString (PyExc_RuntimeError,
-                      _("Cannot set 'stop' method.  There is an " \
-                        "existing GDB condition attached to the " \
-                        "breakpoint."));
-      return -1;
-    }
+      const struct extension_language_defn *extlang = NULL;
 
-  xfree (attr);
+      if (obj->bp->cond_string != NULL)
+       extlang = get_ext_lang_defn (EXT_LANG_GDB);
+      if (extlang == NULL)
+       extlang = get_breakpoint_cond_ext_lang (obj->bp, EXT_LANG_PYTHON);
+      if (extlang != NULL)
+       {
+         char *error_text;
+
+         error_text
+           = xstrprintf (_("Only one stop condition allowed.  There is"
+                           " currently a %s stop condition defined for"
+                           " this breakpoint."),
+                         ext_lang_capitalized_name (extlang));
+         PyErr_SetString (PyExc_RuntimeError, error_text);
+         xfree (error_text);
+         return -1;
+       }
+    }
 
   return PyObject_GenericSetAttr ((PyObject *)self, name, v);
 }
@@ -1003,6 +1117,8 @@ or None if no condition set."},
     "Whether the breakpoint is visible to the user."},
   { "temporary", bppy_get_temporary, NULL,
     "Whether this breakpoint is a temporary breakpoint."},
+  { "pending", bppy_get_pending, NULL,
+    "Whether this breakpoint is a pending breakpoint."},
   { NULL }  /* Sentinel.  */
 };
 
This page took 0.063867 seconds and 4 git commands to generate.