binutils-gdb/gdb/testsuite/gdb.python/py-mi-cmd.exp
Andrew Burgess 740b42ceb7 gdb/python/mi: create MI commands using python
This commit allows a user to create custom MI commands using Python
similarly to what is possible for Python CLI commands.

A new subclass of mi_command is defined for Python MI commands,
mi_command_py. A new file, gdb/python/py-micmd.c contains the logic
for Python MI commands.

This commit is based on work linked too from this mailing list thread:

  https://sourceware.org/pipermail/gdb/2021-November/049774.html

Which has also been previously posted to the mailing list here:

  https://sourceware.org/pipermail/gdb-patches/2019-May/158010.html

And was recently reposted here:

  https://sourceware.org/pipermail/gdb-patches/2022-January/185190.html

The version in this patch takes some core code from the previously
posted patches, but also has some significant differences, especially
after the feedback given here:

  https://sourceware.org/pipermail/gdb-patches/2022-February/185767.html

A new MI command can be implemented in Python like this:

  class echo_args(gdb.MICommand):
      def invoke(self, args):
          return { 'args': args }

  echo_args("-echo-args")

The 'args' parameter (to the invoke method) is a list
containing (almost) all command line arguments passed to the MI
command (--thread and --frame are handled before the Python code is
called, and removed from the args list).  This list can be empty if
the MI command was passed no arguments.

When used within gdb the above command produced output like this:

  (gdb)
  -echo-args a b c
  ^done,args=["a","b","c"]
  (gdb)

The 'invoke' method of the new command must return a dictionary.  The
keys of this dictionary are then used as the field names in the mi
command output (e.g. 'args' in the above).

The values of the result returned by invoke can be dictionaries,
lists, iterators, or an object that can be converted to a string.
These are processed recursively to create the mi output.  And so, this
is valid:

  class new_command(gdb.MICommand):
      def invoke(self,args):
          return { 'result_one': { 'abc': 123, 'def': 'Hello' },
                   'result_two': [ { 'a': 1, 'b': 2 },
                                   { 'c': 3, 'd': 4 } ] }

Which produces output like:

  (gdb)
  -new-command
  ^done,result_one={abc="123",def="Hello"},result_two=[{a="1",b="2"},{c="3",d="4"}]
  (gdb)

I have required that the fields names used in mi result output must
match the regexp: "^[a-zA-Z][-_a-zA-Z0-9]*$" (without the quotes).
This restriction was never written down anywhere before, but seems
sensible to me, and we can always loosen this rule later if it proves
to be a problem.  Much harder to try and add a restriction later, once
people are already using the API.

What follows are some details about how this implementation differs
from the original patch that was posted to the mailing list.

In this patch, I have changed how the lifetime of the Python
gdb.MICommand objects is managed.  In the original patch, these object
were kept alive by an owned reference within the mi_command_py object.
As such, the Python object would not be deleted until the
mi_command_py object itself was deleted.

This caused a problem, the mi_command_py were held in the global mi
command table (in mi/mi-cmds.c), which, as a global, was not cleared
until program shutdown.  By this point the Python interpreter has
already been shutdown.  Attempting to delete the mi_command_py object
at this point was causing GDB to try and invoke Python code after
finalising the Python interpreter, and we would crash.

To work around this problem, the original patch added code in
python/python.c that would search the mi command table, and delete the
mi_command_py objects before the Python environment was finalised.

In contrast, in this patch, I have added a new global dictionary to
the gdb module, gdb._mi_commands.  We already have several such global
data stores related to pretty printers, and frame unwinders.

The MICommand objects are placed into the new gdb.mi_commands
dictionary, and it is this reference that keeps the objects alive.
When GDB's Python interpreter is shut down gdb._mi_commands is deleted,
and any MICommand objects within it are deleted at this point.

This change avoids having to make the mi_cmd_table global, and walk
over it from within GDB's python related code.

This patch handles command redefinition entirely within GDB's python
code, though this does impose one small restriction which is not
present in the original code (detailed below), I don't think this is a
big issue.  However, the original patch relied on being able to
finish executing the mi_command::do_invoke member function after the
mi_command object had been deleted.  Though continuing to execute a
member function after an object is deleted is well defined, it is
also (IMHO) risky, its too easy for someone to later add a use of the
object without realising that the object might sometimes, have been
deleted.  The new patch avoids this issue.

The one restriction that is added to avoid this, is that an MICommand
object can't be reinitialised with a different command name, so:

  (gdb) python cmd = MyMICommand("-abc")
  (gdb) python cmd.__init__("-def")
  can't reinitialize object with a different command name

This feels like a pretty weird edge case, and I'm happy to live with
this restriction.

I have also changed how the memory is managed for the command name.
In the most recently posted patch series, the command name is moved
into a subclass of mi_command, the python mi_command_py, which
inherits from mi_command is then free to use a smart pointer to manage
the memory for the name.

In this patch, I leave the mi_command class unchanged, and instead
hold the memory for the name within the Python object, as the lifetime
of the Python object always exceeds the c++ object stored in the
mi_cmd_table.  This adds a little more complexity in py-micmd.c, but
leaves the mi_command class nice and simple.

Next, this patch adds some extra functionality, there's a
MICommand.name read-only attribute containing the name of the command,
and a read-write MICommand.installed attribute that can be used to
install (make the command available for use) and uninstall (remove the
command from the mi_cmd_table so it can't be used) the command.  This
attribute will be automatically updated if a second command replaces
an earlier command.

This patch adds additional error handling, and makes more use the
gdbpy_handle_exception function.

Co-Authored-By: Jan Vrany <jan.vrany@labware.com>
2022-03-14 14:09:09 +00:00

390 lines
13 KiB
Text

# Copyright (C) 2019-2022 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Test custom MI commands implemented in Python.
load_lib gdb-python.exp
load_lib mi-support.exp
set MIFLAGS "-i=mi"
gdb_exit
if {[mi_gdb_start]} {
continue
}
if {[lsearch -exact [mi_get_features] python] < 0} {
unsupported "python support is disabled"
return -1
}
standard_testfile
mi_gdb_test "set python print-stack full" \
".*\\^done" \
"set python print-stack full"
mi_gdb_test "source ${srcdir}/${subdir}/${testfile}.py" \
".*\\^done" \
"load python file"
mi_gdb_test "python pycmd1('-pycmd')" \
".*\\^done" \
"define -pycmd MI command"
mi_gdb_test "-pycmd int" \
"\\^done,result=\"42\"" \
"-pycmd int"
mi_gdb_test "-pycmd str" \
"\\^done,result=\"Hello world!\"" \
"-pycmd str"
mi_gdb_test "-pycmd ary" \
"\\^done,result=\\\[\"Hello\",\"42\"\\\]" \
"-pycmd ary"
mi_gdb_test "-pycmd dct" \
"\\^done,result={hello=\"world\",times=\"42\"}" \
"-pycmd dct"
mi_gdb_test "-pycmd bk1" \
"\\^error,msg=\"Error occurred in Python: non-string object used as key: Bad Key\"" \
"-pycmd bk1"
mi_gdb_test "-pycmd bk2" \
"\\^error,msg=\"Error occurred in Python: non-string object used as key: 1\"" \
"-pycmd bk2"
mi_gdb_test "-pycmd bk3" \
[multi_line \
"&\"TypeError: __repr__ returned non-string \\(type BadKey\\)..\"" \
"\\^error,msg=\"Error occurred in Python: __repr__ returned non-string \\(type BadKey\\)\""] \
"-pycmd bk3"
mi_gdb_test "-pycmd tpl" \
"\\^done,result=\\\[\"42\",\"Hello\"\\\]" \
"-pycmd tpl"
mi_gdb_test "-pycmd itr" \
"\\^done,result=\\\[\"1\",\"2\",\"3\"\\\]" \
"-pycmd itr"
mi_gdb_test "-pycmd nn1" \
"\\^done" \
"-pycmd nn1"
mi_gdb_test "-pycmd nn2" \
"\\^done,result=\\\[\"None\"\\\]" \
"-pycmd nn2"
mi_gdb_test "-pycmd bogus" \
"\\^error,msg=\"Invalid parameter: bogus\"" \
"-pycmd bogus"
# Check that the top-level result from 'invoke' must be a dictionary.
foreach test_name { nd1 nd2 nd3 } {
mi_gdb_test "-pycmd ${test_name}" \
"\\^error,msg=\"Error occurred in Python: Result from invoke must be a dictionary\""
}
# Check for invalid strings in the result.
foreach test_desc { {ik1 "xxx yyy"} {ik2 "xxx yyy"} {ik3 "xxx\\+yyy"} \
{ik4 "xxx\\.yyy"} {ik5 "123xxxyyy"} } {
lassign $test_desc name pattern
mi_gdb_test "-pycmd ${name}" \
"\\^error,msg=\"Error occurred in Python: Invalid key in MI result: ${pattern}\""
}
mi_gdb_test "-pycmd empty_key" \
"\\^error,msg=\"Error occurred in Python: Invalid empty key in MI result\""
# Check that a dash ('-') can be used in a key name.
mi_gdb_test "-pycmd dash-key" \
"\\^done,the-key=\"123\""
# With this argument the command raises a gdb.GdbError with no message
# string. GDB considers this a bug in the user program, so prints a
# backtrace, and a generic error message.
mi_gdb_test "-pycmd exp" \
[multi_line ".*&\"Traceback \\(most recent call last\\):..\"" \
"&\"\[^\r\n\]+${testfile}.py\[^\r\n\]+\"" \
"&\"\[^\r\n\]+raise gdb.GdbError\\(\\)..\"" \
"&\"gdb.GdbError..\"" \
"\\^error,msg=\"Error occurred in Python\\.\""] \
"-pycmd exp"
mi_gdb_test "python pycmd2('-pycmd')" \
".*\\^done" \
"redefine -pycmd MI command from CLI command"
mi_gdb_test "-pycmd str" \
"\\^done,result=\"Ciao!\"" \
"-pycmd str - redefined from CLI"
mi_gdb_test "-pycmd int" \
"\\^error,msg=\"Invalid parameter: int\"" \
"-pycmd int - redefined from CLI"
mi_gdb_test "-pycmd new" \
"\\^done" \
"Define new command -pycmd-new MI command from Python MI command"
mi_gdb_test "-pycmd red" \
"\\^error,msg=\"Command redefined but we failing anyway\"" \
"redefine -pycmd MI command from Python MI command"
mi_gdb_test "-pycmd int" \
"\\^done,result=\"42\"" \
"-pycmd int - redefined from MI"
mi_gdb_test "-pycmd-new int" \
"\\^done,result=\"42\"" \
"-pycmd-new int - defined from MI"
mi_gdb_test "python pycmd1('')" \
".*&\"ValueError: MI command name is empty\\...\".*\\^error,msg=\"Error while executing Python code\\.\"" \
"empty MI command name"
mi_gdb_test "python pycmd1('-')" \
[multi_line \
".*" \
"&\"ValueError: MI command name does not start with '-' followed by at least one letter or digit\\...\"" \
"&\"Error while executing Python code\\...\"" \
"\\^error,msg=\"Error while executing Python code\\.\""] \
"invalid MI command name"
mi_gdb_test "python pycmd1('-bad-character-@')" \
[multi_line \
".*" \
"&\"ValueError: MI command name contains invalid character: @\\...\"" \
"&\"Error while executing Python code\\...\"" \
"\\^error,msg=\"Error while executing Python code\\.\""] \
"invalid character in MI command name"
mi_gdb_test "python cmd=pycmd1('-abc')" \
".*\\^done" \
"create command -abc, stored in a python variable"
mi_gdb_test "python print(cmd.name)" \
".*\r\n~\"-abc\\\\n\"\r\n\\^done" \
"print the name of the stored mi command"
mi_gdb_test "python print(cmd.installed)" \
".*\r\n~\"True\\\\n\"\r\n\\^done" \
"print the installed status of the stored mi command"
mi_gdb_test "-abc str" \
"\\^done,result=\"Hello world!\"" \
"-abc str"
mi_gdb_test "python cmd.installed = False" \
".*\\^done" \
"uninstall the mi command"
mi_gdb_test "-abc str" \
"\\^error,msg=\"Undefined MI command: abc\",code=\"undefined-command\"" \
"-abc str, but now the command is gone"
mi_gdb_test "python cmd.installed = True" \
".*\\^done" \
"re-install the mi command"
mi_gdb_test "-abc str" \
"\\^done,result=\"Hello world!\"" \
"-abc str, the command is back again"
mi_gdb_test "python other=pycmd2('-abc')" \
".*\\^done" \
"create another command called -abc, stored in a separate python variable"
mi_gdb_test "python print(other.installed)" \
".*\r\n~\"True\\\\n\"\r\n\\^done" \
"print the installed status of the other stored mi command"
mi_gdb_test "python print(cmd.installed)" \
".*\r\n~\"False\\\\n\"\r\n\\^done" \
"print the installed status of the original stored mi command"
mi_gdb_test "-abc str" \
"\\^done,result=\"Ciao!\"" \
"-abc str, when the other command is in place"
mi_gdb_test "python cmd.installed = True" \
".*\\^done" \
"re-install the original mi command"
mi_gdb_test "-abc str" \
"\\^done,result=\"Hello world!\"" \
"-abc str, the original command is back again"
mi_gdb_test "python print(other.installed)" \
".*\r\n~\"False\\\\n\"\r\n\\^done" \
"the other command is now not installed"
mi_gdb_test "python print(cmd.installed)" \
".*\r\n~\"True\\\\n\"\r\n\\^done" \
"the original command is now installed"
mi_gdb_test "python aa = pycmd3('-aa', 'message one', 'xxx')" \
".*\\^done" \
"created a new -aa command"
mi_gdb_test "-aa" \
".*\\^done,xxx={msg=\"message one\"}" \
"call the -aa command"
mi_gdb_test "python aa.__init__('-aa', 'message two', 'yyy')" \
".*\\^done" \
"reinitialise -aa command with a new message"
mi_gdb_test "-aa" \
".*\\^done,yyy={msg=\"message two\"}" \
"call the -aa command, get the new message"
mi_gdb_test "python aa.__init__('-bb', 'message three', 'zzz')" \
[multi_line \
".*" \
"&\"ValueError: can't reinitialize object with a different command name..\"" \
"&\"Error while executing Python code\\...\"" \
"\\^error,msg=\"Error while executing Python code\\.\""] \
"attempt to reinitialise aa variable to a new command name"
mi_gdb_test "-aa" \
".*\\^done,yyy={msg=\"message two\"}" \
"check the aa object has not changed after failed initialization"
mi_gdb_test "python aa.installed = False" \
".*\\^done" \
"uninstall the -aa command"
mi_gdb_test "python aa.__init__('-bb', 'message three', 'zzz')" \
[multi_line \
".*" \
"&\"ValueError: can't reinitialize object with a different command name..\"" \
"&\"Error while executing Python code\\...\"" \
"\\^error,msg=\"Error while executing Python code\\.\""] \
"attempt to reinitialise aa variable to a new command name while uninstalled"
mi_gdb_test "python aa.__init__('-aa', 'message three', 'zzz')" \
".*\\^done" \
"reinitialise -aa command with a new message while uninstalled"
mi_gdb_test "python aa.installed = True" \
".*\\^done" \
"install the -aa command"
mi_gdb_test "-aa" \
".*\\^done,zzz={msg=\"message three\"}" \
"call the -aa command looking for message three"
# Remove the gdb._mi_commands dictionary, then try to register a new
# command.
mi_gdb_test "python del(gdb._mi_commands)" ".*\\^done"
mi_gdb_test "python pycmd3('-hello', 'Hello', 'msg')" \
[multi_line \
".*" \
"&\"AttributeError: module 'gdb' has no attribute '_mi_commands'..\"" \
"&\"Error while executing Python code\\...\"" \
"\\^error,msg=\"Error while executing Python code\\.\""] \
"register a command with no gdb._mi_commands available"
# Set gdb._mi_commands to be something other than a dictionary, and
# try to register a command.
mi_gdb_test "python gdb._mi_commands = 'string'" ".*\\^done"
mi_gdb_test "python pycmd3('-hello', 'Hello', 'msg')" \
[multi_line \
".*" \
"&\"RuntimeError: gdb._mi_commands is not a dictionary as expected..\"" \
"&\"Error while executing Python code\\...\"" \
"\\^error,msg=\"Error while executing Python code\\.\""] \
"register a command when gdb._mi_commands is not a dictionary"
# Restore gdb._mi_commands to a dictionary.
mi_gdb_test "python gdb._mi_commands = {}" ".*\\^done"
# Try to register a command object that is missing an invoke method.
# This is accepted, but will give an error when the user tries to run
# the command.
mi_gdb_test "python no_invoke('-no-invoke')" ".*\\^done" \
"attempt to register command with no invoke method"
mi_gdb_test "-no-invoke" \
[multi_line \
".*" \
"&\"AttributeError: 'no_invoke' object has no attribute 'invoke'..\"" \
"\\^error,msg=\"Error occurred in Python: 'no_invoke' object has no attribute 'invoke'\""] \
"execute -no-invoke command, which is missing the invoke method"
# Register a command, then delete its invoke method. What is the user thinking!!
mi_gdb_test "python setattr(no_invoke, 'invoke', free_invoke)" ".*\\^done"
mi_gdb_test "python cmd = no_invoke('-hello')" ".*\\^done"
mi_gdb_test "-hello" ".*\\^done,result=\\\[\\\]" \
"execute no_invoke command, while it still has an invoke attribute"
mi_gdb_test "python delattr(no_invoke, 'invoke')" ".*\\^done"
mi_gdb_test "-hello" \
[multi_line \
".*" \
"&\"AttributeError: 'no_invoke' object has no attribute 'invoke'..\"" \
"\\^error,msg=\"Error occurred in Python: 'no_invoke' object has no attribute 'invoke'\""] \
"execute -hello command, that had its invoke method removed"
mi_gdb_test "python cmd.invoke = 'string'" ".*\\^done"
mi_gdb_test "-hello" \
[multi_line \
".*" \
"&\"TypeError: 'str' object is not callable..\"" \
"\\^error,msg=\"Error occurred in Python: 'str' object is not callable\""] \
"execute command with invoke set to a string"
# Further checking of corruption to the gdb._mi_commands dictionary.
#
# First, insert an object of the wrong type, then try to register an
# MI command that will go into that same dictionary slot.
mi_gdb_test "python gdb._mi_commands\['blah'\] = 'blah blah blah'" ".*\\^done"
mi_gdb_test "python pycmd2('-blah')" \
[multi_line \
".*" \
"&\"RuntimeError: unexpected object in gdb\\._mi_commands dictionary..\"" \
"&\"Error while executing Python code\\...\"" \
"\\^error,msg=\"Error while executing Python code\\.\""] \
"hit unexpected object in gdb._mi_commands dictionary"
# Next, create a command, uninstall it, then force the command back
# into the dictionary.
mi_gdb_test "python cmd = pycmd2('-foo')" ".*\\^done"
mi_gdb_test "python cmd.installed = False" ".*\\^done"
mi_gdb_test "python gdb._mi_commands\['foo'\] = cmd" ".*\\^done"
mi_gdb_test "python cmd.installed = True" \
[multi_line \
".*" \
"&\"RuntimeError: uninstalled command found in gdb\\._mi_commands dictionary..\"" \
"&\"Error while executing Python code\\...\"" \
"\\^error,msg=\"Error while executing Python code\\.\""] \
"found uninstalled command in gdb._mi_commands dictionary"
# Try to create a new MI command that uses the name of a builtin MI command.
mi_gdb_test "python cmd = pycmd2('-data-disassemble')" \
[multi_line \
".*" \
"&\"RuntimeError: unable to add command, name may already be in use..\"" \
"&\"Error while executing Python code\\...\"" \
"\\^error,msg=\"Error while executing Python code\\.\""] \
"try to register a command that replaces -data-disassemble"
mi_gdb_test "python run_exception_tests()" \
[multi_line \
".*" \
"~\"PASS..\"" \
"\\^done"]