[Patch] -var-evaluate-expression NAME [FORMAT]

Marc Khouzam
2008-01-22T20:38:34Z

Hi,

Here is a patch to add an optional format parameter to -var-evaluate-expression.
-var-evaluate-expression NAME [FORMAT]

This allows a frontend to request a value in a different format without changing
the current format of the variable object.

If the format is not specified, the command behaves as before (current format of
the varObject is used.)
I kind of cheated and used NULL to indicate that the format used should be the
one stored in the varobj structure. Instead, I could have extended the enumeration
varobj_display_formats to have a FORMAT_CURRENT entry, but I didn't like that much.

I did not update tests, run regression or changed the documentation (don't know how
to do any of these things).

Marc

### Eclipse Workspace Patch 1.0
#P gdb
Index: mi/mi-cmd-var.c
===================================================================
RCS file: /cvs/src/src/gdb/mi/mi-cmd-var.c,v
retrieving revision 1.43
diff -u -r1.43 mi-cmd-var.c
--- mi/mi-cmd-var.c 1 Jan 2008 22:53:14 -0000 1.43
+++ mi/mi-cmd-var.c 22 Jan 2008 20:22:07 -0000
@@ -57,7 +57,7 @@
ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));

if (mi_print_value_p (varobj_get_gdb_type (var), print_values))
- ui_out_field_string (uiout, "value", varobj_get_value (var));
+ ui_out_field_string (uiout, "value", varobj_get_value (var, NULL));

type = varobj_get_type (var);
if (type != NULL)
@@ -190,11 +190,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 +233,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);

@@ -489,16 +498,24 @@
mi_cmd_var_evaluate_expression (char *command, char **argv, int argc)
{
struct varobj *var;
+ enum varobj_display_formats format;

- if (argc != 1)
- error (_("mi_cmd_var_evaluate_expression: Usage: NAME."));
+ if (argc != 1 && argc != 2)
+ error (_("mi_cmd_var_evaluate_expression: Usage: NAME [FORMAT]"));

/* 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 (argc == 2)
+ {
+ format = mi_parse_format(argv[1]);
+ ui_out_field_string (uiout, "value", varobj_get_value (var, &format));
+ }
+ else
+ ui_out_field_string (uiout, "value", varobj_get_value (var, NULL));
+
return MI_CMD_DONE;
}

@@ -524,7 +541,7 @@
if (!varobj_set_value (var, expression))
error (_("mi_cmd_var_assign: Could not assign expression to variable object"));

- ui_out_field_string (uiout, "value", varobj_get_value (var));
+ ui_out_field_string (uiout, "value", varobj_get_value (var, NULL));
return MI_CMD_DONE;
}

@@ -645,7 +662,7 @@
cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
ui_out_field_string (uiout, "name", varobj_get_objname (*cc));
if (mi_print_value_p (varobj_get_gdb_type (*cc), print_values))
- ui_out_field_string (uiout, "value", varobj_get_value (*cc));
+ ui_out_field_string (uiout, "value", varobj_get_value (*cc, NULL));
ui_out_field_string (uiout, "in_scope", "true");
ui_out_field_string (uiout, "type_changed", "false");
if (mi_version (uiout) > 1)
Index: varobj.h
===================================================================
RCS file: /cvs/src/src/gdb/varobj.h,v
retrieving revision 1.14
diff -u -r1.14 varobj.h
--- varobj.h 1 Jan 2008 22:53:13 -0000 1.14
+++ varobj.h 22 Jan 2008 20:22:07 -0000
@@ -104,7 +104,8 @@

extern int varobj_get_attributes (struct varobj *var);

-extern char *varobj_get_value (struct varobj *var);
+extern char *varobj_get_value (struct varobj *var,
+ enum varobj_display_formats *format);

extern int varobj_set_value (struct varobj *var, char *expression);

Index: varobj.c
===================================================================
RCS file: /cvs/src/src/gdb/varobj.c,v
retrieving revision 1.99
diff -u -r1.99 varobj.c
--- varobj.c 1 Jan 2008 22:53:13 -0000 1.99
+++ varobj.c 22 Jan 2008 20:22:07 -0000
@@ -221,7 +221,8 @@

static struct value *value_of_child (struct varobj *parent, int index);

-static char *my_value_of_variable (struct varobj *var);
+static char *my_value_of_variable (struct varobj *var,
+ enum varobj_display_formats *format);

static char *value_get_print_value (struct value *value,
enum varobj_display_formats format);
@@ -246,7 +247,8 @@

static struct type *c_type_of_child (struct varobj *parent, int index);

-static char *c_value_of_variable (struct varobj *var);
+static char *c_value_of_variable (struct varobj *var,
+ enum varobj_display_formats *format);

/* C++ implementation */

@@ -266,7 +268,8 @@

static struct type *cplus_type_of_child (struct varobj *parent, int index);

-static char *cplus_value_of_variable (struct varobj *var);
+static char *cplus_value_of_variable (struct varobj *var,
+ enum varobj_display_formats *format);

/* Java implementation */

@@ -284,7 +287,8 @@

static struct type *java_type_of_child (struct varobj *parent, int index);

-static char *java_value_of_variable (struct varobj *var);
+static char *java_value_of_variable (struct varobj *var,
+ enum varobj_display_formats *format);

/* The language specific vector */

@@ -317,7 +321,8 @@
struct type *(*type_of_child) (struct varobj * parent, int index);

/* The current value of VAR. */
- char *(*value_of_variable) (struct varobj * var);
+ char *(*value_of_variable) (struct varobj * var,
+ enum varobj_display_formats *format);
};

/* Array of known source language routines. */
@@ -849,11 +854,10 @@
}

char *
-varobj_get_value (struct varobj *var)
+varobj_get_value (struct varobj *var, enum varobj_display_formats *format)
{
- return my_value_of_variable (var);
+ return my_value_of_variable (var, format);
}
-
/* 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() */
@@ -1786,10 +1790,10 @@

/* GDB already has a command called "value_of_variable". Sigh. */
static char *
-my_value_of_variable (struct varobj *var)
+my_value_of_variable (struct varobj *var, enum varobj_display_formats *format)
{
if (var->root->is_valid)
- return (*var->root->lang->value_of_variable) (var);
+ return (*var->root->lang->value_of_variable) (var, format);
else
return NULL;
}
@@ -2215,8 +2219,10 @@
}

static char *
-c_value_of_variable (struct varobj *var)
+c_value_of_variable (struct varobj *var, enum varobj_display_formats *format)
{
+ enum varobj_display_formats format_to_use;
+
/* BOGUS: if val_print sees a struct/class, or a reference to one,
it will print out its children instead of "{...}". So we need to
catch that case explicitly. */
@@ -2260,7 +2266,12 @@

gdb_assert (varobj_value_is_changeable_p (var));
gdb_assert (!value_lazy (var->value));
- return value_get_print_value (var->value, var->format);
+ if (format == NULL)
+ format_to_use = var->format;
+ else
+ format_to_use = *format;
+
+ return value_get_print_value (var->value, format_to_use);
}
}
}
@@ -2586,7 +2597,7 @@
}

static char *
-cplus_value_of_variable (struct varobj *var)
+cplus_value_of_variable (struct varobj *var, enum varobj_display_formats *format)
{

/* If we have one of our special types, don't print out
@@ -2594,7 +2605,7 @@
if (CPLUS_FAKE_CHILD (var))
return xstrdup ("");

- return c_value_of_variable (var);
+ return c_value_of_variable (var, format);
}

/* Java */
@@ -2669,9 +2680,9 @@
}

static char *
-java_value_of_variable (struct varobj *var)
+java_value_of_variable (struct varobj *var, enum varobj_display_formats *format)
{
- return cplus_value_of_variable (var);
+ return cplus_value_of_variable (var, format);
}

extern void _initialize_varobj (void);

Marc Khouzam
Software Designer, Methods and Tools

Ericsson Canada Inc
EMC/Q
8500 Decarie Blvd.
H4P 2N2, Mont-Royal, Qc, Canada
www.ericsson.comOffice: +514 345 7900 x42350
Fax: +514 345 6159
Mobile: +514 951 7191
Email: Marc.Khouzam@ericsson.com

Ce courriel est confidentiel et uniquement destiné à son ou ses destinataires. Il est défendu de le consulter, de l'utiliser, de le dévoiler ou de le diffuser sans autorisation. Si ce message vous est parvenu par erreur, merci d'en aviser l'expéditeur par retour de courrier et de le détruire sans le divulguer. Un courriel et ses pièces jointes peut être sans autorisation corrompu, interrompu, amendé, altéré et infecté. L'entreprise ne reçoit et n'envoie de courriel qu'avec l'entente qu'elle n'est responsable d'aucune corruption, interception, modification, altération, infection ou conséquence possible.
This communication is confidential and intended solely for the addressee(s). Any unauthorized review, use, disclosure or distribution is prohibited. If you believe this message has been sent to you in error, please notify the sender by replying to this transmission and delete the message without disclosing it. Thank you. E-mail including attachments is susceptible to data corruption, interruption, unauthorized amendment, tampering and viruses, and we only send and receive e-mails on the basis that we are not liable for any such corruption, interception, amendment, tampering or viruses or any consequences thereof.