diff mbox

[2/2] gdb: Simplify variable set hooks

Message ID 1478888325-32039-3-git-send-email-andrew.burgess@embecosm.com
State New
Headers show

Commit Message

Andrew Burgess Nov. 11, 2016, 6:18 p.m. UTC
Now the the variable set-hook mechanism supports automatic rollback of
the variable value if the set-hook throws an error, simplify existing
cases where we manually performed roll-back within the set-hook.

gdb/ChangeLog:

	* dcache.c (set_dcache_size): Don't change value on error path.
	(set_dcache_line_size): Likewise.
	* record.c (record_insn_history_size_setshow_var): Remove.
	(record_call_history_size_setshow_var): Remove.
	(validate_history_size): Simplify to just the error check.
	(set_record_insn_history_size): Update call to
	validate_history_size.
	(set_record_call_history_size): Likewise.
	(_initialize_record): Remove use of *_setshow_var.
	* symtab.c (new_symbol_cache_size): Remove.
	(symbol_cache_size): Update comment.
	(set_symbol_cache_size_handler): Simplify error check.
	(_initialize_symtab): Remove use of new_symbol_cache_size.
	* valprint.c (input_radix_1): Remove.
	(set_input_radix): Remove use of input_radix_1.
	(set_input_radix_1): Likewise.
	(output_radix_1): Remove.
	(set_output_radix): Remove use of output_radix_1.
	(set_output_radix_1): Likewise.
	(_initialize_valprint): Remove use of output_radix_1 and
	input_radix_1.
	* value.c (set_max_value_size): Simplify error case.

gdb/testsuite/ChangeLog:

	* gdb.base/max-value-size.exp: Update expected output.
---
 gdb/ChangeLog                             | 25 +++++++++++++++++++
 gdb/dcache.c                              | 12 +++------
 gdb/record.c                              | 41 ++++++-------------------------
 gdb/symtab.c                              | 23 ++++-------------
 gdb/testsuite/ChangeLog                   |  4 +++
 gdb/testsuite/gdb.base/max-value-size.exp |  4 +--
 gdb/valprint.c                            | 31 ++++++-----------------
 gdb/value.c                               |  6 +----
 8 files changed, 56 insertions(+), 90 deletions(-)

Comments

Luis Machado Nov. 12, 2016, 12:29 a.m. UTC | #1
On 11/11/2016 12:18 PM, Andrew Burgess wrote:
> Now the the variable set-hook mechanism supports automatic rollback of
> the variable value if the set-hook throws an error, simplify existing
> cases where we manually performed roll-back within the set-hook.
>
> gdb/ChangeLog:
>
> 	* dcache.c (set_dcache_size): Don't change value on error path.
> 	(set_dcache_line_size): Likewise.
> 	* record.c (record_insn_history_size_setshow_var): Remove.
> 	(record_call_history_size_setshow_var): Remove.
> 	(validate_history_size): Simplify to just the error check.
> 	(set_record_insn_history_size): Update call to
> 	validate_history_size.
> 	(set_record_call_history_size): Likewise.
> 	(_initialize_record): Remove use of *_setshow_var.
> 	* symtab.c (new_symbol_cache_size): Remove.
> 	(symbol_cache_size): Update comment.
> 	(set_symbol_cache_size_handler): Simplify error check.
> 	(_initialize_symtab): Remove use of new_symbol_cache_size.
> 	* valprint.c (input_radix_1): Remove.
> 	(set_input_radix): Remove use of input_radix_1.
> 	(set_input_radix_1): Likewise.
> 	(output_radix_1): Remove.
> 	(set_output_radix): Remove use of output_radix_1.
> 	(set_output_radix_1): Likewise.
> 	(_initialize_valprint): Remove use of output_radix_1 and
> 	input_radix_1.
> 	* value.c (set_max_value_size): Simplify error case.
>
> gdb/testsuite/ChangeLog:
>
> 	* gdb.base/max-value-size.exp: Update expected output.
> ---
>  gdb/ChangeLog                             | 25 +++++++++++++++++++
>  gdb/dcache.c                              | 12 +++------
>  gdb/record.c                              | 41 ++++++-------------------------
>  gdb/symtab.c                              | 23 ++++-------------
>  gdb/testsuite/ChangeLog                   |  4 +++
>  gdb/testsuite/gdb.base/max-value-size.exp |  4 +--
>  gdb/valprint.c                            | 31 ++++++-----------------
>  gdb/value.c                               |  6 +----
>  8 files changed, 56 insertions(+), 90 deletions(-)

I have no comments on this patch. Looks like a nice cleanup of 
convoluted code.
Metzger, Markus T Nov. 14, 2016, 8:35 a.m. UTC | #2
> -----Original Message-----
> From: gdb-patches-owner@sourceware.org [mailto:gdb-patches-
> owner@sourceware.org] On Behalf Of Andrew Burgess
> Sent: Friday, November 11, 2016 7:19 PM
> To: gdb-patches@sourceware.org
> Cc: Andrew Burgess <andrew.burgess@embecosm.com>
> Subject: [PATCH 2/2] gdb: Simplify variable set hooks

Hi Andrew,

> Now the the variable set-hook mechanism supports automatic rollback of
> the variable value if the set-hook throws an error, simplify existing
> cases where we manually performed roll-back within the set-hook.
> 
> gdb/ChangeLog:
> 
> 	* dcache.c (set_dcache_size): Don't change value on error path.
> 	(set_dcache_line_size): Likewise.
> 	* record.c (record_insn_history_size_setshow_var): Remove.
> 	(record_call_history_size_setshow_var): Remove.
> 	(validate_history_size): Simplify to just the error check.
> 	(set_record_insn_history_size): Update call to
> 	validate_history_size.
> 	(set_record_call_history_size): Likewise.
> 	(_initialize_record): Remove use of *_setshow_var.
> 	* symtab.c (new_symbol_cache_size): Remove.
> 	(symbol_cache_size): Update comment.
> 	(set_symbol_cache_size_handler): Simplify error check.
> 	(_initialize_symtab): Remove use of new_symbol_cache_size.
> 	* valprint.c (input_radix_1): Remove.
> 	(set_input_radix): Remove use of input_radix_1.
> 	(set_input_radix_1): Likewise.
> 	(output_radix_1): Remove.
> 	(set_output_radix): Remove use of output_radix_1.
> 	(set_output_radix_1): Likewise.
> 	(_initialize_valprint): Remove use of output_radix_1 and
> 	input_radix_1.
> 	* value.c (set_max_value_size): Simplify error case.
> 
> gdb/testsuite/ChangeLog:
> 
> 	* gdb.base/max-value-size.exp: Update expected output.
> ---
>  gdb/ChangeLog                             | 25 +++++++++++++++++++
>  gdb/dcache.c                              | 12 +++------
>  gdb/record.c                              | 41 ++++++-------------------------
>  gdb/symtab.c                              | 23 ++++-------------
>  gdb/testsuite/ChangeLog                   |  4 +++
>  gdb/testsuite/gdb.base/max-value-size.exp |  4 +--
>  gdb/valprint.c                            | 31 ++++++-----------------
>  gdb/value.c                               |  6 +----
>  8 files changed, 56 insertions(+), 90 deletions(-)

The record changes look good to me.

Regarding the parent patch, if we passed the to-be-set value to the
set function in addition to the pointer to the set/show variable, we
could leave the actual assignment to the set function.  We wouldn't
need to take care of restoring the previous value in the caller since
we have not updated it, yet.  This might be simpler.

Regards,
Markus.

Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Christian Lamprechter
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928
Pedro Alves Nov. 24, 2016, 11:57 p.m. UTC | #3
On 11/11/2016 06:18 PM, Andrew Burgess wrote:
> Now the the variable set-hook mechanism supports automatic rollback of
> the variable value if the set-hook throws an error, simplify existing
> cases where we manually performed roll-back within the set-hook.

Looks good, if we assume we end up agreeing on the previous patch.

Off hand, I recall "set non-stop" and "maint set target-async" as
two other commands that could be simplified.

Thanks,
Pedro Alves
diff mbox

Patch

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 3cb115f..d142b75 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,30 @@ 
 2016-11-11  Andrew Burgess  <andrew.burgess@embecosm.com>
 
+	* dcache.c (set_dcache_size): Don't change value on error path.
+	(set_dcache_line_size): Likewise.
+	* record.c (record_insn_history_size_setshow_var): Remove.
+	(record_call_history_size_setshow_var): Remove.
+	(validate_history_size): Simplify to just the error check.
+	(set_record_insn_history_size): Update call to
+	validate_history_size.
+	(set_record_call_history_size): Likewise.
+	(_initialize_record): Remove use of *_setshow_var.
+	* symtab.c (new_symbol_cache_size): Remove.
+	(symbol_cache_size): Update comment.
+	(set_symbol_cache_size_handler): Simplify error check.
+	(_initialize_symtab): Remove use of new_symbol_cache_size.
+	* valprint.c (input_radix_1): Remove.
+	(set_input_radix): Remove use of input_radix_1.
+	(set_input_radix_1): Likewise.
+	(output_radix_1): Remove.
+	(set_output_radix): Remove use of output_radix_1.
+	(set_output_radix_1): Likewise.
+	(_initialize_valprint): Remove use of output_radix_1 and
+	input_radix_1.
+	* value.c (set_max_value_size): Simplify error case.
+
+2016-11-11  Andrew Burgess  <andrew.burgess@embecosm.com>
+
 	* cli/cli-setshow.c: Add exceptions.h include.
 	(do_set_comment): When installing a settings new value, don't
 	loose the previous value until the set-hook has been called.  If
diff --git a/gdb/dcache.c b/gdb/dcache.c
index cb43068..053b1de 100644
--- a/gdb/dcache.c
+++ b/gdb/dcache.c
@@ -652,10 +652,7 @@  set_dcache_size (char *args, int from_tty,
 		 struct cmd_list_element *c)
 {
   if (dcache_size == 0)
-    {
-      dcache_size = DCACHE_DEFAULT_SIZE;
-      error (_("Dcache size must be greater than 0."));
-    }
+    error (_("Dcache size must be greater than 0."));
   target_dcache_invalidate ();
 }
 
@@ -665,11 +662,8 @@  set_dcache_line_size (char *args, int from_tty,
 {
   if (dcache_line_size < 2
       || (dcache_line_size & (dcache_line_size - 1)) != 0)
-    {
-      unsigned d = dcache_line_size;
-      dcache_line_size = DCACHE_DEFAULT_LINE_SIZE;
-      error (_("Invalid dcache line size: %u (must be power of 2)."), d);
-    }
+    error (_("Invalid dcache line size: %u (must be power of 2)."),
+	   dcache_line_size);
   target_dcache_invalidate ();
 }
 
diff --git a/gdb/record.c b/gdb/record.c
index 34ebd1b..6b0811b 100644
--- a/gdb/record.c
+++ b/gdb/record.c
@@ -35,18 +35,9 @@  unsigned int record_debug = 0;
 /* The number of instructions to print in "record instruction-history".  */
 static unsigned int record_insn_history_size = 10;
 
-/* The variable registered as control variable in the "record
-   instruction-history" command.  Necessary for extra input
-   validation.  */
-static unsigned int record_insn_history_size_setshow_var;
-
 /* The number of functions to print in "record function-call-history".  */
 static unsigned int record_call_history_size = 10;
 
-/* The variable registered as control variable in the "record
-   call-history" command.  Necessary for extra input validation.  */
-static unsigned int record_call_history_size_setshow_var;
-
 struct cmd_list_element *record_cmdlist = NULL;
 struct cmd_list_element *record_goto_cmdlist = NULL;
 struct cmd_list_element *set_record_cmdlist = NULL;
@@ -690,23 +681,13 @@  cmd_record_call_history (char *arg, int from_tty)
 
 /* Helper for "set record instruction-history-size" and "set record
    function-call-history-size" input validation.  COMMAND_VAR is the
-   variable registered in the command as control variable.  *SETTING
-   is the real setting the command allows changing.  */
+   variable registered in the command as control variable.  */
 
 static void
-validate_history_size (unsigned int *command_var, unsigned int *setting)
+validate_history_size (unsigned int command_var)
 {
-  if (*command_var != UINT_MAX && *command_var > INT_MAX)
-    {
-      unsigned int new_value = *command_var;
-
-      /* Restore previous value.  */
-      *command_var = *setting;
-      error (_("integer %u out of range"), new_value);
-    }
-
-  /* Commit new value.  */
-  *setting = *command_var;
+  if (command_var != UINT_MAX && command_var > INT_MAX)
+    error (_("integer %u out of range"), command_var);
 }
 
 /* Called by do_setshow_command.  We only want values in the
@@ -717,8 +698,7 @@  static void
 set_record_insn_history_size (char *args, int from_tty,
 			      struct cmd_list_element *c)
 {
-  validate_history_size (&record_insn_history_size_setshow_var,
-			 &record_insn_history_size);
+  validate_history_size (record_insn_history_size);
 }
 
 /* Called by do_setshow_command.  We only want values in the
@@ -729,8 +709,7 @@  static void
 set_record_call_history_size (char *args, int from_tty,
 			      struct cmd_list_element *c)
 {
-  validate_history_size (&record_call_history_size_setshow_var,
-			 &record_call_history_size);
+  validate_history_size (record_call_history_size);
 }
 
 /* Provide a prototype to silence -Wmissing-prototypes.  */
@@ -750,7 +729,7 @@  _initialize_record (void)
 			     &showdebuglist);
 
   add_setshow_uinteger_cmd ("instruction-history-size", no_class,
-			    &record_insn_history_size_setshow_var, _("\
+			    &record_insn_history_size, _("\
 Set number of instructions to print in \"record instruction-history\"."), _("\
 Show number of instructions to print in \"record instruction-history\"."), _("\
 A size of \"unlimited\" means unlimited instructions.  The default is 10."),
@@ -758,7 +737,7 @@  A size of \"unlimited\" means unlimited instructions.  The default is 10."),
 			    &set_record_cmdlist, &show_record_cmdlist);
 
   add_setshow_uinteger_cmd ("function-call-history-size", no_class,
-			    &record_call_history_size_setshow_var, _("\
+			    &record_call_history_size, _("\
 Set number of function to print in \"record function-call-history\"."), _("\
 Show number of functions to print in \"record function-call-history\"."), _("\
 A size of \"unlimited\" means unlimited lines.  The default is 10."),
@@ -855,8 +834,4 @@  from the first argument.\n\
 The number of functions to print can be defined with \"set record \
 function-call-history-size\"."),
            &record_cmdlist);
-
-  /* Sync command control variables.  */
-  record_insn_history_size_setshow_var = record_insn_history_size;
-  record_call_history_size_setshow_var = record_call_history_size;
 }
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 430bc8d..b5352fc 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -212,12 +212,7 @@  unsigned int symtab_create_debug = 0;
 /* When non-zero, print debugging messages related to symbol lookup.  */
 unsigned int symbol_lookup_debug = 0;
 
-/* The size of the cache is staged here.  */
-static unsigned int new_symbol_cache_size = DEFAULT_SYMBOL_CACHE_SIZE;
-
-/* The current value of the symbol cache size.
-   This is saved so that if the user enters a value too big we can restore
-   the original value from here.  */
+/* The current value of the symbol cache size.  */
 static unsigned int symbol_cache_size = DEFAULT_SYMBOL_CACHE_SIZE;
 
 /* Non-zero if a file may be known by two different basenames.
@@ -1285,17 +1280,9 @@  static void
 set_symbol_cache_size_handler (char *args, int from_tty,
 			       struct cmd_list_element *c)
 {
-  if (new_symbol_cache_size > MAX_SYMBOL_CACHE_SIZE)
-    {
-      /* Restore the previous value.
-	 This is the value the "show" command prints.  */
-      new_symbol_cache_size = symbol_cache_size;
-
-      error (_("Symbol cache size is too large, max is %u."),
-	     MAX_SYMBOL_CACHE_SIZE);
-    }
-  symbol_cache_size = new_symbol_cache_size;
-
+  if (symbol_cache_size > MAX_SYMBOL_CACHE_SIZE)
+    error (_("Symbol cache size is too large, max is %u."),
+	   MAX_SYMBOL_CACHE_SIZE);
   set_symbol_cache_size (symbol_cache_size);
 }
 
@@ -6222,7 +6209,7 @@  When enabled (non-zero), symbol lookups are logged."),
 			   &setdebuglist, &showdebuglist);
 
   add_setshow_zuinteger_cmd ("symbol-cache-size", no_class,
-			     &new_symbol_cache_size,
+			     &symbol_cache_size,
 			     _("Set the size of the symbol cache."),
 			     _("Show the size of the symbol cache."), _("\
 The size of the symbol cache.\n\
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index 01f737e..14bf74d 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,5 +1,9 @@ 
 2016-11-11  Andrew Burgess  <andrew.burgess@embecosm.com>
 
+	* gdb.base/max-value-size.exp: Update expected output.
+
+2016-11-11  Andrew Burgess  <andrew.burgess@embecosm.com>
+
 	* gdb.base/dcache-line-set-error.exp: New file.
 
 2016-11-09  Pedro Alves  <palves@redhat.com>
diff --git a/gdb/testsuite/gdb.base/max-value-size.exp b/gdb/testsuite/gdb.base/max-value-size.exp
index 63a97a0..e179f2f 100644
--- a/gdb/testsuite/gdb.base/max-value-size.exp
+++ b/gdb/testsuite/gdb.base/max-value-size.exp
@@ -90,8 +90,8 @@  set_and_check_max_value_size "unlimited"
 
 # Check that we can't set the maximum size stupidly low.
 gdb_test "set max-value-size 1" \
-    "max-value-size set too low, increasing to \[0-9\]+ bytes"
+    "max-value-size 1 bytes is too low"
 gdb_test "set max-value-size 0" \
-    "max-value-size set too low, increasing to \[0-9\]+ bytes"
+    "max-value-size 0 bytes is too low"
 gdb_test "set max-value-size -5" \
     "only -1 is allowed to set as unlimited"
diff --git a/gdb/valprint.c b/gdb/valprint.c
index c0cdb34..9f8b233 100644
--- a/gdb/valprint.c
+++ b/gdb/valprint.c
@@ -2955,11 +2955,6 @@  val_print_string (struct type *elttype, const char *encoding,
 }
 
 
-/* The 'set input-radix' command writes to this auxiliary variable.
-   If the requested radix is valid, INPUT_RADIX is updated; otherwise,
-   it is left unchanged.  */
-
-static unsigned input_radix_1 = 10;
 
 /* Validate an input or output radix setting, and make sure the user
    knows what they really did here.  Radix setting is confusing, e.g.
@@ -2968,7 +2963,7 @@  static unsigned input_radix_1 = 10;
 static void
 set_input_radix (char *args, int from_tty, struct cmd_list_element *c)
 {
-  set_input_radix_1 (from_tty, input_radix_1);
+  set_input_radix_1 (from_tty, input_radix);
 }
 
 static void
@@ -2982,12 +2977,9 @@  set_input_radix_1 (int from_tty, unsigned radix)
      (FIXME).  */
 
   if (radix < 2)
-    {
-      input_radix_1 = input_radix;
-      error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
-	     radix);
-    }
-  input_radix_1 = input_radix = radix;
+    error (_("Nonsense input radix ``decimal %u''; input radix unchanged."),
+	   radix);
+  input_radix = radix;
   if (from_tty)
     {
       printf_filtered (_("Input radix now set to "
@@ -2996,16 +2988,10 @@  set_input_radix_1 (int from_tty, unsigned radix)
     }
 }
 
-/* The 'set output-radix' command writes to this auxiliary variable.
-   If the requested radix is valid, OUTPUT_RADIX is updated,
-   otherwise, it is left unchanged.  */
-
-static unsigned output_radix_1 = 10;
-
 static void
 set_output_radix (char *args, int from_tty, struct cmd_list_element *c)
 {
-  set_output_radix_1 (from_tty, output_radix_1);
+  set_output_radix_1 (from_tty, output_radix);
 }
 
 static void
@@ -3025,12 +3011,11 @@  set_output_radix_1 (int from_tty, unsigned radix)
       user_print_options.output_format = 'o';	/* octal */
       break;
     default:
-      output_radix_1 = output_radix;
       error (_("Unsupported output radix ``decimal %u''; "
 	       "output radix unchanged."),
 	     radix);
     }
-  output_radix_1 = output_radix = radix;
+  output_radix = radix;
   if (from_tty)
     {
       printf_filtered (_("Output radix now set to "
@@ -3208,7 +3193,7 @@  Show printing of symbol names when printing pointers."),
 			   show_symbol_print,
 			   &setprintlist, &showprintlist);
 
-  add_setshow_zuinteger_cmd ("input-radix", class_support, &input_radix_1,
+  add_setshow_zuinteger_cmd ("input-radix", class_support, &input_radix,
 			     _("\
 Set default input radix for entering numbers."), _("\
 Show default input radix for entering numbers."), NULL,
@@ -3216,7 +3201,7 @@  Show default input radix for entering numbers."), NULL,
 			     show_input_radix,
 			     &setlist, &showlist);
 
-  add_setshow_zuinteger_cmd ("output-radix", class_support, &output_radix_1,
+  add_setshow_zuinteger_cmd ("output-radix", class_support, &output_radix,
 			     _("\
 Set default output radix for printing of values."), _("\
 Show default output radix for printing of values."), NULL,
diff --git a/gdb/value.c b/gdb/value.c
index 62c5e37..11ce828 100644
--- a/gdb/value.c
+++ b/gdb/value.c
@@ -987,11 +987,7 @@  set_max_value_size (char *args, int from_tty,
   gdb_assert (max_value_size == -1 || max_value_size >= 0);
 
   if (max_value_size > -1 && max_value_size < MIN_VALUE_FOR_MAX_VALUE_SIZE)
-    {
-      max_value_size = MIN_VALUE_FOR_MAX_VALUE_SIZE;
-      error (_("max-value-size set too low, increasing to %d bytes"),
-	     max_value_size);
-    }
+    error (_("max-value-size %d bytes is too low"), max_value_size);
 }
 
 /* Implement the "show max-value-size" command.  */