1 /* Variable user interface for GDB, the GNU debugger.
2 Copyright 1999 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "expression.h"
29 #include "gdbtk-wrapper.h"
33 /* Enumeration type defining the return values for valueChanged */
36 VALUE_UNCHANGED
, /* the variable's value is unchanged */
37 VALUE_CHANGED
, /* the variable's value has changed */
38 VALUE_OUT_OF_SCOPE
/* the variable is no longer in scope */
41 /* String representations of the value_changed enums */
42 static char *value_changed_string
[] = {
45 "VARIABLE_OUT_OF_SCOPE",
49 /* Enumeration for the format types */
52 FORMAT_NATURAL
, /* What gdb actually calls 'natural' */
53 FORMAT_BINARY
, /* Binary display */
54 FORMAT_DECIMAL
, /* Decimal display */
55 FORMAT_HEXADECIMAL
, /* Hex display */
56 FORMAT_OCTAL
/* Octal display */
59 /* Mappings of display_format enums to gdb's format codes */
60 int format_code
[] = {0, 't', 'd', 'x', 'o'};
62 /* String representations of the format codes */
63 char *format_string
[] = {"natural", "binary", "decimal", "hexadecimal", "octal"};
65 /* Every parent variable keeps a linked list of its children, described
66 by the following structure. */
67 struct variable_child
{
69 /* Pointer to the child's data */
70 struct _gdb_variable
*child
;
72 /* Pointer to the next child */
73 struct variable_child
*next
;
76 /* Every variable in the system has a structure of this type defined
77 for it. This structure holds all information necessary to manipulate
78 a particular object variable. Members which must be freed are noted. */
79 struct _gdb_variable
{
81 /* Alloc'd name of the variable for this object.. If this variable is a
82 child, then this name will be the child's source name.
86 /* The alloc'd real name of this variable. This is used to construct the
87 variable's children. It is always a valid expression. */
90 /* The alloc'd name for this variable's object. This is here for
91 convenience when constructing this object's children. */
94 /* Alloc'd expression for this variable */
95 struct expression
*exp
;
97 /* Block for which this expression is valid */
98 struct block
*valid_block
;
100 /* The frame for this expression */
103 /* The value of this expression */
106 /* Did an error occur evaluating the expression or getting its value? */
109 /* The number of (immediate) children this variable has */
112 /* If this object is a child, this points to its parent. */
113 struct _gdb_variable
*parent
;
115 /* A list of this object's children */
116 struct variable_child
*children
;
118 /* The format of the output for this object */
119 enum display_format format
;
122 typedef struct _gdb_variable gdb_variable
;
124 /* This variable will hold the value of the output from gdb
125 for commands executed through call_gdb_* */
126 static Tcl_Obj
*fputs_obj
;
129 * Public functions defined in this file
132 int gdb_variable_init
PARAMS ((Tcl_Interp
*));
135 * Private functions defined in this file
138 /* Entries into this file */
140 static int gdb_variable_command
PARAMS ((ClientData
, Tcl_Interp
*, int,
143 static int variable_create
PARAMS ((Tcl_Interp
*, int, Tcl_Obj
*CONST
[]));
145 static void variable_delete
PARAMS ((Tcl_Interp
*, gdb_variable
*));
147 static void variable_debug
PARAMS ((gdb_variable
*));
149 static int variable_obj_command
PARAMS ((ClientData
, Tcl_Interp
*, int,
151 static Tcl_Obj
*variable_children
PARAMS ((Tcl_Interp
*, gdb_variable
*));
153 static enum value_changed variable_value_changed
PARAMS ((gdb_variable
*));
155 static int variable_format
PARAMS ((Tcl_Interp
*, int, Tcl_Obj
*CONST
[],
158 static int variable_type
PARAMS ((Tcl_Interp
*, int, Tcl_Obj
*CONST
[],
161 static int variable_value
PARAMS ((Tcl_Interp
*, int, Tcl_Obj
*CONST
[],
164 static int variable_editable
PARAMS ((gdb_variable
*));
166 /* Helper functions for the above functions. */
168 static gdb_variable
*create_variable
PARAMS ((char *, char *, CORE_ADDR
));
170 static void delete_children
PARAMS ((Tcl_Interp
*, gdb_variable
*, int));
172 static void install_variable
PARAMS ((Tcl_Interp
*, char *, gdb_variable
*));
174 static void uninstall_variable
PARAMS ((Tcl_Interp
*, gdb_variable
*));
176 static gdb_variable
*child_exists
PARAMS ((gdb_variable
*, char *));
178 static gdb_variable
*create_child
PARAMS ((Tcl_Interp
*, gdb_variable
*,
180 static char *name_of_child
PARAMS ((gdb_variable
*, int));
182 static int number_of_children
PARAMS ((gdb_variable
*));
184 static enum display_format variable_default_display
PARAMS ((gdb_variable
*));
186 static void save_child_in_parent
PARAMS ((gdb_variable
*, gdb_variable
*));
188 static void remove_child_from_parent
PARAMS ((gdb_variable
*, gdb_variable
*));
190 static struct type
*get_type
PARAMS ((value_ptr
));
192 static struct type
*get_target_type
PARAMS ((struct type
*));
194 static Tcl_Obj
*get_call_output
PARAMS ((void));
196 static void clear_gdb_output
PARAMS ((void));
198 static int call_gdb_type_print
PARAMS ((value_ptr
));
200 static int call_gdb_val_print
PARAMS ((value_ptr
, int));
202 static void variable_fputs
PARAMS ((const char *, GDB_FILE
*));
204 static void null_fputs
PARAMS ((const char *, GDB_FILE
*));
206 static int my_value_equal
PARAMS ((gdb_variable
*, value_ptr
));
208 #define INIT_VARIABLE(x) { \
210 (x)->real_name = NULL; \
211 (x)->obj_name = NULL; \
213 (x)->valid_block = NULL; \
214 (x)->frame = (CORE_ADDR) 0; \
217 (x)->num_children = 0; \
218 (x)->parent = NULL; \
219 (x)->children = NULL; \
220 (x)->format = FORMAT_NATURAL; \
226 #define FREEIF(x) if (x != NULL) free((char *) (x))
228 /* Initialize the variable code. This function should be called once
229 to install and initialize the variable code into the interpreter. */
231 gdb_variable_init (interp
)
236 result
= Tcl_CreateObjCommand (interp
, "gdb_variable", call_wrapper
,
237 (ClientData
) gdb_variable_command
, NULL
);
244 /* This function defines the "gdb_variable" command which is used to
245 create variable objects. Its syntax includes:
248 gdb_variable create NAME
249 gdb_variable create -expr EXPR
250 gdb_variable create NAME -expr EXPR
252 NAME = name of object to create. If no NAME, then automatically create
254 EXPR = the gdb expression for which to create a variable. This will
255 be the most common usage.
258 gdb_variable_command (clientData
, interp
, objc
, objv
)
259 ClientData clientData
;
262 Tcl_Obj
*CONST objv
[];
264 static char *commands
[] = { "create", NULL
};
265 enum commands_enum
{ VARIABLE_CREATE
};
270 Tcl_WrongNumArgs (interp
, 1, objv
, "option ?arg...?");
274 if (Tcl_GetIndexFromObj (interp
, objv
[1], commands
, "options", 0,
280 switch ((enum commands_enum
) index
)
282 case VARIABLE_CREATE
:
283 result
= variable_create (interp
, objc
- 2, objv
+ 2);
293 /* This function implements the actual object command for each
294 variable object that is created (and each of its children).
296 Currently the following commands are implemented:
297 - delete delete this object and its children
298 - valueChanged has the value of this object changed since the last check?
299 - numChildren how many children does this object have
300 - children create the children and return a list of their objects
301 - debug print out a little debug info for the object
302 - name print out the name of this variable
305 variable_obj_command (clientData
, interp
, objc
, objv
)
306 ClientData clientData
;
309 Tcl_Obj
*CONST objv
[];
313 VARIABLE_VALUE_CHANGED
,
314 VARIABLE_NUM_CHILDREN
,
323 static char *commands
[] = {
336 gdb_variable
*var
= (gdb_variable
*) clientData
;
341 Tcl_WrongNumArgs (interp
, 1, objv
, "option ?arg...?");
345 if (Tcl_GetIndexFromObj (interp
, objv
[1], commands
, "options", 0,
350 switch ((enum commands_enum
) index
)
352 case VARIABLE_DELETE
:
356 char *s
= Tcl_GetStringFromObj (objv
[2], &len
);
357 if (*s
== 'c' && strncmp (s
, "children", len
) == 0)
359 delete_children (interp
, var
, 1);
363 variable_delete (interp
, var
);
366 case VARIABLE_VALUE_CHANGED
:
368 enum value_changed vc
= variable_value_changed (var
);
369 Tcl_SetObjResult (interp
, Tcl_NewStringObj (value_changed_string
[vc
], -1));
373 case VARIABLE_NUM_CHILDREN
:
374 Tcl_SetObjResult (interp
, Tcl_NewIntObj (var
->num_children
));
377 case VARIABLE_CHILDREN
:
379 Tcl_Obj
*children
= variable_children (interp
, var
);
380 Tcl_SetObjResult (interp
, children
);
385 variable_debug (var
);
388 case VARIABLE_FORMAT
:
389 result
= variable_format (interp
, objc
, objv
, var
);
393 result
= variable_type (interp
, objc
, objv
, var
);
397 result
= variable_value (interp
, objc
, objv
, var
);
401 Tcl_SetObjResult (interp
, Tcl_NewStringObj (var
->name
, -1));
404 case VARIABLE_EDITABLE
:
405 Tcl_SetObjResult (interp
, Tcl_NewIntObj (variable_editable (var
)));
416 * Variable object construction/destruction
419 /* This function is responsible for processing the user's specifications
420 and constructing a variable object. */
422 variable_create (interp
, objc
, objv
)
425 Tcl_Obj
*CONST objv
[];
427 enum create_opts
{ CREATE_EXPR
, CREATE_PC
};
428 static char *create_options
[] = { "-expr", "-pc", NULL
};
434 CORE_ADDR pc
= (CORE_ADDR
) -1;
436 /* REMINDER: This command may be invoked in the following ways:
438 gdb_variable create NAME
439 gdb_variable create -expr EXPR
440 gdb_variable create NAME -expr EXPR
442 NAME = name of object to create. If no NAME, then automatically create
444 EXPR = the gdb expression for which to create a variable. This will
445 be the most common usage.
449 name
= Tcl_GetStringFromObj (objv
[0], NULL
);
450 if (name
== NULL
|| *name
== '-')
452 /* generate a name for this object */
454 sprintf (obj_name
, "var%d", id
);
458 /* specified name for object */
459 strncpy (obj_name
, name
, 30);
464 /* Run through all the possible options for this command */
468 if (Tcl_GetIndexFromObj (interp
, objv
[0], create_options
, "options",
469 0, &index
) != TCL_OK
)
471 result_ptr
->flags
|= GDBTK_IN_TCL_RESULT
;
475 switch ((enum create_opts
) index
)
478 name
= Tcl_GetStringFromObj (objv
[1], NULL
);
486 str
= Tcl_GetStringFromObj (objv
[1], NULL
);
487 pc
= parse_and_eval_address (str
);
501 /* Create the variable */
503 /* Add parentheses to the name so that casts do
505 char *newname
= (char *) xmalloc (strlen (name
) + 3);
506 sprintf (newname
, "(%s)", name
);
507 var
= create_variable (name
, newname
, pc
);
513 /* Install a command into the interpreter that represents this
515 install_variable (interp
, obj_name
, var
);
516 Tcl_SetObjResult (interp
, Tcl_NewStringObj (obj_name
, -1));
517 result_ptr
->flags
|= GDBTK_IN_TCL_RESULT
;
525 /* Fill out a gdb_variable structure for the variable being constructed.
526 This function should never fail if real_name is a valid expression.
527 (That means no longjmp'ing!) */
528 static gdb_variable
*
529 create_variable (name
, real_name
, pc
)
536 struct frame_info
*fi
, *old_fi
;
538 void (*old_fputs
) PARAMS ((const char *, GDB_FILE
*));
541 var
= (gdb_variable
*) xmalloc (sizeof (gdb_variable
));
548 /* Parse and evaluate the expression, filling in as much
549 of the variable's data as possible */
551 /* Allow creator to specify context of variable */
552 if (pc
== (CORE_ADDR
) -1)
556 r
= GDB_block_for_pc (pc
, &block
);
562 innermost_block
= NULL
;
563 r
= GDB_parse_exp_1 (&p
, block
, 0, &(var
->exp
));
566 FREEIF ((char *) var
);
570 /* Don't allow variables to be created for types. */
571 if (var
->exp
->elts
[0].opcode
== OP_TYPE
)
573 free_current_contents ((char **) &(var
->exp
));
575 printf_unfiltered ("Attempt to use a type name as an expression.");
579 var
->valid_block
= innermost_block
;
580 var
->name
= savestring (name
, strlen (name
));
581 var
->real_name
= savestring (real_name
, strlen (real_name
));
583 /* Several of the GDB_* calls can cause messages to be displayed. We swallow
584 those here, because we don't need them (the "value" command will
586 old_fputs
= fputs_unfiltered_hook
;
587 fputs_unfiltered_hook
= null_fputs
;
589 /* When the PC is different from the current PC (pc == -1),
590 then we must select the appropriate frame before parsing
591 the expression, otherwise the value will not be current.
592 Since select_frame is so benign, just call it for all cases. */
593 r
= GDB_block_innermost_frame (var
->valid_block
, &fi
);
597 var
->frame
= FRAME_FP (fi
);
598 old_fi
= selected_frame
;
599 GDB_select_frame (fi
, -1);
601 mark
= value_mark ();
602 if (GDB_evaluate_expression (var
->exp
, &var
->value
) == GDB_OK
)
604 release_value (var
->value
);
605 if (VALUE_LAZY (var
->value
))
607 if (GDB_value_fetch_lazy (var
->value
) != GDB_OK
)
615 value_free_to_mark (mark
);
617 /* Reset the selected frame */
618 GDB_select_frame (old_fi
, -1);
620 /* Restore the output hook to normal */
621 fputs_unfiltered_hook
= old_fputs
;
623 var
->num_children
= number_of_children (var
);
624 var
->format
= variable_default_display (var
);
630 /* Install the given variable VAR into the tcl interpreter with
631 the object name NAME. */
633 install_variable (interp
, name
, var
)
638 var
->obj_name
= savestring (name
, strlen (name
));
639 Tcl_CreateObjCommand (interp
, name
, variable_obj_command
,
640 (ClientData
) var
, NULL
);
643 /* Unistall the object VAR in the tcl interpreter. */
645 uninstall_variable (interp
, var
)
649 Tcl_DeleteCommand (interp
, var
->obj_name
);
652 /* Delete the variable object VAR and its children */
654 variable_delete (interp
, var
)
658 /* Delete any children of this variable, too. */
659 delete_children (interp
, var
, 0);
661 /* If this variable has a parent, remove it from its parent's list */
662 if (var
->parent
!= NULL
)
664 remove_child_from_parent (var
->parent
, var
);
667 uninstall_variable (interp
, var
);
669 /* Free memory associated with this variable */
671 FREEIF (var
->real_name
);
672 FREEIF (var
->obj_name
);
673 if (var
->exp
!= NULL
)
674 free_current_contents ((char **) &var
->exp
);
678 /* Silly debugging info */
685 str
= Tcl_NewStringObj ("name=", -1);
686 Tcl_AppendStringsToObj (str
, var
->name
, "\nreal_name=", var
->real_name
,
687 "\nobj_name=", var
->obj_name
, NULL
);
688 Tcl_SetObjResult (gdbtk_interp
, str
);
692 * Child construction/destruction
695 /* Delete the children associated with the object VAR. If NOTIFY is set,
696 notify the parent object that this child was deleted. This is used as
697 a small optimization when deleting variables and their children. If the
698 parent is also being deleted, don't bother notifying it that its children
699 are being deleted. */
701 delete_children (interp
, var
, notify
)
706 struct variable_child
*vc
;
707 struct variable_child
*next
;
709 for (vc
= var
->children
; vc
!= NULL
; vc
= next
)
712 vc
->child
->parent
= NULL
;
713 variable_delete (interp
, vc
->child
);
719 /* Return the number of children for a given variable.
721 This can get a little complicated, since we would like to make
722 certain assumptions about certain types of variables.
724 - struct/union *: dereference first
725 - (*)(): do not allow derefencing
727 - declared size = num of children or
728 - -1 if we don't know, i.e., int foo [];
729 - if there was an error reported constructing this object,
730 assume it has no children (and try this again later)
731 - void * and char * have no children
734 number_of_children (var
)
741 if (var
->value
!= NULL
)
743 type
= get_type (var
->value
);
744 target
= get_target_type (type
);
747 switch (TYPE_CODE (type
))
749 case TYPE_CODE_ARRAY
:
750 if (TYPE_LENGTH (type
) > 0 && TYPE_LENGTH (target
) > 0
751 && TYPE_ARRAY_UPPER_BOUND_TYPE (type
) != BOUND_CANNOT_BE_DETERMINED
)
752 children
= TYPE_LENGTH (type
) / TYPE_LENGTH (target
);
757 case TYPE_CODE_STRUCT
:
758 case TYPE_CODE_UNION
:
759 /* If we have a virtual table pointer, omit it. */
760 if (TYPE_VPTR_BASETYPE (type
) == type
761 && !(TYPE_VPTR_FIELDNO (type
) < 0))
762 children
= TYPE_NFIELDS (type
) - 1;
764 children
= TYPE_NFIELDS (type
);
768 /* This is where things get compilcated. All pointers have one child.
769 Except, of course, for struct and union ptr, which we automagically
770 dereference for the user and function ptrs, which have no children. */
771 switch (TYPE_CODE (target
))
773 case TYPE_CODE_STRUCT
:
774 case TYPE_CODE_UNION
:
775 /* If we have a virtual table pointer, omit it. */
776 if (TYPE_VPTR_BASETYPE (target
) == target
777 && !(TYPE_VPTR_FIELDNO (target
) < 0))
778 children
= TYPE_NFIELDS (target
) - 1;
780 children
= TYPE_NFIELDS (target
);
788 /* Don't dereference char* or void*. */
789 if (TYPE_NAME (target
) != NULL
790 && (STREQ (TYPE_NAME (target
), "char")
791 || STREQ (TYPE_NAME (target
), "void")))
804 /* var->value can be null if we tried to access non-existent or
805 protected memory. In this case, we simply do not allow any
806 children. This will be checked again when we check if its
807 value has changed. */
814 /* Return a list of all the children of VAR, creating them if necessary. */
816 variable_children (interp
, var
)
825 list
= Tcl_NewListObj (0, NULL
);
826 for (i
= 0; i
< var
->num_children
; i
++)
828 /* check if child exists */
829 name
= name_of_child (var
, i
);
830 child
= child_exists (var
, name
);
833 child
= create_child (interp
, var
, name
, i
);
835 /* name_of_child returns a malloc'd string */
838 Tcl_ListObjAppendElement (NULL
, list
, Tcl_NewStringObj (child
->obj_name
, -1));
844 /* Does a child with the name NAME exist in VAR? If so, return its data.
845 If not, return NULL. */
846 static gdb_variable
*
847 child_exists (var
, name
)
848 gdb_variable
*var
; /* Parent */
849 char *name
; /* name of child */
851 struct variable_child
*vc
;
853 for (vc
= var
->children
; vc
!= NULL
; vc
= vc
->next
)
855 if (STREQ (vc
->child
->name
, name
))
862 /* Create and install a child of the parent of the given name */
863 static gdb_variable
*
864 create_child (interp
, parent
, name
, index
)
866 gdb_variable
*parent
;
873 char separator
[10], prefix
[2048], suffix
[20];
879 /* name should never be null. For pointer derefs, it should contain "*name".
880 For arrays of a known size, the name will simply contain the index into
888 /* This code must contain a lot of the logic for children based on the parent's
890 type
= get_type (parent
->value
);
891 target
= get_target_type (type
);
893 switch (TYPE_CODE (type
))
895 case TYPE_CODE_ARRAY
:
896 sprintf (suffix
, "[%s]", name
);
900 case TYPE_CODE_STRUCT
:
901 case TYPE_CODE_UNION
:
902 if (index
< TYPE_N_BASECLASSES (type
))
904 strcpy (prefix
, "((");
905 strcat (prefix
, name
);
906 strcat (prefix
, ")");
907 strcpy (suffix
, ") ");
911 strcpy (separator
, ".");
915 switch (TYPE_CODE (target
))
917 case TYPE_CODE_STRUCT
:
918 case TYPE_CODE_UNION
:
919 if (index
< TYPE_N_BASECLASSES (target
))
921 strcpy (prefix
, "(*(");
922 strcat (prefix
, name
);
923 strcat (prefix
, " *)");
924 strcpy (suffix
, ")");
928 strcpy (separator
, "->");
940 /* When we get here, we should know how to construct a legal
941 expression for the child's name */
942 len
= strlen (prefix
);
943 len
+= strlen (parent
->real_name
);
944 len
+= strlen (separator
);
945 len
+= strlen (name
);
946 len
+= strlen (suffix
);
949 childs_name
= (char *) xmalloc ((len
+ 1) * sizeof (char));
952 strcpy (childs_name
, "(*");
953 strcat (childs_name
, parent
->real_name
);
954 strcat (childs_name
, suffix
);
955 strcat (childs_name
, ")");
959 strcpy (childs_name
, prefix
);
960 strcat (childs_name
, parent
->real_name
);
961 strcat (childs_name
, separator
);
962 strcat (childs_name
, name
);
963 strcat (childs_name
, suffix
);
966 /* childs_name now contains a valid expression for the child */
967 child
= create_variable (save_name
, childs_name
, (CORE_ADDR
) -1);
968 child
->parent
= parent
;
970 childs_name
= (char *) xmalloc ((strlen (parent
->obj_name
) + strlen (save_name
) + 2)
972 sprintf (childs_name
, "%s.%s", parent
->obj_name
, save_name
);
973 install_variable (interp
, childs_name
, child
);
976 /* Save a pointer to this child in the parent */
977 save_child_in_parent (parent
, child
);
982 /* Save CHILD in the PARENT's data. */
984 save_child_in_parent (parent
, child
)
985 gdb_variable
*parent
;
988 struct variable_child
*vc
;
990 /* Insert the child at the top */
991 vc
= parent
->children
;
993 (struct variable_child
*) xmalloc (sizeof (struct variable_child
));
995 parent
->children
->next
= vc
;
996 parent
->children
->child
= child
;
999 /* Remove the CHILD from the PARENT's list of children. */
1001 remove_child_from_parent (parent
, child
)
1002 gdb_variable
*parent
;
1003 gdb_variable
*child
;
1005 struct variable_child
*vc
, *prev
;
1007 /* Find the child in the parent's list */
1009 for (vc
= parent
->children
; vc
!= NULL
; )
1011 if (vc
->child
== child
)
1018 parent
->children
= vc
->next
;
1020 prev
->next
= vc
->next
;
1024 /* What is the name of the INDEX'th child of VAR? */
1026 name_of_child (var
, index
)
1031 struct type
*target
;
1035 type
= get_type (var
->value
);
1036 target
= get_target_type (type
);
1038 switch (TYPE_CODE (type
))
1040 case TYPE_CODE_ARRAY
:
1042 /* We never get here unless var->num_children is greater than 0... */
1044 while ((int) pow ((double) 10, (double) len
) < index
)
1046 name
= (char *) xmalloc (1 + len
* sizeof (char));
1047 sprintf (name
, "%d", index
);
1051 case TYPE_CODE_STRUCT
:
1052 case TYPE_CODE_UNION
:
1053 string
= TYPE_FIELD_NAME (type
, index
);
1054 name
= savestring (string
, strlen (string
));
1058 switch (TYPE_CODE (target
))
1060 case TYPE_CODE_STRUCT
:
1061 case TYPE_CODE_UNION
:
1062 string
= TYPE_FIELD_NAME (target
, index
);
1063 name
= savestring (string
, strlen (string
));
1067 name
= (char *) xmalloc ((strlen (var
->name
) + 2) * sizeof (char));
1068 sprintf (name
, "*%s", var
->name
);
1076 /* Has the value of this object changed since the last time we looked?
1078 There are some special cases:
1079 - structs/unions/arrays. The "value" of these never changes.
1080 Only their children's values change.
1081 - if an error occurred with evaluate_expression or fetch_value_lazy,
1082 then we need to be a little more elaborate with our determination
1083 of "value changed". Specifically, the value does not change when
1084 both the previous evaluate fails and the one done here also fails.
1086 static enum value_changed
1087 variable_value_changed (var
)
1090 value_ptr mark
, new_val
;
1091 struct frame_info
*fi
, *old_fi
;
1093 enum value_changed result
;
1096 /* Save the selected stack frame, since we will need to change it
1097 in order to evaluate expressions. */
1098 old_fi
= selected_frame
;
1100 /* Determine whether the variable is still around. */
1101 if (var
->valid_block
== NULL
)
1105 GDB_reinit_frame_cache ();
1106 r
= GDB_find_frame_addr_in_frame_chain (var
->frame
, &fi
);
1109 within_scope
= fi
!= NULL
;
1110 /* FIXME: GDB_select_frame could fail */
1112 GDB_select_frame (fi
, -1);
1115 result
= VALUE_OUT_OF_SCOPE
;
1118 struct type
*type
= get_type (var
->value
);
1120 /* Arrays, struct, classes, unions never change value */
1121 if (type
!= NULL
&& (TYPE_CODE (type
) == TYPE_CODE_STRUCT
1122 || TYPE_CODE (type
) == TYPE_CODE_UNION
1123 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
))
1124 result
= VALUE_UNCHANGED
;
1127 mark
= value_mark ();
1128 if (GDB_evaluate_expression (var
->exp
, &new_val
) == GDB_OK
)
1130 if (!my_value_equal (var
, new_val
))
1133 release_value (new_val
);
1134 if (var
->value
== NULL
)
1136 /* This can happen if there was an error
1137 evaluating the expression (like deref NULL) */
1138 var
->num_children
= number_of_children (var
);
1140 value_free (var
->value
);
1141 var
->value
= new_val
;
1142 result
= VALUE_CHANGED
;
1145 result
= VALUE_UNCHANGED
;
1149 /* evaluate expression failed. If we failed before, then
1150 the value of this variable has not changed. If we
1151 succeed before, then the value did change. */
1152 if (var
->value
== NULL
)
1153 result
= VALUE_UNCHANGED
;
1158 result
= VALUE_CHANGED
;
1162 value_free_to_mark (mark
);
1166 /* Restore selected frame */
1167 GDB_select_frame (old_fi
, -1);
1173 variable_format (interp
, objc
, objv
, var
)
1176 Tcl_Obj
*CONST objv
[];
1182 /* Set the format of VAR to given format */
1184 char *fmt
= Tcl_GetStringFromObj (objv
[2], &len
);
1185 if (STREQN (fmt
, "natural", len
))
1186 var
->format
= FORMAT_NATURAL
;
1187 else if (STREQN (fmt
, "binary", len
))
1188 var
->format
= FORMAT_NATURAL
;
1189 else if (STREQN (fmt
, "decimal", len
))
1190 var
->format
= FORMAT_DECIMAL
;
1191 else if (STREQN (fmt
, "hexadecimal", len
))
1192 var
->format
= FORMAT_HEXADECIMAL
;
1193 else if (STREQN (fmt
, "octal", len
))
1194 var
->format
= FORMAT_OCTAL
;
1197 Tcl_Obj
*obj
= Tcl_NewStringObj (NULL
, 0);
1198 Tcl_AppendStringsToObj (obj
, "unknown display format \"",
1199 fmt
, "\": must be: \"natural\", \"binary\""
1200 ", \"decimal\", \"hexadecimal\", or \"octal\"",
1202 Tcl_SetObjResult (interp
, obj
);
1208 /* Report the current format */
1211 fmt
= Tcl_NewStringObj (format_string
[(int) var
->format
], -1);
1212 Tcl_SetObjResult (interp
, fmt
);
1218 /* What is the default display for this variable? We assume that
1219 everything is "natural". Any exceptions? */
1220 static enum display_format
1221 variable_default_display (var
)
1224 return FORMAT_NATURAL
;
1227 /* This function returns the type of a variable in the interpreter (or an error)
1228 and returns either TCL_OK or TCL_ERROR as appropriate. */
1230 variable_type (interp
, objc
, objv
, var
)
1233 Tcl_Obj
*CONST objv
[];
1238 char *first
, *last
, *string
;
1242 if (var
->value
!= NULL
)
1246 r
= GDB_evaluate_type (var
->exp
, &val
);
1251 result
= call_gdb_type_print (val
);
1252 if (result
== TCL_OK
)
1254 string
= strdup (Tcl_GetStringFromObj (get_call_output (), NULL
));
1257 /* gdb will print things out like "struct {...}" for anonymous structs.
1258 In gui-land, we don't want the {...}, so we strip it here. */
1259 regexp
= Tcl_RegExpCompile (interp
, "{...}");
1260 if (Tcl_RegExpExec (interp
, regexp
, string
, first
))
1262 /* We have an anonymous struct/union/class/enum */
1263 Tcl_RegExpRange (regexp
, 0, &first
, &last
);
1264 if (*(first
- 1) == ' ')
1269 Tcl_SetObjResult (interp
, Tcl_NewStringObj (string
, -1));
1274 Tcl_SetObjResult (interp
, get_call_output ());
1278 /* This function returns the value of a variable in the interpreter (or an error)
1279 and returns either TCL_OK or TCL_ERROR as appropriate. */
1281 variable_value (interp
, objc
, objv
, var
)
1284 Tcl_Obj
*CONST objv
[];
1292 int real_addressprint
;
1294 /* If we set the value of the variable, objv[2] will contain the
1295 variable's new value. We need to first construct a legal expression
1299 /* Does this cover all the bases? */
1300 struct expression
*exp
;
1302 int saved_input_radix
= input_radix
;
1304 if (VALUE_LVAL (var
->value
) != not_lval
&& var
->value
->modifiable
)
1308 input_radix
= 10; /* ALWAYS reset to decimal temporarily */
1309 s
= Tcl_GetStringFromObj (objv
[2], NULL
);
1310 r
= GDB_parse_exp_1 (&s
, 0, 0, &exp
);
1313 if (GDB_evaluate_expression (exp
, &value
) != GDB_OK
)
1316 val
= value_assign (var
->value
, value
);
1317 value_free (var
->value
);
1318 release_value (val
);
1320 input_radix
= saved_input_radix
;
1326 if (var
->value
!= NULL
)
1330 /* This can happen if we attempt to get the value of a struct
1331 member when the parent is an invalid pointer.
1333 GDB reports the error as the error derived from accessing the
1334 parent, but we don't have access to that here... */
1335 Tcl_SetObjResult (interp
, Tcl_NewStringObj ("???", -1));
1339 /* C++: addressprint causes val_print to print the
1340 address of the reference, too. So clear it to get
1341 the real value -- BUT ONLY FOR C++ REFERENCE TYPES! */
1342 real_addressprint
= addressprint
;
1344 /* BOGUS: if val_print sees a struct/class, it will print out its
1345 children instead of "{...}" */
1346 type
= get_type (val
);
1347 switch (TYPE_CODE (type
))
1349 case TYPE_CODE_STRUCT
:
1350 case TYPE_CODE_UNION
:
1351 str
= Tcl_NewStringObj ("{...}", -1);
1354 case TYPE_CODE_ARRAY
:
1357 str
= Tcl_NewStringObj (NULL
, 0);
1358 sprintf (number
, "%d", var
->num_children
);
1359 Tcl_AppendStringsToObj (str
, "[", number
, "]", NULL
);
1364 /* Clear addressprint so that the actual value is printed */
1369 result
= call_gdb_val_print (val
, format_code
[(int) var
->format
]);
1370 Tcl_SetObjResult (interp
, get_call_output ());
1372 /* Restore addressprint */
1373 addressprint
= real_addressprint
;
1377 /* We only get here if we encountered one of the "special types" above */
1379 /* Restore addressprint */
1380 addressprint
= real_addressprint
;
1382 Tcl_SetObjResult (interp
, str
);
1386 /* Is this variable editable? Use the variable's type to make
1387 this determination. */
1389 variable_editable (var
)
1396 type
= get_type (var
->value
);
1400 r
= GDB_evaluate_type (var
->exp
, &val
);
1403 type
= get_type (val
);
1406 switch (TYPE_CODE (type
))
1408 case TYPE_CODE_STRUCT
:
1409 case TYPE_CODE_UNION
:
1410 case TYPE_CODE_ARRAY
:
1411 case TYPE_CODE_FUNC
:
1412 case TYPE_CODE_MEMBER
:
1413 case TYPE_CODE_METHOD
:
1426 * Call stuff. These functions are used to capture the output of gdb commands
1427 * without going through the tcl interpreter.
1430 /* Retrieve gdb output in the buffer since last call. */
1434 /* Clear the error flags, in case we errored. */
1435 if (result_ptr
!= NULL
)
1436 result_ptr
->flags
&= ~GDBTK_ERROR_ONLY
;
1440 /* Clear the output of the buffer. */
1444 if (fputs_obj
!= NULL
)
1445 Tcl_DecrRefCount (fputs_obj
);
1447 fputs_obj
= Tcl_NewStringObj (NULL
, -1);
1448 Tcl_IncrRefCount (fputs_obj
);
1451 /* Call the gdb command "type_print", retaining its output in the buffer. */
1453 call_gdb_type_print (val
)
1456 void (*old_hook
) PARAMS ((const char *, GDB_FILE
*));
1459 /* Save the old hook and install new hook */
1460 old_hook
= fputs_unfiltered_hook
;
1461 fputs_unfiltered_hook
= variable_fputs
;
1463 /* Call our command with our args */
1464 clear_gdb_output ();
1467 if (GDB_type_print (val
, "", gdb_stdout
, -1) == GDB_OK
)
1472 /* Restore fputs hook */
1473 fputs_unfiltered_hook
= old_hook
;
1478 /* Call the gdb command "val_print", retaining its output in the buffer. */
1480 call_gdb_val_print (val
, format
)
1484 void (*old_hook
) PARAMS ((const char *, GDB_FILE
*));
1488 /* Save the old hook and install new hook */
1489 old_hook
= fputs_unfiltered_hook
;
1490 fputs_unfiltered_hook
= variable_fputs
;
1492 /* Call our command with our args */
1493 clear_gdb_output ();
1495 if (VALUE_LAZY (val
))
1497 r
= GDB_value_fetch_lazy (val
);
1500 fputs_unfiltered_hook
= old_hook
;
1504 r
= GDB_val_print (VALUE_TYPE (val
), VALUE_CONTENTS_RAW (val
), VALUE_ADDRESS (val
),
1505 gdb_stdout
, format
, 1, 0, 0);
1511 /* Restore fputs hook */
1512 fputs_unfiltered_hook
= old_hook
;
1517 /* The fputs_unfiltered_hook function used to save the output from one of the
1518 call commands in this file. */
1520 variable_fputs (text
, stream
)
1524 /* Just append everything to the fputs_obj... Issues with stderr/stdout? */
1525 Tcl_AppendToObj (fputs_obj
, (char *) text
, -1);
1528 /* Empty handler for the fputs_unfiltered_hook. Set the hook to this function
1529 whenever the output is irrelevent. */
1531 null_fputs (text
, stream
)
1539 * Special wrapper-like stuff to supplement the generic wrappers
1542 /* This returns the type of the variable. This skips past typedefs
1543 and returns the real type of the variable. */
1544 static struct type
*
1548 struct type
*type
= NULL
;
1552 type
= VALUE_TYPE (val
);
1553 while (type
!= NULL
&& TYPE_CODE (type
) == TYPE_CODE_TYPEDEF
)
1554 type
= TYPE_TARGET_TYPE (type
);
1560 /* This returns the target type (or NULL) of TYPE, also skipping
1561 past typedefs, just like get_type (). */
1562 static struct type
*
1563 get_target_type (type
)
1568 type
= TYPE_TARGET_TYPE (type
);
1569 while (type
!= NULL
&& TYPE_CODE (type
) == TYPE_CODE_TYPEDEF
)
1570 type
= TYPE_TARGET_TYPE (type
);
1576 /* This function is a special wrap. This call never "fails".*/
1578 my_value_equal (var
, val2
)
1584 /* This is bogus, but unfortunately necessary. We must know
1585 exactly what caused an error -- reading var->val (which we
1586 get from var->error and/or val2, so that we can really determine
1587 if we think that something has changed. */
1590 if (VALUE_LAZY (val2
) && GDB_value_fetch_lazy (val2
) != GDB_OK
)
1593 /* Another special case: NULL values. If both are null, say
1595 if (var
->value
== NULL
&& val2
== NULL
)
1597 else if (var
->value
== NULL
|| val2
== NULL
)
1600 if (GDB_value_equal (var
->value
, val2
, &r
) != GDB_OK
)
1602 /* An error occurred, this could have happened if
1603 either val1 or val2 errored. ERR1 and ERR2 tell
1604 us which of these it is. If both errored, then
1605 we assume nothing has changed. If one of them is
1606 valid, though, then something has changed. */
1609 /* both the old and new values caused errors, so
1610 we say the value did not change */
1611 /* This is indeterminate, though. Perhaps we should
1612 be safe and say, yes, it changed anyway?? */
1617 /* err2 replaces var->error since this new value
1618 WILL replace the old one. */
1627 /* Local variables: */
1628 /* change-log-default-name: "ChangeLog-gdbtk" */