]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
python: Make gdb.execute("show commands") work (PR 23669)
authorSimon Marchi <simon.marchi@polymtl.ca>
Mon, 17 Sep 2018 12:26:24 +0000 (08:26 -0400)
committerSimon Marchi <simon.marchi@ericsson.com>
Mon, 17 Sep 2018 17:51:45 +0000 (13:51 -0400)
Since commit

  56bcdbea2bed ("Let gdb.execute handle multi-line commands")

trying to use a command like gdb.execute("show commands") in Python
fails.  GDB ends up trying to run the "commands" command.

The reason is that GDB gets confused with the special "commands"
command.  In process_next_line, the lookup_cmd_1 function returns the
cmd_list_element representing the "commands" sub-command of "show".
Lower, we check the cmd_list_element to see if it matches various
control commands by name, including the "commands" command.  This is
where we wrongfully conclude that the executed command must be
"commands", when in reality it was "show commands".

The fix proposed in this patch removes the comparisons by name, instead
comparing the cmd_list_element object by pointer with the objects
created at initialization time.

Tested on the buildbot, though on a single builder (Fedora-x86_64-m64).

gdb/ChangeLog:

PR python/23669
* breakpoint.c (commands_cmd_element): New.
(_initialize_breakpoint): Assign commands_cmd_element.
* breakpoint.h (commands_cmd_element): New.
* cli/cli-script.c (while_cmd_element, if_command,
define_cmd_element): New.
(command_name_equals): Remove.
(process_next_line): Compare commands by pointer, not by name.
(_initialize_cli_script): Assign the various cmd_list_element
variables.
* compile/compile.c (compile_cmd_element): New.
(_initialize_compile): Assign compile_cmd_element.
* compile/compile.h (compile_cmd_element): New.
* guile/guile.c (guile_cmd_element): New.
(install_gdb_commands): Assign guile_cmd_element.
* guile/guile.h (guile_cmd_element): New.
* python/python.c (python_cmd_element): New.
(_initialize_python): Assign python_cmd_element.
* python/python.h (python_cmd_element): New.
* tracepoint.c (while_stepping_cmd_element): New.
(_initialize_tracepoint): Assign while_stepping_cmd_element.
* tracepoint.h (while_stepping_cmd_element): New.

gdb/testsuite/ChangeLog:

PR python/23669
* gdb.python/python.exp: Test gdb.execute("show commands").

14 files changed:
gdb/ChangeLog
gdb/breakpoint.c
gdb/breakpoint.h
gdb/cli/cli-script.c
gdb/compile/compile.c
gdb/compile/compile.h
gdb/guile/guile.c
gdb/guile/guile.h
gdb/python/python.c
gdb/python/python.h
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.python/python.exp
gdb/tracepoint.c
gdb/tracepoint.h

index 800b942240f0757e32470474884663c45e3194d5..fbbeb165b11fef3f0201d436ece36aac266bbfbb 100644 (file)
@@ -1,3 +1,28 @@
+2018-09-17  Simon Marchi  <simon.marchi@polymtl.ca>
+
+       PR python/23669
+       * breakpoint.c (commands_cmd_element): New.
+       (_initialize_breakpoint): Assign commands_cmd_element.
+       * breakpoint.h (commands_cmd_element): New.
+       * cli/cli-script.c (while_cmd_element, if_command,
+       define_cmd_element): New.
+       (command_name_equals): Remove.
+       (process_next_line): Compare commands by pointer, not by name.
+       (_initialize_cli_script): Assign the various cmd_list_element
+       variables.
+       * compile/compile.c (compile_cmd_element): New.
+       (_initialize_compile): Assign compile_cmd_element.
+       * compile/compile.h (compile_cmd_element): New.
+       * guile/guile.c (guile_cmd_element): New.
+       (install_gdb_commands): Assign guile_cmd_element.
+       * guile/guile.h (guile_cmd_element): New.
+       * python/python.c (python_cmd_element): New.
+       (_initialize_python): Assign python_cmd_element.
+       * python/python.h (python_cmd_element): New.
+       * tracepoint.c (while_stepping_cmd_element): New.
+       (_initialize_tracepoint): Assign while_stepping_cmd_element.
+       * tracepoint.h (while_stepping_cmd_element): New.
+
 2018-09-15  Sergio Durigan Junior  <sergiodj@redhat.com>
 
        PR gdb/23663
index 2c031c053a6601b3e308ff712939e8bd9ce685f7..f63801f192601b30e744ddcc5c6710d4f411bd42 100644 (file)
@@ -15522,6 +15522,10 @@ initialize_breakpoint_ops (void)
 
 static struct cmd_list_element *enablebreaklist = NULL;
 
+/* See breakpoint.h.  */
+
+cmd_list_element *commands_cmd_element = nullptr;
+
 void
 _initialize_breakpoint (void)
 {
@@ -15547,7 +15551,8 @@ _initialize_breakpoint (void)
 Set ignore-count of breakpoint number N to COUNT.\n\
 Usage is `ignore N COUNT'."));
 
-  add_com ("commands", class_breakpoint, commands_command, _("\
+  commands_cmd_element = add_com ("commands", class_breakpoint,
+                                 commands_command, _("\
 Set commands to be executed when the given breakpoints are hit.\n\
 Give a space-separated breakpoint list as argument after \"commands\".\n\
 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
index 7da9452fd1539fbdcbb6480a683972395bc58691..6681ba2a7734ea0b98227db948ea3f2371d87a56 100644 (file)
@@ -1682,4 +1682,7 @@ extern void maybe_print_thread_hit_breakpoint (struct ui_out *uiout);
 /* Print the specified breakpoint.  */
 extern void print_breakpoint (breakpoint *bp);
 
+/* Command element for the 'commands' command.  */
+extern cmd_list_element *commands_cmd_element;
+
 #endif /* !defined (BREAKPOINT_H) */
index fef03d353e1d2a115e43fdf8c59b74d7ce10ee0a..1c5f37c557162412664af7284bedd951dc449a19 100644 (file)
@@ -25,6 +25,7 @@
 #include "ui-out.h"
 #include "top.h"
 #include "breakpoint.h"
+#include "tracepoint.h"
 #include "cli/cli-cmds.h"
 #include "cli/cli-decode.h"
 #include "cli/cli-script.h"
@@ -33,6 +34,8 @@
 #include "interps.h"
 #include "compile/compile.h"
 #include "common/gdb_string_view.h"
+#include "python/python.h"
+#include "guile/guile.h"
 
 #include <vector>
 
@@ -58,6 +61,15 @@ static int command_nest_depth = 1;
 /* This is to prevent certain commands being printed twice.  */
 static int suppress_next_print_command_trace = 0;
 
+/* Command element for the 'while' command.  */
+static cmd_list_element *while_cmd_element = nullptr;
+
+/* Command element for the 'if' command.  */
+static cmd_list_element *if_cmd_element = nullptr;
+
+/* Command element for the 'define' command.  */
+static cmd_list_element *define_cmd_element = nullptr;
+
 /* Structure for arguments to user defined functions.  */
 
 class user_args
@@ -906,16 +918,6 @@ read_next_line (void)
   return command_line_input (prompt_ptr, "commands");
 }
 
-/* Return true if CMD's name is NAME.  */
-
-static bool
-command_name_equals (struct cmd_list_element *cmd, const char *name)
-{
-  return (cmd != NULL
-         && cmd != CMD_LIST_AMBIGUOUS
-         && strcmp (cmd->name, name) == 0);
-}
-
 /* Given an input line P, skip the command and return a pointer to the
    first argument.  */
 
@@ -990,7 +992,7 @@ process_next_line (const char *p, struct command_line **command,
 
       /* Check for while, if, break, continue, etc and build a new
         command line structure for them.  */
-      if (command_name_equals (cmd, "while-stepping"))
+      if (cmd == while_stepping_cmd_element)
        {
          /* Because validate_actionline and encode_action lookup
             command's line as command, we need the line to
@@ -1005,34 +1007,28 @@ process_next_line (const char *p, struct command_line **command,
             not.  */
          *command = build_command_line (while_stepping_control, p);
        }
-      else if (command_name_equals (cmd, "while"))
-       {
-         *command = build_command_line (while_control, line_first_arg (p));
-       }
-      else if (command_name_equals (cmd, "if"))
-       {
-         *command = build_command_line (if_control, line_first_arg (p));
-       }
-      else if (command_name_equals (cmd, "commands"))
-       {
-         *command = build_command_line (commands_control, line_first_arg (p));
-       }
-      else if (command_name_equals (cmd, "define"))
+      else if (cmd == while_cmd_element)
+       *command = build_command_line (while_control, line_first_arg (p));
+      else if (cmd == if_cmd_element)
+       *command = build_command_line (if_control, line_first_arg (p));
+      else if (cmd == commands_cmd_element)
+       *command = build_command_line (commands_control, line_first_arg (p));
+      else if (cmd == define_cmd_element)
        *command = build_command_line (define_control, line_first_arg (p));
-      else if (command_name_equals (cmd, "python") && !inline_cmd)
+      else if (cmd == python_cmd_element && !inline_cmd)
        {
          /* Note that we ignore the inline "python command" form
             here.  */
          *command = build_command_line (python_control, "");
        }
-      else if (command_name_equals (cmd, "compile") && !inline_cmd)
+      else if (cmd == compile_cmd_element && !inline_cmd)
        {
          /* Note that we ignore the inline "compile command" form
             here.  */
          *command = build_command_line (compile_control, "");
          (*command)->control_u.compile.scope = COMPILE_I_INVALID_SCOPE;
        }
-      else if (command_name_equals (cmd, "guile") && !inline_cmd)
+      else if (cmd == guile_cmd_element && !inline_cmd)
        {
          /* Note that we ignore the inline "guile command" form here.  */
          *command = build_command_line (guile_control, "");
@@ -1597,7 +1593,7 @@ _initialize_cli_script (void)
 Document a user-defined command.\n\
 Give command name as argument.  Give documentation on following lines.\n\
 End with a line of just \"end\"."));
-  add_com ("define", class_support, define_command, _("\
+  define_cmd_element = add_com ("define", class_support, define_command, _("\
 Define a new command name.  Command name is argument.\n\
 Definition appears on following lines, one command per line.\n\
 End with a line of just \"end\".\n\
@@ -1606,13 +1602,13 @@ Commands defined in this way may accept an unlimited number of arguments\n\
 accessed via $arg0 .. $argN.  $argc tells how many arguments have\n\
 been passed."));
 
-  add_com ("while", class_support, while_command, _("\
+  while_cmd_element = add_com ("while", class_support, while_command, _("\
 Execute nested commands WHILE the conditional expression is non zero.\n\
 The conditional expression must follow the word `while' and must in turn be\n\
 followed by a new line.  The nested commands must be entered one per line,\n\
 and should be terminated by the word `end'."));
 
-  add_com ("if", class_support, if_command, _("\
+  if_cmd_element = add_com ("if", class_support, if_command, _("\
 Execute nested commands once IF the conditional expression is non zero.\n\
 The conditional expression must follow the word `if' and must in turn be\n\
 followed by a new line.  The nested commands must be entered one per line,\n\
index be1cca0862a646beaeb3d59734702b6d288f1462..5fc329e97d559377d7f27f485f8f8711358d1427 100644 (file)
@@ -691,13 +691,16 @@ compile_register_name_demangle (struct gdbarch *gdbarch,
   error (_("Cannot find gdbarch register \"%s\"."), regname);
 }
 
+/* See compile.h.  */
+cmd_list_element *compile_cmd_element = nullptr;
+
 void
 _initialize_compile (void)
 {
   struct cmd_list_element *c = NULL;
 
-  add_prefix_cmd ("compile", class_obscure, compile_command,
-                 _("\
+  compile_cmd_element = add_prefix_cmd ("compile", class_obscure,
+                                       compile_command, _("\
 Command to compile source code and inject it into the inferior."),
                  &compile_command_list, "compile ", 1, &cmdlist);
   add_com_alias ("expression", "compile", class_obscure, 0);
index 6e03d7e97be8b78bdee6bf339cbcf428c22f65ff..bb2ba8797a3fc1e396a5f58b79ed011aa42429da 100644 (file)
@@ -103,4 +103,7 @@ extern void compile_dwarf_bounds_to_c (string_file &stream,
 
 extern void compile_print_value (struct value *val, void *data_voidp);
 
+/* Command element for the 'compile' command.  */
+extern cmd_list_element *compile_cmd_element;
+
 #endif /* GDB_COMPILE_H */
index 0bbbf6eac1f238d1a8974ce220244ddeb2145a29..bb1366e69d286a5e935ec5ea9f13a6ec1744c44e 100644 (file)
@@ -724,6 +724,9 @@ gdbscm_set_backtrace (int enable)
 
 #endif /* HAVE_GUILE */
 
+/* See guile.h.  */
+cmd_list_element *guile_cmd_element = nullptr;
+
 /* Install the various gdb commands used by Guile.  */
 
 static void
@@ -749,7 +752,7 @@ This command is only a placeholder.")
 
   /* Since "help guile" is easy to type, and intuitive, we add general help
      in using GDB+Guile to this command.  */
-  add_com ("guile", class_obscure, guile_command,
+  guile_cmd_element = add_com ("guile", class_obscure, guile_command,
 #ifdef HAVE_GUILE
           _("\
 Evaluate one or more Guile expressions.\n\
index aaa005816e626faef62dba93b699eb8223bcdf4c..2faa23da814c81ead027a22ce24c37e63fc5c9bd 100644 (file)
@@ -25,4 +25,7 @@
 /* This is all that guile exports to gdb.  */
 extern const struct extension_language_defn extension_language_guile;
 
+/* Command element for the 'guile' command.  */
+extern cmd_list_element *guile_cmd_element;
+
 #endif /* GDB_GUILE_H */
index 20fc674f20ad6cd686976eaa879f7325ea39b17f..55119cc6143d916beee3a91b4c960e14be3b0a97 100644 (file)
@@ -1839,6 +1839,9 @@ do_start_initialization ()
 
 #endif /* HAVE_PYTHON */
 
+/* See python.h.  */
+cmd_list_element *python_cmd_element = nullptr;
+
 void
 _initialize_python (void)
 {
@@ -1868,7 +1871,7 @@ This command is only a placeholder.")
           );
   add_com_alias ("pi", "python-interactive", class_obscure, 1);
 
-  add_com ("python", class_obscure, python_command,
+  python_cmd_element = add_com ("python", class_obscure, python_command,
 #ifdef HAVE_PYTHON
           _("\
 Evaluate a Python command.\n\
index de58fd81df83f196258950cf512733df015fe121..4b36f8385313fe9f7aa4c8dac14fd3870936baf9 100644 (file)
@@ -25,4 +25,7 @@
 /* This is all that python exports to gdb.  */
 extern const struct extension_language_defn extension_language_python;
 
+/* Command element for the 'python' command.  */
+extern cmd_list_element *python_cmd_element;
+
 #endif /* GDB_PYTHON_H */
index b4a55605b17b5a08b4c1abc63da8bc81dc3b3183..4d53bad0ab654acdbd673f61278661b3a6b79f1e 100644 (file)
@@ -1,3 +1,8 @@
+2018-09-17  Simon Marchi  <simon.marchi@polymtl.ca>
+
+       PR python/23669
+       * gdb.python/python.exp: Test gdb.execute("show commands").
+
 2018-09-13  Tom Tromey  <tom@tromey.com>
 
        PR rust/23650:
index 1d7ea5b6b4b2e2cd175ef1d6cd1267bf33889b6c..a37113c17c7b13f0fdfc00911afb8ed06fe80881 100644 (file)
@@ -497,3 +497,7 @@ gdb_test "python print(gdb.convenience_variable('cvar3'))" "None" \
     "print reset convenience variable from python"
 gdb_test "print \$cvar3" "= void" \
     "print reset convenience variable from CLI"
+
+# Test PR 23669, the following would invoke the "commands" command instead of
+# "show commands".
+gdb_test "python gdb.execute(\"show commands\")" "$decimal  print \\\$cvar3.*"
index 2f0715a0dd82a69f354a9841822d30f21f4d525a..d73517dcc34601fef5c41df9730fe7073c2a593b 100644 (file)
@@ -3935,6 +3935,9 @@ static const struct internalvar_funcs sdata_funcs =
   NULL
 };
 
+/* See tracepoint.h.  */
+cmd_list_element *while_stepping_cmd_element = nullptr;
+
 /* module initialization */
 void
 _initialize_tracepoint (void)
@@ -4046,7 +4049,8 @@ Entering \"end\" on a line by itself is the normal way to terminate\n\
 such a list.\n\n\
 Note: the \"end\" command cannot be used at the gdb prompt."));
 
-  add_com ("while-stepping", class_trace, while_stepping_pseudocommand, _("\
+  while_stepping_cmd_element = add_com ("while-stepping", class_trace,
+                                       while_stepping_pseudocommand, _("\
 Specify single-stepping behavior at a tracepoint.\n\
 Argument is number of instructions to trace in single-step mode\n\
 following the tracepoint.  This command is normally followed by\n\
index 42e413018a902db49f79997032c94822d94b0a77..06cbee6694f0bcb9a46b2c6ac1b00bc0856d93ee 100644 (file)
@@ -431,4 +431,7 @@ extern struct traceframe_info *get_traceframe_info (void);
 
 extern struct bp_location *get_traceframe_location (int *stepping_frame_p);
 
+/* Command element for the 'while-stepping' command.  */
+extern cmd_list_element *while_stepping_cmd_element;
+
 #endif /* TRACEPOINT_H */