[PATCH v2 0/4] Introduce the "with" command

classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH v2 0/4] Introduce the "with" command

Pedro Alves-7
( See original discussion and prototype here:
   https://sourceware.org/ml/gdb-patches/2019-05/msg00570.html )

 (gdb) help with
 Temporarily set SETTING to VALUE, run COMMAND, and restore SETTING.
 Usage: with SETTING [VALUE] [-- COMMAND]
 Usage: w SETTING [VALUE] [-- COMMAND]
 With no COMMAND, repeats the last executed command.
 SETTING is any setting settable with the "set" command.
 E.g.:
   with language pascal -- print obj
   with print elements unlimited -- print obj

More details in patch #4.

New in v2:

 - Now a series of 4 patches instead of a single patch.  The main
   patch is patch #4.  This patch includes documentation bits.

 - The "with" command's implementation moved from printcmd.c to
   cli/cli-cmds.c, near "show".

 - Philippe pointed out a bug in v1.  The issue was related to how on
   var_uinteger commands, "unlimited" is user-visible as "0", but
   stored internally in the command's control variable as "-1".
   Without proper internal/user-visible translation, restoring a
   setting's original value failed, if the setting was originally set
   to "unlimited".  In order to fix that, in v2 I'm reusing code from
   do_show_command to convert the set/show command's control variable
   to a string representation.

 - In order to thoroughly test the point above, I thought I'd reuse
   the recently introduced "maint test-settings set/show
   uinteger/zuinteger-unlimited/..." commands, in order to test "with"
   against all command type variants (all enum var_types).  But,
   instead of adding a new "maint test-settings with" command just for
   that, I thought that it was better if we added a more broadly
   usable "maint with" command that worked with all maintenance
   settings.  So the series includes a patch to rename "maint
   test-settings set/show" to "maint set/show test-settings".  That's
   patch #3.  That patch includes documentation bits, though
   of borderline-obvious kind.

- Making the new gdb.base/with.exp testcase exercise "maint with
  test-settings" uncovered bugs in the "maint set/show test-settings"
  settings.  Those are fixed by patch #1.

Pedro Alves (4):
  Fix defaults of some "maint test-settings" subcommands
  Fix a few comments in maint-test-settings.c
  "maint test-settings set/show" -> "maint set/show test-settings"
  Introduce the "with" command

 gdb/doc/gdb.texinfo                 | 101 +++++++++++++-
 gdb/NEWS                            |  18 ++-
 gdb/cli/cli-cmds.c                  | 129 +++++++++++++++++-
 gdb/cli/cli-cmds.h                  |  13 ++
 gdb/cli/cli-setshow.c               |  74 +++++-----
 gdb/cli/cli-setshow.h               |   5 +
 gdb/command.h                       |  19 +--
 gdb/maint-test-settings.c           | 167 +++++++++++------------
 gdb/maint.c                         |  27 ++++
 gdb/testsuite/gdb.base/settings.exp |  35 ++---
 gdb/testsuite/gdb.base/with.c       |  41 ++++++
 gdb/testsuite/gdb.base/with.exp     | 261 ++++++++++++++++++++++++++++++++++++
 gdb/top.c                           |   7 +-
 13 files changed, 738 insertions(+), 159 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/with.c
 create mode 100644 gdb/testsuite/gdb.base/with.exp

--
2.14.5

Reply | Threaded
Open this post in threaded view
|

[PATCH v2 1/4] Fix defaults of some "maint test-settings" subcommands

Pedro Alves-7
New tests added later for the incoming "with" command exposed a couple
invalid-default-value bugs in the "maint test-settings" commands:

- var_filename commands don't allow setting the filename to the empty
  string (unlike var_optional_filename commands), yet, "maint
  test-settings filename"'s control variable was not initialized, so
  on startup, "maint test-settings show filename" shows an empty
  string.

- "maint test-settings enum"'s control variable was not initialized,
  so on startup, "maint test-settings show enum" shows an empty value
  instead of a valid enum value.

Both issues are fixed by initializing the control variables.

gdb/ChangeLog:
yyyy-mm-dd  Pedro Alves  <[hidden email]>

        * maint-test-settings.c (maintenance_test_settings_xxx)
        (maintenance_test_settings_yyy, maintenance_test_settings_zzz):
        New.
        (maintenance_test_settings_enums): Use them.
        (maintenance_test_settings_enum): Default to
        maintenance_test_settings_xxx.
        (_initialize_maint_test_settings): Initialize
        MAINTENANCE_TEST_SETTINGS_FILENAME.

gdb/testsuite/ChangeLog:
yyyy-mm-dd  Pedro Alves  <[hidden email]>

        * gdb.base/settings.exp (test-string): Adjust expected out when
        testing "maint test-settings show filename"
---
 gdb/maint-test-settings.c           | 16 +++++++++++++---
 gdb/testsuite/gdb.base/settings.exp |  9 ++++++---
 2 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/gdb/maint-test-settings.c b/gdb/maint-test-settings.c
index fa13519eb96..79e002e727f 100644
--- a/gdb/maint-test-settings.c
+++ b/gdb/maint-test-settings.c
@@ -85,14 +85,22 @@ static char *maintenance_test_settings_optional_filename;
 
 static char *maintenance_test_settings_filename;
 
-static const char *maintenance_test_settings_enum;
-
 /* Enum values for the "maintenance test-settings set/show boolean"
    commands.  */
+static const char maintenance_test_settings_xxx[] = "xxx";
+static const char maintenance_test_settings_yyy[] = "yyy";
+static const char maintenance_test_settings_zzz[] = "zzz";
+
 static const char *const maintenance_test_settings_enums[] = {
-  "xxx", "yyy", "zzz", nullptr
+  maintenance_test_settings_xxx,
+  maintenance_test_settings_yyy,
+  maintenance_test_settings_zzz,
+  nullptr
 };
 
+static const char *maintenance_test_settings_enum
+  = maintenance_test_settings_xxx;
+
 /* The "maintenance test-options show xxx" commands.  */
 
 static void
@@ -107,6 +115,8 @@ maintenance_test_settings_show_value_cmd
 void
 _initialize_maint_test_settings (void)
 {
+  maintenance_test_settings_filename = xstrdup ("/foo/bar");
+
   add_prefix_cmd ("test-settings", no_class,
   maintenance_test_settings_cmd,
   _("\
diff --git a/gdb/testsuite/gdb.base/settings.exp b/gdb/testsuite/gdb.base/settings.exp
index aeca67c0e7f..b691ad8cf68 100644
--- a/gdb/testsuite/gdb.base/settings.exp
+++ b/gdb/testsuite/gdb.base/settings.exp
@@ -447,9 +447,12 @@ proc test-string {variant} {
     set set_cmd "maint test-settings set $variant"
     set show_cmd "maint test-settings show $variant"
 
-    # Empty string.  Also checks that gdb doesn't crash if we haven't
-    # set the string yet.
-    gdb_test "$show_cmd" "^$show_cmd\r\n" "$show_cmd: empty first time"
+    # Checks that gdb doesn't crash if we haven't set the string yet.
+    if {$variant != "filename"} {
+ gdb_test "$show_cmd" "^$show_cmd\r\n" "$show_cmd: show default"
+    } else {
+ gdb_test "$show_cmd" "/foo/bar" "$show_cmd: show default"
+    }
 
     # A string value.
     gdb_test_no_output "$set_cmd hello world"
--
2.14.5

Reply | Threaded
Open this post in threaded view
|

[PATCH v2 2/4] Fix a few comments in maint-test-settings.c

Pedro Alves-7
In reply to this post by Pedro Alves-7
Fix the file's intro comment, and s/test-options/test-settings/.

gdb/ChangeLog:
yyyy-mm-dd  Pedro Alves  <[hidden email]>

        * maint-test-settings.c: Fix file's intro comment.  Replace all
        references to "test-options" with references to "test-settings",
        in comments.
---
 gdb/maint-test-settings.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/gdb/maint-test-settings.c b/gdb/maint-test-settings.c
index 79e002e727f..5075ed1aac0 100644
--- a/gdb/maint-test-settings.c
+++ b/gdb/maint-test-settings.c
@@ -1,4 +1,4 @@
-/* Support for GDB maintenance commands.
+/* Maintenance commands for testing the settings framework.
 
    Copyright (C) 2019 Free Software Foundation, Inc.
 
@@ -28,7 +28,7 @@ static cmd_list_element *maintenance_test_settings_list;
 static cmd_list_element *maintenance_test_settings_set_list;
 static cmd_list_element *maintenance_test_settings_show_list;
 
-/* The "maintenance test-options" prefix command.  */
+/* The "maintenance test-settings" prefix command.  */
 
 static void
 maintenance_test_settings_cmd (const char *arg, int from_tty)
@@ -40,7 +40,7 @@ maintenance_test_settings_cmd (const char *arg, int from_tty)
      all_commands, gdb_stdout);
 }
 
-/* The "maintenance test-options set" prefix command.  */
+/* The "maintenance test-settings set" prefix command.  */
 
 static void
 maintenance_test_settings_set_cmd (const char *args, int from_tty)
@@ -52,7 +52,7 @@ maintenance_test_settings_set_cmd (const char *args, int from_tty)
      all_commands, gdb_stdout);
 }
 
-/* The "maintenance test-options show" prefix command.  */
+/* The "maintenance test-settings show" prefix command.  */
 
 static void
 maintenance_test_settings_show_cmd (const char *args, int from_tty)
@@ -60,7 +60,7 @@ maintenance_test_settings_show_cmd (const char *args, int from_tty)
   cmd_show_list (maintenance_test_settings_show_list, from_tty, "");
 }
 
-/* Control variables for all the "maintenance test-options set/show
+/* Control variables for all the "maintenance test-settings set/show
    xxx" commands.  */
 
 static int maintenance_test_settings_boolean;
@@ -101,7 +101,7 @@ static const char *const maintenance_test_settings_enums[] = {
 static const char *maintenance_test_settings_enum
   = maintenance_test_settings_xxx;
 
-/* The "maintenance test-options show xxx" commands.  */
+/* The "maintenance test-settings show xxx" commands.  */
 
 static void
 maintenance_test_settings_show_value_cmd
--
2.14.5

Reply | Threaded
Open this post in threaded view
|

[PATCH v2 3/4] "maint test-settings set/show" -> "maint set/show test-settings"

Pedro Alves-7
In reply to this post by Pedro Alves-7
This commit renames "maint test-settings set/show" to "maint set/show
test-settings".

This helps the following patch, which introduce a "maint with" command
what works with all "maint set" settings.

gdb/ChangeLog:
yyyy-mm-dd  Pedro Alves  <[hidden email]>

        * NEWS (New commands): Mention "maint set/show test-settings"
        instead of "maint test-settings".
        * maint-test-settings.c (maintenance_test_settings_list): Delete.
        (maintenance_test_settings_set_list): Rename to ...
        (maintenance_set_test_settings_list): ... this.
        (maintenance_test_settings_show_list): Rename to  ...
        (maintenance_show_test_settings_list): ... this.
        (maintenance_test_settings_cmd): Delete.
        (maintenance_test_settings_set_cmd): ...
        (maintenance_set_test_settings_cmd): ... this.
        (maintenance_test_settings_show_cmd): ...
        (maintenance_show_test_settings_cmd): ... this.
        (maintenance_test_settings_show_value_cmd):
        (maintenance_show_test_settings_value_cmd): ... this.
        (_initialize_maint_test_settings): No longer install the "maint
        test-settings" prefix command.  Rename "maint test-settings set"
        to "maint set test-settings", and "maint test-settings show" to
        "maint show test-settings".  Adjust all subcommands.

gdb/doc/ChangeLog:
yyyy-mm-dd  Pedro Alves  <[hidden email]>

        * gdb.texinfo (Maintenance Commands): Document "maint set/show
        test-settings" instead of "maint test-settings set/show".

gdb/testsuite/ChangeLog:
yyyy-mm-dd  Pedro Alves  <[hidden email]>

        * gdb.base/settings.exp: Replace all references to "maint
        test-settings set" with references to "maint set test-settings",
        and all references to "maint test-settings show" with references
        to "maint show test-settings".
---
 gdb/doc/gdb.texinfo                 |   7 +-
 gdb/NEWS                            |   3 +-
 gdb/maint-test-settings.c           | 149 ++++++++++++++++--------------------
 gdb/testsuite/gdb.base/settings.exp |  26 +++----
 4 files changed, 83 insertions(+), 102 deletions(-)

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 9a0320e5d8f..a279a145502 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -37626,9 +37626,10 @@ Shows the result of completing the @code{maint test-options}
 subcommands.  This is used by the testsuite to validate completion
 support in the command options framework.
 
-@kindex maint test-settings
-@item maint test-settings set @var{kind}
-@itemx maint test-settings show @var{kind}
+@kindex maint set test-settings
+@kindex maint show test-settings
+@item maint set test-settings @var{kind}
+@itemx maint show test-settings @var{kind}
 These are representative commands for each @var{kind} of setting type
 @value{GDBN} supports.  They are used by the testsuite for exercising
 the settings infrastructure.
diff --git a/gdb/NEWS b/gdb/NEWS
index 62521ed75a1..4a7a117970a 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -84,7 +84,8 @@ set style highlight background COLOR
 set style highlight intensity VALUE
   Control the styling of highlightings.
 
-maint test-settings KIND
+maint set test-settings KIND
+maint show test-settings KIND
   A set of commands used by the testsuite for exercising the settings
   infrastructure.
 
diff --git a/gdb/maint-test-settings.c b/gdb/maint-test-settings.c
index 5075ed1aac0..b549b8d39bf 100644
--- a/gdb/maint-test-settings.c
+++ b/gdb/maint-test-settings.c
@@ -21,46 +21,33 @@
 #include "defs.h"
 #include "gdbcmd.h"
 
-/* Command list for "maint test-settings".  */
-static cmd_list_element *maintenance_test_settings_list;
+/* Command list for "maint set test-settings".  */
+static cmd_list_element *maintenance_set_test_settings_list;
 
-/* Command list for "maint test-settings set/show".  */
-static cmd_list_element *maintenance_test_settings_set_list;
-static cmd_list_element *maintenance_test_settings_show_list;
+/* Command list for "maint show test-settings".  */
+static cmd_list_element *maintenance_show_test_settings_list;
 
-/* The "maintenance test-settings" prefix command.  */
+/* The "maintenance set test-settings" prefix command.  */
 
 static void
-maintenance_test_settings_cmd (const char *arg, int from_tty)
+maintenance_set_test_settings_cmd (const char *args, int from_tty)
 {
-  printf_unfiltered
-    (_("\"maintenance test-settings\" must be followed "
-       "by the name of a subcommand.\n"));
-  help_list (maintenance_test_settings_list, "maintenance test-settings ",
-     all_commands, gdb_stdout);
-}
-
-/* The "maintenance test-settings set" prefix command.  */
-
-static void
-maintenance_test_settings_set_cmd (const char *args, int from_tty)
-{
-  printf_unfiltered (_("\"maintenance test-settings set\" must be followed "
+  printf_unfiltered (_("\"maintenance set test-settings\" must be followed "
        "by the name of a set command.\n"));
-  help_list (maintenance_test_settings_set_list,
-     "maintenance test-settings set ",
+  help_list (maintenance_set_test_settings_list,
+     "maintenance set test-settings ",
      all_commands, gdb_stdout);
 }
 
-/* The "maintenance test-settings show" prefix command.  */
+/* The "maintenance show test-settings" prefix command.  */
 
 static void
-maintenance_test_settings_show_cmd (const char *args, int from_tty)
+maintenance_show_test_settings_cmd (const char *args, int from_tty)
 {
-  cmd_show_list (maintenance_test_settings_show_list, from_tty, "");
+  cmd_show_list (maintenance_show_test_settings_list, from_tty, "");
 }
 
-/* Control variables for all the "maintenance test-settings set/show
+/* Control variables for all the "maintenance set/show test-settings
    xxx" commands.  */
 
 static int maintenance_test_settings_boolean;
@@ -85,7 +72,7 @@ static char *maintenance_test_settings_optional_filename;
 
 static char *maintenance_test_settings_filename;
 
-/* Enum values for the "maintenance test-settings set/show boolean"
+/* Enum values for the "maintenance set/show test-settings boolean"
    commands.  */
 static const char maintenance_test_settings_xxx[] = "xxx";
 static const char maintenance_test_settings_yyy[] = "yyy";
@@ -101,10 +88,10 @@ static const char *const maintenance_test_settings_enums[] = {
 static const char *maintenance_test_settings_enum
   = maintenance_test_settings_xxx;
 
-/* The "maintenance test-settings show xxx" commands.  */
+/* The "maintenance show test-settings xxx" commands.  */
 
 static void
-maintenance_test_settings_show_value_cmd
+maintenance_show_test_settings_value_cmd
   (struct ui_file *file, int from_tty,
    struct cmd_list_element *c, const char *value)
 {
@@ -117,29 +104,21 @@ _initialize_maint_test_settings (void)
 {
   maintenance_test_settings_filename = xstrdup ("/foo/bar");
 
-  add_prefix_cmd ("test-settings", no_class,
-  maintenance_test_settings_cmd,
-  _("\
-Generic command for testing the settings infrastructure."),
-  &maintenance_test_settings_list,
-  "maintenance test-settings ", 0,
-  &maintenancelist);
-
-  add_prefix_cmd ("set", class_maintenance,
-  maintenance_test_settings_set_cmd, _("\
+  add_prefix_cmd ("test-settings", class_maintenance,
+  maintenance_set_test_settings_cmd, _("\
 Set GDB internal variables used for set/show command infrastructure testing."),
-  &maintenance_test_settings_set_list,
-  "maintenance test-settings set ",
+  &maintenance_set_test_settings_list,
+  "maintenance set test-settings ",
   0/*allow-unknown*/,
-  &maintenance_test_settings_list);
+  &maintenance_set_cmdlist);
 
-  add_prefix_cmd ("show", class_maintenance,
-  maintenance_test_settings_show_cmd, _("\
+  add_prefix_cmd ("test-settings", class_maintenance,
+  maintenance_show_test_settings_cmd, _("\
 Show GDB internal variables used for set/show command infrastructure testing."),
-  &maintenance_test_settings_show_list,
-  "maintenance test-settings show ",
+  &maintenance_show_test_settings_list,
+  "maintenance show test-settings ",
   0/*allow-unknown*/,
-  &maintenance_test_settings_list);
+  &maintenance_show_cmdlist);
 
   add_setshow_boolean_cmd ("boolean", class_maintenance,
    &maintenance_test_settings_boolean, _("\
@@ -147,9 +126,9 @@ command used for internal testing"), _("\
 command used for internal testing"),
    nullptr, /* help_doc */
    nullptr, /* set_cmd */
-   maintenance_test_settings_show_value_cmd,
-   &maintenance_test_settings_set_list,
-   &maintenance_test_settings_show_list);
+   maintenance_show_test_settings_value_cmd,
+   &maintenance_set_test_settings_list,
+   &maintenance_show_test_settings_list);
 
   add_setshow_auto_boolean_cmd ("auto-boolean", class_maintenance,
  &maintenance_test_settings_auto_boolean, _("\
@@ -157,19 +136,19 @@ command used for internal testing"), _("\
 command used for internal testing"),
  nullptr, /* help_doc */
  nullptr, /* set_cmd */
- maintenance_test_settings_show_value_cmd,
- &maintenance_test_settings_set_list,
- &maintenance_test_settings_show_list);
+ maintenance_show_test_settings_value_cmd,
+ &maintenance_set_test_settings_list,
+ &maintenance_show_test_settings_list);
 
   add_setshow_uinteger_cmd ("uinteger", class_maintenance,
    &maintenance_test_settings_uinteger, _("\
 command used for internal testing"), _("\
 command used for internal testing"),
-   nullptr, /* help_doc */
-   nullptr, /* set_cmd */
-   maintenance_test_settings_show_value_cmd,
-   &maintenance_test_settings_set_list,
-   &maintenance_test_settings_show_list);
+    nullptr, /* help_doc */
+    nullptr, /* set_cmd */
+    maintenance_show_test_settings_value_cmd,
+    &maintenance_set_test_settings_list,
+    &maintenance_show_test_settings_list);
 
   add_setshow_integer_cmd ("integer", class_maintenance,
    &maintenance_test_settings_integer, _("\
@@ -177,9 +156,9 @@ command used for internal testing"), _("\
 command used for internal testing"),
    nullptr, /* help_doc */
    nullptr, /* set_cmd */
-   maintenance_test_settings_show_value_cmd,
-   &maintenance_test_settings_set_list,
-   &maintenance_test_settings_show_list);
+   maintenance_show_test_settings_value_cmd,
+   &maintenance_set_test_settings_list,
+   &maintenance_show_test_settings_list);
 
   add_setshow_string_cmd ("string", class_maintenance,
      &maintenance_test_settings_string, _("\
@@ -187,9 +166,9 @@ command used for internal testing"), _("\
 command used for internal testing"),
      nullptr, /* help_doc */
      nullptr, /* set_cmd */
-     maintenance_test_settings_show_value_cmd,
-     &maintenance_test_settings_set_list,
-     &maintenance_test_settings_show_list);
+  maintenance_show_test_settings_value_cmd,
+  &maintenance_set_test_settings_list,
+  &maintenance_show_test_settings_list);
 
   add_setshow_string_noescape_cmd
     ("string-noescape", class_maintenance,
@@ -198,9 +177,9 @@ command used for internal testing"), _("\
 command used for internal testing"),
      nullptr, /* help_doc */
      nullptr, /* set_cmd */
-     maintenance_test_settings_show_value_cmd,
-     &maintenance_test_settings_set_list,
-     &maintenance_test_settings_show_list);
+     maintenance_show_test_settings_value_cmd,
+     &maintenance_set_test_settings_list,
+     &maintenance_show_test_settings_list);
 
   add_setshow_optional_filename_cmd
     ("optional-filename", class_maintenance,
@@ -209,9 +188,9 @@ command used for internal testing"), _("\
 command used for internal testing"),
      nullptr, /* help_doc */
      nullptr, /* set_cmd */
-     maintenance_test_settings_show_value_cmd,
-     &maintenance_test_settings_set_list,
-     &maintenance_test_settings_show_list);
+     maintenance_show_test_settings_value_cmd,
+     &maintenance_set_test_settings_list,
+     &maintenance_show_test_settings_list);
 
   add_setshow_filename_cmd ("filename", class_maintenance,
     &maintenance_test_settings_filename, _("\
@@ -219,9 +198,9 @@ command used for internal testing"), _("\
 command used for internal testing"),
     nullptr, /* help_doc */
     nullptr, /* set_cmd */
-    maintenance_test_settings_show_value_cmd,
-    &maintenance_test_settings_set_list,
-    &maintenance_test_settings_show_list);
+    maintenance_show_test_settings_value_cmd,
+    &maintenance_set_test_settings_list,
+    &maintenance_show_test_settings_list);
 
   add_setshow_zinteger_cmd ("zinteger", class_maintenance,
     &maintenance_test_settings_zinteger, _("\
@@ -229,9 +208,9 @@ command used for internal testing"), _("\
 command used for internal testing"),
     nullptr, /* help_doc */
     nullptr, /* set_cmd */
-    maintenance_test_settings_show_value_cmd,
-    &maintenance_test_settings_set_list,
-    &maintenance_test_settings_show_list);
+    maintenance_show_test_settings_value_cmd,
+    &maintenance_set_test_settings_list,
+    &maintenance_show_test_settings_list);
 
   add_setshow_zuinteger_cmd ("zuinteger", class_maintenance,
      &maintenance_test_settings_zuinteger, _("\
@@ -239,9 +218,9 @@ command used for internal testing"), _("\
 command used for internal testing"),
      nullptr, /* help_doc */
      nullptr, /* set_cmd */
-     maintenance_test_settings_show_value_cmd,
-     &maintenance_test_settings_set_list,
-     &maintenance_test_settings_show_list);
+     maintenance_show_test_settings_value_cmd,
+     &maintenance_set_test_settings_list,
+     &maintenance_show_test_settings_list);
 
   add_setshow_zuinteger_unlimited_cmd
     ("zuinteger-unlimited", class_maintenance,
@@ -250,9 +229,9 @@ command used for internal testing"), _("\
 command used for internal testing"),
      nullptr, /* help_doc */
      nullptr, /* set_cmd */
-     maintenance_test_settings_show_value_cmd,
-     &maintenance_test_settings_set_list,
-     &maintenance_test_settings_show_list);
+     maintenance_show_test_settings_value_cmd,
+     &maintenance_set_test_settings_list,
+     &maintenance_show_test_settings_list);
 
   add_setshow_enum_cmd ("enum", class_maintenance,
  maintenance_test_settings_enums,
@@ -261,7 +240,7 @@ command used for internal testing"), _("\
 command used for internal testing"),
  nullptr, /* help_doc */
  nullptr, /* set_cmd */
- maintenance_test_settings_show_value_cmd,
- &maintenance_test_settings_set_list,
- &maintenance_test_settings_show_list);
+ maintenance_show_test_settings_value_cmd,
+ &maintenance_set_test_settings_list,
+ &maintenance_show_test_settings_list);
 }
diff --git a/gdb/testsuite/gdb.base/settings.exp b/gdb/testsuite/gdb.base/settings.exp
index b691ad8cf68..53049d6b592 100644
--- a/gdb/testsuite/gdb.base/settings.exp
+++ b/gdb/testsuite/gdb.base/settings.exp
@@ -45,8 +45,8 @@ proc show_setting {show_cmd expected_re} {
 # var_Xinteger tests.  VARIANT determines which command/variant to
 # exercise.
 proc test-integer {variant} {
-    set set_cmd "maint test-settings set $variant"
-    set show_cmd "maint test-settings show $variant"
+    set set_cmd "maint set test-settings $variant"
+    set show_cmd "maint show test-settings $variant"
 
     # A bogus value.
     gdb_test "$set_cmd bogus" \
@@ -122,7 +122,7 @@ proc test-integer {variant} {
     }
 
     if {$variant == "zuinteger"} {
- test_gdb_complete_multiple "maint test-settings set " "zuinteger" "" {
+ test_gdb_complete_multiple "maint set test-settings " "zuinteger" "" {
     "zuinteger"
     "zuinteger-unlimited"
  }
@@ -166,7 +166,7 @@ proc test-integer {variant} {
 
     # Check show command completion.
     if {$variant == "zuinteger"} {
- test_gdb_complete_multiple "maintenance test-settings show " "zuinteger" "" {
+ test_gdb_complete_multiple "maintenance show test-settings " "zuinteger" "" {
     "zuinteger"
     "zuinteger-unlimited"
  }
@@ -182,8 +182,8 @@ proc test-integer {variant} {
 proc_with_prefix test-boolean {} {
     # Use these variables to make sure we don't call the wrong command
     # by mistake.
-    set set_cmd "maint test-settings set boolean"
-    set show_cmd "maint test-settings show boolean"
+    set set_cmd "maint set test-settings boolean"
+    set show_cmd "maint show test-settings boolean"
 
     # A bogus value.
     gdb_test "$set_cmd bogus" \
@@ -277,8 +277,8 @@ proc_with_prefix test-boolean {} {
 proc_with_prefix test-auto-boolean {} {
     # Use these variables to make sure we don't call the wrong command
     # by mistake.
-    set set_cmd "maint test-settings set auto-boolean"
-    set show_cmd "maint test-settings show auto-boolean"
+    set set_cmd "maint set test-settings auto-boolean"
+    set show_cmd "maint show test-settings auto-boolean"
 
     # A bogus value.
     gdb_test "$set_cmd bogus" \
@@ -381,8 +381,8 @@ proc_with_prefix test-auto-boolean {} {
 proc_with_prefix test-enum {} {
     # Use these variables to make sure we don't call the wrong command
     # by mistake.
-    set set_cmd "maint test-settings set enum"
-    set show_cmd "maint test-settings show enum"
+    set set_cmd "maint set test-settings enum"
+    set show_cmd "maint show test-settings enum"
 
     # Missing value.
     gdb_test "$set_cmd" \
@@ -444,8 +444,8 @@ proc test-string {variant} {
 
     # Use these variables to make sure we don't call the wrong command
     # by mistake.
-    set set_cmd "maint test-settings set $variant"
-    set show_cmd "maint test-settings show $variant"
+    set set_cmd "maint set test-settings $variant"
+    set show_cmd "maint show test-settings $variant"
 
     # Checks that gdb doesn't crash if we haven't set the string yet.
     if {$variant != "filename"} {
@@ -519,7 +519,7 @@ proc test-string {variant} {
 
     # Check show command completion.
     if {$variant == "string"} {
- test_gdb_complete_multiple "maint test-settings show " "string" "" {
+ test_gdb_complete_multiple "maint show test-settings " "string" "" {
     "string"
     "string-noescape"
  }
--
2.14.5

Reply | Threaded
Open this post in threaded view
|

[PATCH v2 4/4] Introduce the "with" command

Pedro Alves-7
In reply to this post by Pedro Alves-7
( See original discussion and prototype here:
   https://sourceware.org/ml/gdb-patches/2019-05/msg00570.html )

 (gdb) help with
 Temporarily set SETTING to VALUE, run COMMAND, and restore SETTING.
 Usage: with SETTING [VALUE] [-- COMMAND]
 Usage: w SETTING [VALUE] [-- COMMAND]
 With no COMMAND, repeats the last executed command.
 SETTING is any setting settable with the "set" command.
 E.g.:
   with language pascal -- print obj
   with print elements unlimited -- print obj

As can be seen above, the "with" command is just like "set", but
instead of setting the setting permanently, it sets the setting, runs
a command and then restores the setting.

 (gdb) p g_s
 $1 = {a = 1, b = 2, c = 3}
 (gdb) with language ada -- print g_s
 $2 = (a => 1, b => 2, c => 3)
 Warning: the current language does not match this frame.
 (gdb) show language
 The current source language is "auto; currently c".
 (gdb) with print elements 100 -- with print object on -- print 1
 $3 = 1

You can shorten things a bit though, as long as unambiguous.  So this:

 (gdb) with print elements 100 -- with print object off -- print 1

is the same as:

 (gdb) w p el 100 -- w p o 0 -- p 1

Note that the patch adds a "w" alias for "with", as "w" is not
currently taken:

 (gdb) w
 Ambiguous command "w": watch, wh, whatis, where, while, while-stepping, winheight, ws.

Let me know if you'd prefer to reserve "w" for one of the other
commands above.  IMHO, this command will end up being used frequently
enough that it deserves the "w" shorthand.

A nice feature is that this is fully integrated with TAB-completion:

 (gdb) with p[TAB]
 pagination  print       prompt      python
 (gdb) with print [TAB]
 address                max-depth              static-members
 array                  max-symbolic-offset    symbol
 array-indexes          null-stop              symbol-filename
 asm-demangle           object                 symbol-loading
 demangle               pascal_static-members  thread-events
 elements               pretty                 type
 entry-values           raw                    union
 frame-arguments        repeats                vtbl
 inferior-events        sevenbit-strings
 (gdb) with print [TAB]

 (gdb) with print elements unlimited -- thread apply all -[TAB]
 -ascending  -c          -q          -s

 (gdb) with print elements unlimited -- print -[TAB]
 -address         -max-depth       -repeats         -vtbl
 -array           -null-stop       -static-members
 -array-indexes   -object          -symbol
 -elements        -pretty          -union

The main advantage of this new command compared to command options,
like the new "print -OPT", is that this command works with any
setting, and, it works nicely when you want to override a setting
while running a user-defined command, like:

 (gdb) with print pretty -- usercmd

The disadvantage is that it isn't as compact or easy to type.  I think
of command options and this command as complementary.  I think that
even with this new command, it makes sense to continue developing the
command options in the direction of exposing most-oft-used settings as
command options.

Inspired by Philippe's "/" command proposal, if no command is
specified, then the last command is re-invoked, under the overridden
setting:

 (gdb) p g_s
 $1 = {a = 1, b = 2, c = 3}
 (gdb) with language ada
 $2 = (a => 1, b => 2, c => 3)
 Warning: the current language does not match this frame.

Note: "with" requires "--" to separate the setting from the command.
It might be possible to do without that, but, I haven't tried it yet,
and I think that this can go in without it.  We can always downgrade
to making "--" optional if we manage to make it work.

On to the patch itself, the implementation of the command is simpler
than one might expect.  A few details:

- I factored out a bit from pipe_command into repeat_previous
  directly, because otherwise I'd need to copy&paste the same code and
  same error message in the with command.

- The parse_cli_var_uinteger / parse_cli_var_zuinteger_unlimited /
  do_set_command changes are necessary since we can now pass an empty
  string as argument.

- do_show_command was split in two, as a FIXME comment suggests, but
  for a different reason: we need to get a string version of a "set"
  command's value, and we already had code for that in
  do_show_command.  That code is now factored out to the new
  get_setshow_command_value_string function.

- There's a new "maint with" command added too:

   (gdb) help maint with
   Like "with", but works with "maintenance set" variables.
   Usage: maintenance with SETTING [VALUE] [-- COMMAND]
   With no COMMAND, repeats the last executed command.
   SETTING is any setting settable with the "maintenance set" subcommands.

  "with" and "maint with" share 99% of the implementation.

  This might be useful on its own, but it's also useful for testing,
  since with this, we can use the "maint set/show test-settings"
  settings for exercising the "with" machinery with all the command
  type variants (all enum var_types).  This is done in the new
  gdb/base/with.exp testcase.

The documentation bits are originally based on Philippe's docs for the
"/" command, hence the attribution in the ChangeLog.

gdb/ChangeLog:
yyyy-mm-dd  Pedro Alves  <[hidden email]>

        * NEWS (New commands): Mention "with" and "maint with".
        * cli/cli-cmds.c (with_command_1, with_command_completer_1)
        (with_command, with_command_completer): New.
        (pipe_command): Adjust to new repeat_previous
        interface.
        (_initialize_cli_cmds): Install the "with" command and its "w"
        alias.
        * cli/cli-cmds.h (with_command_1, with_command_completer_1): New
        declarations.
        * cli/cli-setshow.c (parse_cli_var_uinteger)
        (parse_cli_var_zuinteger_unlimited, do_set_command): Handle empty
        argument strings for all var_types.
        (get_setshow_command_value_string): New, factored out from ...
        (do_show_command): ... this.
        * cli/cli-setshow.h: Include <string>.
        (get_setshow_command_value_string): Declare.
        * command.h (repeat_previous): Now returns const char *.  Adjust
        comment.
        * maint.c: Include "cli/cli-cmds.h".
        (maintenance_with_cmd, maintenance_with_cmd_completer): New.
        (_initialize_maint_cmds): Register the "maintenance with" command.
        * top.c (repeat_previous): Move bits from pipe_command here:
        Return the saved command line, if any; error out if there's no
        command to relaunch.

gdb/doc/ChangeLog:
yyyy-mm-dd  Pedro Alves  <[hidden email]>
            Philippe Waroquiers  <[hidden email]>

        * gdb.texinfo (Command Settings): New node documenting the general
        concept of settings, how to change them, and the new "with"
        command.
        (Maintenance Commands): Document "maint with".

gdb/testsuite/ChangeLog:
yyyy-mm-dd  Pedro Alves  <[hidden email]>

        * gdb.base/with.c: New file.
        * gdb.base/with.exp: New file.
---
 gdb/doc/gdb.texinfo             |  94 +++++++++++++++
 gdb/NEWS                        |  15 +++
 gdb/cli/cli-cmds.c              | 129 +++++++++++++++++++-
 gdb/cli/cli-cmds.h              |  13 ++
 gdb/cli/cli-setshow.c           |  74 ++++++------
 gdb/cli/cli-setshow.h           |   5 +
 gdb/command.h                   |  19 +--
 gdb/maint.c                     |  27 +++++
 gdb/testsuite/gdb.base/with.c   |  41 +++++++
 gdb/testsuite/gdb.base/with.exp | 261 ++++++++++++++++++++++++++++++++++++++++
 gdb/top.c                       |   7 +-
 11 files changed, 635 insertions(+), 50 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/with.c
 create mode 100644 gdb/testsuite/gdb.base/with.exp

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index a279a145502..635bcff46e1 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -1560,6 +1560,7 @@ show you the alternatives available, if there is more than one possibility).
 
 @menu
 * Command Syntax::              How to give commands to @value{GDBN}
+* Command Settings::            How to change default behavior of commands
 * Completion::                  Command completion
 * Command Options::             Command options
 * Help::                        How to ask @value{GDBN} for help
@@ -1616,6 +1617,92 @@ commands.  This command accepts the current line, like @key{RET}, and
 then fetches the next line relative to the current line from the history
 for editing.
 
+
+@node Command Settings
+@section Command Settings
+@cindex change default behavior of commands
+@cindex change default settings
+
+Many commands change their behavior according to command-specific
+variables or settings.  These settings can be changed with the
+@code{set} subcommands.  For example, the @code{print} command
+(@pxref{Data, ,Examining Data}) prints arrays differently depending on
+settings changeable with the commands @code{set print elements
+NUMBER-OF-ELEMENTS} and @code{set print array-indexes}, among others.
+
+You can set these settings to your preference in the gdbinit files
+loaded at @value{GDBN} startup.  @xref{Startup}.
+
+The settings can also be changed interactively during the debugging
+session.  For example, to change the limit of array elements to print,
+you can do the following:
+@smallexample
+(@value{GDBN}) set print elements 10
+(@value{GDBN}) print some_array
+$1 = @{0, 10, 20, 30, 40, 50, 60, 70, 80, 90...@}
+@end smallexample
+
+The above @code{set print elements 10} command changes the number of
+elements to print from the default of 200 to 10.  If you only intend
+this limit of 10 to be used for printing @code{some_array}, then you
+must restore the limit back to 200, with @code{set print elements
+200}.
+
+Some commands allow overriding settings with command options.  For
+example, the @code{print} command supports a number of options that
+allow overriding relevant global print settings as set by @code{set
+print} subcommands.  @xref{print options}.  The example above could be
+rewritten as:
+@smallexample
+(@value{GDBN}) print -elements 10 -- some_array
+$1 = @{0, 10, 20, 30, 40, 50, 60, 70, 80, 90...@}
+@end smallexample
+
+Alternatively, you can use the @code{with} command to change a setting
+temporarily, for the duration of a command invocation.
+
+@table @code
+@kindex with command
+@kindex w @r{(@code{with})}
+@cindex settings
+@cindex temporarily change settings
+@item with @var{setting} [@var{value}] [-- @var{command}]
+@itemx w @var{setting} [@var{value}] [-- @var{command}]
+Temporarily set @var{setting} to @var{value} for the duration of
+@var{command}.
+
+If no @var{command} is provided, the last command executed is
+repeated.
+
+@var{setting} is any setting settable with the @code{set} subcommands.
+
+For example, the command
+@smallexample
+(@value{GDBN}) with print array on -- print some_array
+@end smallexample
+@noindent
+is equivalent to the following 3 commands:
+@smallexample
+(@value{GDBN}) set print array on
+(@value{GDBN}) print some_array
+(@value{GDBN}) set print array off
+@end smallexample
+
+The @code{with} command is particularly useful when you want to
+override a setting while running user-defined commands, or commands
+defined in Python or Guile.  @xref{Extending GDB,, Extending GDB}.
+
+@smallexample
+(@value{GDBN}) with print pretty on -- my_complex_command
+@end smallexample
+
+To change several settings for the same command, you can nest
+@code{with} commands.  For example, @code{with language ada -- with
+print elements 10} temporarily changes the language to Ada and sets a
+limit of 10 elements to print for arrays and strings.
+
+@end table
+
 @node Completion
 @section Command Completion
 
@@ -37633,6 +37720,13 @@ support in the command options framework.
 These are representative commands for each @var{kind} of setting type
 @value{GDBN} supports.  They are used by the testsuite for exercising
 the settings infrastructure.
+
+@kindex maint with
+@item maint with @var{setting} [@var{value}] [-- @var{command}]
+Like the @code{with} command, but works with @code{maintenance set}
+variables.  This is used by the testsuite to exercise the @code{with}
+command's infrastructure.
+
 @end table
 
 The following command is useful for non-interactive invocations of
diff --git a/gdb/NEWS b/gdb/NEWS
index 4a7a117970a..850ab44df3c 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -46,6 +46,21 @@ pipe -d DELIM COMMAND DELIM SHELL_COMMAND
   With no COMMAND, repeat the last executed command
   and send its output to SHELL_COMMAND.
 
+with SETTING [VALUE] [-- COMMAND]
+w SETTING [VALUE] [-- COMMAND]
+  Temporarily set SETTING, run COMMAND, and restore SETTING.
+  Usage: with SETTING -- COMMAND
+  With no COMMAND, repeats the last executed command.
+  SETTING is any GDB setting settable with the "set" command.
+  For example, 'with language c -- print someobj' temporarily switches
+  to the C language in order to print someobj.  Settings can be
+  combined: 'w lang c -- w print elements unlimited -- usercmd'
+  switches to the C language and runs usercmd with no limit on array
+  elements to print.
+
+maint with SETTING [VALUE] [-- COMMAND]
+  Like "with", but works with "maintenance set" variables.
+
 set may-call-functions [on|off]
 show may-call-functions
   This controls whether GDB will attempt to call functions in
diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index 62f4d7f0c5e..b3d4a291c3e 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -211,6 +211,116 @@ show_command (const char *arg, int from_tty)
   cmd_show_list (showlist, from_tty, "");
 }
 
+/* See cli/cli-cmds.h.  */
+
+void
+with_command_1 (cmd_list_element *setlist, const char *args, int from_tty)
+{
+  const char *delim = strstr (args, "--");
+  const char *nested_cmd = nullptr;
+
+  if (delim == args)
+    error (_("Missing setting before '--' delimiter"));
+
+  if (delim == nullptr || *skip_spaces (&delim[2]) == '\0')
+    nested_cmd = repeat_previous ();
+
+  const char *cmd = args;
+  cmd_list_element *set_cmd = lookup_cmd (&args, setlist, "with", 0, 1);
+
+  if (set_cmd == nullptr)
+    error (_("Unknown setting %s"), cmd);
+
+  if (set_cmd->var == nullptr)
+    error (_("Cannot use this setting with the \"with\" command"));
+
+  std::string temp_value
+    = (delim == nullptr ? args : std::string (args, delim - args));
+
+  if (nested_cmd == nullptr)
+    nested_cmd = skip_spaces (delim + 2);
+
+  std::string org_value = get_setshow_command_value_string (set_cmd);
+
+  /* Tweak the setting to the new temporary value.  */
+  do_set_command (temp_value.c_str (), from_tty, set_cmd);
+
+  try
+    {
+      scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
+
+      /* Execute the nested command.  */
+      execute_command (nested_cmd, from_tty);
+    }
+  catch (const gdb_exception &ex)
+    {
+      /* Restore the setting and rethrow.  If restoring the setting
+ throws, swallow the new exception and warn.  There's nothing
+ else we can reasonably do.  */
+      try
+ {
+  do_set_command (org_value.c_str (), from_tty, set_cmd);
+ }
+      catch (...)
+ {
+  warning (_("Couldn't restore setting"));
+ }
+
+      throw;
+    }
+
+  /* Restore the setting.  */
+  do_set_command (org_value.c_str (), from_tty, set_cmd);
+}
+
+/* See cli/cli-cmds.h.  */
+
+void
+with_command_completer_1 (const char *set_cmd_prefix,
+  completion_tracker &tracker,
+  const char *text)
+{
+  tracker.set_use_custom_word_point (true);
+
+  const char *delim = strstr (text, "--");
+
+  /* If we're still not past the "--" delimiter, complete the "with"
+     command as if it was a "set" command.  */
+  if (delim == text
+      || delim == nullptr
+      || !isspace (delim[-1])
+      || !(isspace (delim[2]) || delim[2] == '\0'))
+    {
+      std::string new_text = std::string (set_cmd_prefix) + text;
+      tracker.advance_custom_word_point_by (-(int) strlen (set_cmd_prefix));
+      complete_nested_command_line (tracker, new_text.c_str ());
+      return;
+    }
+
+  /* We're past the "--" delimiter.  Complete on the sub command.  */
+  const char *nested_cmd = skip_spaces (delim + 2);
+  tracker.advance_custom_word_point_by (nested_cmd - text);
+  complete_nested_command_line (tracker, nested_cmd);
+}
+
+/* The "with" command.  */
+
+static void
+with_command (const char *args, int from_tty)
+{
+  with_command_1 (setlist, args, from_tty);
+}
+
+/* "with" command completer.  */
+
+static void
+with_command_completer (struct cmd_list_element *ignore,
+ completion_tracker &tracker,
+ const char *text, const char * /*word*/)
+{
+  with_command_completer_1 ("set ", tracker,  text);
+}
+
 
 /* Provide documentation on command or list given by COMMAND.  FROM_TTY
    is ignored.  */
@@ -878,12 +988,7 @@ pipe_command (const char *arg, int from_tty)
   arg += delim.length (); /* Skip the delimiter.  */
 
   if (gdb_cmd.empty ())
-    {
-      repeat_previous ();
-      gdb_cmd = skip_spaces (get_saved_command_line ());
-      if (gdb_cmd.empty ())
- error (_("No previous command to relaunch"));
-    }
+    gdb_cmd = repeat_previous ();
 
   const char *shell_command = skip_spaces (arg);
   if (*shell_command == '\0')
@@ -1849,6 +1954,18 @@ Generic command for showing things about the debugger."),
   /* Another way to get at the same thing.  */
   add_info ("set", show_command, _("Show all GDB settings."));
 
+  c = add_com ("with", class_vars, with_command, _("\
+Temporarily set SETTING to VALUE, run COMMAND, and restore SETTING.\n\
+Usage: with SETTING [VALUE] [-- COMMAND]\n\
+Usage: w SETTING [VALUE] [-- COMMAND]\n\
+With no COMMAND, repeats the last executed command.\n\
+SETTING is any setting settable with the \"set\" subcommands.\n\
+E.g.:\n\
+  with language pascal -- print obj\n\
+  with print elements unlimited -- print obj"));
+  set_cmd_completer_handle_brkchars (c, with_command_completer);
+  add_com_alias ("w", "with", class_vars, 1);
+
   add_cmd ("commands", no_set_class, show_commands, _("\
 Show the history of commands you typed.\n\
 You can supply a command number to start with, or a `+' to start after\n\
diff --git a/gdb/cli/cli-cmds.h b/gdb/cli/cli-cmds.h
index 63b8c4018ce..2c8e9a676c1 100644
--- a/gdb/cli/cli-cmds.h
+++ b/gdb/cli/cli-cmds.h
@@ -142,4 +142,17 @@ extern gdb::optional<open_script>
 extern int source_verbose;
 extern int trace_commands;
 
+/* Common code for the "with" and "maintenance with" commands.
+   SETLIST is the command list for corresponding "set" command: i.e.,
+   "set" or "maintenance set".  */
+extern void with_command_1 (cmd_list_element *setlist,
+    const char *args, int from_tty);
+
+/* Common code for the completers of the "with" and "maintenance with"
+   commands.  SET_CMD_PREFIX is the spelling of the corresponding
+   "set" command prefix: i.e., "set " or "maintenance set ".  */
+extern void with_command_completer_1 (const char *set_cmd_prefix,
+      completion_tracker &tracker,
+      const char *text);
+
 #endif /* CLI_CLI_CMDS_H */
diff --git a/gdb/cli/cli-setshow.c b/gdb/cli/cli-setshow.c
index d588d04ab17..6fb32441acc 100644
--- a/gdb/cli/cli-setshow.c
+++ b/gdb/cli/cli-setshow.c
@@ -190,7 +190,7 @@ parse_cli_var_uinteger (var_types var_type, const char **arg,
 {
   LONGEST val;
 
-  if (*arg == nullptr)
+  if (*arg == nullptr || **arg == '\0')
     {
       if (var_type == var_uinteger)
  error_no_arg (_("integer to set it to, or \"unlimited\"."));
@@ -225,7 +225,7 @@ parse_cli_var_zuinteger_unlimited (const char **arg, bool expression)
 {
   LONGEST val;
 
-  if (*arg == nullptr)
+  if (*arg == nullptr || **arg == '\0')
     error_no_arg (_("integer to set it to, or \"unlimited\"."));
 
   if (is_unlimited_literal (arg, expression))
@@ -308,6 +308,9 @@ do_set_command (const char *arg, int from_tty, struct cmd_list_element *c)
 
   gdb_assert (c->type == set_cmd);
 
+  if (arg == NULL)
+    arg = "";
+
   switch (c->var_type)
     {
     case var_string:
@@ -317,8 +320,6 @@ do_set_command (const char *arg, int from_tty, struct cmd_list_element *c)
  char *q;
  int ch;
 
- if (arg == NULL)
-  arg = "";
  newobj = (char *) xmalloc (strlen (arg) + 2);
  p = arg;
  q = newobj;
@@ -364,9 +365,6 @@ do_set_command (const char *arg, int from_tty, struct cmd_list_element *c)
       }
       break;
     case var_string_noescape:
-      if (arg == NULL)
- arg = "";
-
       if (*(char **) c->var == NULL || strcmp (*(char **) c->var, arg) != 0)
  {
   xfree (*(char **) c->var);
@@ -376,14 +374,14 @@ do_set_command (const char *arg, int from_tty, struct cmd_list_element *c)
  }
       break;
     case var_filename:
-      if (arg == NULL)
+      if (*arg == '\0')
  error_no_arg (_("filename to set it to."));
       /* FALLTHROUGH */
     case var_optional_filename:
       {
  char *val = NULL;
 
- if (arg != NULL)
+ if (*arg != '\0')
   {
     /* Clear trailing whitespace of filename.  */
     const char *ptr = arg + strlen (arg) - 1;
@@ -455,7 +453,7 @@ do_set_command (const char *arg, int from_tty, struct cmd_list_element *c)
       {
  LONGEST val;
 
- if (arg == NULL)
+ if (*arg == '\0')
   {
     if (c->var_type == var_integer)
       error_no_arg (_("integer to set it to, or \"unlimited\"."));
@@ -625,24 +623,13 @@ do_set_command (const char *arg, int from_tty, struct cmd_list_element *c)
     }
 }
 
-/* Do a "show" command.  ARG is NULL if no argument, or the
-   text of the argument, and FROM_TTY is nonzero if this command is
-   being entered directly by the user (i.e. these are just like any
-   other command).  C is the command list element for the command.  */
+/* See cli/cli-setshow.h.  */
 
-void
-do_show_command (const char *arg, int from_tty, struct cmd_list_element *c)
+std::string
+get_setshow_command_value_string (cmd_list_element *c)
 {
-  struct ui_out *uiout = current_uiout;
-
-  gdb_assert (c->type == show_cmd);
-
   string_file stb;
 
-  /* Possibly call the pre hook.  */
-  if (c->pre_show_hook)
-    (c->pre_show_hook) (c);
-
   switch (c->var_type)
     {
     case var_string:
@@ -672,9 +659,7 @@ do_show_command (const char *arg, int from_tty, struct cmd_list_element *c)
   stb.puts ("auto");
   break;
  default:
-  internal_error (__FILE__, __LINE__,
-  _("do_show_command: "
-    "invalid var_auto_boolean"));
+  gdb_assert_not_reached ("invalid var_auto_boolean");
   break;
  }
       break;
@@ -703,23 +688,42 @@ do_show_command (const char *arg, int from_tty, struct cmd_list_element *c)
       }
       break;
     default:
-      error (_("gdb internal error: bad var_type in do_show_command"));
+      gdb_assert_not_reached ("bad var_type");
     }
 
+  return std::move (stb.string ());
+}
+
+
+/* Do a "show" command.  ARG is NULL if no argument, or the
+   text of the argument, and FROM_TTY is nonzero if this command is
+   being entered directly by the user (i.e. these are just like any
+   other command).  C is the command list element for the command.  */
+
+void
+do_show_command (const char *arg, int from_tty, struct cmd_list_element *c)
+{
+  struct ui_out *uiout = current_uiout;
+
+  gdb_assert (c->type == show_cmd);
+
+  /* Possibly call the pre hook.  */
+  if (c->pre_show_hook)
+    (c->pre_show_hook) (c);
+
+  std::string val = get_setshow_command_value_string (c);
 
-  /* FIXME: cagney/2005-02-10: Need to split this in half: code to
-     convert the value into a string (esentially the above); and
-     code to print the value out.  For the latter there should be
-     MI and CLI specific versions.  */
+  /* FIXME: cagney/2005-02-10: There should be MI and CLI specific
+     versions of code to print the value out.  */
 
   if (uiout->is_mi_like_p ())
-    uiout->field_stream ("value", stb);
+    uiout->field_string ("value", val.c_str ());
   else
     {
       if (c->show_value_func != NULL)
- c->show_value_func (gdb_stdout, from_tty, c, stb.c_str ());
+ c->show_value_func (gdb_stdout, from_tty, c, val.c_str ());
       else
- deprecated_show_value_hack (gdb_stdout, from_tty, c, stb.c_str ());
+ deprecated_show_value_hack (gdb_stdout, from_tty, c, val.c_str ());
     }
 
   c->func (c, NULL, from_tty);
diff --git a/gdb/cli/cli-setshow.h b/gdb/cli/cli-setshow.h
index c00a0989145..8bfe7e89f09 100644
--- a/gdb/cli/cli-setshow.h
+++ b/gdb/cli/cli-setshow.h
@@ -17,6 +17,8 @@
 #ifndef CLI_CLI_SETSHOW_H
 #define CLI_CLI_SETSHOW_H
 
+#include <string>
+
 struct cmd_list_element;
 
 /* Parse ARG, an option to a boolean variable.
@@ -55,6 +57,9 @@ extern void do_set_command (const char *arg, int from_tty,
 extern void do_show_command (const char *arg, int from_tty,
      struct cmd_list_element *c);
 
+/* Get a string version of C's current value.  */
+extern std::string get_setshow_command_value_string (cmd_list_element *c);
+
 extern void cmd_show_list (struct cmd_list_element *list, int from_tty,
    const char *prefix);
 
diff --git a/gdb/command.h b/gdb/command.h
index 4d52f00de51..648950b5a44 100644
--- a/gdb/command.h
+++ b/gdb/command.h
@@ -461,14 +461,17 @@ extern void error_no_arg (const char *) ATTRIBUTE_NORETURN;
 
 extern void dont_repeat ();
 
-/* Commands call repeat_previous if they want to repeat the previous command.
-   Such commands that repeat the previous command must indicate
-   to not repeat themselves, to avoid recursive repeat.
-   repeat_previous will mark the current command as not repeating,
-   and will ensure get_saved_command_line returns the previous command,
-   so that the currently executing command can repeat it.  */
-
-extern void repeat_previous ();
+/* Commands call repeat_previous if they want to repeat the previous
+   command.  Such commands that repeat the previous command must
+   indicate to not repeat themselves, to avoid recursive repeat.
+   repeat_previous marks the current command as not repeating, and
+   ensures get_saved_command_line returns the previous command, so
+   that the currently executing command can repeat it.  If there's no
+   previous command, throws an error.  Otherwise, returns the result
+   of get_saved_command_line, which now points at the command to
+   repeat.  */
+
+extern const char *repeat_previous ();
 
 /* Prevent dont_repeat from working, and return a cleanup that
    restores the previous state.  */
diff --git a/gdb/maint.c b/gdb/maint.c
index aaabb352249..10bb4fe7e9a 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -43,6 +43,7 @@
 #include "cli/cli-decode.h"
 #include "cli/cli-utils.h"
 #include "cli/cli-setshow.h"
+#include "cli/cli-cmds.h"
 
 static void maintenance_do_deprecate (const char *, int);
 
@@ -634,6 +635,24 @@ maintenance_show_cmd (const char *args, int from_tty)
   cmd_show_list (maintenance_show_cmdlist, from_tty, "");
 }
 
+/* "maintenance with" command.  */
+
+static void
+maintenance_with_cmd (const char *args, int from_tty)
+{
+  with_command_1 (maintenance_set_cmdlist, args, from_tty);
+}
+
+/* "maintenance with" command completer.  */
+
+static void
+maintenance_with_cmd_completer (struct cmd_list_element *ignore,
+ completion_tracker &tracker,
+ const char *text, const char * /*word*/)
+{
+  with_command_completer_1 ("maintenance set ", tracker,  text);
+}
+
 /* Profiling support.  */
 
 static int maintenance_profile_p;
@@ -1023,6 +1042,14 @@ Configure variables internal to GDB that aid in GDB's maintenance"),
   0/*allow-unknown*/,
   &maintenancelist);
 
+  cmd = add_cmd ("with", class_maintenance, maintenance_with_cmd, _("\
+Like \"with\", but works with \"maintenance set\" variables.\n\
+Usage: maintenance with SETTING [VALUE] [-- COMMAND]\n\
+With no COMMAND, repeats the last executed command.\n\
+SETTING is any setting settable with the \"maintenance set\" subcommands."),
+ &maintenancelist);
+  set_cmd_completer_handle_brkchars (cmd, maintenance_with_cmd_completer);
+
 #ifndef _WIN32
   add_cmd ("dump-me", class_maintenance, maintenance_dump_me, _("\
 Get fatal error; make debugger dump its core.\n\
diff --git a/gdb/testsuite/gdb.base/with.c b/gdb/testsuite/gdb.base/with.c
new file mode 100644
index 00000000000..c6426625d4a
--- /dev/null
+++ b/gdb/testsuite/gdb.base/with.c
@@ -0,0 +1,41 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2019 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/>.  */
+
+int xxx1 = 123;
+
+struct S
+{
+  int a;
+  int b;
+  int c;
+};
+
+struct S g_s = {1, 2, 3};
+
+static void
+inc ()
+{
+  g_s.a++;;
+}
+
+int
+main ()
+{
+  inc ();
+
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/with.exp b/gdb/testsuite/gdb.base/with.exp
new file mode 100644
index 00000000000..eb1b156c664
--- /dev/null
+++ b/gdb/testsuite/gdb.base/with.exp
@@ -0,0 +1,261 @@
+# This testcase is part of GDB, the GNU debugger.
+
+# Copyright 2019 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 the "with" command.
+
+load_lib completion-support.exp
+
+standard_testfile .c
+
+if {[build_executable "failed to prepare" $testfile $srcfile debug]} {
+    return -1
+}
+
+clean_restart $binfile
+
+# Test "maint with".  VALUES is a list of values.  A nested "with" is
+# performed with each combination of pair of values from this list.
+# This exercises setting a value, and restoring it too.  This is
+# particularly important for the "special" values like "unlimited",
+# which for example for var_uinteger maps to 0 at the user-visible
+# level, but map to -1 internally.
+
+proc test_with {setting values} {
+    foreach val1 $values {
+ foreach val2 $values {
+    gdb_test \
+ "maint with test-settings $setting $val1 -- maint with test-settings $setting $val2 -- p 1" \
+ " = 1"
+ }
+    }
+}
+
+# Test "maint with" in the error case.  SETTING is the "maint set
+# test-setting" setting to exercise.  TMP_VAL is the value to set the
+# setting to.  EXPECTED_RE is the expected GDB output, which should be
+# an error of some kind.  Also checks that the setting's original
+# value is preserved across the error.
+
+proc test_with_error {setting tmp_val expected_re} {
+    global gdb_prompt
+
+    with_test_prefix "$setting, $tmp_val" {
+ set test "save org value"
+ set org_val ""
+ gdb_test_multiple "maint show test-settings $setting" $test {
+    -re "(.*)\r\n$gdb_prompt $" {
+ set org_val $expect_out(1,string)
+ pass $test
+    }
+ }
+
+ gdb_test \
+    "maint with test-settings $setting $tmp_val -- p 1" \
+    $expected_re
+
+ gdb_test "maint show test-settings $setting" "^$org_val" \
+    "value hasn't changed across error"
+    }
+}
+
+# Test "with" framework basics, using the internal "maint with
+# test-settings" subcommands.
+with_test_prefix "maint" {
+    test_with "auto-boolean" {"on" "off" "auto"}
+    test_with "boolean" {"" "on" "off" "0" "1" "enable" "disable"}
+    test_with "integer" {"0" "1" "-1" "unlimited"}
+    test_with "uinteger" {"0" "1" "unlimited"}
+    test_with "zinteger" {"0" "1" "-1"}
+    test_with "zuinteger" {"0" "1"}
+    test_with "zuinteger-unlimited" {"-1" "unlimited" "0" "1"}
+    test_with "string" {"" "foo" "\"hello world\""}
+    test_with "string-noescape" {"" "foo" "\"hello world\""}
+    test_with "filename" {"/foo" "bar/x/y"}
+    test_with "optional-filename" {"" "/foo" "bar/x/y"}
+    test_with "enum" {"xxx" "yyy"}
+
+    # Check the most importan error conditions.  E.g., empty, negative
+    # or "unlimited" values for settings that don't accept it.
+    # Exhaustive error coverage is left to "set" testing, in
+    # gdb.base/settings.exp.
+    test_with_error "auto-boolean" "" "\"on\", \"off\" or \"auto\" expected\\."
+    test_with_error "auto-boolean" "xxx" "\"on\", \"off\" or \"auto\" expected\\."
+    test_with_error "boolean" "2" "\"on\" or \"off\" expected\\."
+    test_with_error "uinteger" "-1" "integer -1 out of range"
+    test_with_error "zuinteger" "-1" "integer -1 out of range"
+    test_with_error "zuinteger-unlimited" "-2" "only -1 is allowed to set as unlimited"
+    test_with_error "filename" "" "Argument required \\(filename to set it to\\.\\)\\."
+    test_with_error "enum" "" "Requires an argument\\. Valid arguments are xxx, yyy, zzz\\."
+}
+
+# Basic/core tests using user-visible commands.
+with_test_prefix "basics" {
+    gdb_test "print g_s" " = {a = 1, b = 2, c = 3}"
+    gdb_test "with print pretty -- print g_s" \
+ [multi_line  \
+     " = {" \
+     "  a = 1," \
+     "  b = 2," \
+     "  c = 3" \
+     "}"]
+
+    # A boolean setting.
+    gdb_test "with non-stop on -- show non-stop" \
+ "Controlling the inferior in non-stop mode is on\\."
+    gdb_test "show non-stop" \
+ "Controlling the inferior in non-stop mode is off\\."
+
+    # Language.
+    gdb_test "with language pascal -- show language" \
+ "The current source language is \"pascal\"\\."
+
+    gdb_test "show language" \
+ "The current source language is \"auto; currently c\"\\."
+
+    gdb_test "with language ada -- print g_s" \
+ " = \\(a => 1, b => 2, c => 3\\)"
+
+    # Nested "with"s.
+    gdb_test "with language ada -- with language c -- print g_s" \
+ " = {a = 1, b = 2, c = 3}"
+
+    # "w" alias.
+    gdb_test "w language pascal -- show language" \
+ "The current source language is \"pascal\"\\." \
+ "w alias works"
+
+    # An early prototype of the "with" command got this wrong.
+    gdb_test \
+ "w print repeats unlimited -- w print repeats 1 -- p \"1223334444\"" \
+ " = \"1\", '2' <repeats 2 times>, '3' <repeats 3 times>, '4' <repeats 4 times>"
+}
+
+# Check a user defined command.
+with_test_prefix "user defined" {
+    # A user defined command.
+    set test "define usercmd"
+    gdb_test_multiple "define usercmd" $test {
+ -re "End with"  {
+    gdb_test \
+ [multi_line_input \
+     {print g_s} \
+     {end}] \
+ "" \
+ $test
+ }
+    }
+    gdb_test "with language ada -- usercmd" \
+ " = \\(a => 1, b => 2, c => 3\\)"
+}
+
+# Check repeating.
+with_test_prefix "repeat" {
+
+    clean_restart $binfile
+
+    # "with" with no command reinvokes the previous command.
+    gdb_test "with language ada" \
+ "No previous command to relaunch" \
+ "reinvoke with no previous command to relaunch"
+
+    gdb_test "print g_s" " = {a = 1, b = 2, c = 3}"
+
+    gdb_test "with language ada" \
+ " = \\(a => 1, b => 2, c => 3\\)" \
+ "reinvoke with language"
+
+    # Same, but with "--".
+    gdb_test "with language fortran --" \
+ " = \\( a = 1, b = 2, c = 3 \\)" \
+ "reinvoke with language and --"
+
+    # Repeating repeats the original "print g_s", not the last "with"
+    # command.
+    set test "repeat command line"
+    send_gdb "\n"
+    gdb_test_multiple "" $test {
+ -re " = {a = 1, b = 2, c = 3}\r\n$gdb_prompt $" {
+    pass $test
+ }
+    }
+}
+
+# Basic run control.
+with_test_prefix "run control" {
+    clean_restart $binfile
+
+    if ![runto_main] {
+ fail "cannot run to main"
+ return
+    }
+
+    # Check "with" with a synchronous execution command.
+    gdb_test "with disassemble-next-line on -- next" \
+ "return 0;.*=>.*"
+}
+
+# Check errors.
+with_test_prefix "errors" {
+    gdb_test "with variable xxx=1" \
+ "Cannot use this setting with the \"with\" command"
+
+    gdb_test "with print elements -- p 1" \
+ "Argument required \\(integer to set it to, or \"unlimited\"\\.\\)\\."
+
+    gdb_test "with -- p 1" \
+ "Missing setting before '--' delimiter"
+
+    # Check that the setting is restored even if the command throws.
+    gdb_test "with print elements 1 -- unknowncommand" \
+ "Undefined command: \"unknowncommand\"\\.  Try \"help\"\\."
+    gdb_test "show print elements" \
+ "Limit on string chars or array elements to print is 200\\."
+}
+
+# Check completion.
+with_test_prefix "completion" {
+    test_gdb_complete_unique \
+ "with pri" \
+ "with print"
+
+    test_gdb_complete_unique \
+ "with print ele" \
+ "with print elements"
+
+    test_gdb_complete_unique \
+ "with print elements u" \
+ "with print elements unlimited"
+
+    test_gdb_complete_none \
+ "with print elements unlimited "
+
+    test_gdb_completion_offers_commands "with print elements unlimited -- "
+
+    # Check that the completer nests into the nested command line's
+    # completer.
+    test_gdb_complete_unique \
+ "with print elements unlimited -- with print ele" \
+ "with print elements unlimited -- with print elements"
+
+    # Check completion of "maint with".  "maint with" and "with"'s
+    # completers share 99% of the code.  All we need to care about
+    # here is that the completion word point is computed correctly, so
+    # any simple completion is sufficient.
+    test_gdb_complete_unique \
+ "maint with test-set" \
+ "maint with test-settings"
+}
diff --git a/gdb/top.c b/gdb/top.c
index 857207c3767..9a345538634 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -735,7 +735,7 @@ dont_repeat (void)
 
 /* See command.h  */
 
-void
+const char *
 repeat_previous ()
 {
   /* Do not repeat this command, as this command is a repeating command.  */
@@ -745,6 +745,11 @@ repeat_previous ()
      so swap it with previous_saved_command_line.  */
   std::swap (previous_saved_command_line, saved_command_line);
   std::swap (previous_repeat_arguments, repeat_arguments);
+
+  const char *prev = skip_spaces (get_saved_command_line ());
+  if (*prev == '\0')
+    error (_("No previous command to relaunch"));
+  return prev;
 }
 
 /* See command.h.  */
--
2.14.5

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v2 3/4] "maint test-settings set/show" -> "maint set/show test-settings"

Eli Zaretskii
In reply to this post by Pedro Alves-7
> From: Pedro Alves <[hidden email]>
> Cc: Philippe Waroquiers <[hidden email]>
> Date: Tue, 18 Jun 2019 01:39:01 +0100
>
> This commit renames "maint test-settings set/show" to "maint set/show
> test-settings".
>
> This helps the following patch, which introduce a "maint with" command
> what works with all "maint set" settings.
>
> gdb/ChangeLog:
> yyyy-mm-dd  Pedro Alves  <[hidden email]>
>
> * NEWS (New commands): Mention "maint set/show test-settings"
> instead of "maint test-settings".
> * maint-test-settings.c (maintenance_test_settings_list): Delete.
> (maintenance_test_settings_set_list): Rename to ...
> (maintenance_set_test_settings_list): ... this.
> (maintenance_test_settings_show_list): Rename to  ...
> (maintenance_show_test_settings_list): ... this.
> (maintenance_test_settings_cmd): Delete.
> (maintenance_test_settings_set_cmd): ...
> (maintenance_set_test_settings_cmd): ... this.
> (maintenance_test_settings_show_cmd): ...
> (maintenance_show_test_settings_cmd): ... this.
> (maintenance_test_settings_show_value_cmd):
> (maintenance_show_test_settings_value_cmd): ... this.
> (_initialize_maint_test_settings): No longer install the "maint
> test-settings" prefix command.  Rename "maint test-settings set"
> to "maint set test-settings", and "maint test-settings show" to
> "maint show test-settings".  Adjust all subcommands.
>
> gdb/doc/ChangeLog:
> yyyy-mm-dd  Pedro Alves  <[hidden email]>
>
> * gdb.texinfo (Maintenance Commands): Document "maint set/show
> test-settings" instead of "maint test-settings set/show".
>
> gdb/testsuite/ChangeLog:
> yyyy-mm-dd  Pedro Alves  <[hidden email]>
>
> * gdb.base/settings.exp: Replace all references to "maint
> test-settings set" with references to "maint set test-settings",
> and all references to "maint test-settings show" with references
> to "maint show test-settings".

OK for the documentation parts.

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v2 4/4] Introduce the "with" command

Eli Zaretskii
In reply to this post by Pedro Alves-7
> From: Pedro Alves <[hidden email]>
> Cc: Philippe Waroquiers <[hidden email]>
> Date: Tue, 18 Jun 2019 01:39:02 +0100
>
> +@cindex change default behavior of commands
> +@cindex change default settings

Two index entries that start with the same text and point to the same
place are not useful.  I'd drop the second one.

> +Many commands change their behavior according to command-specific
> +variables or settings.  These settings can be changed with the
> +@code{set} subcommands.  For example, the @code{print} command
> +(@pxref{Data, ,Examining Data}) prints arrays differently depending on
> +settings changeable with the commands @code{set print elements
> +NUMBER-OF-ELEMENTS} and @code{set print array-indexes}, among others.
> +
> +You can set these settings to your preference in the gdbinit files
> +loaded at @value{GDBN} startup.  @xref{Startup}.
> +
> +The settings can also be changed interactively during the debugging
> +session.  For example, to change the limit of array elements to print,
> +you can do the following:
> +@smallexample
> +(@value{GDBN}) set print elements 10
> +(@value{GDBN}) print some_array
> +$1 = @{0, 10, 20, 30, 40, 50, 60, 70, 80, 90...@}
> +@end smallexample
> +
> +The above @code{set print elements 10} command changes the number of
> +elements to print from the default of 200 to 10.  If you only intend
> +this limit of 10 to be used for printing @code{some_array}, then you
> +must restore the limit back to 200, with @code{set print elements
> +200}.
> +
> +Some commands allow overriding settings with command options.  For
> +example, the @code{print} command supports a number of options that
> +allow overriding relevant global print settings as set by @code{set
> +print} subcommands.  @xref{print options}.  The example above could be
> +rewritten as:
> +@smallexample
> +(@value{GDBN}) print -elements 10 -- some_array
> +$1 = @{0, 10, 20, 30, 40, 50, 60, 70, 80, 90...@}
> +@end smallexample
> +
> +Alternatively, you can use the @code{with} command to change a setting
> +temporarily, for the duration of a command invocation.
> +
> +@table @code
> +@kindex with command
> +@kindex w @r{(@code{with})}
> +@cindex settings
> +@cindex temporarily change settings
> +@item with @var{setting} [@var{value}] [-- @var{command}]
> +@itemx w @var{setting} [@var{value}] [-- @var{command}]
> +Temporarily set @var{setting} to @var{value} for the duration of
> +@var{command}.
> +
> +If no @var{command} is provided, the last command executed is
> +repeated.
> +
> +@var{setting} is any setting settable with the @code{set} subcommands.
> +
> +For example, the command
> +@smallexample
> +(@value{GDBN}) with print array on -- print some_array
> +@end smallexample
> +@noindent
> +is equivalent to the following 3 commands:
> +@smallexample
> +(@value{GDBN}) set print array on
> +(@value{GDBN}) print some_array
> +(@value{GDBN}) set print array off
> +@end smallexample
> +
> +The @code{with} command is particularly useful when you want to
> +override a setting while running user-defined commands, or commands
> +defined in Python or Guile.  @xref{Extending GDB,, Extending GDB}.
> +
> +@smallexample
> +(@value{GDBN}) with print pretty on -- my_complex_command
> +@end smallexample
> +
> +To change several settings for the same command, you can nest
> +@code{with} commands.  For example, @code{with language ada -- with
> +print elements 10} temporarily changes the language to Ada and sets a
> +limit of 10 elements to print for arrays and strings.
> +
> +@end table

This text is OK, but it keeps absolute silence regarding the "--"
delimiter.  It should explain why it is used here and how to use it
with multiple "with" prefixes.

> +  SETTING is any GDB setting settable with the "set" command.

"SETTING ... setting settable ... set".  How about rewording to use
the root "set" slightly fewer times?

The documentation parts are OK with these nits fixed.

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v2 0/4] Introduce the "with" command

Philippe Waroquiers
In reply to this post by Pedro Alves-7

On Tue, 2019-06-18 at 01:38 +0100, Pedro Alves wrote:

> ( See original discussion and prototype here:
>    https://sourceware.org/ml/gdb-patches/2019-05/msg00570.html )
>
>  (gdb) help with
>  Temporarily set SETTING to VALUE, run COMMAND, and restore SETTING.
>  Usage: with SETTING [VALUE] [-- COMMAND]
>  Usage: w SETTING [VALUE] [-- COMMAND]
>  With no COMMAND, repeats the last executed command.
>  SETTING is any setting settable with the "set" command.
>  E.g.:
>    with language pascal -- print obj
>    with print elements unlimited -- print obj
>
> More details in patch #4.
>
> New in v2:
I played a little bit with this version, no bug encountered.

2 small nits in the error message for unknown 'with settings':
   (gdb) with xxxx yyyy -- echo coucou
   Undefined withcommand: "xxxx".  Try "help wit".
   (gdb) 

(this message is produced by lookup_cmd, that is not too
much 'with' aware it seems ...)

Philippe

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v2 0/4] Introduce the "with" command

Pedro Alves-7
On 6/19/19 1:34 AM, Philippe Waroquiers wrote:

>> New in v2:
> I played a little bit with this version, no bug encountered.
>

Thanks much, again.

> 2 small nits in the error message for unknown 'with settings':
>    (gdb) with xxxx yyyy -- echo coucou
>    Undefined withcommand: "xxxx".  Try "help wit".
>    (gdb) 
>
> (this message is produced by lookup_cmd, that is not too
> much 'with' aware it seems ...)

Eh, somehow I missed adding a testcase for this scenario.
Thanks for noticing this.

I had code in with_command_1 to throw on unknown setting:

  if (set_cmd == nullptr)
    error (_("Unknown setting %s"), cmd);

but it's not reachable, because I'm passing "0" as
allow_unknown argument to lookup_cmd...  I tried passing
"1" instead, and, of course that error becomes reachable.

However, if the setting is a prefixed setting, like
"with print elements", then lookup_cmd still throws
an error, near the bottom, from the else branch:

      if (c->prefixlist && **line && !c->allow_unknown)
        undef_cmd_error (c->prefixname, *line);

regardless of the allow_unknown parameter.

This made me reconsider, and I'm thinking that indeed,
passing allow_unknown as false is the right thing to do,
so that we get consistent error messages:

 (gdb) with foo
 Undefined set command: "foo".  Try "help set".
 (gdb) with print foo
Undefined set print command: "foo".  Try "help set print".
 (gdb) maint with foo
 Undefined maintenance set command: "foo".  Try "help maintenance set".
 (gdb) maint with test-settings foo
 Undefined maintenance set test-settings command: "foo".  Try "help maintenance set test-settings".

I'm thinking that the errors talking about "set" instead of
"with" can be seen as a feature.  If you consider the prefixed
case, like "with print foo", the error is telling you where
to look at the available settings for that prefix.

Looking at the lookup_cmd code, I realized that I was missing
a test for ambiguous settings too.  Now added.

The funny missing space and 'h' were because we're supposed
to include a space in the CMDTYPE argument passed to
lookup_cmd, and I was passing "with", with no space.  I added a new
parameter to with_command_1, so that we can pass down
"maintenance with " too.  The completer function already had
the same parameter.

WDYT?

From 4db38e12c0b393231a7af955a1bd4e9573bfe19e Mon Sep 17 00:00:00 2001
From: Pedro Alves <[hidden email]>
Date: Wed, 19 Jun 2019 13:29:08 +0100
Subject: [PATCH] fix

---
 gdb/cli/cli-cmds.c              | 14 +++++++-------
 gdb/cli/cli-cmds.h              |  8 +++++---
 gdb/maint.c                     |  2 +-
 gdb/testsuite/gdb.base/with.exp | 18 ++++++++++++++++++
 4 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index 0f5da72e128..283bc446026 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -214,7 +214,8 @@ show_command (const char *arg, int from_tty)
 /* See cli/cli-cmds.h.  */
 
 void
-with_command_1 (cmd_list_element *setlist, const char *args, int from_tty)
+with_command_1 (const char *set_cmd_prefix,
+ cmd_list_element *setlist, const char *args, int from_tty)
 {
   const char *delim = strstr (args, "--");
   const char *nested_cmd = nullptr;
@@ -225,11 +226,10 @@ with_command_1 (cmd_list_element *setlist, const char *args, int from_tty)
   if (delim == nullptr || *skip_spaces (&delim[2]) == '\0')
     nested_cmd = repeat_previous ();
 
-  const char *cmd = args;
-  cmd_list_element *set_cmd = lookup_cmd (&args, setlist, "with", 0, 1);
-
-  if (set_cmd == nullptr)
-    error (_("Unknown setting %s"), cmd);
+  cmd_list_element *set_cmd = lookup_cmd (&args, setlist, set_cmd_prefix,
+  /*allow_unknown=*/ 0,
+  /*ignore_help_classes=*/ 1);
+  gdb_assert (set_cmd != nullptr);
 
   if (set_cmd->var == nullptr)
     error (_("Cannot use this setting with the \"with\" command"));
@@ -308,7 +308,7 @@ with_command_completer_1 (const char *set_cmd_prefix,
 static void
 with_command (const char *args, int from_tty)
 {
-  with_command_1 (setlist, args, from_tty);
+  with_command_1 ("set ", setlist, args, from_tty);
 }
 
 /* "with" command completer.  */
diff --git a/gdb/cli/cli-cmds.h b/gdb/cli/cli-cmds.h
index 2c8e9a676c1..94e210a84eb 100644
--- a/gdb/cli/cli-cmds.h
+++ b/gdb/cli/cli-cmds.h
@@ -143,9 +143,11 @@ extern int source_verbose;
 extern int trace_commands;
 
 /* Common code for the "with" and "maintenance with" commands.
-   SETLIST is the command list for corresponding "set" command: i.e.,
-   "set" or "maintenance set".  */
-extern void with_command_1 (cmd_list_element *setlist,
+   SET_CMD_PREFIX is the spelling of the corresponding "set" command
+   prefix: i.e., "set " or "maintenance set ".  SETLIST is the command
+   element for the same "set" command prefix.  */
+extern void with_command_1 (const char *set_cmd_prefix,
+    cmd_list_element *setlist,
     const char *args, int from_tty);
 
 /* Common code for the completers of the "with" and "maintenance with"
diff --git a/gdb/maint.c b/gdb/maint.c
index 10bb4fe7e9a..2c10903539b 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -640,7 +640,7 @@ maintenance_show_cmd (const char *args, int from_tty)
 static void
 maintenance_with_cmd (const char *args, int from_tty)
 {
-  with_command_1 (maintenance_set_cmdlist, args, from_tty);
+  with_command_1 ("maintenance set ", maintenance_set_cmdlist, args, from_tty);
 }
 
 /* "maintenance with" command completer.  */
diff --git a/gdb/testsuite/gdb.base/with.exp b/gdb/testsuite/gdb.base/with.exp
index b1d3adb3fda..9ea768563a3 100644
--- a/gdb/testsuite/gdb.base/with.exp
+++ b/gdb/testsuite/gdb.base/with.exp
@@ -220,6 +220,24 @@ with_test_prefix "run control" {
 
 # Check errors.
 with_test_prefix "errors" {
+    # Try both an unknown root setting and an unknown prefixed
+    # setting.  The errors come from different locations in the
+    # sources.
+    gdb_test "with xxxx yyyy" \
+ "Undefined set command: \"xxxx\".  Try \"help set\"\\."
+    gdb_test "with print xxxx yyyy" \
+ "Undefined set print command: \"xxxx yyyy\".  Try \"help set print\"\\."
+    # Try one error case for "maint with", to make sure the right
+    # "maintenance with" prefix is shown.
+    gdb_test "maint with xxxx yyyy" \
+ "Undefined maintenance set command: \"xxxx\".  Try \"help maintenance set\"\\."
+
+    # Try ambiguous settings.
+    gdb_test "with w" \
+ "Ambiguous set command \"w\": watchdog, width, write\\."
+    gdb_test "with print m" \
+ "Ambiguous set print command \"m\": max-depth, max-symbolic-offset\\."
+
     gdb_test "with variable xxx=1" \
  "Cannot use this setting with the \"with\" command"
 
--
2.14.5

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v2 0/4] Introduce the "with" command

Philippe Waroquiers
On Wed, 2019-06-19 at 14:05 +0100, Pedro Alves wrote:
> I'm thinking that the errors talking about "set" instead of
> "with" can be seen as a feature.  If you consider the prefixed
> case, like "with print foo", the error is telling you where
> to look at the available settings for that prefix.
Yes, I think this is a good approach, and the error messages
are very clear.

Thanks

Philippe

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v2 4/4] Introduce the "with" command

Pedro Alves-7
In reply to this post by Eli Zaretskii
On 6/18/19 5:42 PM, Eli Zaretskii wrote:
>> From: Pedro Alves <[hidden email]>
>> Cc: Philippe Waroquiers <[hidden email]>
>> Date: Tue, 18 Jun 2019 01:39:02 +0100
>>
>> +@cindex change default behavior of commands
>> +@cindex change default settings
>
> Two index entries that start with the same text and point to the same
> place are not useful.  I'd drop the second one.

I thought it was useful because someone skimming the
index might not relate "behavior of commands" to "settings"
and vice versa.  (settings affect more than commands, like
e.g., they change how gdb reacts to inferior events, like forking.)
But looking at the resulting page, I have to agree with you,
at least the the repetition seems odd.

Some settings use a comma to add some kind of context.
So how about doing something similar here, like:

 @cindex change default behavior of commands, settings

?

>
>> +Many commands change their behavior according to command-specific
>> +variables or settings.  These settings can be changed with the
>> +@code{set} subcommands.  For example, the @code{print} command
>> +(@pxref{Data, ,Examining Data}) prints arrays differently depending on
>> +settings changeable with the commands @code{set print elements
>> +NUMBER-OF-ELEMENTS} and @code{set print array-indexes}, among others.
>> +
>> +You can set these settings to your preference in the gdbinit files
>> +loaded at @value{GDBN} startup.  @xref{Startup}.
>> +
>> +The settings can also be changed interactively during the debugging
>> +session.  For example, to change the limit of array elements to print,
>> +you can do the following:
>> +@smallexample
>> +(@value{GDBN}) set print elements 10
>> +(@value{GDBN}) print some_array
>> +$1 = @{0, 10, 20, 30, 40, 50, 60, 70, 80, 90...@}
>> +@end smallexample
>> +
>> +The above @code{set print elements 10} command changes the number of
>> +elements to print from the default of 200 to 10.  If you only intend
>> +this limit of 10 to be used for printing @code{some_array}, then you
>> +must restore the limit back to 200, with @code{set print elements
>> +200}.
>> +
>> +Some commands allow overriding settings with command options.  For
>> +example, the @code{print} command supports a number of options that
>> +allow overriding relevant global print settings as set by @code{set
>> +print} subcommands.  @xref{print options}.  The example above could be
>> +rewritten as:
>> +@smallexample
>> +(@value{GDBN}) print -elements 10 -- some_array
>> +$1 = @{0, 10, 20, 30, 40, 50, 60, 70, 80, 90...@}
>> +@end smallexample
>> +
>> +Alternatively, you can use the @code{with} command to change a setting
>> +temporarily, for the duration of a command invocation.
>> +
>> +@table @code
>> +@kindex with command
>> +@kindex w @r{(@code{with})}
>> +@cindex settings
>> +@cindex temporarily change settings
>> +@item with @var{setting} [@var{value}] [-- @var{command}]
>> +@itemx w @var{setting} [@var{value}] [-- @var{command}]
>> +Temporarily set @var{setting} to @var{value} for the duration of
>> +@var{command}.
>> +
>> +If no @var{command} is provided, the last command executed is
>> +repeated.
>> +
>> +@var{setting} is any setting settable with the @code{set} subcommands.
>> +
>> +For example, the command
>> +@smallexample
>> +(@value{GDBN}) with print array on -- print some_array
>> +@end smallexample
>> +@noindent
>> +is equivalent to the following 3 commands:
>> +@smallexample
>> +(@value{GDBN}) set print array on
>> +(@value{GDBN}) print some_array
>> +(@value{GDBN}) set print array off
>> +@end smallexample
>> +
>> +The @code{with} command is particularly useful when you want to
>> +override a setting while running user-defined commands, or commands
>> +defined in Python or Guile.  @xref{Extending GDB,, Extending GDB}.
>> +
>> +@smallexample
>> +(@value{GDBN}) with print pretty on -- my_complex_command
>> +@end smallexample
>> +
>> +To change several settings for the same command, you can nest
>> +@code{with} commands.  For example, @code{with language ada -- with
>> +print elements 10} temporarily changes the language to Ada and sets a
>> +limit of 10 elements to print for arrays and strings.
>> +
>> +@end table
>
> This text is OK, but it keeps absolute silence regarding the "--"
> delimiter.  It should explain why it is used here and how to use it
> with multiple "with" prefixes.

I added this where the command's arguments are explained:

 +If a @var{command} is provided, it must be preceded by a double dash
 +(@code{--}) separator.  This is required because some settings accept
 +free-form arguments, such as expressions or filenames.
 
As I had mentioned before, it may be we can make this delimiter
optional.  Also, we may need a "with -separate SEP" option in
case someone wants to use an expression with "--" in it.  However,
I'd rather leave it to a separate pass, as I don't want to spend
much more time on this at this point and the need for that should
be quite rare.

>
>> +  SETTING is any GDB setting settable with the "set" command.
>
> "SETTING ... setting settable ... set".  How about rewording to use
> the root "set" slightly fewer times?

Yeah.. I found the repetition just a natural consequence of the nature
of the command.  The first SETTING is the metavariable name, which
has that name because it's the most natural I could find.  The
last "set" is the name of the "set" command.  

That leaves the "GDB setting settable" part.  But what other
name can we give these knobs, other than "GDB settings"?
I just referred "knob", but that sounds a bit too informal.
I've seen them being called "variables", but that is ambiguous
with program variables, and unlike the "set" command, with
"set VARIABLE = EXPRESSION", the "with" command does not work
with program variables, only GDB settings.

And then, what verb to use to refer to tweaking a setting?
We're using "change" in some spots, including the cindex discussed
above, is that a better choice here?  Like:

 SETTING is any GDB setting changeable with the "set" command.

Do you have a better suggestion?

>
> The documentation parts are OK with these nits fixed.
>
> Thanks.
>

I noticed now that I hadn't documented the @var{value}
argument, so I fixed it too.

These are the tweaks that I currently have locally.

Note this fixes NEWS and help along the same lines too.

From f235d6b5a28594ddd481d36976a8fa7c6ff65062 Mon Sep 17 00:00:00 2001
From: Pedro Alves <[hidden email]>
Date: Wed, 19 Jun 2019 16:00:14 +0100
Subject: [PATCH] fix-docs

---
 gdb/doc/gdb.texinfo | 11 ++++++++---
 gdb/NEWS            |  4 ++--
 gdb/cli/cli-cmds.c  |  2 +-
 gdb/maint.c         |  2 +-
 4 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 635bcff46e1..c9933af8cc8 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -1620,8 +1620,7 @@ for editing.
 
 @node Command Settings
 @section Command Settings
-@cindex change default behavior of commands
-@cindex change default settings
+@cindex change default behavior of commands, settings
 
 Many commands change their behavior according to command-specific
 variables or settings.  These settings can be changed with the
@@ -1671,10 +1670,16 @@ temporarily, for the duration of a command invocation.
 Temporarily set @var{setting} to @var{value} for the duration of
 @var{command}.
 
+@var{setting} is any setting changeable with the @code{set}
+subcommands.  @var{value} is the value the setting is changed to while
+the command is run.
+
 If no @var{command} is provided, the last command executed is
 repeated.
 
-@var{setting} is any setting settable with the @code{set} subcommands.
+If a @var{command} is provided, it must be preceded by a double dash
+(@code{--}) separator.  This is required because some settings accept
+free-form arguments, such as expressions or filenames.
 
 For example, the command
 @smallexample
diff --git a/gdb/NEWS b/gdb/NEWS
index 850ab44df3c..dc4b0012919 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -51,7 +51,7 @@ w SETTING [VALUE] [-- COMMAND]
   Temporarily set SETTING, run COMMAND, and restore SETTING.
   Usage: with SETTING -- COMMAND
   With no COMMAND, repeats the last executed command.
-  SETTING is any GDB setting settable with the "set" command.
+  SETTING is any GDB setting changeable with the "set" subcommands.
   For example, 'with language c -- print someobj' temporarily switches
   to the C language in order to print someobj.  Settings can be
   combined: 'w lang c -- w print elements unlimited -- usercmd'
@@ -59,7 +59,7 @@ w SETTING [VALUE] [-- COMMAND]
   elements to print.
 
 maint with SETTING [VALUE] [-- COMMAND]
-  Like "with", but works with "maintenance set" variables.
+  Like "with", but works with "maintenance set" settings.
 
 set may-call-functions [on|off]
 show may-call-functions
diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index 283bc446026..5a88c9f3c1b 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -1959,7 +1959,7 @@ Temporarily set SETTING to VALUE, run COMMAND, and restore SETTING.\n\
 Usage: with SETTING [VALUE] [-- COMMAND]\n\
 Usage: w SETTING [VALUE] [-- COMMAND]\n\
 With no COMMAND, repeats the last executed command.\n\
-SETTING is any setting settable with the \"set\" subcommands.\n\
+SETTING is any setting changeable with the \"set\" subcommands.\n\
 E.g.:\n\
   with language pascal -- print obj\n\
   with print elements unlimited -- print obj"));
diff --git a/gdb/maint.c b/gdb/maint.c
index 2c10903539b..4e93583b806 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -1046,7 +1046,7 @@ Configure variables internal to GDB that aid in GDB's maintenance"),
 Like \"with\", but works with \"maintenance set\" variables.\n\
 Usage: maintenance with SETTING [VALUE] [-- COMMAND]\n\
 With no COMMAND, repeats the last executed command.\n\
-SETTING is any setting settable with the \"maintenance set\" subcommands."),
+SETTING is any setting changeable with the \"maintenance set\" subcommands."),
  &maintenancelist);
   set_cmd_completer_handle_brkchars (cmd, maintenance_with_cmd_completer);
 
--
2.14.5


Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v2 4/4] Introduce the "with" command

Eli Zaretskii
> Cc: [hidden email], [hidden email]
> From: Pedro Alves <[hidden email]>
> Date: Wed, 19 Jun 2019 16:46:23 +0100
>
> >> +@cindex change default behavior of commands
> >> +@cindex change default settings
> >
> > Two index entries that start with the same text and point to the same
> > place are not useful.  I'd drop the second one.
>
> I thought it was useful because someone skimming the
> index might not relate "behavior of commands" to "settings"
> and vice versa.  (settings affect more than commands, like
> e.g., they change how gdb reacts to inferior events, like forking.)
> But looking at the resulting page, I have to agree with you,
> at least the the repetition seems odd.
>
> Some settings use a comma to add some kind of context.
> So how about doing something similar here, like:
>
>  @cindex change default behavior of commands, settings
>
> ?

I'd suggest to reorder the words of your original second entry:

  @cindex default settings, changing

Then we can have both of these entries.

>  +If a @var{command} is provided, it must be preceded by a double dash
>  +(@code{--}) separator.  This is required because some settings accept
>  +free-form arguments, such as expressions or filenames.
>  
> As I had mentioned before, it may be we can make this delimiter
> optional.  Also, we may need a "with -separate SEP" option in
> case someone wants to use an expression with "--" in it.  However,
> I'd rather leave it to a separate pass, as I don't want to spend
> much more time on this at this point and the need for that should
> be quite rare.

OK.  We can revisit the text when we get to that second pass.

>  SETTING is any GDB setting changeable with the "set" command.
>
> Do you have a better suggestion?

Instead of "changeable", I'd use "you can change".  Otherwise, SGTM.

> +              @var{value} is the value the setting is changed to while
> +the command is run.

Let's try to avoid passive tense, okay?

  @var{value} is the value to assign to @var{setting} while running
  @var{command}.

WDYT?

Thanks.
Reply | Threaded
Open this post in threaded view
|

[PATCH v2.1] Introduce the "with" command

Pedro Alves-7
On 6/19/19 5:53 PM, Eli Zaretskii wrote:

> I'd suggest to reorder the words of your original second entry:
>
>   @cindex default settings, changing
>
> Then we can have both of these entries.

Alright, I did that.

>>  SETTING is any GDB setting changeable with the "set" command.
>>
>> Do you have a better suggestion?
>
> Instead of "changeable", I'd use "you can change".  Otherwise, SGTM.

Thanks, I like it.

>
>> +              @var{value} is the value the setting is changed to while
>> +the command is run.
>
> Let's try to avoid passive tense, okay?
>
>   @var{value} is the value to assign to @var{setting} while running
>   @var{command}.
>
> WDYT?

I think that looks good.

Thanks!

I propagated the wording changes to NEWS and help too.

Here's the updated full patch that incorporates the changes addressing
both Eli's and Philippe's comments.

From 7eb5cafe55eb5c267681cf2b4d8310abeb86e21c Mon Sep 17 00:00:00 2001
From: Pedro Alves <[hidden email]>
Date: Wed, 19 Jun 2019 18:14:15 +0100
Subject: [PATCH v2.1] Introduce the "with" command

( See original discussion and prototype here:
   https://sourceware.org/ml/gdb-patches/2019-05/msg00570.html )

 (gdb) help with
 Temporarily set SETTING to VALUE, run COMMAND, and restore SETTING.
 Usage: with SETTING [VALUE] [-- COMMAND]
 Usage: w SETTING [VALUE] [-- COMMAND]
 With no COMMAND, repeats the last executed command.
 SETTING is any setting you can change with the "set" subcommands.
 E.g.:
   with language pascal -- print obj
   with print elements unlimited -- print obj

As can be seen above, the "with" command is just like "set", but
instead of setting the setting permanently, it sets the setting, runs
a command and then restores the setting.

 (gdb) p g_s
 $1 = {a = 1, b = 2, c = 3}
 (gdb) with language ada -- print g_s
 $2 = (a => 1, b => 2, c => 3)
 Warning: the current language does not match this frame.
 (gdb) show language
 The current source language is "auto; currently c".
 (gdb) with print elements 100 -- with print object on -- print 1
 $3 = 1

You can shorten things a bit though, as long as unambiguous.  So this:

 (gdb) with print elements 100 -- with print object off -- print 1

is the same as:

 (gdb) w p el 100 -- w p o 0 -- p 1

Note that the patch adds a "w" alias for "with", as "w" is not
currently taken:

 (gdb) w
 Ambiguous command "w": watch, wh, whatis, where, while, while-stepping, winheight, ws.

Let me know if you'd prefer to reserve "w" for one of the other
commands above.  IMHO, this command will end up being used frequently
enough that it deserves the "w" shorthand.

A nice feature is that this is fully integrated with TAB-completion:

 (gdb) with p[TAB]
 pagination  print       prompt      python
 (gdb) with print [TAB]
 address                max-depth              static-members
 array                  max-symbolic-offset    symbol
 array-indexes          null-stop              symbol-filename
 asm-demangle           object                 symbol-loading
 demangle               pascal_static-members  thread-events
 elements               pretty                 type
 entry-values           raw                    union
 frame-arguments        repeats                vtbl
 inferior-events        sevenbit-strings
 (gdb) with print [TAB]

 (gdb) with print elements unlimited -- thread apply all -[TAB]
 -ascending  -c          -q          -s

 (gdb) with print elements unlimited -- print -[TAB]
 -address         -max-depth       -repeats         -vtbl
 -array           -null-stop       -static-members
 -array-indexes   -object          -symbol
 -elements        -pretty          -union

The main advantage of this new command compared to command options,
like the new "print -OPT", is that this command works with any
setting, and, it works nicely when you want to override a setting
while running a user-defined command, like:

 (gdb) with print pretty -- usercmd

The disadvantage is that it isn't as compact or easy to type.  I think
of command options and this command as complementary.  I think that
even with this new command, it makes sense to continue developing the
command options in the direction of exposing most-oft-used settings as
command options.

Inspired by Philippe's "/" command proposal, if no command is
specified, then the last command is re-invoked, under the overridden
setting:

 (gdb) p g_s
 $1 = {a = 1, b = 2, c = 3}
 (gdb) with language ada
 $2 = (a => 1, b => 2, c => 3)
 Warning: the current language does not match this frame.

Note: "with" requires "--" to separate the setting from the command.
It might be possible to do without that, but, I haven't tried it yet,
and I think that this can go in without it.  We can always downgrade
to making "--" optional if we manage to make it work.

On to the patch itself, the implementation of the command is simpler
than one might expect.  A few details:

- I factored out a bit from pipe_command into repeat_previous
  directly, because otherwise I'd need to copy&paste the same code and
  same error message in the with command.

- The parse_cli_var_uinteger / parse_cli_var_zuinteger_unlimited /
  do_set_command changes are necessary since we can now pass an empty
  string as argument.

- do_show_command was split in two, as a FIXME comment suggests, but
  for a different reason: we need to get a string version of a "set"
  command's value, and we already had code for that in
  do_show_command.  That code is now factored out to the new
  get_setshow_command_value_string function.

- There's a new "maint with" command added too:

   (gdb) help maint with
   Like "with", but works with "maintenance set" variables.
   Usage: maintenance with SETTING [VALUE] [-- COMMAND]
   With no COMMAND, repeats the last executed command.
   SETTING is any setting you can change with the "maintenance set"
   subcommands.

  "with" and "maint with" share 99% of the implementation.

  This might be useful on its own, but it's also useful for testing,
  since with this, we can use the "maint set/show test-settings"
  settings for exercising the "with" machinery with all the command
  type variants (all enum var_types).  This is done in the new
  gdb/base/with.exp testcase.

The documentation bits are originally based on Philippe's docs for the
"/" command, hence the attribution in the ChangeLog.

gdb/ChangeLog:
yyyy-mm-dd  Pedro Alves  <[hidden email]>

        * NEWS (New commands): Mention "with" and "maint with".
        * cli/cli-cmds.c (with_command_1, with_command_completer_1)
        (with_command, with_command_completer): New.
        (pipe_command): Adjust to new repeat_previous
        interface.
        (_initialize_cli_cmds): Install the "with" command and its "w"
        alias.
        * cli/cli-cmds.h (with_command_1, with_command_completer_1): New
        declarations.
        * cli/cli-setshow.c (parse_cli_var_uinteger)
        (parse_cli_var_zuinteger_unlimited, do_set_command): Handle empty
        argument strings for all var_types.
        (get_setshow_command_value_string): New, factored out from ...
        (do_show_command): ... this.
        * cli/cli-setshow.h: Include <string>.
        (get_setshow_command_value_string): Declare.
        * command.h (repeat_previous): Now returns const char *.  Adjust
        comment.
        * maint.c: Include "cli/cli-cmds.h".
        (maintenance_with_cmd, maintenance_with_cmd_completer): New.
        (_initialize_maint_cmds): Register the "maintenance with" command.
        * top.c (repeat_previous): Move bits from pipe_command here:
        Return the saved command line, if any; error out if there's no
        command to relaunch.

gdb/doc/ChangeLog:
yyyy-mm-dd  Pedro Alves  <[hidden email]>
            Philippe Waroquiers  <[hidden email]>

        * gdb.texinfo (Command Settings): New node documenting the general
        concept of settings, how to change them, and the new "with"
        command.
        (Maintenance Commands): Document "maint with".

gdb/testsuite/ChangeLog:
yyyy-mm-dd  Pedro Alves  <[hidden email]>

        * gdb.base/with.c: New file.
        * gdb.base/with.exp: New file.
---
 gdb/doc/gdb.texinfo             | 100 ++++++++++++++
 gdb/NEWS                        |  15 +++
 gdb/cli/cli-cmds.c              | 129 +++++++++++++++++-
 gdb/cli/cli-cmds.h              |  15 +++
 gdb/cli/cli-setshow.c           |  74 +++++-----
 gdb/cli/cli-setshow.h           |   5 +
 gdb/command.h                   |  19 +--
 gdb/maint.c                     |  28 ++++
 gdb/testsuite/gdb.base/with.c   |  41 ++++++
 gdb/testsuite/gdb.base/with.exp | 289 ++++++++++++++++++++++++++++++++++++++++
 gdb/top.c                       |   7 +-
 11 files changed, 672 insertions(+), 50 deletions(-)
 create mode 100644 gdb/testsuite/gdb.base/with.c
 create mode 100644 gdb/testsuite/gdb.base/with.exp

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index a279a145502..038a110c240 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -1560,6 +1560,7 @@ show you the alternatives available, if there is more than one possibility).
 
 @menu
 * Command Syntax::              How to give commands to @value{GDBN}
+* Command Settings::            How to change default behavior of commands
 * Completion::                  Command completion
 * Command Options::             Command options
 * Help::                        How to ask @value{GDBN} for help
@@ -1616,6 +1617,98 @@ commands.  This command accepts the current line, like @key{RET}, and
 then fetches the next line relative to the current line from the history
 for editing.
 
+
+@node Command Settings
+@section Command Settings
+@cindex default behavior of commands, changing
+@cindex default settings, changing
+
+Many commands change their behavior according to command-specific
+variables or settings.  These settings can be changed with the
+@code{set} subcommands.  For example, the @code{print} command
+(@pxref{Data, ,Examining Data}) prints arrays differently depending on
+settings changeable with the commands @code{set print elements
+NUMBER-OF-ELEMENTS} and @code{set print array-indexes}, among others.
+
+You can set these settings to your preference in the gdbinit files
+loaded at @value{GDBN} startup.  @xref{Startup}.
+
+The settings can also be changed interactively during the debugging
+session.  For example, to change the limit of array elements to print,
+you can do the following:
+@smallexample
+(@value{GDBN}) set print elements 10
+(@value{GDBN}) print some_array
+$1 = @{0, 10, 20, 30, 40, 50, 60, 70, 80, 90...@}
+@end smallexample
+
+The above @code{set print elements 10} command changes the number of
+elements to print from the default of 200 to 10.  If you only intend
+this limit of 10 to be used for printing @code{some_array}, then you
+must restore the limit back to 200, with @code{set print elements
+200}.
+
+Some commands allow overriding settings with command options.  For
+example, the @code{print} command supports a number of options that
+allow overriding relevant global print settings as set by @code{set
+print} subcommands.  @xref{print options}.  The example above could be
+rewritten as:
+@smallexample
+(@value{GDBN}) print -elements 10 -- some_array
+$1 = @{0, 10, 20, 30, 40, 50, 60, 70, 80, 90...@}
+@end smallexample
+
+Alternatively, you can use the @code{with} command to change a setting
+temporarily, for the duration of a command invocation.
+
+@table @code
+@kindex with command
+@kindex w @r{(@code{with})}
+@cindex settings
+@cindex temporarily change settings
+@item with @var{setting} [@var{value}] [-- @var{command}]
+@itemx w @var{setting} [@var{value}] [-- @var{command}]
+Temporarily set @var{setting} to @var{value} for the duration of
+@var{command}.
+
+@var{setting} is any setting you can change with the @code{set}
+subcommands.  @var{value} is the value to assign to @code{setting}
+while running @code{command}.
+
+If no @var{command} is provided, the last command executed is
+repeated.
+
+If a @var{command} is provided, it must be preceded by a double dash
+(@code{--}) separator.  This is required because some settings accept
+free-form arguments, such as expressions or filenames.
+
+For example, the command
+@smallexample
+(@value{GDBN}) with print array on -- print some_array
+@end smallexample
+@noindent
+is equivalent to the following 3 commands:
+@smallexample
+(@value{GDBN}) set print array on
+(@value{GDBN}) print some_array
+(@value{GDBN}) set print array off
+@end smallexample
+
+The @code{with} command is particularly useful when you want to
+override a setting while running user-defined commands, or commands
+defined in Python or Guile.  @xref{Extending GDB,, Extending GDB}.
+
+@smallexample
+(@value{GDBN}) with print pretty on -- my_complex_command
+@end smallexample
+
+To change several settings for the same command, you can nest
+@code{with} commands.  For example, @code{with language ada -- with
+print elements 10} temporarily changes the language to Ada and sets a
+limit of 10 elements to print for arrays and strings.
+
+@end table
+
 @node Completion
 @section Command Completion
 
@@ -37633,6 +37726,13 @@ support in the command options framework.
 These are representative commands for each @var{kind} of setting type
 @value{GDBN} supports.  They are used by the testsuite for exercising
 the settings infrastructure.
+
+@kindex maint with
+@item maint with @var{setting} [@var{value}] [-- @var{command}]
+Like the @code{with} command, but works with @code{maintenance set}
+variables.  This is used by the testsuite to exercise the @code{with}
+command's infrastructure.
+
 @end table
 
 The following command is useful for non-interactive invocations of
diff --git a/gdb/NEWS b/gdb/NEWS
index 4a7a117970a..34c544c3d51 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -46,6 +46,21 @@ pipe -d DELIM COMMAND DELIM SHELL_COMMAND
   With no COMMAND, repeat the last executed command
   and send its output to SHELL_COMMAND.
 
+with SETTING [VALUE] [-- COMMAND]
+w SETTING [VALUE] [-- COMMAND]
+  Temporarily set SETTING, run COMMAND, and restore SETTING.
+  Usage: with SETTING -- COMMAND
+  With no COMMAND, repeats the last executed command.
+  SETTING is any GDB setting you can change with the "set"
+  subcommands.  For example, 'with language c -- print someobj'
+  temporarily switches to the C language in order to print someobj.
+  Settings can be combined: 'w lang c -- w print elements unlimited --
+  usercmd' switches to the C language and runs usercmd with no limit
+  of array elements to print.
+
+maint with SETTING [VALUE] [-- COMMAND]
+  Like "with", but works with "maintenance set" settings.
+
 set may-call-functions [on|off]
 show may-call-functions
   This controls whether GDB will attempt to call functions in
diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index 62f4d7f0c5e..aa7e3921f42 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -211,6 +211,116 @@ show_command (const char *arg, int from_tty)
   cmd_show_list (showlist, from_tty, "");
 }
 
+/* See cli/cli-cmds.h.  */
+
+void
+with_command_1 (const char *set_cmd_prefix,
+ cmd_list_element *setlist, const char *args, int from_tty)
+{
+  const char *delim = strstr (args, "--");
+  const char *nested_cmd = nullptr;
+
+  if (delim == args)
+    error (_("Missing setting before '--' delimiter"));
+
+  if (delim == nullptr || *skip_spaces (&delim[2]) == '\0')
+    nested_cmd = repeat_previous ();
+
+  cmd_list_element *set_cmd = lookup_cmd (&args, setlist, set_cmd_prefix,
+  /*allow_unknown=*/ 0,
+  /*ignore_help_classes=*/ 1);
+  gdb_assert (set_cmd != nullptr);
+
+  if (set_cmd->var == nullptr)
+    error (_("Cannot use this setting with the \"with\" command"));
+
+  std::string temp_value
+    = (delim == nullptr ? args : std::string (args, delim - args));
+
+  if (nested_cmd == nullptr)
+    nested_cmd = skip_spaces (delim + 2);
+
+  std::string org_value = get_setshow_command_value_string (set_cmd);
+
+  /* Tweak the setting to the new temporary value.  */
+  do_set_command (temp_value.c_str (), from_tty, set_cmd);
+
+  try
+    {
+      scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
+
+      /* Execute the nested command.  */
+      execute_command (nested_cmd, from_tty);
+    }
+  catch (const gdb_exception &ex)
+    {
+      /* Restore the setting and rethrow.  If restoring the setting
+ throws, swallow the new exception and warn.  There's nothing
+ else we can reasonably do.  */
+      try
+ {
+  do_set_command (org_value.c_str (), from_tty, set_cmd);
+ }
+      catch (const gdb_exception &ex2)
+ {
+  warning (_("Couldn't restore setting: %s"), ex2.what ());
+ }
+
+      throw;
+    }
+
+  /* Restore the setting.  */
+  do_set_command (org_value.c_str (), from_tty, set_cmd);
+}
+
+/* See cli/cli-cmds.h.  */
+
+void
+with_command_completer_1 (const char *set_cmd_prefix,
+  completion_tracker &tracker,
+  const char *text)
+{
+  tracker.set_use_custom_word_point (true);
+
+  const char *delim = strstr (text, "--");
+
+  /* If we're still not past the "--" delimiter, complete the "with"
+     command as if it was a "set" command.  */
+  if (delim == text
+      || delim == nullptr
+      || !isspace (delim[-1])
+      || !(isspace (delim[2]) || delim[2] == '\0'))
+    {
+      std::string new_text = std::string (set_cmd_prefix) + text;
+      tracker.advance_custom_word_point_by (-(int) strlen (set_cmd_prefix));
+      complete_nested_command_line (tracker, new_text.c_str ());
+      return;
+    }
+
+  /* We're past the "--" delimiter.  Complete on the sub command.  */
+  const char *nested_cmd = skip_spaces (delim + 2);
+  tracker.advance_custom_word_point_by (nested_cmd - text);
+  complete_nested_command_line (tracker, nested_cmd);
+}
+
+/* The "with" command.  */
+
+static void
+with_command (const char *args, int from_tty)
+{
+  with_command_1 ("set ", setlist, args, from_tty);
+}
+
+/* "with" command completer.  */
+
+static void
+with_command_completer (struct cmd_list_element *ignore,
+ completion_tracker &tracker,
+ const char *text, const char * /*word*/)
+{
+  with_command_completer_1 ("set ", tracker,  text);
+}
+
 
 /* Provide documentation on command or list given by COMMAND.  FROM_TTY
    is ignored.  */
@@ -878,12 +988,7 @@ pipe_command (const char *arg, int from_tty)
   arg += delim.length (); /* Skip the delimiter.  */
 
   if (gdb_cmd.empty ())
-    {
-      repeat_previous ();
-      gdb_cmd = skip_spaces (get_saved_command_line ());
-      if (gdb_cmd.empty ())
- error (_("No previous command to relaunch"));
-    }
+    gdb_cmd = repeat_previous ();
 
   const char *shell_command = skip_spaces (arg);
   if (*shell_command == '\0')
@@ -1849,6 +1954,18 @@ Generic command for showing things about the debugger."),
   /* Another way to get at the same thing.  */
   add_info ("set", show_command, _("Show all GDB settings."));
 
+  c = add_com ("with", class_vars, with_command, _("\
+Temporarily set SETTING to VALUE, run COMMAND, and restore SETTING.\n\
+Usage: with SETTING [VALUE] [-- COMMAND]\n\
+Usage: w SETTING [VALUE] [-- COMMAND]\n\
+With no COMMAND, repeats the last executed command.\n\
+SETTING is any setting you can change with the \"set\" subcommands.\n\
+E.g.:\n\
+  with language pascal -- print obj\n\
+  with print elements unlimited -- print obj"));
+  set_cmd_completer_handle_brkchars (c, with_command_completer);
+  add_com_alias ("w", "with", class_vars, 1);
+
   add_cmd ("commands", no_set_class, show_commands, _("\
 Show the history of commands you typed.\n\
 You can supply a command number to start with, or a `+' to start after\n\
diff --git a/gdb/cli/cli-cmds.h b/gdb/cli/cli-cmds.h
index 63b8c4018ce..94e210a84eb 100644
--- a/gdb/cli/cli-cmds.h
+++ b/gdb/cli/cli-cmds.h
@@ -142,4 +142,19 @@ extern gdb::optional<open_script>
 extern int source_verbose;
 extern int trace_commands;
 
+/* Common code for the "with" and "maintenance with" commands.
+   SET_CMD_PREFIX is the spelling of the corresponding "set" command
+   prefix: i.e., "set " or "maintenance set ".  SETLIST is the command
+   element for the same "set" command prefix.  */
+extern void with_command_1 (const char *set_cmd_prefix,
+    cmd_list_element *setlist,
+    const char *args, int from_tty);
+
+/* Common code for the completers of the "with" and "maintenance with"
+   commands.  SET_CMD_PREFIX is the spelling of the corresponding
+   "set" command prefix: i.e., "set " or "maintenance set ".  */
+extern void with_command_completer_1 (const char *set_cmd_prefix,
+      completion_tracker &tracker,
+      const char *text);
+
 #endif /* CLI_CLI_CMDS_H */
diff --git a/gdb/cli/cli-setshow.c b/gdb/cli/cli-setshow.c
index d588d04ab17..6fb32441acc 100644
--- a/gdb/cli/cli-setshow.c
+++ b/gdb/cli/cli-setshow.c
@@ -190,7 +190,7 @@ parse_cli_var_uinteger (var_types var_type, const char **arg,
 {
   LONGEST val;
 
-  if (*arg == nullptr)
+  if (*arg == nullptr || **arg == '\0')
     {
       if (var_type == var_uinteger)
  error_no_arg (_("integer to set it to, or \"unlimited\"."));
@@ -225,7 +225,7 @@ parse_cli_var_zuinteger_unlimited (const char **arg, bool expression)
 {
   LONGEST val;
 
-  if (*arg == nullptr)
+  if (*arg == nullptr || **arg == '\0')
     error_no_arg (_("integer to set it to, or \"unlimited\"."));
 
   if (is_unlimited_literal (arg, expression))
@@ -308,6 +308,9 @@ do_set_command (const char *arg, int from_tty, struct cmd_list_element *c)
 
   gdb_assert (c->type == set_cmd);
 
+  if (arg == NULL)
+    arg = "";
+
   switch (c->var_type)
     {
     case var_string:
@@ -317,8 +320,6 @@ do_set_command (const char *arg, int from_tty, struct cmd_list_element *c)
  char *q;
  int ch;
 
- if (arg == NULL)
-  arg = "";
  newobj = (char *) xmalloc (strlen (arg) + 2);
  p = arg;
  q = newobj;
@@ -364,9 +365,6 @@ do_set_command (const char *arg, int from_tty, struct cmd_list_element *c)
       }
       break;
     case var_string_noescape:
-      if (arg == NULL)
- arg = "";
-
       if (*(char **) c->var == NULL || strcmp (*(char **) c->var, arg) != 0)
  {
   xfree (*(char **) c->var);
@@ -376,14 +374,14 @@ do_set_command (const char *arg, int from_tty, struct cmd_list_element *c)
  }
       break;
     case var_filename:
-      if (arg == NULL)
+      if (*arg == '\0')
  error_no_arg (_("filename to set it to."));
       /* FALLTHROUGH */
     case var_optional_filename:
       {
  char *val = NULL;
 
- if (arg != NULL)
+ if (*arg != '\0')
   {
     /* Clear trailing whitespace of filename.  */
     const char *ptr = arg + strlen (arg) - 1;
@@ -455,7 +453,7 @@ do_set_command (const char *arg, int from_tty, struct cmd_list_element *c)
       {
  LONGEST val;
 
- if (arg == NULL)
+ if (*arg == '\0')
   {
     if (c->var_type == var_integer)
       error_no_arg (_("integer to set it to, or \"unlimited\"."));
@@ -625,24 +623,13 @@ do_set_command (const char *arg, int from_tty, struct cmd_list_element *c)
     }
 }
 
-/* Do a "show" command.  ARG is NULL if no argument, or the
-   text of the argument, and FROM_TTY is nonzero if this command is
-   being entered directly by the user (i.e. these are just like any
-   other command).  C is the command list element for the command.  */
+/* See cli/cli-setshow.h.  */
 
-void
-do_show_command (const char *arg, int from_tty, struct cmd_list_element *c)
+std::string
+get_setshow_command_value_string (cmd_list_element *c)
 {
-  struct ui_out *uiout = current_uiout;
-
-  gdb_assert (c->type == show_cmd);
-
   string_file stb;
 
-  /* Possibly call the pre hook.  */
-  if (c->pre_show_hook)
-    (c->pre_show_hook) (c);
-
   switch (c->var_type)
     {
     case var_string:
@@ -672,9 +659,7 @@ do_show_command (const char *arg, int from_tty, struct cmd_list_element *c)
   stb.puts ("auto");
   break;
  default:
-  internal_error (__FILE__, __LINE__,
-  _("do_show_command: "
-    "invalid var_auto_boolean"));
+  gdb_assert_not_reached ("invalid var_auto_boolean");
   break;
  }
       break;
@@ -703,23 +688,42 @@ do_show_command (const char *arg, int from_tty, struct cmd_list_element *c)
       }
       break;
     default:
-      error (_("gdb internal error: bad var_type in do_show_command"));
+      gdb_assert_not_reached ("bad var_type");
     }
 
+  return std::move (stb.string ());
+}
+
+
+/* Do a "show" command.  ARG is NULL if no argument, or the
+   text of the argument, and FROM_TTY is nonzero if this command is
+   being entered directly by the user (i.e. these are just like any
+   other command).  C is the command list element for the command.  */
+
+void
+do_show_command (const char *arg, int from_tty, struct cmd_list_element *c)
+{
+  struct ui_out *uiout = current_uiout;
+
+  gdb_assert (c->type == show_cmd);
+
+  /* Possibly call the pre hook.  */
+  if (c->pre_show_hook)
+    (c->pre_show_hook) (c);
+
+  std::string val = get_setshow_command_value_string (c);
 
-  /* FIXME: cagney/2005-02-10: Need to split this in half: code to
-     convert the value into a string (esentially the above); and
-     code to print the value out.  For the latter there should be
-     MI and CLI specific versions.  */
+  /* FIXME: cagney/2005-02-10: There should be MI and CLI specific
+     versions of code to print the value out.  */
 
   if (uiout->is_mi_like_p ())
-    uiout->field_stream ("value", stb);
+    uiout->field_string ("value", val.c_str ());
   else
     {
       if (c->show_value_func != NULL)
- c->show_value_func (gdb_stdout, from_tty, c, stb.c_str ());
+ c->show_value_func (gdb_stdout, from_tty, c, val.c_str ());
       else
- deprecated_show_value_hack (gdb_stdout, from_tty, c, stb.c_str ());
+ deprecated_show_value_hack (gdb_stdout, from_tty, c, val.c_str ());
     }
 
   c->func (c, NULL, from_tty);
diff --git a/gdb/cli/cli-setshow.h b/gdb/cli/cli-setshow.h
index c00a0989145..8bfe7e89f09 100644
--- a/gdb/cli/cli-setshow.h
+++ b/gdb/cli/cli-setshow.h
@@ -17,6 +17,8 @@
 #ifndef CLI_CLI_SETSHOW_H
 #define CLI_CLI_SETSHOW_H
 
+#include <string>
+
 struct cmd_list_element;
 
 /* Parse ARG, an option to a boolean variable.
@@ -55,6 +57,9 @@ extern void do_set_command (const char *arg, int from_tty,
 extern void do_show_command (const char *arg, int from_tty,
      struct cmd_list_element *c);
 
+/* Get a string version of C's current value.  */
+extern std::string get_setshow_command_value_string (cmd_list_element *c);
+
 extern void cmd_show_list (struct cmd_list_element *list, int from_tty,
    const char *prefix);
 
diff --git a/gdb/command.h b/gdb/command.h
index 4d52f00de51..648950b5a44 100644
--- a/gdb/command.h
+++ b/gdb/command.h
@@ -461,14 +461,17 @@ extern void error_no_arg (const char *) ATTRIBUTE_NORETURN;
 
 extern void dont_repeat ();
 
-/* Commands call repeat_previous if they want to repeat the previous command.
-   Such commands that repeat the previous command must indicate
-   to not repeat themselves, to avoid recursive repeat.
-   repeat_previous will mark the current command as not repeating,
-   and will ensure get_saved_command_line returns the previous command,
-   so that the currently executing command can repeat it.  */
-
-extern void repeat_previous ();
+/* Commands call repeat_previous if they want to repeat the previous
+   command.  Such commands that repeat the previous command must
+   indicate to not repeat themselves, to avoid recursive repeat.
+   repeat_previous marks the current command as not repeating, and
+   ensures get_saved_command_line returns the previous command, so
+   that the currently executing command can repeat it.  If there's no
+   previous command, throws an error.  Otherwise, returns the result
+   of get_saved_command_line, which now points at the command to
+   repeat.  */
+
+extern const char *repeat_previous ();
 
 /* Prevent dont_repeat from working, and return a cleanup that
    restores the previous state.  */
diff --git a/gdb/maint.c b/gdb/maint.c
index aaabb352249..cecde84e9a2 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -43,6 +43,7 @@
 #include "cli/cli-decode.h"
 #include "cli/cli-utils.h"
 #include "cli/cli-setshow.h"
+#include "cli/cli-cmds.h"
 
 static void maintenance_do_deprecate (const char *, int);
 
@@ -634,6 +635,24 @@ maintenance_show_cmd (const char *args, int from_tty)
   cmd_show_list (maintenance_show_cmdlist, from_tty, "");
 }
 
+/* "maintenance with" command.  */
+
+static void
+maintenance_with_cmd (const char *args, int from_tty)
+{
+  with_command_1 ("maintenance set ", maintenance_set_cmdlist, args, from_tty);
+}
+
+/* "maintenance with" command completer.  */
+
+static void
+maintenance_with_cmd_completer (struct cmd_list_element *ignore,
+ completion_tracker &tracker,
+ const char *text, const char * /*word*/)
+{
+  with_command_completer_1 ("maintenance set ", tracker,  text);
+}
+
 /* Profiling support.  */
 
 static int maintenance_profile_p;
@@ -1023,6 +1042,15 @@ Configure variables internal to GDB that aid in GDB's maintenance"),
   0/*allow-unknown*/,
   &maintenancelist);
 
+  cmd = add_cmd ("with", class_maintenance, maintenance_with_cmd, _("\
+Like \"with\", but works with \"maintenance set\" variables.\n\
+Usage: maintenance with SETTING [VALUE] [-- COMMAND]\n\
+With no COMMAND, repeats the last executed command.\n\
+SETTING is any setting you can change with the \"maintenance set\"\n\
+subcommands."),
+ &maintenancelist);
+  set_cmd_completer_handle_brkchars (cmd, maintenance_with_cmd_completer);
+
 #ifndef _WIN32
   add_cmd ("dump-me", class_maintenance, maintenance_dump_me, _("\
 Get fatal error; make debugger dump its core.\n\
diff --git a/gdb/testsuite/gdb.base/with.c b/gdb/testsuite/gdb.base/with.c
new file mode 100644
index 00000000000..c6426625d4a
--- /dev/null
+++ b/gdb/testsuite/gdb.base/with.c
@@ -0,0 +1,41 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2019 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/>.  */
+
+int xxx1 = 123;
+
+struct S
+{
+  int a;
+  int b;
+  int c;
+};
+
+struct S g_s = {1, 2, 3};
+
+static void
+inc ()
+{
+  g_s.a++;;
+}
+
+int
+main ()
+{
+  inc ();
+
+  return 0;
+}
diff --git a/gdb/testsuite/gdb.base/with.exp b/gdb/testsuite/gdb.base/with.exp
new file mode 100644
index 00000000000..9ea768563a3
--- /dev/null
+++ b/gdb/testsuite/gdb.base/with.exp
@@ -0,0 +1,289 @@
+# This testcase is part of GDB, the GNU debugger.
+
+# Copyright 2019 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 the "with" command.
+
+load_lib completion-support.exp
+
+standard_testfile .c
+
+if {[build_executable "failed to prepare" $testfile $srcfile debug]} {
+    return -1
+}
+
+clean_restart $binfile
+
+# Test "maint with".  VALUES is a list of values.  A nested "with" is
+# performed with each combination of pair of values from this list.
+# This exercises setting a value, and restoring it too.  This is
+# particularly important for the "special" values like "unlimited",
+# which for example for var_uinteger maps to 0 at the user-visible
+# level, but maps to -1 internally.
+
+proc test_with {setting values} {
+    foreach val1 $values {
+ foreach val2 $values {
+    gdb_test \
+ "maint with test-settings $setting $val1 -- maint with test-settings $setting $val2 -- p 1" \
+ " = 1"
+ }
+    }
+}
+
+# Test "maint with" in the error case.  SETTING is the "maint set
+# test-setting" setting to exercise.  TMP_VAL is the value to set the
+# setting to.  EXPECTED_RE is the expected GDB output, which should be
+# an error of some kind.  Also checks that the setting's original
+# value is preserved across the error.
+
+proc test_with_error {setting tmp_val expected_re} {
+    global gdb_prompt
+
+    with_test_prefix "$setting, $tmp_val" {
+ set test "save org value"
+ set org_val ""
+ gdb_test_multiple "maint show test-settings $setting" $test {
+    -re "(.*)\r\n$gdb_prompt $" {
+ set org_val $expect_out(1,string)
+ pass $test
+    }
+ }
+
+ gdb_test \
+    "maint with test-settings $setting $tmp_val -- p 1" \
+    $expected_re
+
+ gdb_test "maint show test-settings $setting" "^$org_val" \
+    "value hasn't changed across error"
+    }
+}
+
+# Test "with" framework basics, using the internal "maint with
+# test-settings" subcommands.
+with_test_prefix "maint" {
+    test_with "auto-boolean" {"on" "off" "auto"}
+    test_with "boolean" {"" "on" "off" "0" "1" "enable" "disable"}
+    test_with "integer" {"0" "1" "-1" "unlimited"}
+    test_with "uinteger" {"0" "1" "unlimited"}
+    test_with "zinteger" {"0" "1" "-1"}
+    test_with "zuinteger" {"0" "1"}
+    test_with "zuinteger-unlimited" {"-1" "unlimited" "0" "1"}
+    test_with "string" {"" "foo" "\"hello world\""}
+    test_with "string-noescape" {"" "foo" "\"hello world\""}
+    test_with "filename" {"/foo" "bar/x/y"}
+    test_with "optional-filename" {"" "/foo" "bar/x/y"}
+    test_with "enum" {"xxx" "yyy"}
+
+    # Check the most important error conditions.  E.g., empty,
+    # negative or "unlimited" values for settings that don't accept
+    # those.  Exhaustive error coverage of the set/with value parsing
+    # is left to "set" testing, in gdb.base/settings.exp.
+    test_with_error "auto-boolean" "" \
+ "\"on\", \"off\" or \"auto\" expected\\."
+    test_with_error "auto-boolean" "xxx" \
+ "\"on\", \"off\" or \"auto\" expected\\."
+    test_with_error "boolean" "2" "\"on\" or \"off\" expected\\."
+    test_with_error "uinteger" "-1" "integer -1 out of range"
+    test_with_error "uinteger" "" \
+ "Argument required \\(integer to set it to, or \"unlimited\"\\.\\)\\."
+    test_with_error "zuinteger" "-1" "integer -1 out of range"
+    test_with_error "zuinteger" "" \
+ "Argument required \\(integer to set it to\\.\\)\\."
+    test_with_error "zuinteger-unlimited" "-2" \
+ "only -1 is allowed to set as unlimited"
+    test_with_error "zuinteger-unlimited" "" \
+ "Argument required \\(integer to set it to, or \"unlimited\"\\.\\)\\."
+    test_with_error "filename" "" \
+ "Argument required \\(filename to set it to\\.\\)\\."
+    test_with_error "enum" "" \
+ "Requires an argument\\. Valid arguments are xxx, yyy, zzz\\."
+}
+
+# Basic/core tests using user-visible commands.
+with_test_prefix "basics" {
+    gdb_test "print g_s" " = {a = 1, b = 2, c = 3}"
+    gdb_test "with print pretty -- print g_s" \
+ [multi_line  \
+     " = {" \
+     "  a = 1," \
+     "  b = 2," \
+     "  c = 3" \
+     "}"]
+
+    # A boolean setting.
+    gdb_test "with non-stop on -- show non-stop" \
+ "Controlling the inferior in non-stop mode is on\\."
+    gdb_test "show non-stop" \
+ "Controlling the inferior in non-stop mode is off\\."
+
+    # Language.
+    gdb_test "with language pascal -- show language" \
+ "The current source language is \"pascal\"\\."
+
+    gdb_test "show language" \
+ "The current source language is \"auto; currently c\"\\."
+
+    gdb_test "with language ada -- print g_s" \
+ " = \\(a => 1, b => 2, c => 3\\)"
+
+    # Nested "with"s.
+    gdb_test "with language ada -- with language c -- print g_s" \
+ " = {a = 1, b = 2, c = 3}"
+
+    # "w" alias.
+    gdb_test "w language pascal -- show language" \
+ "The current source language is \"pascal\"\\." \
+ "w alias works"
+
+    # An early prototype of the "with" command got this wrong.
+    gdb_test \
+ "w print repeats unlimited -- w print repeats 1 -- p \"1223334444\"" \
+ " = \"1\", '2' <repeats 2 times>, '3' <repeats 3 times>, '4' <repeats 4 times>"
+}
+
+# Check a user-defined command.
+with_test_prefix "user-defined" {
+    # A user defined command.
+    set test "define usercmd"
+    gdb_test_multiple "define usercmd" $test {
+ -re "End with"  {
+    gdb_test \
+ [multi_line_input \
+     {print g_s} \
+     {end}] \
+ "" \
+ $test
+ }
+    }
+    gdb_test "with language ada -- usercmd" \
+ " = \\(a => 1, b => 2, c => 3\\)"
+}
+
+# Check repeating.
+with_test_prefix "repeat" {
+    clean_restart $binfile
+
+    # "with" with no command reinvokes the previous command.
+    gdb_test "with language ada" \
+ "No previous command to relaunch" \
+ "reinvoke with no previous command to relaunch"
+
+    gdb_test "print g_s" " = {a = 1, b = 2, c = 3}"
+
+    gdb_test "with language ada" \
+ " = \\(a => 1, b => 2, c => 3\\)" \
+ "reinvoke with language"
+
+    # Same, but with "--".
+    gdb_test "with language fortran --" \
+ " = \\( a = 1, b = 2, c = 3 \\)" \
+ "reinvoke with language and --"
+
+    # Repeating repeats the original "print g_s", not the last "with"
+    # command.
+    set test "repeat command line"
+    send_gdb "\n"
+    gdb_test_multiple "" $test {
+ -re " = {a = 1, b = 2, c = 3}\r\n$gdb_prompt $" {
+    pass $test
+ }
+    }
+}
+
+# Basic run control.
+with_test_prefix "run control" {
+    clean_restart $binfile
+
+    if ![runto_main] {
+ fail "cannot run to main"
+ return
+    }
+
+    # Check "with" with a synchronous execution command.
+    gdb_test "with disassemble-next-line on -- next" \
+ "return 0;.*=>.*"
+}
+
+# Check errors.
+with_test_prefix "errors" {
+    # Try both an unknown root setting and an unknown prefixed
+    # setting.  The errors come from different locations in the
+    # sources.
+    gdb_test "with xxxx yyyy" \
+ "Undefined set command: \"xxxx\".  Try \"help set\"\\."
+    gdb_test "with print xxxx yyyy" \
+ "Undefined set print command: \"xxxx yyyy\".  Try \"help set print\"\\."
+    # Try one error case for "maint with", to make sure the right
+    # "maintenance with" prefix is shown.
+    gdb_test "maint with xxxx yyyy" \
+ "Undefined maintenance set command: \"xxxx\".  Try \"help maintenance set\"\\."
+
+    # Try ambiguous settings.
+    gdb_test "with w" \
+ "Ambiguous set command \"w\": watchdog, width, write\\."
+    gdb_test "with print m" \
+ "Ambiguous set print command \"m\": max-depth, max-symbolic-offset\\."
+
+    gdb_test "with variable xxx=1" \
+ "Cannot use this setting with the \"with\" command"
+
+    gdb_test "with print elements -- p 1" \
+ "Argument required \\(integer to set it to, or \"unlimited\"\\.\\)\\."
+
+    gdb_test "with -- p 1" \
+ "Missing setting before '--' delimiter"
+
+    # Check that the setting is restored even if the command throws.
+    gdb_test "with print elements 1 -- unknowncommand" \
+ "Undefined command: \"unknowncommand\"\\.  Try \"help\"\\."
+    gdb_test "show print elements" \
+ "Limit on string chars or array elements to print is 200\\."
+}
+
+# Check completion.
+with_test_prefix "completion" {
+    test_gdb_complete_unique \
+ "with pri" \
+ "with print"
+
+    test_gdb_complete_unique \
+ "with print ele" \
+ "with print elements"
+
+    test_gdb_complete_unique \
+ "with print elements u" \
+ "with print elements unlimited"
+
+    test_gdb_complete_none \
+ "with print elements unlimited "
+
+    test_gdb_completion_offers_commands "with print elements unlimited -- "
+
+    # Check that the completer nests into the nested command line's
+    # completer.
+    test_gdb_complete_unique \
+ "with print elements unlimited -- with print ele" \
+ "with print elements unlimited -- with print elements"
+
+    # Check completion of "maint with".  "maint with" and "with"'s
+    # completers share 99% of the code.  All we need to care about
+    # here is that the completion word point is computed correctly, so
+    # any simple completion is sufficient.
+    test_gdb_complete_unique \
+ "maint with test-set" \
+ "maint with test-settings"
+}
diff --git a/gdb/top.c b/gdb/top.c
index 857207c3767..9a345538634 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -735,7 +735,7 @@ dont_repeat (void)
 
 /* See command.h  */
 
-void
+const char *
 repeat_previous ()
 {
   /* Do not repeat this command, as this command is a repeating command.  */
@@ -745,6 +745,11 @@ repeat_previous ()
      so swap it with previous_saved_command_line.  */
   std::swap (previous_saved_command_line, saved_command_line);
   std::swap (previous_repeat_arguments, repeat_arguments);
+
+  const char *prev = skip_spaces (get_saved_command_line ());
+  if (*prev == '\0')
+    error (_("No previous command to relaunch"));
+  return prev;
 }
 
 /* See command.h.  */
--
2.14.5

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v2.1] Introduce the "with" command

Philippe Waroquiers
On Wed, 2019-06-19 at 18:20 +0100, Pedro Alves wrote:
> Here's the updated full patch that incorporates the changes addressing
> both Eli's and Philippe's comments.
I quickly re-read the patch and did some trials.

Two small remarks:
* The tests related to "maint with" are failing.
  It looks like this patch misses the change
  "maint test-settings" to "maint set|show test-settings".
* Maybe it would be worth expanding slightly the on-line help?
What about:

(gdb) help with
Temporarily set SETTING to VALUE, run COMMAND, and restore SETTING.
Usage: with SETTING [VALUE] [-- COMMAND]
Usage: w SETTING [VALUE] [-- COMMAND]
With no COMMAND, repeats the last executed command.

SETTING is any setting you can change with the "set" subcommands.
Examples:
  with language pascal -- print obj
  with print elements unlimited -- print obj
  
Multiple settings can be changed using nested with, and abbreviations
can be used for commands and/or values.
Example:
  w la p -- w p el u -- p obj
(gdb) 


Thanks

Philippe

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v2.1] Introduce the "with" command

Pedro Alves-7
On 6/22/19 11:30 AM, Philippe Waroquiers wrote:
> On Wed, 2019-06-19 at 18:20 +0100, Pedro Alves wrote:
>> Here's the updated full patch that incorporates the changes addressing
>> both Eli's and Philippe's comments.
> I quickly re-read the patch and did some trials.
>

Thanks!

> Two small remarks:
> * The tests related to "maint with" are failing.
>   It looks like this patch misses the change
>   "maint test-settings" to "maint set|show test-settings".

Hmm, with.exp and settings.exp both pass for me.  Could you try the
users/palves/cli-options branch?  It contains the latest version,
including the change below.

> * Maybe it would be worth expanding slightly the on-line help?
> What about:
>
> (gdb) help with
> Temporarily set SETTING to VALUE, run COMMAND, and restore SETTING.
> Usage: with SETTING [VALUE] [-- COMMAND]
> Usage: w SETTING [VALUE] [-- COMMAND]
> With no COMMAND, repeats the last executed command.
>
> SETTING is any setting you can change with the "set" subcommands.
> Examples:
>   with language pascal -- print obj
>   with print elements unlimited -- print obj
>   
> Multiple settings can be changed using nested with, and abbreviations
> can be used for commands and/or values.
> Example:
>   w la p -- w p el u -- p obj
> (gdb) 

Sounds good to me, with a change to switch to active voice, to go
with the "changeable" -> "you can change" tweak in the previous round.

Like:

(gdb) help with
Temporarily set SETTING to VALUE, run COMMAND, and restore SETTING.
Usage: with SETTING [VALUE] [-- COMMAND]
Usage: w SETTING [VALUE] [-- COMMAND]
With no COMMAND, repeats the last executed command.

SETTING is any setting you can change with the "set" subcommands.
E.g.:
  with language pascal -- print obj
  with print elements unlimited -- print obj

You can change multiple settings using nested with, and use
abbreviations for commands and/or values.  E.g.:
  w la p -- w p el u -- p obj
(gdb)

WDYT?

Thanks,
Pedro Alves
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v2.1] Introduce the "with" command

Philippe Waroquiers
On Sat, 2019-06-22 at 12:48 +0100, Pedro Alves wrote:
> Hmm, with.exp and settings.exp both pass for me.  Could you try the
> users/palves/cli-options branch?  It contains the latest version,
> including the change below.
The branch works ok.

> You can change multiple settings using nested with, and use
> abbreviations for commands and/or values.  E.g.:
>   w la p -- w p el u -- p obj
> (gdb)
>
> WDYT?
Looks nice(r) to me, thanks.

Philippe

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH v2 0/4] Introduce the "with" command

Pedro Alves-7
In reply to this post by Pedro Alves-7
On 6/18/19 1:38 AM, Pedro Alves wrote:

>
>  (gdb) help with
>  Temporarily set SETTING to VALUE, run COMMAND, and restore SETTING.
>  Usage: with SETTING [VALUE] [-- COMMAND]
>  Usage: w SETTING [VALUE] [-- COMMAND]
>  With no COMMAND, repeats the last executed command.
>  SETTING is any setting settable with the "set" command.
>  E.g.:
>    with language pascal -- print obj
>    with print elements unlimited -- print obj
>

I've merged this in now.

Thanks,
Pedro Alves
Reply | Threaded
Open this post in threaded view
|

New FAIL on gdb.base/with.exp on native-extended-gdbserver (was: Re: [PATCH v2 0/4] Introduce the "with" command)

Sergio Durigan Junior
On Wednesday, July 03 2019, Pedro Alves wrote:

> On 6/18/19 1:38 AM, Pedro Alves wrote:
>>
>>  (gdb) help with
>>  Temporarily set SETTING to VALUE, run COMMAND, and restore SETTING.
>>  Usage: with SETTING [VALUE] [-- COMMAND]
>>  Usage: w SETTING [VALUE] [-- COMMAND]
>>  With no COMMAND, repeats the last executed command.
>>  SETTING is any setting settable with the "set" command.
>>  E.g.:
>>    with language pascal -- print obj
>>    with print elements unlimited -- print obj
>>
>
> I've merged this in now.

Hi Pedro,

First of all, thanks for the nice feature.

BuildBot has unfortunately caught a new failure on gdb.base/with.exp
when run agains native-extended-gdbserver:

  https://sourceware.org/ml/gdb-testers/2019-q3/msg00213.html

  new FAIL: gdb.base/with.exp: repeat: reinvoke with no previous command to relaunch

I've also found it here when preparing a new Fedora GDB release.

Thanks,

--
Sergio
GPG key ID: 237A 54B1 0287 28BF 00EF  31F4 D0EB 7628 65FC 5E36
Please send encrypted e-mail if possible
http://sergiodj.net/