This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
RE: [Patch] -var-evaluate-expression NAME [FORMAT]
- From: "Marc Khouzam" <marc dot khouzam at ericsson dot com>
- To: "Nick Roberts" <nickrob at snap dot net dot nz>
- Cc: "Daniel Jacobowitz" <drow at false dot org>, <gdb-patches at sourceware dot org>
- Date: Thu, 24 Jan 2008 16:35:37 -0500
- Subject: RE: [Patch] -var-evaluate-expression NAME [FORMAT]
New version based on comments.
This patch also includes tests and documentation update.
I ran regression and got no failure in the new tests
that I added.
Comments?
Marc
### Eclipse Workspace Patch 1.0
#P src
Index: gdb/testsuite/gdb.mi/mi-var-display.exp
===================================================================
RCS file: /cvs/src/src/gdb/testsuite/gdb.mi/mi-var-display.exp,v
retrieving revision 1.21
diff -u -r1.21 mi-var-display.exp
--- gdb/testsuite/gdb.mi/mi-var-display.exp 23 Jan 2008 06:20:57 -0000 1.21
+++ gdb/testsuite/gdb.mi/mi-var-display.exp 24 Jan 2008 21:26:46 -0000
@@ -171,6 +171,49 @@
mi_gdb_test "-var-evaluate-expression foo" \
"\\^done,value=\"3\"" \
"eval variable foo"
+
+# Test: c_variable-6.19
+# Desc: check optional format parameter of var-evaluate-expression
+# and check that current format is not changed
+mi_gdb_test "-var-evaluate-expression foo -f hex" \
+ "\\^done,value=\"0x3\"" \
+ "eval variable foo in hex"
+
+mi_gdb_test "-var-show-format foo" \
+ "\\^done,format=\"decimal\"" \
+ "show format variable foo after eval in hex"
+
+mi_gdb_test "-var-evaluate-expression foo -f octal" \
+ "\\^done,value=\"03\"" \
+ "eval variable foo in octal"
+
+mi_gdb_test "-var-show-format foo" \
+ "\\^done,format=\"decimal\"" \
+ "show format variable foo after eval in octal"
+
+mi_gdb_test "-var-evaluate-expression foo -f decimal" \
+ "\\^done,value=\"3\"" \
+ "eval variable foo in decimal"
+
+mi_gdb_test "-var-show-format foo" \
+ "\\^done,format=\"decimal\"" \
+ "show format variable foo after eval in decimal"
+
+mi_gdb_test "-var-evaluate-expression foo -f nat" \
+ "\\^done,value=\"0x3\"" \
+ "eval variable foo in natural"
+
+mi_gdb_test "-var-show-format foo" \
+ "\\^done,format=\"decimal\"" \
+ "show format variable foo after eval in natural"
+
+mi_gdb_test "-var-evaluate-expression foo -f bin" \
+ "\\^done,value=\"11\"" \
+ "eval variable foo in binary"
+
+mi_gdb_test "-var-show-format foo" \
+ "\\^done,format=\"decimal\"" \
+ "show format variable foo after eval in binary"
mi_gdb_test "-var-delete foo" \
"\\^done,ndeleted=\"1\"" \
Index: gdb/testsuite/gdb.mi/mi2-var-display.exp
===================================================================
RCS file: /cvs/src/src/gdb/testsuite/gdb.mi/mi2-var-display.exp,v
retrieving revision 1.14
diff -u -r1.14 mi2-var-display.exp
--- gdb/testsuite/gdb.mi/mi2-var-display.exp 23 Jan 2008 21:05:16 -0000 1.14
+++ gdb/testsuite/gdb.mi/mi2-var-display.exp 24 Jan 2008 21:26:46 -0000
@@ -171,6 +171,49 @@
mi_gdb_test "-var-evaluate-expression foo" \
"\\^done,value=\"3\"" \
"eval variable foo"
+
+# Test: c_variable-6.19
+# Desc: check optional format parameter of var-evaluate-expression
+# and check that current format is not changed
+mi_gdb_test "-var-evaluate-expression foo -f hex" \
+ "\\^done,value=\"0x3\"" \
+ "eval variable foo in hex"
+
+mi_gdb_test "-var-show-format foo" \
+ "\\^done,format=\"decimal\"" \
+ "show format variable foo after eval in hex"
+
+mi_gdb_test "-var-evaluate-expression foo -f octal" \
+ "\\^done,value=\"03\"" \
+ "eval variable foo in octal"
+
+mi_gdb_test "-var-show-format foo" \
+ "\\^done,format=\"decimal\"" \
+ "show format variable foo after eval in octal"
+
+mi_gdb_test "-var-evaluate-expression foo -f decimal" \
+ "\\^done,value=\"3\"" \
+ "eval variable foo in decimal"
+
+mi_gdb_test "-var-show-format foo" \
+ "\\^done,format=\"decimal\"" \
+ "show format variable foo after eval in decimal"
+
+mi_gdb_test "-var-evaluate-expression foo -f nat" \
+ "\\^done,value=\"0x3\"" \
+ "eval variable foo in natural"
+
+mi_gdb_test "-var-show-format foo" \
+ "\\^done,format=\"decimal\"" \
+ "show format variable foo after eval in natural"
+
+mi_gdb_test "-var-evaluate-expression foo -f bin" \
+ "\\^done,value=\"11\"" \
+ "eval variable foo in binary"
+
+mi_gdb_test "-var-show-format foo" \
+ "\\^done,format=\"decimal\"" \
+ "show format variable foo after eval in binary"
mi_gdb_test "-var-delete foo" \
"\\^done,ndeleted=\"1\"" \
Index: gdb/varobj.h
===================================================================
RCS file: /cvs/src/src/gdb/varobj.h,v
retrieving revision 1.14
diff -u -r1.14 varobj.h
--- gdb/varobj.h 1 Jan 2008 22:53:13 -0000 1.14
+++ gdb/varobj.h 24 Jan 2008 21:26:41 -0000
@@ -104,6 +104,9 @@
extern int varobj_get_attributes (struct varobj *var);
+extern char *varobj_get_formatted_value (struct varobj *var,
+ enum varobj_display_formats format);
+
extern char *varobj_get_value (struct varobj *var);
extern int varobj_set_value (struct varobj *var, char *expression);
Index: gdb/varobj.c
===================================================================
RCS file: /cvs/src/src/gdb/varobj.c,v
retrieving revision 1.99
diff -u -r1.99 varobj.c
--- gdb/varobj.c 1 Jan 2008 22:53:13 -0000 1.99
+++ gdb/varobj.c 24 Jan 2008 21:26:41 -0000
@@ -192,6 +192,9 @@
static struct cleanup *make_cleanup_free_variable (struct varobj *var);
+static struct cleanup *make_cleanup_set_format (struct varobj *var,
+ enum varobj_display_formats format);
+
static struct type *get_type (struct varobj *var);
static struct type *get_value_type (struct varobj *var);
@@ -854,6 +857,24 @@
return my_value_of_variable (var);
}
+char *
+varobj_get_formatted_value (struct varobj *var,
+ enum varobj_display_formats format)
+{
+ enum varobj_display_formats oldformat;
+ struct cleanup *old_chain;
+ char* value;
+
+ oldformat = var->format;
+ old_chain = make_cleanup_set_format (var, oldformat);
+
+ var->format = format;
+ value = my_value_of_variable (var);
+
+ do_cleanups (old_chain);
+ return value;
+}
+
/* Set the value of an object variable (if it is editable) to the
value of the given expression */
/* Note: Invokes functions that can call error() */
@@ -1543,6 +1564,36 @@
return make_cleanup (do_free_variable_cleanup, var);
}
+struct cleanup_set_format_struct
+{
+ struct varobj *var;
+ enum varobj_display_formats format;
+};
+
+static void
+do_set_format_cleanup (void *cleanup_set_format)
+{
+ struct cleanup_set_format_struct *cleanup_struct = cleanup_set_format;
+
+ varobj_set_display_format (cleanup_struct->var,
+ cleanup_struct->format);
+
+ xfree (cleanup_struct);
+}
+
+static struct cleanup *
+make_cleanup_set_format (struct varobj *var,
+ enum varobj_display_formats format)
+{
+ struct cleanup_set_format_struct *cleanup_struct;
+
+ cleanup_struct = XMALLOC (struct cleanup_set_format_struct);
+ cleanup_struct->var = var;
+ cleanup_struct->format = format;
+
+ return make_cleanup (do_set_format_cleanup, cleanup_struct);
+}
+
/* This returns the type of the variable. It also skips past typedefs
to return the real type of the variable.
Index: gdb/doc/gdb.texinfo
===================================================================
RCS file: /cvs/src/src/gdb/doc/gdb.texinfo,v
retrieving revision 1.459
diff -u -r1.459 gdb.texinfo
--- gdb/doc/gdb.texinfo 23 Jan 2008 11:26:29 -0000 1.459
+++ gdb/doc/gdb.texinfo 24 Jan 2008 21:26:46 -0000
@@ -19995,12 +19995,16 @@
@subsubheading Synopsis
@smallexample
- -var-evaluate-expression @var{name}
+ -var-evaluate-expression @var{name} [-f @var{format-spec}]
@end smallexample
Evaluates the expression that is represented by the specified variable
-object and returns its value as a string. The format of the
-string can be changed using the @code{-var-set-format} command.
+object and returns its value as a string. The format of the string
+can be specified with the @samp{-f} option. The possible values of
+this option are the same as for @code{-var-set-format}
+(@pxref{-var-set-format}). If the @samp{-f} option is not specified,
+the current display format will be used. The current display format
+can be changed using the @code{-var-set-format} command.
@smallexample
value=@var{value}
@@ -20053,7 +20057,7 @@
object names, all existing variable objects are updated, except
for frozen ones (@pxref{-var-set-frozen}). The option
@var{print-values} determines whether both names and values, or just
-names are printed. The possible values of this options are the same
+names are printed. The possible values of this option are the same
as for @code{-var-list-children} (@pxref{-var-list-children}). It is
recommended to use the @samp{--all-values} option, to reduce the
number of MI commands needed on each program stop.
Index: gdb/mi/mi-cmd-var.c
===================================================================
RCS file: /cvs/src/src/gdb/mi/mi-cmd-var.c,v
retrieving revision 1.44
diff -u -r1.44 mi-cmd-var.c
--- gdb/mi/mi-cmd-var.c 23 Jan 2008 06:13:44 -0000 1.44
+++ gdb/mi/mi-cmd-var.c 24 Jan 2008 21:26:46 -0000
@@ -28,6 +28,7 @@
#include "value.h"
#include <ctype.h>
#include "gdb_string.h"
+#include "mi-getopt.h"
const char mi_no_values[] = "--no-values";
const char mi_simple_values[] = "--simple-values";
@@ -190,11 +191,33 @@
return MI_CMD_DONE;
}
+/* Parse a string argument into a format value. */
+
+static enum varobj_display_formats
+mi_parse_format (const char *arg)
+{
+ int len;
+
+ len = strlen (arg);
+
+ if (strncmp (arg, "natural", len) == 0)
+ return FORMAT_NATURAL;
+ else if (strncmp (arg, "binary", len) == 0)
+ return FORMAT_BINARY;
+ else if (strncmp (arg, "decimal", len) == 0)
+ return FORMAT_DECIMAL;
+ else if (strncmp (arg, "hexadecimal", len) == 0)
+ return FORMAT_HEXADECIMAL;
+ else if (strncmp (arg, "octal", len) == 0)
+ return FORMAT_OCTAL;
+ else
+ error (_("Unknown display format: must be: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
+}
+
enum mi_cmd_result
mi_cmd_var_set_format (char *command, char **argv, int argc)
{
enum varobj_display_formats format;
- int len;
struct varobj *var;
char *formspec;
@@ -211,21 +234,8 @@
if (formspec == NULL)
error (_("mi_cmd_var_set_format: Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
- len = strlen (formspec);
-
- if (strncmp (formspec, "natural", len) == 0)
- format = FORMAT_NATURAL;
- else if (strncmp (formspec, "binary", len) == 0)
- format = FORMAT_BINARY;
- else if (strncmp (formspec, "decimal", len) == 0)
- format = FORMAT_DECIMAL;
- else if (strncmp (formspec, "hexadecimal", len) == 0)
- format = FORMAT_HEXADECIMAL;
- else if (strncmp (formspec, "octal", len) == 0)
- format = FORMAT_OCTAL;
- else
- error (_("mi_cmd_var_set_format: Unknown display format: must be: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
-
+ format = mi_parse_format (formspec);
+
/* Set the format of VAR to given format */
varobj_set_display_format (var, format);
@@ -492,16 +502,59 @@
mi_cmd_var_evaluate_expression (char *command, char **argv, int argc)
{
struct varobj *var;
+ enum varobj_display_formats format;
+ int formatFound;
+ int optind;
+ char *optarg;
+
+ enum opt
+ {
+ OP_FORMAT
+ };
+ static struct mi_opt opts[] =
+ {
+ {"f", OP_FORMAT, 1},
+ { 0, 0, 0 }
+ };
- if (argc != 1)
- error (_("mi_cmd_var_evaluate_expression: Usage: NAME."));
-
+
+ if (argc == 0)
+ error (_("mi_cmd_var_evaluate_expression: Usage: NAME [-f FORMAT]"));
+
+ /* Parse arguments starting at the second one. */
+ format = FORMAT_NATURAL;
+ formatFound = 0;
+ optind = 1;
+ while (1)
+ {
+ int opt = mi_getopt ("mi_cmd_var_evaluate_expression", argc, argv, opts, &optind, &optarg);
+ if (opt < 0)
+ break;
+ switch ((enum opt) opt)
+ {
+ case OP_FORMAT:
+ if (formatFound)
+ error (_("mi_cmd_var_evaluate_expression: cannot specify format more than once"));
+
+ format = mi_parse_format (optarg);
+ formatFound = 1;
+ break;
+ }
+ }
+
+ if (optind < argc)
+ error (_("mi_cmd_var_evaluate_expression: Garbage at end of command"));
+
/* Get varobj handle, if a valid var obj name was specified */
var = varobj_get_handle (argv[0]);
if (var == NULL)
error (_("mi_cmd_var_evaluate_expression: Variable object not found"));
- ui_out_field_string (uiout, "value", varobj_get_value (var));
+ if (formatFound)
+ ui_out_field_string (uiout, "value", varobj_get_formatted_value (var, format));
+ else
+ ui_out_field_string (uiout, "value", varobj_get_value (var));
+
return MI_CMD_DONE;
}