From 53e5f3cf290c7591f459743845dd24391d72796c Mon Sep 17 00:00:00 2001 From: Andrew Stubbs Date: Fri, 2 Dec 2005 11:44:19 +0000 Subject: [PATCH] 2005-12-02 Andrew Stubbs * value.c (init_if_undefined_command): New function. (_initialize_values): Add command init-if-undefined. * NEWS (Changes since GDB 6.3): Rename to 'Changes in GDB 6.4'. (Changes since GDB 6.4): New section. Mention new command init-if-undefined. doc/ * gdb.texinfo (Convenience variables): Add init-if-undefined command. --- gdb/ChangeLog | 8 ++++++++ gdb/NEWS | 9 ++++++++- gdb/doc/ChangeLog | 4 ++++ gdb/doc/gdb.texinfo | 12 ++++++++++++ gdb/value.c | 40 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 1 deletion(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index b4ad773325d..6177fa22f06 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,11 @@ +2005-12-02 Andrew Stubbs + + * value.c (init_if_undefined_command): New function. + (_initialize_values): Add command init-if-undefined. + * NEWS (Changes since GDB 6.3): Rename to 'Changes in GDB 6.4'. + (Changes since GDB 6.4): New section. + Mention new command init-if-undefined. + 2005-12-02 Andrew Stubbs * symfile.c (symbol_file_clear): Test symfile_objfile is not NULL diff --git a/gdb/NEWS b/gdb/NEWS index 84dc8279229..719fbe3f682 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -1,7 +1,14 @@ What has changed in GDB? (Organized release by release) -*** Changes since GDB 6.3 +*** Changes since GDB 6.4 + +* New commands + +init-if-undefined Initialize a convenience variable, but + only if it doesn't already have a value. + +*** Changes in GDB 6.4 * New native configurations diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog index 3f070f3f5a1..d62231d6752 100644 --- a/gdb/doc/ChangeLog +++ b/gdb/doc/ChangeLog @@ -1,3 +1,7 @@ +2005-12-02 Andrew Stubbs + + * gdb.texinfo (Convenience variables): Add init-if-undefined command. + 2005-11-25 Joel Brobecker * gdbint.texinfo (Start of New Year Procedure): New chapter. diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index fef25953031..2eb1dd85e2a 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -6131,6 +6131,18 @@ variable, when used as an expression, has the type of its current value. @item show convenience Print a list of convenience variables used so far, and their values. Abbreviated @code{show conv}. + +@kindex init-if-undefined +@cindex convenience variables, initializing +@item init-if-undefined $@var{variable} = @var{expression} +Set a convenience variable if it has not already been set. This is useful +for user-defined commands that keep some state. It is similar, in concept, +to using local static variables with initializers in C (except that +convenience variables are global). It can also be used to allow users to +override default values used in a command script. + +If the variable is already defined then the expression is not evaluated so +any side-effects do not occur. @end table One of the ways to use a convenience variable is as a counter to be diff --git a/gdb/value.c b/gdb/value.c index 097d9414018..82746ae6042 100644 --- a/gdb/value.c +++ b/gdb/value.c @@ -727,6 +727,39 @@ show_values (char *num_exp, int from_tty) static struct internalvar *internalvars; +/* If the variable does not already exist create it and give it the value given. + If no value is given then the default is zero. */ +static void +init_if_undefined_command (char* args, int from_tty) +{ + struct internalvar* intvar; + + /* Parse the expression - this is taken from set_command(). */ + struct expression *expr = parse_expression (args); + register struct cleanup *old_chain = + make_cleanup (free_current_contents, &expr); + + /* Validate the expression. + Was the expression an assignment? + Or even an expression at all? */ + if (expr->nelts == 0 || expr->elts[0].opcode != BINOP_ASSIGN) + error (_("Init-if-undefined requires an assignment expression.")); + + /* Extract the variable from the parsed expression. + In the case of an assign the lvalue will be in elts[1] and elts[2]. */ + if (expr->elts[1].opcode != OP_INTERNALVAR) + error (_("The first parameter to init-if-undefined should be a GDB variable.")); + intvar = expr->elts[2].internalvar; + + /* Only evaluate the expression if the lvalue is void. + This may still fail if the expresssion is invalid. */ + if (TYPE_CODE (value_type (intvar->value)) == TYPE_CODE_VOID) + evaluate_expression (expr); + + do_cleanups (old_chain); +} + + /* Look up an internal variable with name NAME. NAME should not normally include a dollar sign. @@ -1639,4 +1672,11 @@ A few convenience variables are given values automatically:\n\ add_cmd ("values", no_class, show_values, _("Elements of value history around item number IDX (or last ten)."), &showlist); + + add_com ("init-if-undefined", class_vars, init_if_undefined_command, _("\ +Initialize a convenience variable if necessary.\n\ +init-if-undefined VARIABLE = EXPRESSION\n\ +Set an internal VARIABLE to the result of the EXPRESSION if it does not\n\ +exist or does not contain a value. The EXPRESSION is not evaluated if the\n\ +VARIABLE is already initialized.")); } -- 2.30.2