[1/3] Refactor body of bppy_init

Message ID 20221208191804.3819129-2-tromey@adacore.com
State New
Headers
Series Add "announce" flag to Python breakpoint creation |

Commit Message

Tom Tromey Dec. 8, 2022, 7:18 p.m. UTC
  This patch pulls the body of bppy_init out into a separate function.
This will simplify a subsequent patch.
---
 gdb/python/py-breakpoint.c | 161 ++++++++++++++++++++-----------------
 1 file changed, 87 insertions(+), 74 deletions(-)
  

Patch

diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c
index 63b18bd0f92..917fd367d06 100644
--- a/gdb/python/py-breakpoint.c
+++ b/gdb/python/py-breakpoint.c
@@ -813,6 +813,90 @@  bppy_init_validate_args (const char *spec, char *source,
   return 1;
 }
 
+/* Helper function for breakpoint creation.  */
+
+static void
+bppy_create_breakpoint (enum bptype type, int access_type, int temporary_bp,
+			int internal_bp, const char *spec,
+			PyObject *qualified, const char *source,
+			const char *function, const char *label,
+			const char *line)
+{
+  switch (type)
+    {
+    case bp_breakpoint:
+    case bp_hardware_breakpoint:
+      {
+	location_spec_up locspec;
+	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 ();
+
+	    locspec  = string_to_location_spec (&copy,
+						current_language,
+						func_name_match_type);
+	  }
+	else
+	  {
+	    std::unique_ptr<explicit_location_spec> explicit_loc
+	      (new explicit_location_spec ());
+
+	    explicit_loc->source_filename
+	      = source != nullptr ? xstrdup (source) : nullptr;
+	    explicit_loc->function_name
+	      = function != nullptr ? xstrdup (function) : nullptr;
+	    explicit_loc->label_name
+	      = label != nullptr ? xstrdup (label) : nullptr;
+
+	    if (line != NULL)
+	      explicit_loc->line_offset = linespec_parse_line_offset (line);
+
+	    explicit_loc->func_name_match_type = func_name_match_type;
+
+	    locspec.reset (explicit_loc.release ());
+	  }
+
+	const struct breakpoint_ops *ops
+	  = breakpoint_ops_for_location_spec (locspec.get (), false);
+
+	create_breakpoint (gdbpy_enter::get_gdbarch (),
+			   locspec.get (), NULL, -1, NULL, false,
+			   0,
+			   temporary_bp, type,
+			   0,
+			   AUTO_BOOLEAN_TRUE,
+			   ops,
+			   0, 1, internal_bp, 0);
+	break;
+      }
+    case bp_watchpoint:
+      {
+	spec = skip_spaces (spec);
+
+	if (access_type == hw_write)
+	  watch_command_wrapper (spec, 0, internal_bp);
+	else if (access_type == hw_access)
+	  awatch_command_wrapper (spec, 0, internal_bp);
+	else if (access_type == hw_read)
+	  rwatch_command_wrapper (spec, 0, internal_bp);
+	else
+	  error(_("Cannot understand watchpoint access type."));
+	break;
+      }
+    case bp_catchpoint:
+      error (_("BP_CATCHPOINT not supported"));
+    default:
+      error(_("Do not understand breakpoint type to set."));
+    }
+}
+
 /* Python function to create a new breakpoint.  */
 static int
 bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
@@ -881,80 +965,9 @@  bppy_init (PyObject *self, PyObject *args, PyObject *kwargs)
 
   try
     {
-      switch (type)
-	{
-	case bp_breakpoint:
-	case bp_hardware_breakpoint:
-	  {
-	    location_spec_up locspec;
-	    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 ();
-
-		locspec  = string_to_location_spec (&copy,
-						    current_language,
-						    func_name_match_type);
-	      }
-	    else
-	      {
-		std::unique_ptr<explicit_location_spec> explicit_loc
-		  (new explicit_location_spec ());
-
-		explicit_loc->source_filename
-		  = source != nullptr ? xstrdup (source) : nullptr;
-		explicit_loc->function_name
-		  = function != nullptr ? xstrdup (function) : nullptr;
-		explicit_loc->label_name
-		  = label != nullptr ? xstrdup (label) : nullptr;
-
-		if (line != NULL)
-		  explicit_loc->line_offset
-		    = linespec_parse_line_offset (line.get ());
-
-		explicit_loc->func_name_match_type = func_name_match_type;
-
-		locspec.reset (explicit_loc.release ());
-	      }
-
-	    const struct breakpoint_ops *ops
-	      = breakpoint_ops_for_location_spec (locspec.get (), false);
-
-	    create_breakpoint (gdbpy_enter::get_gdbarch (),
-			       locspec.get (), NULL, -1, NULL, false,
-			       0,
-			       temporary_bp, type,
-			       0,
-			       AUTO_BOOLEAN_TRUE,
-			       ops,
-			       0, 1, internal_bp, 0);
-	    break;
-	  }
-	case bp_watchpoint:
-	  {
-	    spec = skip_spaces (spec);
-
-	    if (access_type == hw_write)
-	      watch_command_wrapper (spec, 0, internal_bp);
-	    else if (access_type == hw_access)
-	      awatch_command_wrapper (spec, 0, internal_bp);
-	    else if (access_type == hw_read)
-	      rwatch_command_wrapper (spec, 0, internal_bp);
-	    else
-	      error(_("Cannot understand watchpoint access type."));
-	    break;
-	  }
-	case bp_catchpoint:
-	  error (_("BP_CATCHPOINT not supported"));
-	default:
-	  error(_("Do not understand breakpoint type to set."));
-	}
+      bppy_create_breakpoint (type, access_type, temporary_bp, internal_bp,
+			      spec, qualified, source, function, label,
+			      line.get ());
     }
   catch (const gdb_exception &except)
     {