From: Jan Vrany <jan.vrany@fit.cvut.cz>
To: gdb-patches@sourceware.org
Cc: Jan Vrany <jan.vrany@fit.cvut.cz>
Subject: [PATCH v3 4/5] mi/python: Allow redefinition of python MI commands
Date: Thu, 30 May 2019 13:49:00 -0000 [thread overview]
Message-ID: <20190530134850.3236-5-jan.vrany@fit.cvut.cz> (raw)
In-Reply-To: <20190128124101.26243-1-jan.vrany@fit.cvut.cz>
Redefining python MI commands is especially useful when developing
them.
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 | 24 +++++++++++++++++-------
gdb/python/py-micmd.h | 2 +-
5 files changed, 38 insertions(+), 9 deletions(-)
diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 241e5da68f..1d264c44f7 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 0aead954f9..e280e51daf 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 5ca7232fca..331b1349df 100644
--- a/gdb/mi/mi-cmds.h
+++ b/gdb/mi/mi-cmds.h
@@ -143,6 +143,9 @@ public:
/* 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 e99632c97a..4f2b13f3c7 100644
--- a/gdb/python/py-micmd.c
+++ b/gdb/python/py-micmd.c
@@ -171,6 +171,12 @@ mi_command_py::mi_command_py (const char *name, gdbpy_ref<> object)
{
}
+bool
+mi_command_py::can_be_redefined()
+{
+ return true;
+}
+
void
mi_command_py::do_invoke (struct mi_parse *parse)
{
@@ -179,6 +185,7 @@ mi_command_py::do_invoke (struct mi_parse *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 ();
gdbpy_enter enter_py (get_current_arch (), current_language);
@@ -187,15 +194,14 @@ mi_command_py::do_invoke (struct mi_parse *parse)
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 (int i = 0; i < parse->argc; ++i)
@@ -205,11 +211,16 @@ mi_command_py::do_invoke (struct mi_parse *parse)
if (PyList_SetItem (argobj.get (), i, str.release ()) != 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)
@@ -218,9 +229,9 @@ mi_command_py::do_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
{
@@ -233,7 +244,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 deadc0116b..0d61069ca5 100644
--- a/gdb/python/py-micmd.h
+++ b/gdb/python/py-micmd.h
@@ -44,7 +44,7 @@ class mi_command_py : public mi_command
mi_command_py (const char *name, gdbpy_ref<> object);
-
+ bool can_be_redefined() override;
/* This is called just before shutting down a Python interpreter
to release python object implementing the command. */
--
2.20.1
next prev parent reply other threads:[~2019-05-30 13:49 UTC|newest]
Thread overview: 50+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-03 22:30 [PATCH] MI: Add new command -complete Jan Vrany
2019-01-16 9:21 ` Jan Vrany
2019-01-17 20:29 ` Tom Tromey
2019-01-17 21:01 ` Jan Vrany
2019-01-28 12:41 ` [PATCH v2 0/2] " Jan Vrany
2019-01-28 12:41 ` [PATCH v2 2/2] " Jan Vrany
2019-01-28 12:41 ` [PATCH v2 1/2] MI: extract command completion logic from complete_command() Jan Vrany
2019-02-27 20:41 ` Pedro Alves
[not found] ` <9ddd13d90ac5d77067f5690743149be8a2dcdd1a.camel@fit.cvut.cz>
2019-02-13 9:24 ` [PATCH v2 0/2] MI: Add new command -complete Jan Vrany
2019-02-19 7:33 ` Jan Vrany
2019-02-20 21:20 ` Tom Tromey
2019-02-21 16:05 ` Jan Vrany
2019-02-26 19:49 ` Tom Tromey
2019-02-27 10:41 ` Jan Vrany
2019-02-27 20:41 ` Pedro Alves
2019-02-28 10:18 ` Jan Vrany
2019-03-05 20:53 ` Pedro Alves
2019-03-06 15:09 ` Jan Vrany
2019-03-06 15:45 ` Eli Zaretskii
2019-03-06 16:37 ` Jan Vrany
2019-03-06 17:36 ` Eli Zaretskii
2019-03-04 14:52 ` [PATCH v3 2/2] " Jan Vrany
2019-03-04 17:35 ` Eli Zaretskii
2019-04-03 19:23 ` Pedro Alves
2019-03-04 14:52 ` [PATCH v3 1/2] MI: extract command completion logic from complete_command() Jan Vrany
2019-03-04 14:52 ` [PATCH v3 0/2] MI: Add new command -complete Jan Vrany
2019-04-18 11:59 ` [PATCH v4 2/2] " Jan Vrany
2019-04-18 12:51 ` Eli Zaretskii
2019-04-18 14:15 ` Pedro Alves
2019-04-18 14:55 ` Jan Vrany
2019-04-18 16:14 ` Pedro Alves
2019-05-16 11:27 ` Jan Vrany
2019-05-16 17:31 ` Tom Tromey
2019-04-18 11:59 ` [PATCH v4 1/2] MI: extract command completion logic from complete_command() Jan Vrany
2019-04-18 11:59 ` [PATCH v4 0/2] MI: Add new command -complete Jan Vrany
2019-04-18 14:59 ` [PATCH v5 " Jan Vrany
2019-04-18 14:59 ` [PATCH v5 2/2] " Jan Vrany
2019-04-18 15:23 ` Eli Zaretskii
2019-04-18 14:59 ` [PATCH v5 1/2] MI: extract command completion logic from complete_command() Jan Vrany
2019-05-30 13:49 ` [PATCH v3 0/5] Create MI commands using python Jan Vrany
2019-06-10 12:20 ` Jan Vrany
2019-05-30 13:49 ` [PATCH v3 2/5] Use classes to represent MI Command instead of structures Jan Vrany
2019-06-18 19:38 ` Pedro Alves
2019-05-30 13:49 ` Jan Vrany [this message]
2019-06-18 20:03 ` [PATCH v3 4/5] mi/python: Allow redefinition of python MI commands Pedro Alves
2019-05-30 13:49 ` [PATCH v3 1/5] Use std::map for MI commands in mi-cmds.c Jan Vrany
2019-05-30 13:49 ` [PATCH v3 3/5] Create MI commands using python Jan Vrany
2019-06-18 19:43 ` Pedro Alves
2019-05-30 14:19 ` [PATCH v3 5/5] mi/python: Add tests for python-defined MI commands Jan Vrany
2019-06-18 20:11 ` Pedro Alves
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=20190530134850.3236-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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).