Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Jan Vrany <jan.vrany@fit.cvut.cz>
To: gdb-patches@sourceware.org
Cc: Jan Vrany <jan.vrany@fit.cvut.cz>
Subject: [PATCH v2 4/5] mi/python: Allow redefinition of python MI commands
Date: Tue, 14 May 2019 11:57:00 -0000	[thread overview]
Message-ID: <20190514112418.24091-5-jan.vrany@fit.cvut.cz> (raw)
In-Reply-To: <20190418152337.6376-1-jan.vrany@fit.cvut.cz>

Redefining python MI commands is especially useful when developing
then.

gdb/Changelog:

	* mi/mi-cmds.h (mi_command::can_be_redefined): New method.
	* mi/mi-cmds.c: (mi_command::can_be_redefined): New method.
	(insert_mi_cmd_entry): Allow redefinition of python-defined MI commands.
	* python/py-micmd.h (mi_command_py::can_be_redefined): New method.
	* python/py-micmd.c: (mi_command_py::can_be_redefined): New method.
---
 gdb/ChangeLog         |  8 ++++++++
 gdb/mi/mi-cmds.c      | 10 +++++++++-
 gdb/mi/mi-cmds.h      |  3 +++
 gdb/python/py-micmd.c | 34 +++++++++++++++++++++-------------
 gdb/python/py-micmd.h |  3 ++-
 5 files changed, 43 insertions(+), 15 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 4cbe97002b..0a5d95b4e3 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,11 @@
+2019-05-02  Jan Vrany  <jan.vrany@fit.cvut.cz>
+
+	* mi/mi-cmds.h (mi_command::can_be_redefined): New method.
+	* mi/mi-cmds.c: (mi_command::can_be_redefined): New method.
+	(insert_mi_cmd_entry): Allow redefinition of python-defined MI commands.
+	* python/py-micmd.h (mi_command_py::can_be_redefined): New method.
+	* python/py-micmd.c: (mi_command_py::can_be_redefined): New method.
+
 2019-05-02  Didier Nadeau  <didier.nadeau@gmail.com>
             Jan Vrany  <jan.vrany@fit.cvut.cz>
 
diff --git a/gdb/mi/mi-cmds.c b/gdb/mi/mi-cmds.c
index 7e1d2c3f2d..d7ac76d450 100644
--- a/gdb/mi/mi-cmds.c
+++ b/gdb/mi/mi-cmds.c
@@ -41,7 +41,8 @@ insert_mi_cmd_entry (mi_cmd_up command)
   const std::string &name = command->name ();
 
   if (mi_cmd_table.find (name) != mi_cmd_table.end ())
-    return false;
+    if (! mi_cmd_table[name]->can_be_redefined ())
+      return false;
 
   mi_cmd_table[name] = std::move (command);
 
@@ -80,6 +81,13 @@ mi_command::mi_command (const char *name, int *suppress_notification)
     m_suppress_notification (suppress_notification)
 {}
 
+bool
+mi_command::can_be_redefined()
+{
+  return false;
+}
+
+
 
 void
 mi_command::invoke (struct mi_parse *parse)
diff --git a/gdb/mi/mi-cmds.h b/gdb/mi/mi-cmds.h
index a2757bae20..dd2738f831 100644
--- a/gdb/mi/mi-cmds.h
+++ b/gdb/mi/mi-cmds.h
@@ -142,6 +142,9 @@ class mi_command
     /* Execute the MI command.  */
     void invoke (struct mi_parse *parse);
 
+    /* Return TRUE if command can be redefined, FALSE otherwise. */
+    virtual bool can_be_redefined();
+
   protected:
     gdb::optional<scoped_restore_tmpl<int>> do_suppress_notification ();
     virtual void do_invoke(struct mi_parse *parse) = 0;
diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c
index c8d429bb4b..994d715d02 100644
--- a/gdb/python/py-micmd.c
+++ b/gdb/python/py-micmd.c
@@ -158,17 +158,22 @@ mi_command_py::mi_command_py (const char *name, int *suppress_notification,
 {
 }
 
-void
-mi_command_py::invoke (struct mi_parse *parse)
+bool
+mi_command_py::can_be_redefined()
 {
-  std::unique_ptr<scoped_restore_tmpl<int>> restore
-    = do_suppress_notification ();
+  return true;
+}
+
 
+void
+mi_command_py::do_invoke (struct mi_parse *parse)
+{
   mi_parse_argv (parse->args, parse);
 
   if (parse->argv == NULL)
     error (_("Problem parsing arguments: %s %s"), parse->command, parse->args);
 
+  std::string name (this->name ());
   PyObject *obj = this->pyobj.get ();
   int i;
 
@@ -176,20 +181,19 @@ mi_command_py::invoke (struct mi_parse *parse)
 
   if (!obj)
     error (_("-%s: invalid invocation of Python micommand object."),
-	   name ().c_str ());
+	   name.c_str ());
 
   if (!PyObject_HasAttr (obj, invoke_cst))
     {
       error (_("-%s: python command object missing 'invoke' method."),
-	     name ().c_str ());
+	     name.c_str ());
     }
-
   gdbpy_ref<> argobj (PyList_New (parse->argc));
   if (argobj == nullptr)
     {
       gdbpy_print_stack ();
       error (_("-%s: failed to create the python arguments list."),
-	     name ().c_str ());
+	     name.c_str ());
     }
 
   for (i = 0; i < parse->argc; ++i)
@@ -201,11 +205,16 @@ mi_command_py::invoke (struct mi_parse *parse)
       if (PyList_SetItem (argobj.get (), i, str) != 0)
 	{
 	  error (_("-%s: failed to create the python arguments list."),
-		 name ().c_str ());
+		 name.c_str ());
 	}
     }
 
   gdb_assert (PyErr_Occurred () == NULL);
+
+  /* From this point on, THIS must not be used since Python code may replace
+     the the very same command that is currently executing.  This in turn leads
+     to desctruction of THIS making it invalid.  See insert_mi_cmd_entry. */
+
   gdbpy_ref<> result (
     PyObject_CallMethodObjArgs (obj, invoke_cst, argobj.get (), NULL));
   if (PyErr_Occurred () != NULL)
@@ -214,9 +223,9 @@ mi_command_py::invoke (struct mi_parse *parse)
       gdb::unique_xmalloc_ptr<char> ex_msg (ex.to_string ());
 
       if (ex_msg == NULL || *ex_msg == '\0')
-	error (_("-%s: failed to execute command"), name ().c_str ());
+	error (_("-%s: failed to execute command"), name.c_str ());
       else
-	error (_("-%s: %s"), name ().c_str (), ex_msg.get ());
+	error (_("-%s: %s"), name.c_str (), ex_msg.get ());
     }
   else if (result != nullptr)
     {
@@ -228,7 +237,7 @@ mi_command_py::invoke (struct mi_parse *parse)
       error (
 	_("-%s: command invoke() method returned NULL but no python exception "
 	  "is set"),
-	name ().c_str ());
+	name.c_str ());
     }
 }
 
@@ -236,7 +245,6 @@ void mi_command_py::finalize ()
 {
   this->pyobj.reset (nullptr);
 }
-
 /* Initialize the MI command object.  */
 
 int
diff --git a/gdb/python/py-micmd.h b/gdb/python/py-micmd.h
index 418de41a10..eaa5a6c47c 100644
--- a/gdb/python/py-micmd.h
+++ b/gdb/python/py-micmd.h
@@ -38,7 +38,7 @@ class mi_command_py : public mi_command
 public:
   mi_command_py (const char *name, int *suppress_notification,
 		 gdbpy_ref<> object);
-  void invoke (struct mi_parse *parse) override;
+  bool can_be_redefined() override;
 
   /* This is called just before shutting down a Python interpreter
      to release python object implementing the command */
@@ -46,6 +46,7 @@ public:
 
 private:
   gdbpy_ref<> pyobj;
+  virtual void do_invoke(struct mi_parse *parse) override;
 };
 
 #endif
-- 
2.20.1


      parent reply	other threads:[~2019-05-14 11:57 UTC|newest]

Thread overview: 45+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-18 15:23 [RFC 0/8] Create MI commands using python Jan Vrany
2019-04-18 15:23 ` [RFC 1/8] Use std::map for MI commands in mi-cmds.c Jan Vrany
2019-04-25 19:13   ` Tom Tromey
2019-04-25 19:15   ` Tom Tromey
2019-04-25 19:34     ` Jan Vrany
2019-05-03 22:40   ` Simon Marchi
2019-05-03 22:45     ` Simon Marchi
2019-04-18 15:24 ` [RFC 3/8] Create MI commands using python Jan Vrany
2019-04-25 19:42   ` Tom Tromey
2019-04-18 15:24 ` [RFC 8/8] mi/python: Allow redefinition of python MI commands Jan Vrany
2019-04-25 19:50   ` Tom Tromey
2019-05-03 15:26     ` Jan Vrany
2019-05-06 21:40       ` Tom Tromey
2019-05-07 11:26         ` Jan Vrany
2019-05-07 13:09           ` Simon Marchi
2019-05-07 13:19             ` Jan Vrany
2019-05-08  0:10               ` Simon Marchi
2019-05-08 18:00                 ` Tom Tromey
2019-04-18 15:24 ` [RFC 4/8] mi/python: C++ify python MI command handling code Jan Vrany
2019-04-25 19:43   ` Tom Tromey
2019-04-18 15:24 ` [RFC 2/8] Use classes to represent MI Command instead of structures Jan Vrany
2019-04-25 19:25   ` Tom Tromey
2019-05-03 22:49     ` Simon Marchi
2019-05-03 22:57       ` Simon Marchi
2019-04-18 15:24 ` [RFC 6/8] mi/python: Handle python exception when executiong python-defined MI commands Jan Vrany
2019-04-25 19:46   ` Tom Tromey
2019-04-26 10:19     ` Jan Vrany
2019-04-18 15:24 ` [RFC 7/8] mi/python: Add tests for " Jan Vrany
2019-04-25 19:48   ` Tom Tromey
2019-04-18 16:03 ` [RFC 0/8] Create MI commands using python Simon Marchi
2019-04-20  7:20   ` Jan Vrany
2019-04-18 16:12 ` [RFC 5/8] mi/python: Polish MI output of python commands Jan Vrany
2019-04-25 19:50   ` Tom Tromey
2019-04-25 18:03 ` [RFC 0/8] Create MI commands using python Tom Tromey
2019-04-25 19:00   ` Simon Marchi
2019-04-25 19:01     ` Simon Marchi
2019-05-14 11:24 ` [PATCH v2 2/5] Use classes to represent MI Command instead of structures Jan Vrany
2019-05-17  3:12   ` Simon Marchi
2019-05-14 11:24 ` [PATCH v2 1/5] Use std::map for MI commands in mi-cmds.c Jan Vrany
2019-05-14 11:24 ` [PATCH v2 3/5] Create MI commands using python Jan Vrany
2019-05-17  4:29   ` Simon Marchi
2019-05-28 20:35     ` Jan Vrany
2019-05-14 11:24 ` [PATCH v2 5/5] mi/python: Add tests for python-defined MI commands Jan Vrany
2019-05-14 11:24 ` [PATCH v2 0/5] Create MI commands using python Jan Vrany
2019-05-14 11:57 ` Jan Vrany [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190514112418.24091-5-jan.vrany@fit.cvut.cz \
    --to=jan.vrany@fit.cvut.cz \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox