Improve "backtrace" help text
[deliverable/binutils-gdb.git] / gdb / python / py-breakpoint.c
index c8847ffe08a7867b941393127a7ba4383cc20bf6..d654b92a8cd5ce4a0e92e4774a09f21d657ecbd3 100644 (file)
@@ -1,6 +1,6 @@
 /* Python interface to breakpoints
 
-   Copyright (C) 2008-2017 Free Software Foundation, Inc.
+   Copyright (C) 2008-2018 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
 #include "breakpoint.h"
 #include "gdbcmd.h"
 #include "gdbthread.h"
-#include "observer.h"
+#include "observable.h"
 #include "cli/cli-script.h"
 #include "ada-lang.h"
 #include "arch-utils.h"
 #include "language.h"
 #include "location.h"
 #include "py-event.h"
+#include "linespec.h"
 
 /* Number of live breakpoints.  */
 static int bppy_live;
@@ -41,7 +42,7 @@ static int bppy_live;
 gdbpy_breakpoint_object *bppy_pending_object;
 
 /* Function that is called when a Python condition is evaluated.  */
-static char * const stop_func = "stop";
+static const char stop_func[] = "stop";
 
 /* This is used to initialize various gdb.bp_* constants.  */
 struct pybp_code
@@ -390,7 +391,7 @@ bppy_get_location (PyObject *self, void *closure)
   if (obj->bp->type != bp_breakpoint)
     Py_RETURN_NONE;
 
-  str = event_location_to_string (obj->bp->location);
+  str = event_location_to_string (obj->bp->location.get ());
   if (! str)
     str = "";
   return host_string_to_python_string (str);
@@ -400,7 +401,7 @@ bppy_get_location (PyObject *self, void *closure)
 static PyObject *
 bppy_get_expression (PyObject *self, void *closure)
 {
-  char *str;
+  const char *str;
   gdbpy_breakpoint_object *obj = (gdbpy_breakpoint_object *) self;
   struct watchpoint *wp;
 
@@ -484,20 +485,15 @@ bppy_get_commands (PyObject *self, void *closure)
 {
   gdbpy_breakpoint_object *self_bp = (gdbpy_breakpoint_object *) self;
   struct breakpoint *bp = self_bp->bp;
-  long length;
-  struct ui_file *string_file;
-  PyObject *result;
-  struct cleanup *chain;
 
   BPPY_REQUIRE_VALID (self_bp);
 
   if (! self_bp->bp->commands)
     Py_RETURN_NONE;
 
-  string_file = mem_fileopen ();
-  chain = make_cleanup_ui_file_delete (string_file);
+  string_file stb;
 
-  current_uiout->redirect (string_file);
+  current_uiout->redirect (&stb);
   TRY
     {
       print_command_lines (current_uiout, breakpoint_commands (bp), 0);
@@ -505,17 +501,13 @@ bppy_get_commands (PyObject *self, void *closure)
   CATCH (except, RETURN_MASK_ALL)
     {
       current_uiout->redirect (NULL);
-      do_cleanups (chain);
       gdbpy_convert_exception (except);
       return NULL;
     }
   END_CATCH
 
   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;
+  return host_string_to_python_string (stb.c_str ());
 }
 
 /* Python function to get the breakpoint type.  */
@@ -640,25 +632,106 @@ bppy_get_ignore_count (PyObject *self, void *closure)
   return PyInt_FromLong (self_bp->bp->ignore_count);
 }
 
+/* Internal function to validate the Python parameters/keywords
+   provided to bppy_init.  */
+
+static int
+bppy_init_validate_args (const char *spec, char *source,
+                        char *function, char *label,
+                        char *line, enum bptype type)
+{
+  /* If spec is defined, ensure that none of the explicit location
+     keywords are also defined.  */
+  if (spec != NULL)
+    {
+      if (source != NULL || function != NULL || label != NULL || line != NULL)
+       {
+         PyErr_SetString (PyExc_RuntimeError,
+                          _("Breakpoints specified with spec cannot "
+                            "have source, function, label or line defined."));
+         return -1;
+       }
+    }
+  else
+    {
+      /* If spec isn't defined, ensure that the user is not trying to
+        define a watchpoint with an explicit location.  */
+      if (type == bp_watchpoint)
+       {
+         PyErr_SetString (PyExc_RuntimeError,
+                          _("Watchpoints cannot be set by explicit "
+                            "location parameters."));
+         return -1;
+       }
+      else
+       {
+         /* Otherwise, ensure some explicit locations are defined.  */
+         if (source == NULL && function == NULL && label == NULL
+             && line == NULL)
+           {
+             PyErr_SetString (PyExc_RuntimeError,
+                              _("Neither spec nor explicit location set."));
+             return -1;
+           }
+         /* Finally, if source is specified, ensure that line, label
+            or function are specified too.  */
+         if (source != NULL && function == NULL && label == NULL
+             && line == NULL)
+           {
+             PyErr_SetString (PyExc_RuntimeError,
+                              _("Specifying a source must also include a "
+                                "line, label or function."));
+             return -1;
+           }
+       }
+    }
+  return 1;
+}
+
 /* Python function to create a new breakpoint.  */
 static int
 bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
 {
-  static char *keywords[] = { "spec", "type", "wp_class", "internal",
-                             "temporary", NULL };
-  const char *spec;
-  int type = bp_breakpoint;
+  static const char *keywords[] = { "spec", "type", "wp_class", "internal",
+                                   "temporary","source", "function",
+                                   "label", "line", "qualified", NULL };
+  const char *spec = NULL;
+  enum bptype type = bp_breakpoint;
   int access_type = hw_write;
   PyObject *internal = NULL;
   PyObject *temporary = NULL;
+  PyObject *lineobj = NULL;;
   int internal_bp = 0;
   int temporary_bp = 0;
-
-  if (! PyArg_ParseTupleAndKeywords (args, kwargs, "s|iiOO", keywords,
-                                    &spec, &type, &access_type,
-                                    &internal, &temporary))
+  gdb::unique_xmalloc_ptr<char> line;
+  char *label = NULL;
+  char *source = NULL;
+  char *function = NULL;
+  PyObject * qualified = NULL;
+
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "|siiOOsssOO", keywords,
+                                       &spec, &type, &access_type,
+                                       &internal,
+                                       &temporary, &source,
+                                       &function, &label, &lineobj,
+                                       &qualified))
     return -1;
 
+
+  if (lineobj != NULL)
+    {
+      if (PyInt_Check (lineobj))
+       line.reset (xstrprintf ("%ld", PyInt_AsLong (lineobj)));
+      else if (PyString_Check (lineobj))
+       line = python_string_to_host_string (lineobj);
+      else
+       {
+         PyErr_SetString (PyExc_RuntimeError,
+                          _("Line keyword should be an integer or a string. "));
+         return -1;
+       }
+    }
+
   if (internal)
     {
       internal_bp = PyObject_IsTrue (internal);
@@ -673,40 +746,70 @@ bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
        return -1;
     }
 
+  if (bppy_init_validate_args (spec, source, function, label, line.get (),
+                              type) == -1)
+    return -1;
+
   bppy_pending_object = (gdbpy_breakpoint_object *) self;
   bppy_pending_object->number = -1;
   bppy_pending_object->bp = NULL;
 
   TRY
     {
-      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;
+           event_location_up location;
+           symbol_name_match_type func_name_match_type
+             = (qualified != NULL && PyObject_IsTrue (qualified)
+                 ? symbol_name_match_type::FULL
+                 : symbol_name_match_type::WILD);
+
+           if (spec != NULL)
+             {
+               gdb::unique_xmalloc_ptr<char>
+                 copy_holder (xstrdup (skip_spaces (spec)));
+               const char *copy = copy_holder.get ();
+
+               location  = string_to_event_location (&copy,
+                                                     current_language,
+                                                     func_name_match_type);
+             }
+           else
+             {
+               struct explicit_location explicit_loc;
+
+               initialize_explicit_location (&explicit_loc);
+               explicit_loc.source_filename = source;
+               explicit_loc.function_name = function;
+               explicit_loc.label_name = label;
+
+               if (line != NULL)
+                 explicit_loc.line_offset =
+                   linespec_parse_line_offset (line.get ());
+
+               explicit_loc.func_name_match_type = func_name_match_type;
+
+               location = new_explicit_location (&explicit_loc);
+             }
 
-           location
-             = string_to_event_location_basic (&copy, current_language);
-           cleanup = make_cleanup_delete_event_location (location);
            create_breakpoint (python_gdbarch,
-                              location, NULL, -1, NULL,
+                              location.get (), 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:
+       case bp_watchpoint:
          {
+           gdb::unique_xmalloc_ptr<char>
+             copy_holder (xstrdup (skip_spaces (spec)));
+           char *copy = copy_holder.get ();
+
            if (access_type == hw_write)
              watch_command_wrapper (copy, 0, internal_bp);
            else if (access_type == hw_access)
@@ -762,28 +865,20 @@ build_bp_list (struct breakpoint *b, void *arg)
 PyObject *
 gdbpy_breakpoints (PyObject *self, PyObject *args)
 {
-  PyObject *list, *tuple;
-
   if (bppy_live == 0)
     return PyTuple_New (0);
 
-  list = PyList_New (0);
-  if (!list)
+  gdbpy_ref<> list (PyList_New (0));
+  if (list == NULL)
     return NULL;
 
   /* 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)
-    {
-      Py_DECREF (list);
-      return NULL;
-    }
-
-  tuple = PyList_AsTuple (list);
-  Py_DECREF (list);
+  if (iterate_over_breakpoints (build_bp_list, list.get ()) != NULL)
+    return NULL;
 
-  return tuple;
+  return PyList_AsTuple (list.get ());
 }
 
 /* Call the "stop" method (if implemented) in the breakpoint
@@ -799,26 +894,26 @@ gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn *extlang,
   struct gdbpy_breakpoint_object *bp_obj = b->py_bp_object;
   PyObject *py_bp = (PyObject *) bp_obj;
   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);
+
+  gdbpy_enter enter_py (garch, current_language);
 
   if (bp_obj->is_finish_bp)
     bpfinishpy_pre_stop_hook (bp_obj);
 
   if (PyObject_HasAttrString (py_bp, stop_func))
     {
-      PyObject *result = PyObject_CallMethod (py_bp, stop_func, NULL);
+      gdbpy_ref<> result (PyObject_CallMethod (py_bp, stop_func, NULL));
 
       stop = 1;
-      if (result)
+      if (result != NULL)
        {
-         int evaluate = PyObject_IsTrue (result);
+         int evaluate = PyObject_IsTrue (result.get ());
 
          if (evaluate == -1)
            gdbpy_print_stack ();
@@ -827,8 +922,6 @@ gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn *extlang,
             the Python breakpoint wants GDB to continue.  */
          if (! evaluate)
            stop = 0;
-
-         Py_DECREF (result);
        }
       else
        gdbpy_print_stack ();
@@ -837,8 +930,6 @@ gdbpy_breakpoint_cond_says_stop (const struct extension_language_defn *extlang,
   if (bp_obj->is_finish_bp)
     bpfinishpy_post_stop_hook (bp_obj);
 
-  do_cleanups (cleanup);
-
   if (stop < 0)
     return EXT_LANG_BP_STOP_UNSET;
   return stop ? EXT_LANG_BP_STOP_YES : EXT_LANG_BP_STOP_NO;
@@ -852,21 +943,17 @@ int
 gdbpy_breakpoint_has_cond (const struct extension_language_defn *extlang,
                           struct breakpoint *b)
 {
-  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;
+  gdbpy_enter enter_py (garch, current_language);
+  return PyObject_HasAttrString (py_bp, stop_func);
 }
 
 \f
@@ -934,25 +1021,23 @@ gdbpy_breakpoint_deleted (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)
     {
-      bp_obj = bp->py_bp_object;
-      if (bp_obj)
+      gdbpy_ref<gdbpy_breakpoint_object> bp_obj (bp->py_bp_object);
+      if (bp_obj != NULL)
        {
          if (!evregpy_no_listeners_p (gdb_py_events.breakpoint_deleted))
            {
-             if (evpy_emit_event ((PyObject *) bp_obj,
+             if (evpy_emit_event ((PyObject *) bp_obj.get (),
                                   gdb_py_events.breakpoint_deleted) < 0)
                gdbpy_print_stack ();
            }
 
          bp_obj->bp = NULL;
          --bppy_live;
-         Py_DECREF (bp_obj);
        }
     }
   PyGILState_Release (state);
@@ -966,7 +1051,6 @@ 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);
@@ -1002,9 +1086,9 @@ gdbpy_initialize_breakpoints (void)
                              (PyObject *) &breakpoint_object_type) < 0)
     return -1;
 
-  observer_attach_breakpoint_created (gdbpy_breakpoint_created);
-  observer_attach_breakpoint_deleted (gdbpy_breakpoint_deleted);
-  observer_attach_breakpoint_modified (gdbpy_breakpoint_modified);
+  gdb::observers::breakpoint_created.attach (gdbpy_breakpoint_created);
+  gdb::observers::breakpoint_deleted.attach (gdbpy_breakpoint_deleted);
+  gdb::observers::breakpoint_modified.attach (gdbpy_breakpoint_modified);
 
   /* Add breakpoint types constants.  */
   for (i = 0; pybp_codes[i].name; ++i)
@@ -1057,15 +1141,12 @@ local_setattro (PyObject *self, PyObject *name, PyObject *v)
        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);
+         std::string error_text
+           = string_printf (_("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.c_str ());
          return -1;
        }
     }
@@ -1073,7 +1154,7 @@ local_setattro (PyObject *self, PyObject *name, PyObject *v)
   return PyObject_GenericSetAttr ((PyObject *)self, name, v);
 }
 
-static PyGetSetDef breakpoint_object_getset[] = {
+static gdb_PyGetSetDef breakpoint_object_getset[] = {
   { "enabled", bppy_get_enabled, bppy_set_enabled,
     "Boolean telling whether the breakpoint is enabled.", NULL },
   { "silent", bppy_get_silent, bppy_set_silent,
This page took 0.033662 seconds and 4 git commands to generate.