* varobj.c (struct varobj_root): Clarify
[deliverable/binutils-gdb.git] / gdb / varobj.c
CommitLineData
8b93c638 1/* Implementation of the GDB variable objects API.
bc8332bb 2
9b254dd1 3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
1ecb4ee0 4 Free Software Foundation, Inc.
8b93c638
JM
5
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
a9762ec7 8 the Free Software Foundation; either version 3 of the License, or
8b93c638
JM
9 (at your option) any later version.
10
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.
15
16 You should have received a copy of the GNU General Public License
a9762ec7 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
8b93c638
JM
18
19#include "defs.h"
a6c442d8 20#include "exceptions.h"
8b93c638
JM
21#include "value.h"
22#include "expression.h"
23#include "frame.h"
8b93c638
JM
24#include "language.h"
25#include "wrapper.h"
26#include "gdbcmd.h"
d2353924 27#include "block.h"
a6c442d8
MK
28
29#include "gdb_assert.h"
b66d6d2e 30#include "gdb_string.h"
8b93c638
JM
31
32#include "varobj.h"
28335dcc 33#include "vec.h"
8b93c638
JM
34
35/* Non-zero if we want to see trace of varobj level stuff. */
36
37int varobjdebug = 0;
920d2a44
AC
38static void
39show_varobjdebug (struct ui_file *file, int from_tty,
40 struct cmd_list_element *c, const char *value)
41{
42 fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
43}
8b93c638
JM
44
45/* String representations of gdb's format codes */
46char *varobj_format_string[] =
72330bd6 47 { "natural", "binary", "decimal", "hexadecimal", "octal" };
8b93c638
JM
48
49/* String representations of gdb's known languages */
72330bd6 50char *varobj_language_string[] = { "unknown", "C", "C++", "Java" };
8b93c638
JM
51
52/* Data structures */
53
54/* Every root variable has one of these structures saved in its
55 varobj. Members which must be free'd are noted. */
56struct varobj_root
72330bd6 57{
8b93c638 58
72330bd6
AC
59 /* Alloc'd expression for this parent. */
60 struct expression *exp;
8b93c638 61
72330bd6
AC
62 /* Block for which this expression is valid */
63 struct block *valid_block;
8b93c638 64
44a67aa7
VP
65 /* The frame for this expression. This field is set iff valid_block is
66 not NULL. */
e64d9b3d 67 struct frame_id frame;
8b93c638 68
72330bd6
AC
69 /* If 1, "update" always recomputes the frame & valid block
70 using the currently selected frame. */
71 int use_selected_frame;
73a93a32 72
8756216b
DP
73 /* Flag that indicates validity: set to 0 when this varobj_root refers
74 to symbols that do not exist anymore. */
75 int is_valid;
76
72330bd6
AC
77 /* Language info for this variable and its children */
78 struct language_specific *lang;
8b93c638 79
72330bd6
AC
80 /* The varobj for this root node. */
81 struct varobj *rootvar;
8b93c638 82
72330bd6
AC
83 /* Next root variable */
84 struct varobj_root *next;
85};
8b93c638
JM
86
87/* Every variable in the system has a structure of this type defined
88 for it. This structure holds all information necessary to manipulate
89 a particular object variable. Members which must be freed are noted. */
90struct varobj
72330bd6 91{
8b93c638 92
72330bd6
AC
93 /* Alloc'd name of the variable for this object.. If this variable is a
94 child, then this name will be the child's source name.
95 (bar, not foo.bar) */
96 /* NOTE: This is the "expression" */
97 char *name;
8b93c638 98
02142340
VP
99 /* Alloc'd expression for this child. Can be used to create a
100 root variable corresponding to this child. */
101 char *path_expr;
102
72330bd6
AC
103 /* The alloc'd name for this variable's object. This is here for
104 convenience when constructing this object's children. */
105 char *obj_name;
8b93c638 106
72330bd6
AC
107 /* Index of this variable in its parent or -1 */
108 int index;
8b93c638 109
202ddcaa
VP
110 /* The type of this variable. This can be NULL
111 for artifial variable objects -- currently, the "accessibility"
112 variable objects in C++. */
72330bd6 113 struct type *type;
8b93c638 114
b20d8971
VP
115 /* The value of this expression or subexpression. A NULL value
116 indicates there was an error getting this value.
b2c2bd75
VP
117 Invariant: if varobj_value_is_changeable_p (this) is non-zero,
118 the value is either NULL, or not lazy. */
30b28db1 119 struct value *value;
8b93c638 120
72330bd6
AC
121 /* The number of (immediate) children this variable has */
122 int num_children;
8b93c638 123
72330bd6
AC
124 /* If this object is a child, this points to its immediate parent. */
125 struct varobj *parent;
8b93c638 126
28335dcc
VP
127 /* Children of this object. */
128 VEC (varobj_p) *children;
8b93c638 129
72330bd6
AC
130 /* Description of the root variable. Points to root variable for children. */
131 struct varobj_root *root;
8b93c638 132
72330bd6
AC
133 /* The format of the output for this object */
134 enum varobj_display_formats format;
fb9b6b35
JJ
135
136 /* Was this variable updated via a varobj_set_value operation */
137 int updated;
85265413
NR
138
139 /* Last print value. */
140 char *print_value;
25d5ea92
VP
141
142 /* Is this variable frozen. Frozen variables are never implicitly
143 updated by -var-update *
144 or -var-update <direct-or-indirect-parent>. */
145 int frozen;
146
147 /* Is the value of this variable intentionally not fetched? It is
148 not fetched if either the variable is frozen, or any parents is
149 frozen. */
150 int not_fetched;
72330bd6 151};
8b93c638 152
8b93c638 153struct cpstack
72330bd6
AC
154{
155 char *name;
156 struct cpstack *next;
157};
8b93c638
JM
158
159/* A list of varobjs */
160
161struct vlist
72330bd6
AC
162{
163 struct varobj *var;
164 struct vlist *next;
165};
8b93c638
JM
166
167/* Private function prototypes */
168
169/* Helper functions for the above subcommands. */
170
a14ed312 171static int delete_variable (struct cpstack **, struct varobj *, int);
8b93c638 172
a14ed312
KB
173static void delete_variable_1 (struct cpstack **, int *,
174 struct varobj *, int, int);
8b93c638 175
a14ed312 176static int install_variable (struct varobj *);
8b93c638 177
a14ed312 178static void uninstall_variable (struct varobj *);
8b93c638 179
a14ed312 180static struct varobj *create_child (struct varobj *, int, char *);
8b93c638 181
8b93c638
JM
182/* Utility routines */
183
a14ed312 184static struct varobj *new_variable (void);
8b93c638 185
a14ed312 186static struct varobj *new_root_variable (void);
8b93c638 187
a14ed312 188static void free_variable (struct varobj *var);
8b93c638 189
74b7792f
AC
190static struct cleanup *make_cleanup_free_variable (struct varobj *var);
191
a14ed312 192static struct type *get_type (struct varobj *var);
8b93c638 193
6e2a9270
VP
194static struct type *get_value_type (struct varobj *var);
195
a14ed312 196static struct type *get_target_type (struct type *);
8b93c638 197
a14ed312 198static enum varobj_display_formats variable_default_display (struct varobj *);
8b93c638 199
a14ed312 200static void cppush (struct cpstack **pstack, char *name);
8b93c638 201
a14ed312 202static char *cppop (struct cpstack **pstack);
8b93c638 203
acd65feb
VP
204static int install_new_value (struct varobj *var, struct value *value,
205 int initial);
206
8b93c638
JM
207/* Language-specific routines. */
208
a14ed312 209static enum varobj_languages variable_language (struct varobj *var);
8b93c638 210
a14ed312 211static int number_of_children (struct varobj *);
8b93c638 212
a14ed312 213static char *name_of_variable (struct varobj *);
8b93c638 214
a14ed312 215static char *name_of_child (struct varobj *, int);
8b93c638 216
30b28db1 217static struct value *value_of_root (struct varobj **var_handle, int *);
8b93c638 218
30b28db1 219static struct value *value_of_child (struct varobj *parent, int index);
8b93c638 220
a14ed312 221static char *my_value_of_variable (struct varobj *var);
8b93c638 222
85265413
NR
223static char *value_get_print_value (struct value *value,
224 enum varobj_display_formats format);
225
b2c2bd75
VP
226static int varobj_value_is_changeable_p (struct varobj *var);
227
228static int is_root_p (struct varobj *var);
8b93c638
JM
229
230/* C implementation */
231
a14ed312 232static int c_number_of_children (struct varobj *var);
8b93c638 233
a14ed312 234static char *c_name_of_variable (struct varobj *parent);
8b93c638 235
a14ed312 236static char *c_name_of_child (struct varobj *parent, int index);
8b93c638 237
02142340
VP
238static char *c_path_expr_of_child (struct varobj *child);
239
30b28db1 240static struct value *c_value_of_root (struct varobj **var_handle);
8b93c638 241
30b28db1 242static struct value *c_value_of_child (struct varobj *parent, int index);
8b93c638 243
a14ed312 244static struct type *c_type_of_child (struct varobj *parent, int index);
8b93c638 245
a14ed312 246static char *c_value_of_variable (struct varobj *var);
8b93c638
JM
247
248/* C++ implementation */
249
a14ed312 250static int cplus_number_of_children (struct varobj *var);
8b93c638 251
a14ed312 252static void cplus_class_num_children (struct type *type, int children[3]);
8b93c638 253
a14ed312 254static char *cplus_name_of_variable (struct varobj *parent);
8b93c638 255
a14ed312 256static char *cplus_name_of_child (struct varobj *parent, int index);
8b93c638 257
02142340
VP
258static char *cplus_path_expr_of_child (struct varobj *child);
259
30b28db1 260static struct value *cplus_value_of_root (struct varobj **var_handle);
8b93c638 261
30b28db1 262static struct value *cplus_value_of_child (struct varobj *parent, int index);
8b93c638 263
a14ed312 264static struct type *cplus_type_of_child (struct varobj *parent, int index);
8b93c638 265
a14ed312 266static char *cplus_value_of_variable (struct varobj *var);
8b93c638
JM
267
268/* Java implementation */
269
a14ed312 270static int java_number_of_children (struct varobj *var);
8b93c638 271
a14ed312 272static char *java_name_of_variable (struct varobj *parent);
8b93c638 273
a14ed312 274static char *java_name_of_child (struct varobj *parent, int index);
8b93c638 275
02142340
VP
276static char *java_path_expr_of_child (struct varobj *child);
277
30b28db1 278static struct value *java_value_of_root (struct varobj **var_handle);
8b93c638 279
30b28db1 280static struct value *java_value_of_child (struct varobj *parent, int index);
8b93c638 281
a14ed312 282static struct type *java_type_of_child (struct varobj *parent, int index);
8b93c638 283
a14ed312 284static char *java_value_of_variable (struct varobj *var);
8b93c638
JM
285
286/* The language specific vector */
287
288struct language_specific
72330bd6 289{
8b93c638 290
72330bd6
AC
291 /* The language of this variable */
292 enum varobj_languages language;
8b93c638 293
72330bd6
AC
294 /* The number of children of PARENT. */
295 int (*number_of_children) (struct varobj * parent);
8b93c638 296
72330bd6
AC
297 /* The name (expression) of a root varobj. */
298 char *(*name_of_variable) (struct varobj * parent);
8b93c638 299
72330bd6
AC
300 /* The name of the INDEX'th child of PARENT. */
301 char *(*name_of_child) (struct varobj * parent, int index);
8b93c638 302
02142340
VP
303 /* Returns the rooted expression of CHILD, which is a variable
304 obtain that has some parent. */
305 char *(*path_expr_of_child) (struct varobj * child);
306
30b28db1
AC
307 /* The ``struct value *'' of the root variable ROOT. */
308 struct value *(*value_of_root) (struct varobj ** root_handle);
8b93c638 309
30b28db1
AC
310 /* The ``struct value *'' of the INDEX'th child of PARENT. */
311 struct value *(*value_of_child) (struct varobj * parent, int index);
8b93c638 312
72330bd6
AC
313 /* The type of the INDEX'th child of PARENT. */
314 struct type *(*type_of_child) (struct varobj * parent, int index);
8b93c638 315
72330bd6
AC
316 /* The current value of VAR. */
317 char *(*value_of_variable) (struct varobj * var);
318};
8b93c638
JM
319
320/* Array of known source language routines. */
d5d6fca5 321static struct language_specific languages[vlang_end] = {
8b93c638
JM
322 /* Unknown (try treating as C */
323 {
72330bd6
AC
324 vlang_unknown,
325 c_number_of_children,
326 c_name_of_variable,
327 c_name_of_child,
02142340 328 c_path_expr_of_child,
72330bd6
AC
329 c_value_of_root,
330 c_value_of_child,
331 c_type_of_child,
72330bd6 332 c_value_of_variable}
8b93c638
JM
333 ,
334 /* C */
335 {
72330bd6
AC
336 vlang_c,
337 c_number_of_children,
338 c_name_of_variable,
339 c_name_of_child,
02142340 340 c_path_expr_of_child,
72330bd6
AC
341 c_value_of_root,
342 c_value_of_child,
343 c_type_of_child,
72330bd6 344 c_value_of_variable}
8b93c638
JM
345 ,
346 /* C++ */
347 {
72330bd6
AC
348 vlang_cplus,
349 cplus_number_of_children,
350 cplus_name_of_variable,
351 cplus_name_of_child,
02142340 352 cplus_path_expr_of_child,
72330bd6
AC
353 cplus_value_of_root,
354 cplus_value_of_child,
355 cplus_type_of_child,
72330bd6 356 cplus_value_of_variable}
8b93c638
JM
357 ,
358 /* Java */
359 {
72330bd6
AC
360 vlang_java,
361 java_number_of_children,
362 java_name_of_variable,
363 java_name_of_child,
02142340 364 java_path_expr_of_child,
72330bd6
AC
365 java_value_of_root,
366 java_value_of_child,
367 java_type_of_child,
72330bd6 368 java_value_of_variable}
8b93c638
JM
369};
370
371/* A little convenience enum for dealing with C++/Java */
372enum vsections
72330bd6
AC
373{
374 v_public = 0, v_private, v_protected
375};
8b93c638
JM
376
377/* Private data */
378
379/* Mappings of varobj_display_formats enums to gdb's format codes */
72330bd6 380static int format_code[] = { 0, 't', 'd', 'x', 'o' };
8b93c638
JM
381
382/* Header of the list of root variable objects */
383static struct varobj_root *rootlist;
384static int rootcount = 0; /* number of root varobjs in the list */
385
386/* Prime number indicating the number of buckets in the hash table */
387/* A prime large enough to avoid too many colisions */
388#define VAROBJ_TABLE_SIZE 227
389
390/* Pointer to the varobj hash table (built at run time) */
391static struct vlist **varobj_table;
392
8b93c638
JM
393/* Is the variable X one of our "fake" children? */
394#define CPLUS_FAKE_CHILD(x) \
395((x) != NULL && (x)->type == NULL && (x)->value == NULL)
396\f
397
398/* API Implementation */
b2c2bd75
VP
399static int
400is_root_p (struct varobj *var)
401{
402 return (var->root->rootvar == var);
403}
8b93c638
JM
404
405/* Creates a varobj (not its children) */
406
7d8547c9
AC
407/* Return the full FRAME which corresponds to the given CORE_ADDR
408 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
409
410static struct frame_info *
411find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
412{
413 struct frame_info *frame = NULL;
414
415 if (frame_addr == (CORE_ADDR) 0)
416 return NULL;
417
418 while (1)
419 {
420 frame = get_prev_frame (frame);
421 if (frame == NULL)
422 return NULL;
eb5492fa 423 if (get_frame_base_address (frame) == frame_addr)
7d8547c9
AC
424 return frame;
425 }
426}
427
8b93c638
JM
428struct varobj *
429varobj_create (char *objname,
72330bd6 430 char *expression, CORE_ADDR frame, enum varobj_type type)
8b93c638
JM
431{
432 struct varobj *var;
2c67cb8b
AC
433 struct frame_info *fi;
434 struct frame_info *old_fi = NULL;
8b93c638
JM
435 struct block *block;
436 struct cleanup *old_chain;
437
438 /* Fill out a varobj structure for the (root) variable being constructed. */
439 var = new_root_variable ();
74b7792f 440 old_chain = make_cleanup_free_variable (var);
8b93c638
JM
441
442 if (expression != NULL)
443 {
444 char *p;
445 enum varobj_languages lang;
e55dccf0 446 struct value *value = NULL;
02142340 447 int expr_len;
8b93c638
JM
448
449 /* Parse and evaluate the expression, filling in as much
450 of the variable's data as possible */
451
452 /* Allow creator to specify context of variable */
72330bd6 453 if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
206415a3 454 fi = deprecated_safe_get_selected_frame ();
8b93c638 455 else
7d8547c9
AC
456 /* FIXME: cagney/2002-11-23: This code should be doing a
457 lookup using the frame ID and not just the frame's
458 ``address''. This, of course, means an interface change.
459 However, with out that interface change ISAs, such as the
460 ia64 with its two stacks, won't work. Similar goes for the
461 case where there is a frameless function. */
8b93c638
JM
462 fi = find_frame_addr_in_frame_chain (frame);
463
73a93a32
JI
464 /* frame = -2 means always use selected frame */
465 if (type == USE_SELECTED_FRAME)
466 var->root->use_selected_frame = 1;
467
8b93c638
JM
468 block = NULL;
469 if (fi != NULL)
ae767bfb 470 block = get_frame_block (fi, 0);
8b93c638
JM
471
472 p = expression;
473 innermost_block = NULL;
73a93a32
JI
474 /* Wrap the call to parse expression, so we can
475 return a sensible error. */
476 if (!gdb_parse_exp_1 (&p, block, 0, &var->root->exp))
477 {
478 return NULL;
479 }
8b93c638
JM
480
481 /* Don't allow variables to be created for types. */
482 if (var->root->exp->elts[0].opcode == OP_TYPE)
483 {
484 do_cleanups (old_chain);
bc8332bb
AC
485 fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
486 " as an expression.\n");
8b93c638
JM
487 return NULL;
488 }
489
490 var->format = variable_default_display (var);
491 var->root->valid_block = innermost_block;
02142340
VP
492 expr_len = strlen (expression);
493 var->name = savestring (expression, expr_len);
494 /* For a root var, the name and the expr are the same. */
495 var->path_expr = savestring (expression, expr_len);
8b93c638
JM
496
497 /* When the frame is different from the current frame,
498 we must select the appropriate frame before parsing
499 the expression, otherwise the value will not be current.
500 Since select_frame is so benign, just call it for all cases. */
44a67aa7 501 if (innermost_block && fi != NULL)
8b93c638 502 {
7a424e99 503 var->root->frame = get_frame_id (fi);
206415a3 504 old_fi = get_selected_frame (NULL);
0f7d239c 505 select_frame (fi);
8b93c638
JM
506 }
507
340a7723 508 /* We definitely need to catch errors here.
8b93c638
JM
509 If evaluate_expression succeeds we got the value we wanted.
510 But if it fails, we still go on with a call to evaluate_type() */
acd65feb 511 if (!gdb_evaluate_expression (var->root->exp, &value))
e55dccf0
VP
512 {
513 /* Error getting the value. Try to at least get the
514 right type. */
515 struct value *type_only_value = evaluate_type (var->root->exp);
516 var->type = value_type (type_only_value);
517 }
518 else
519 var->type = value_type (value);
acd65feb 520
acd65feb 521 install_new_value (var, value, 1 /* Initial assignment */);
8b93c638
JM
522
523 /* Set language info */
524 lang = variable_language (var);
d5d6fca5 525 var->root->lang = &languages[lang];
8b93c638
JM
526
527 /* Set ourselves as our root */
528 var->root->rootvar = var;
529
530 /* Reset the selected frame */
531 if (fi != NULL)
0f7d239c 532 select_frame (old_fi);
8b93c638
JM
533 }
534
73a93a32
JI
535 /* If the variable object name is null, that means this
536 is a temporary variable, so don't install it. */
537
538 if ((var != NULL) && (objname != NULL))
8b93c638
JM
539 {
540 var->obj_name = savestring (objname, strlen (objname));
541
542 /* If a varobj name is duplicated, the install will fail so
543 we must clenup */
544 if (!install_variable (var))
545 {
546 do_cleanups (old_chain);
547 return NULL;
548 }
549 }
550
551 discard_cleanups (old_chain);
552 return var;
553}
554
555/* Generates an unique name that can be used for a varobj */
556
557char *
558varobj_gen_name (void)
559{
560 static int id = 0;
e64d9b3d 561 char *obj_name;
8b93c638
JM
562
563 /* generate a name for this object */
564 id++;
b435e160 565 obj_name = xstrprintf ("var%d", id);
8b93c638 566
e64d9b3d 567 return obj_name;
8b93c638
JM
568}
569
570/* Given an "objname", returns the pointer to the corresponding varobj
571 or NULL if not found */
572
573struct varobj *
574varobj_get_handle (char *objname)
575{
576 struct vlist *cv;
577 const char *chp;
578 unsigned int index = 0;
579 unsigned int i = 1;
580
581 for (chp = objname; *chp; chp++)
582 {
583 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
584 }
585
586 cv = *(varobj_table + index);
587 while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
588 cv = cv->next;
589
590 if (cv == NULL)
8a3fe4f8 591 error (_("Variable object not found"));
8b93c638
JM
592
593 return cv->var;
594}
595
596/* Given the handle, return the name of the object */
597
598char *
599varobj_get_objname (struct varobj *var)
600{
601 return var->obj_name;
602}
603
604/* Given the handle, return the expression represented by the object */
605
606char *
607varobj_get_expression (struct varobj *var)
608{
609 return name_of_variable (var);
610}
611
612/* Deletes a varobj and all its children if only_children == 0,
613 otherwise deletes only the children; returns a malloc'ed list of all the
614 (malloc'ed) names of the variables that have been deleted (NULL terminated) */
615
616int
617varobj_delete (struct varobj *var, char ***dellist, int only_children)
618{
619 int delcount;
620 int mycount;
621 struct cpstack *result = NULL;
622 char **cp;
623
624 /* Initialize a stack for temporary results */
625 cppush (&result, NULL);
626
627 if (only_children)
628 /* Delete only the variable children */
629 delcount = delete_variable (&result, var, 1 /* only the children */ );
630 else
631 /* Delete the variable and all its children */
632 delcount = delete_variable (&result, var, 0 /* parent+children */ );
633
634 /* We may have been asked to return a list of what has been deleted */
635 if (dellist != NULL)
636 {
637 *dellist = xmalloc ((delcount + 1) * sizeof (char *));
638
639 cp = *dellist;
640 mycount = delcount;
641 *cp = cppop (&result);
642 while ((*cp != NULL) && (mycount > 0))
643 {
644 mycount--;
645 cp++;
646 *cp = cppop (&result);
647 }
648
649 if (mycount || (*cp != NULL))
8a3fe4f8 650 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
72330bd6 651 mycount);
8b93c638
JM
652 }
653
654 return delcount;
655}
656
657/* Set/Get variable object display format */
658
659enum varobj_display_formats
660varobj_set_display_format (struct varobj *var,
661 enum varobj_display_formats format)
662{
663 switch (format)
664 {
665 case FORMAT_NATURAL:
666 case FORMAT_BINARY:
667 case FORMAT_DECIMAL:
668 case FORMAT_HEXADECIMAL:
669 case FORMAT_OCTAL:
670 var->format = format;
671 break;
672
673 default:
674 var->format = variable_default_display (var);
675 }
676
ae7d22a6
VP
677 if (varobj_value_is_changeable_p (var)
678 && var->value && !value_lazy (var->value))
679 {
680 free (var->print_value);
681 var->print_value = value_get_print_value (var->value, var->format);
682 }
683
8b93c638
JM
684 return var->format;
685}
686
687enum varobj_display_formats
688varobj_get_display_format (struct varobj *var)
689{
690 return var->format;
691}
692
25d5ea92
VP
693void
694varobj_set_frozen (struct varobj *var, int frozen)
695{
696 /* When a variable is unfrozen, we don't fetch its value.
697 The 'not_fetched' flag remains set, so next -var-update
698 won't complain.
699
700 We don't fetch the value, because for structures the client
701 should do -var-update anyway. It would be bad to have different
702 client-size logic for structure and other types. */
703 var->frozen = frozen;
704}
705
706int
707varobj_get_frozen (struct varobj *var)
708{
709 return var->frozen;
710}
711
712
8b93c638
JM
713int
714varobj_get_num_children (struct varobj *var)
715{
716 if (var->num_children == -1)
717 var->num_children = number_of_children (var);
718
719 return var->num_children;
720}
721
722/* Creates a list of the immediate children of a variable object;
723 the return code is the number of such children or -1 on error */
724
d56d46f5
VP
725VEC (varobj_p)*
726varobj_list_children (struct varobj *var)
8b93c638
JM
727{
728 struct varobj *child;
729 char *name;
730 int i;
731
8b93c638
JM
732 if (var->num_children == -1)
733 var->num_children = number_of_children (var);
734
74a44383
DJ
735 /* If that failed, give up. */
736 if (var->num_children == -1)
d56d46f5 737 return var->children;
74a44383 738
28335dcc
VP
739 /* If we're called when the list of children is not yet initialized,
740 allocate enough elements in it. */
741 while (VEC_length (varobj_p, var->children) < var->num_children)
742 VEC_safe_push (varobj_p, var->children, NULL);
743
8b93c638
JM
744 for (i = 0; i < var->num_children; i++)
745 {
d56d46f5 746 varobj_p existing = VEC_index (varobj_p, var->children, i);
28335dcc
VP
747
748 if (existing == NULL)
749 {
750 /* Either it's the first call to varobj_list_children for
751 this variable object, and the child was never created,
752 or it was explicitly deleted by the client. */
753 name = name_of_child (var, i);
754 existing = create_child (var, i, name);
755 VEC_replace (varobj_p, var->children, i, existing);
756 }
8b93c638
JM
757 }
758
d56d46f5 759 return var->children;
8b93c638
JM
760}
761
762/* Obtain the type of an object Variable as a string similar to the one gdb
763 prints on the console */
764
765char *
766varobj_get_type (struct varobj *var)
767{
30b28db1 768 struct value *val;
8b93c638
JM
769 struct cleanup *old_chain;
770 struct ui_file *stb;
771 char *thetype;
772 long length;
773
774 /* For the "fake" variables, do not return a type. (It's type is
8756216b
DP
775 NULL, too.)
776 Do not return a type for invalid variables as well. */
777 if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
8b93c638
JM
778 return NULL;
779
780 stb = mem_fileopen ();
781 old_chain = make_cleanup_ui_file_delete (stb);
782
30b28db1 783 /* To print the type, we simply create a zero ``struct value *'' and
8b93c638
JM
784 cast it to our type. We then typeprint this variable. */
785 val = value_zero (var->type, not_lval);
df407dfe 786 type_print (value_type (val), "", stb, -1);
8b93c638
JM
787
788 thetype = ui_file_xstrdup (stb, &length);
789 do_cleanups (old_chain);
790 return thetype;
791}
792
1ecb4ee0
DJ
793/* Obtain the type of an object variable. */
794
795struct type *
796varobj_get_gdb_type (struct varobj *var)
797{
798 return var->type;
799}
800
02142340
VP
801/* Return a pointer to the full rooted expression of varobj VAR.
802 If it has not been computed yet, compute it. */
803char *
804varobj_get_path_expr (struct varobj *var)
805{
806 if (var->path_expr != NULL)
807 return var->path_expr;
808 else
809 {
810 /* For root varobjs, we initialize path_expr
811 when creating varobj, so here it should be
812 child varobj. */
813 gdb_assert (!is_root_p (var));
814 return (*var->root->lang->path_expr_of_child) (var);
815 }
816}
817
8b93c638
JM
818enum varobj_languages
819varobj_get_language (struct varobj *var)
820{
821 return variable_language (var);
822}
823
824int
825varobj_get_attributes (struct varobj *var)
826{
827 int attributes = 0;
828
340a7723 829 if (varobj_editable_p (var))
8b93c638
JM
830 /* FIXME: define masks for attributes */
831 attributes |= 0x00000001; /* Editable */
832
833 return attributes;
834}
835
836char *
837varobj_get_value (struct varobj *var)
838{
839 return my_value_of_variable (var);
840}
841
842/* Set the value of an object variable (if it is editable) to the
843 value of the given expression */
844/* Note: Invokes functions that can call error() */
845
846int
847varobj_set_value (struct varobj *var, char *expression)
848{
30b28db1 849 struct value *val;
8b93c638 850 int offset = 0;
a6c442d8 851 int error = 0;
8b93c638
JM
852
853 /* The argument "expression" contains the variable's new value.
854 We need to first construct a legal expression for this -- ugh! */
855 /* Does this cover all the bases? */
856 struct expression *exp;
30b28db1 857 struct value *value;
8b93c638 858 int saved_input_radix = input_radix;
340a7723
NR
859 char *s = expression;
860 int i;
8b93c638 861
340a7723 862 gdb_assert (varobj_editable_p (var));
8b93c638 863
340a7723
NR
864 input_radix = 10; /* ALWAYS reset to decimal temporarily */
865 exp = parse_exp_1 (&s, 0, 0);
866 if (!gdb_evaluate_expression (exp, &value))
867 {
868 /* We cannot proceed without a valid expression. */
869 xfree (exp);
870 return 0;
8b93c638
JM
871 }
872
340a7723
NR
873 /* All types that are editable must also be changeable. */
874 gdb_assert (varobj_value_is_changeable_p (var));
875
876 /* The value of a changeable variable object must not be lazy. */
877 gdb_assert (!value_lazy (var->value));
878
879 /* Need to coerce the input. We want to check if the
880 value of the variable object will be different
881 after assignment, and the first thing value_assign
882 does is coerce the input.
883 For example, if we are assigning an array to a pointer variable we
884 should compare the pointer with the the array's address, not with the
885 array's content. */
886 value = coerce_array (value);
887
888 /* The new value may be lazy. gdb_value_assign, or
889 rather value_contents, will take care of this.
890 If fetching of the new value will fail, gdb_value_assign
891 with catch the exception. */
892 if (!gdb_value_assign (var->value, value, &val))
893 return 0;
894
895 /* If the value has changed, record it, so that next -var-update can
896 report this change. If a variable had a value of '1', we've set it
897 to '333' and then set again to '1', when -var-update will report this
898 variable as changed -- because the first assignment has set the
899 'updated' flag. There's no need to optimize that, because return value
900 of -var-update should be considered an approximation. */
901 var->updated = install_new_value (var, val, 0 /* Compare values. */);
902 input_radix = saved_input_radix;
903 return 1;
8b93c638
JM
904}
905
906/* Returns a malloc'ed list with all root variable objects */
907int
908varobj_list (struct varobj ***varlist)
909{
910 struct varobj **cv;
911 struct varobj_root *croot;
912 int mycount = rootcount;
913
914 /* Alloc (rootcount + 1) entries for the result */
915 *varlist = xmalloc ((rootcount + 1) * sizeof (struct varobj *));
916
917 cv = *varlist;
918 croot = rootlist;
919 while ((croot != NULL) && (mycount > 0))
920 {
921 *cv = croot->rootvar;
922 mycount--;
923 cv++;
924 croot = croot->next;
925 }
926 /* Mark the end of the list */
927 *cv = NULL;
928
929 if (mycount || (croot != NULL))
72330bd6
AC
930 warning
931 ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)",
932 rootcount, mycount);
8b93c638
JM
933
934 return rootcount;
935}
936
acd65feb
VP
937/* Assign a new value to a variable object. If INITIAL is non-zero,
938 this is the first assignement after the variable object was just
939 created, or changed type. In that case, just assign the value
940 and return 0.
941 Otherwise, assign the value and if type_changeable returns non-zero,
942 find if the new value is different from the current value.
b26ed50d
VP
943 Return 1 if so, and 0 if the values are equal.
944
945 The VALUE parameter should not be released -- the function will
946 take care of releasing it when needed. */
acd65feb
VP
947static int
948install_new_value (struct varobj *var, struct value *value, int initial)
949{
950 int changeable;
951 int need_to_fetch;
952 int changed = 0;
25d5ea92 953 int intentionally_not_fetched = 0;
7a4d50bf 954 char *print_value = NULL;
acd65feb 955
acd65feb
VP
956 /* We need to know the varobj's type to decide if the value should
957 be fetched or not. C++ fake children (public/protected/private) don't have
958 a type. */
959 gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
b2c2bd75 960 changeable = varobj_value_is_changeable_p (var);
acd65feb
VP
961 need_to_fetch = changeable;
962
b26ed50d
VP
963 /* We are not interested in the address of references, and given
964 that in C++ a reference is not rebindable, it cannot
965 meaningfully change. So, get hold of the real value. */
966 if (value)
967 {
968 value = coerce_ref (value);
969 release_value (value);
970 }
971
acd65feb
VP
972 if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
973 /* For unions, we need to fetch the value implicitly because
974 of implementation of union member fetch. When gdb
975 creates a value for a field and the value of the enclosing
976 structure is not lazy, it immediately copies the necessary
977 bytes from the enclosing values. If the enclosing value is
978 lazy, the call to value_fetch_lazy on the field will read
979 the data from memory. For unions, that means we'll read the
980 same memory more than once, which is not desirable. So
981 fetch now. */
982 need_to_fetch = 1;
983
984 /* The new value might be lazy. If the type is changeable,
985 that is we'll be comparing values of this type, fetch the
986 value now. Otherwise, on the next update the old value
987 will be lazy, which means we've lost that old value. */
988 if (need_to_fetch && value && value_lazy (value))
989 {
25d5ea92
VP
990 struct varobj *parent = var->parent;
991 int frozen = var->frozen;
992 for (; !frozen && parent; parent = parent->parent)
993 frozen |= parent->frozen;
994
995 if (frozen && initial)
996 {
997 /* For variables that are frozen, or are children of frozen
998 variables, we don't do fetch on initial assignment.
999 For non-initial assignemnt we do the fetch, since it means we're
1000 explicitly asked to compare the new value with the old one. */
1001 intentionally_not_fetched = 1;
1002 }
1003 else if (!gdb_value_fetch_lazy (value))
acd65feb 1004 {
acd65feb
VP
1005 /* Set the value to NULL, so that for the next -var-update,
1006 we don't try to compare the new value with this value,
1007 that we couldn't even read. */
1008 value = NULL;
1009 }
acd65feb
VP
1010 }
1011
7a4d50bf
VP
1012 /* Below, we'll be comparing string rendering of old and new
1013 values. Don't get string rendering if the value is
1014 lazy -- if it is, the code above has decided that the value
1015 should not be fetched. */
1016 if (value && !value_lazy (value))
1017 print_value = value_get_print_value (value, var->format);
1018
acd65feb
VP
1019 /* If the type is changeable, compare the old and the new values.
1020 If this is the initial assignment, we don't have any old value
1021 to compare with. */
7a4d50bf 1022 if (!initial && changeable)
acd65feb
VP
1023 {
1024 /* If the value of the varobj was changed by -var-set-value, then the
1025 value in the varobj and in the target is the same. However, that value
1026 is different from the value that the varobj had after the previous
57e66780 1027 -var-update. So need to the varobj as changed. */
acd65feb 1028 if (var->updated)
57e66780 1029 {
57e66780
DJ
1030 changed = 1;
1031 }
acd65feb
VP
1032 else
1033 {
1034 /* Try to compare the values. That requires that both
1035 values are non-lazy. */
25d5ea92
VP
1036 if (var->not_fetched && value_lazy (var->value))
1037 {
1038 /* This is a frozen varobj and the value was never read.
1039 Presumably, UI shows some "never read" indicator.
1040 Now that we've fetched the real value, we need to report
1041 this varobj as changed so that UI can show the real
1042 value. */
1043 changed = 1;
1044 }
1045 else if (var->value == NULL && value == NULL)
acd65feb
VP
1046 /* Equal. */
1047 ;
1048 else if (var->value == NULL || value == NULL)
57e66780 1049 {
57e66780
DJ
1050 changed = 1;
1051 }
acd65feb
VP
1052 else
1053 {
1054 gdb_assert (!value_lazy (var->value));
1055 gdb_assert (!value_lazy (value));
85265413 1056
57e66780 1057 gdb_assert (var->print_value != NULL && print_value != NULL);
85265413 1058 if (strcmp (var->print_value, print_value) != 0)
7a4d50bf 1059 changed = 1;
acd65feb
VP
1060 }
1061 }
1062 }
85265413 1063
acd65feb 1064 /* We must always keep the new value, since children depend on it. */
25d5ea92 1065 if (var->value != NULL && var->value != value)
acd65feb
VP
1066 value_free (var->value);
1067 var->value = value;
7a4d50bf
VP
1068 if (var->print_value)
1069 xfree (var->print_value);
1070 var->print_value = print_value;
25d5ea92
VP
1071 if (value && value_lazy (value) && intentionally_not_fetched)
1072 var->not_fetched = 1;
1073 else
1074 var->not_fetched = 0;
acd65feb 1075 var->updated = 0;
85265413 1076
b26ed50d 1077 gdb_assert (!var->value || value_type (var->value));
acd65feb
VP
1078
1079 return changed;
1080}
acd65feb 1081
8b93c638
JM
1082/* Update the values for a variable and its children. This is a
1083 two-pronged attack. First, re-parse the value for the root's
1084 expression to see if it's changed. Then go all the way
1085 through its children, reconstructing them and noting if they've
1086 changed.
8756216b
DP
1087 Return value:
1088 < 0 for error values, see varobj.h.
1089 Otherwise it is the number of children + parent changed.
8b93c638 1090
25d5ea92
VP
1091 The EXPLICIT parameter specifies if this call is result
1092 of MI request to update this specific variable, or
1093 result of implicit -var-update *. For implicit request, we don't
1094 update frozen variables.
705da579
KS
1095
1096 NOTE: This function may delete the caller's varobj. If it
8756216b
DP
1097 returns TYPE_CHANGED, then it has done this and VARP will be modified
1098 to point to the new varobj. */
8b93c638
JM
1099
1100int
25d5ea92
VP
1101varobj_update (struct varobj **varp, struct varobj ***changelist,
1102 int explicit)
8b93c638
JM
1103{
1104 int changed = 0;
25d5ea92 1105 int type_changed = 0;
8b93c638
JM
1106 int i;
1107 int vleft;
8b93c638
JM
1108 struct varobj *v;
1109 struct varobj **cv;
2c67cb8b 1110 struct varobj **templist = NULL;
30b28db1 1111 struct value *new;
28335dcc
VP
1112 VEC (varobj_p) *stack = NULL;
1113 VEC (varobj_p) *result = NULL;
e64d9b3d
MH
1114 struct frame_id old_fid;
1115 struct frame_info *fi;
8b93c638 1116
8756216b 1117 /* sanity check: have we been passed a pointer? */
a1f42e84 1118 gdb_assert (changelist);
8b93c638 1119
25d5ea92
VP
1120 /* Frozen means frozen -- we don't check for any change in
1121 this varobj, including its going out of scope, or
1122 changing type. One use case for frozen varobjs is
1123 retaining previously evaluated expressions, and we don't
1124 want them to be reevaluated at all. */
1125 if (!explicit && (*varp)->frozen)
1126 return 0;
8756216b
DP
1127
1128 if (!(*varp)->root->is_valid)
1129 return INVALID;
8b93c638 1130
25d5ea92 1131 if ((*varp)->root->rootvar == *varp)
ae093f96 1132 {
25d5ea92
VP
1133 /* Save the selected stack frame, since we will need to change it
1134 in order to evaluate expressions. */
1135 old_fid = get_frame_id (deprecated_safe_get_selected_frame ());
1136
1137 /* Update the root variable. value_of_root can return NULL
1138 if the variable is no longer around, i.e. we stepped out of
1139 the frame in which a local existed. We are letting the
1140 value_of_root variable dispose of the varobj if the type
1141 has changed. */
1142 type_changed = 1;
1143 new = value_of_root (varp, &type_changed);
1144
1145 /* Restore selected frame. */
1146 fi = frame_find_by_id (old_fid);
1147 if (fi)
1148 select_frame (fi);
1149
1150 /* If this is a "use_selected_frame" varobj, and its type has changed,
1151 them note that it's changed. */
1152 if (type_changed)
1153 VEC_safe_push (varobj_p, result, *varp);
1154
1155 if (install_new_value ((*varp), new, type_changed))
1156 {
1157 /* If type_changed is 1, install_new_value will never return
1158 non-zero, so we'll never report the same variable twice. */
1159 gdb_assert (!type_changed);
1160 VEC_safe_push (varobj_p, result, *varp);
1161 }
8b93c638 1162
25d5ea92
VP
1163 if (new == NULL)
1164 {
1165 /* This means the varobj itself is out of scope.
1166 Report it. */
1167 VEC_free (varobj_p, result);
1168 return NOT_IN_SCOPE;
1169 }
b20d8971
VP
1170 }
1171
28335dcc 1172 VEC_safe_push (varobj_p, stack, *varp);
8b93c638 1173
8756216b 1174 /* Walk through the children, reconstructing them all. */
28335dcc 1175 while (!VEC_empty (varobj_p, stack))
8b93c638 1176 {
28335dcc
VP
1177 v = VEC_pop (varobj_p, stack);
1178
1179 /* Push any children. Use reverse order so that the first
1180 child is popped from the work stack first, and so
1181 will be added to result first. This does not
1182 affect correctness, just "nicer". */
1183 for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i)
8b93c638 1184 {
28335dcc
VP
1185 varobj_p c = VEC_index (varobj_p, v->children, i);
1186 /* Child may be NULL if explicitly deleted by -var-delete. */
25d5ea92 1187 if (c != NULL && !c->frozen)
28335dcc 1188 VEC_safe_push (varobj_p, stack, c);
8b93c638
JM
1189 }
1190
28335dcc
VP
1191 /* Update this variable, unless it's a root, which is already
1192 updated. */
25d5ea92 1193 if (v->root->rootvar != v)
28335dcc
VP
1194 {
1195 new = value_of_child (v->parent, v->index);
1196 if (install_new_value (v, new, 0 /* type not changed */))
1197 {
1198 /* Note that it's changed */
1199 VEC_safe_push (varobj_p, result, v);
1200 v->updated = 0;
1201 }
8b93c638 1202 }
8b93c638
JM
1203 }
1204
8756216b 1205 /* Alloc (changed + 1) list entries. */
28335dcc 1206 changed = VEC_length (varobj_p, result);
8b93c638 1207 *changelist = xmalloc ((changed + 1) * sizeof (struct varobj *));
28335dcc 1208 cv = *changelist;
8b93c638 1209
28335dcc 1210 for (i = 0; i < changed; ++i)
8b93c638 1211 {
28335dcc
VP
1212 *cv = VEC_index (varobj_p, result, i);
1213 gdb_assert (*cv != NULL);
1214 ++cv;
8b93c638 1215 }
28335dcc 1216 *cv = 0;
8b93c638 1217
93b979d6
NR
1218 VEC_free (varobj_p, stack);
1219 VEC_free (varobj_p, result);
1220
73a93a32 1221 if (type_changed)
8756216b 1222 return TYPE_CHANGED;
73a93a32
JI
1223 else
1224 return changed;
8b93c638
JM
1225}
1226\f
1227
1228/* Helper functions */
1229
1230/*
1231 * Variable object construction/destruction
1232 */
1233
1234static int
fba45db2
KB
1235delete_variable (struct cpstack **resultp, struct varobj *var,
1236 int only_children_p)
8b93c638
JM
1237{
1238 int delcount = 0;
1239
1240 delete_variable_1 (resultp, &delcount, var,
1241 only_children_p, 1 /* remove_from_parent_p */ );
1242
1243 return delcount;
1244}
1245
1246/* Delete the variable object VAR and its children */
1247/* IMPORTANT NOTE: If we delete a variable which is a child
1248 and the parent is not removed we dump core. It must be always
1249 initially called with remove_from_parent_p set */
1250static void
72330bd6
AC
1251delete_variable_1 (struct cpstack **resultp, int *delcountp,
1252 struct varobj *var, int only_children_p,
1253 int remove_from_parent_p)
8b93c638 1254{
28335dcc 1255 int i;
8b93c638
JM
1256
1257 /* Delete any children of this variable, too. */
28335dcc
VP
1258 for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
1259 {
1260 varobj_p child = VEC_index (varobj_p, var->children, i);
214270ab
VP
1261 if (!child)
1262 continue;
8b93c638 1263 if (!remove_from_parent_p)
28335dcc
VP
1264 child->parent = NULL;
1265 delete_variable_1 (resultp, delcountp, child, 0, only_children_p);
8b93c638 1266 }
28335dcc 1267 VEC_free (varobj_p, var->children);
8b93c638
JM
1268
1269 /* if we were called to delete only the children we are done here */
1270 if (only_children_p)
1271 return;
1272
1273 /* Otherwise, add it to the list of deleted ones and proceed to do so */
73a93a32
JI
1274 /* If the name is null, this is a temporary variable, that has not
1275 yet been installed, don't report it, it belongs to the caller... */
1276 if (var->obj_name != NULL)
8b93c638 1277 {
5b616ba1 1278 cppush (resultp, xstrdup (var->obj_name));
8b93c638
JM
1279 *delcountp = *delcountp + 1;
1280 }
1281
1282 /* If this variable has a parent, remove it from its parent's list */
1283 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1284 (as indicated by remove_from_parent_p) we don't bother doing an
1285 expensive list search to find the element to remove when we are
1286 discarding the list afterwards */
72330bd6 1287 if ((remove_from_parent_p) && (var->parent != NULL))
8b93c638 1288 {
28335dcc 1289 VEC_replace (varobj_p, var->parent->children, var->index, NULL);
8b93c638 1290 }
72330bd6 1291
73a93a32
JI
1292 if (var->obj_name != NULL)
1293 uninstall_variable (var);
8b93c638
JM
1294
1295 /* Free memory associated with this variable */
1296 free_variable (var);
1297}
1298
1299/* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1300static int
fba45db2 1301install_variable (struct varobj *var)
8b93c638
JM
1302{
1303 struct vlist *cv;
1304 struct vlist *newvl;
1305 const char *chp;
1306 unsigned int index = 0;
1307 unsigned int i = 1;
1308
1309 for (chp = var->obj_name; *chp; chp++)
1310 {
1311 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1312 }
1313
1314 cv = *(varobj_table + index);
1315 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1316 cv = cv->next;
1317
1318 if (cv != NULL)
8a3fe4f8 1319 error (_("Duplicate variable object name"));
8b93c638
JM
1320
1321 /* Add varobj to hash table */
1322 newvl = xmalloc (sizeof (struct vlist));
1323 newvl->next = *(varobj_table + index);
1324 newvl->var = var;
1325 *(varobj_table + index) = newvl;
1326
1327 /* If root, add varobj to root list */
b2c2bd75 1328 if (is_root_p (var))
8b93c638
JM
1329 {
1330 /* Add to list of root variables */
1331 if (rootlist == NULL)
1332 var->root->next = NULL;
1333 else
1334 var->root->next = rootlist;
1335 rootlist = var->root;
1336 rootcount++;
1337 }
1338
1339 return 1; /* OK */
1340}
1341
1342/* Unistall the object VAR. */
1343static void
fba45db2 1344uninstall_variable (struct varobj *var)
8b93c638
JM
1345{
1346 struct vlist *cv;
1347 struct vlist *prev;
1348 struct varobj_root *cr;
1349 struct varobj_root *prer;
1350 const char *chp;
1351 unsigned int index = 0;
1352 unsigned int i = 1;
1353
1354 /* Remove varobj from hash table */
1355 for (chp = var->obj_name; *chp; chp++)
1356 {
1357 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1358 }
1359
1360 cv = *(varobj_table + index);
1361 prev = NULL;
1362 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1363 {
1364 prev = cv;
1365 cv = cv->next;
1366 }
1367
1368 if (varobjdebug)
1369 fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
1370
1371 if (cv == NULL)
1372 {
72330bd6
AC
1373 warning
1374 ("Assertion failed: Could not find variable object \"%s\" to delete",
1375 var->obj_name);
8b93c638
JM
1376 return;
1377 }
1378
1379 if (prev == NULL)
1380 *(varobj_table + index) = cv->next;
1381 else
1382 prev->next = cv->next;
1383
b8c9b27d 1384 xfree (cv);
8b93c638
JM
1385
1386 /* If root, remove varobj from root list */
b2c2bd75 1387 if (is_root_p (var))
8b93c638
JM
1388 {
1389 /* Remove from list of root variables */
1390 if (rootlist == var->root)
1391 rootlist = var->root->next;
1392 else
1393 {
1394 prer = NULL;
1395 cr = rootlist;
1396 while ((cr != NULL) && (cr->rootvar != var))
1397 {
1398 prer = cr;
1399 cr = cr->next;
1400 }
1401 if (cr == NULL)
1402 {
72330bd6
AC
1403 warning
1404 ("Assertion failed: Could not find varobj \"%s\" in root list",
1405 var->obj_name);
8b93c638
JM
1406 return;
1407 }
1408 if (prer == NULL)
1409 rootlist = NULL;
1410 else
1411 prer->next = cr->next;
1412 }
1413 rootcount--;
1414 }
1415
1416}
1417
8b93c638
JM
1418/* Create and install a child of the parent of the given name */
1419static struct varobj *
fba45db2 1420create_child (struct varobj *parent, int index, char *name)
8b93c638
JM
1421{
1422 struct varobj *child;
1423 char *childs_name;
acd65feb 1424 struct value *value;
8b93c638
JM
1425
1426 child = new_variable ();
1427
1428 /* name is allocated by name_of_child */
1429 child->name = name;
1430 child->index = index;
acd65feb 1431 value = value_of_child (parent, index);
8b93c638
JM
1432 child->parent = parent;
1433 child->root = parent->root;
b435e160 1434 childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
8b93c638
JM
1435 child->obj_name = childs_name;
1436 install_variable (child);
1437
acd65feb
VP
1438 /* Compute the type of the child. Must do this before
1439 calling install_new_value. */
1440 if (value != NULL)
1441 /* If the child had no evaluation errors, var->value
1442 will be non-NULL and contain a valid type. */
1443 child->type = value_type (value);
1444 else
1445 /* Otherwise, we must compute the type. */
1446 child->type = (*child->root->lang->type_of_child) (child->parent,
1447 child->index);
1448 install_new_value (child, value, 1);
1449
8b93c638
JM
1450 return child;
1451}
8b93c638
JM
1452\f
1453
1454/*
1455 * Miscellaneous utility functions.
1456 */
1457
1458/* Allocate memory and initialize a new variable */
1459static struct varobj *
1460new_variable (void)
1461{
1462 struct varobj *var;
1463
1464 var = (struct varobj *) xmalloc (sizeof (struct varobj));
1465 var->name = NULL;
02142340 1466 var->path_expr = NULL;
8b93c638
JM
1467 var->obj_name = NULL;
1468 var->index = -1;
1469 var->type = NULL;
1470 var->value = NULL;
8b93c638
JM
1471 var->num_children = -1;
1472 var->parent = NULL;
1473 var->children = NULL;
1474 var->format = 0;
1475 var->root = NULL;
fb9b6b35 1476 var->updated = 0;
85265413 1477 var->print_value = NULL;
25d5ea92
VP
1478 var->frozen = 0;
1479 var->not_fetched = 0;
8b93c638
JM
1480
1481 return var;
1482}
1483
1484/* Allocate memory and initialize a new root variable */
1485static struct varobj *
1486new_root_variable (void)
1487{
1488 struct varobj *var = new_variable ();
1489 var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));;
1490 var->root->lang = NULL;
1491 var->root->exp = NULL;
1492 var->root->valid_block = NULL;
7a424e99 1493 var->root->frame = null_frame_id;
73a93a32 1494 var->root->use_selected_frame = 0;
8b93c638 1495 var->root->rootvar = NULL;
8756216b 1496 var->root->is_valid = 1;
8b93c638
JM
1497
1498 return var;
1499}
1500
1501/* Free any allocated memory associated with VAR. */
1502static void
fba45db2 1503free_variable (struct varobj *var)
8b93c638
JM
1504{
1505 /* Free the expression if this is a root variable. */
b2c2bd75 1506 if (is_root_p (var))
8b93c638 1507 {
96c1eda2 1508 free_current_contents (&var->root->exp);
8038e1e2 1509 xfree (var->root);
8b93c638
JM
1510 }
1511
8038e1e2
AC
1512 xfree (var->name);
1513 xfree (var->obj_name);
85265413 1514 xfree (var->print_value);
02142340 1515 xfree (var->path_expr);
8038e1e2 1516 xfree (var);
8b93c638
JM
1517}
1518
74b7792f
AC
1519static void
1520do_free_variable_cleanup (void *var)
1521{
1522 free_variable (var);
1523}
1524
1525static struct cleanup *
1526make_cleanup_free_variable (struct varobj *var)
1527{
1528 return make_cleanup (do_free_variable_cleanup, var);
1529}
1530
6766a268
DJ
1531/* This returns the type of the variable. It also skips past typedefs
1532 to return the real type of the variable.
94b66fa7
KS
1533
1534 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1535 except within get_target_type and get_type. */
8b93c638 1536static struct type *
fba45db2 1537get_type (struct varobj *var)
8b93c638
JM
1538{
1539 struct type *type;
1540 type = var->type;
1541
6766a268
DJ
1542 if (type != NULL)
1543 type = check_typedef (type);
8b93c638
JM
1544
1545 return type;
1546}
1547
6e2a9270
VP
1548/* Return the type of the value that's stored in VAR,
1549 or that would have being stored there if the
1550 value were accessible.
1551
1552 This differs from VAR->type in that VAR->type is always
1553 the true type of the expession in the source language.
1554 The return value of this function is the type we're
1555 actually storing in varobj, and using for displaying
1556 the values and for comparing previous and new values.
1557
1558 For example, top-level references are always stripped. */
1559static struct type *
1560get_value_type (struct varobj *var)
1561{
1562 struct type *type;
1563
1564 if (var->value)
1565 type = value_type (var->value);
1566 else
1567 type = var->type;
1568
1569 type = check_typedef (type);
1570
1571 if (TYPE_CODE (type) == TYPE_CODE_REF)
1572 type = get_target_type (type);
1573
1574 type = check_typedef (type);
1575
1576 return type;
1577}
1578
8b93c638 1579/* This returns the target type (or NULL) of TYPE, also skipping
94b66fa7
KS
1580 past typedefs, just like get_type ().
1581
1582 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1583 except within get_target_type and get_type. */
8b93c638 1584static struct type *
fba45db2 1585get_target_type (struct type *type)
8b93c638
JM
1586{
1587 if (type != NULL)
1588 {
1589 type = TYPE_TARGET_TYPE (type);
6766a268
DJ
1590 if (type != NULL)
1591 type = check_typedef (type);
8b93c638
JM
1592 }
1593
1594 return type;
1595}
1596
1597/* What is the default display for this variable? We assume that
1598 everything is "natural". Any exceptions? */
1599static enum varobj_display_formats
fba45db2 1600variable_default_display (struct varobj *var)
8b93c638
JM
1601{
1602 return FORMAT_NATURAL;
1603}
1604
8b93c638
JM
1605/* FIXME: The following should be generic for any pointer */
1606static void
fba45db2 1607cppush (struct cpstack **pstack, char *name)
8b93c638
JM
1608{
1609 struct cpstack *s;
1610
1611 s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
1612 s->name = name;
1613 s->next = *pstack;
1614 *pstack = s;
1615}
1616
1617/* FIXME: The following should be generic for any pointer */
1618static char *
fba45db2 1619cppop (struct cpstack **pstack)
8b93c638
JM
1620{
1621 struct cpstack *s;
1622 char *v;
1623
1624 if ((*pstack)->name == NULL && (*pstack)->next == NULL)
1625 return NULL;
1626
1627 s = *pstack;
1628 v = s->name;
1629 *pstack = (*pstack)->next;
b8c9b27d 1630 xfree (s);
8b93c638
JM
1631
1632 return v;
1633}
1634\f
1635/*
1636 * Language-dependencies
1637 */
1638
1639/* Common entry points */
1640
1641/* Get the language of variable VAR. */
1642static enum varobj_languages
fba45db2 1643variable_language (struct varobj *var)
8b93c638
JM
1644{
1645 enum varobj_languages lang;
1646
1647 switch (var->root->exp->language_defn->la_language)
1648 {
1649 default:
1650 case language_c:
1651 lang = vlang_c;
1652 break;
1653 case language_cplus:
1654 lang = vlang_cplus;
1655 break;
1656 case language_java:
1657 lang = vlang_java;
1658 break;
1659 }
1660
1661 return lang;
1662}
1663
1664/* Return the number of children for a given variable.
1665 The result of this function is defined by the language
1666 implementation. The number of children returned by this function
1667 is the number of children that the user will see in the variable
1668 display. */
1669static int
fba45db2 1670number_of_children (struct varobj *var)
8b93c638
JM
1671{
1672 return (*var->root->lang->number_of_children) (var);;
1673}
1674
1675/* What is the expression for the root varobj VAR? Returns a malloc'd string. */
1676static char *
fba45db2 1677name_of_variable (struct varobj *var)
8b93c638
JM
1678{
1679 return (*var->root->lang->name_of_variable) (var);
1680}
1681
1682/* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
1683static char *
fba45db2 1684name_of_child (struct varobj *var, int index)
8b93c638
JM
1685{
1686 return (*var->root->lang->name_of_child) (var, index);
1687}
1688
30b28db1 1689/* What is the ``struct value *'' of the root variable VAR?
73a93a32
JI
1690 TYPE_CHANGED controls what to do if the type of a
1691 use_selected_frame = 1 variable changes. On input,
1692 TYPE_CHANGED = 1 means discard the old varobj, and replace
1693 it with this one. TYPE_CHANGED = 0 means leave it around.
1694 NB: In both cases, var_handle will point to the new varobj,
1695 so if you use TYPE_CHANGED = 0, you will have to stash the
1696 old varobj pointer away somewhere before calling this.
1697 On return, TYPE_CHANGED will be 1 if the type has changed, and
1698 0 otherwise. */
30b28db1 1699static struct value *
fba45db2 1700value_of_root (struct varobj **var_handle, int *type_changed)
8b93c638 1701{
73a93a32
JI
1702 struct varobj *var;
1703
1704 if (var_handle == NULL)
1705 return NULL;
1706
1707 var = *var_handle;
1708
1709 /* This should really be an exception, since this should
1710 only get called with a root variable. */
1711
b2c2bd75 1712 if (!is_root_p (var))
73a93a32
JI
1713 return NULL;
1714
1715 if (var->root->use_selected_frame)
1716 {
1717 struct varobj *tmp_var;
1718 char *old_type, *new_type;
6225abfa 1719
73a93a32
JI
1720 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
1721 USE_SELECTED_FRAME);
1722 if (tmp_var == NULL)
1723 {
1724 return NULL;
1725 }
6225abfa 1726 old_type = varobj_get_type (var);
73a93a32 1727 new_type = varobj_get_type (tmp_var);
72330bd6 1728 if (strcmp (old_type, new_type) == 0)
73a93a32
JI
1729 {
1730 varobj_delete (tmp_var, NULL, 0);
1731 *type_changed = 0;
1732 }
1733 else
1734 {
1735 if (*type_changed)
1736 {
72330bd6 1737 tmp_var->obj_name =
73a93a32 1738 savestring (var->obj_name, strlen (var->obj_name));
f7635dd9 1739 varobj_delete (var, NULL, 0);
73a93a32
JI
1740 }
1741 else
1742 {
72330bd6 1743 tmp_var->obj_name = varobj_gen_name ();
73a93a32
JI
1744 }
1745 install_variable (tmp_var);
1746 *var_handle = tmp_var;
705da579 1747 var = *var_handle;
73a93a32
JI
1748 *type_changed = 1;
1749 }
74dddad3
MS
1750 xfree (old_type);
1751 xfree (new_type);
73a93a32
JI
1752 }
1753 else
1754 {
1755 *type_changed = 0;
1756 }
1757
1758 return (*var->root->lang->value_of_root) (var_handle);
8b93c638
JM
1759}
1760
30b28db1
AC
1761/* What is the ``struct value *'' for the INDEX'th child of PARENT? */
1762static struct value *
fba45db2 1763value_of_child (struct varobj *parent, int index)
8b93c638 1764{
30b28db1 1765 struct value *value;
8b93c638
JM
1766
1767 value = (*parent->root->lang->value_of_child) (parent, index);
1768
8b93c638
JM
1769 return value;
1770}
1771
8b93c638
JM
1772/* GDB already has a command called "value_of_variable". Sigh. */
1773static char *
fba45db2 1774my_value_of_variable (struct varobj *var)
8b93c638 1775{
8756216b
DP
1776 if (var->root->is_valid)
1777 return (*var->root->lang->value_of_variable) (var);
1778 else
1779 return NULL;
8b93c638
JM
1780}
1781
85265413
NR
1782static char *
1783value_get_print_value (struct value *value, enum varobj_display_formats format)
1784{
1785 long dummy;
57e66780
DJ
1786 struct ui_file *stb;
1787 struct cleanup *old_chain;
85265413 1788 char *thevalue;
57e66780
DJ
1789
1790 if (value == NULL)
1791 return NULL;
1792
1793 stb = mem_fileopen ();
1794 old_chain = make_cleanup_ui_file_delete (stb);
1795
85265413
NR
1796 common_val_print (value, stb, format_code[(int) format], 1, 0, 0);
1797 thevalue = ui_file_xstrdup (stb, &dummy);
57e66780 1798
85265413
NR
1799 do_cleanups (old_chain);
1800 return thevalue;
1801}
1802
340a7723
NR
1803int
1804varobj_editable_p (struct varobj *var)
1805{
1806 struct type *type;
1807 struct value *value;
1808
1809 if (!(var->root->is_valid && var->value && VALUE_LVAL (var->value)))
1810 return 0;
1811
1812 type = get_value_type (var);
1813
1814 switch (TYPE_CODE (type))
1815 {
1816 case TYPE_CODE_STRUCT:
1817 case TYPE_CODE_UNION:
1818 case TYPE_CODE_ARRAY:
1819 case TYPE_CODE_FUNC:
1820 case TYPE_CODE_METHOD:
1821 return 0;
1822 break;
1823
1824 default:
1825 return 1;
1826 break;
1827 }
1828}
1829
acd65feb
VP
1830/* Return non-zero if changes in value of VAR
1831 must be detected and reported by -var-update.
1832 Return zero is -var-update should never report
1833 changes of such values. This makes sense for structures
1834 (since the changes in children values will be reported separately),
1835 or for artifical objects (like 'public' pseudo-field in C++).
1836
1837 Return value of 0 means that gdb need not call value_fetch_lazy
1838 for the value of this variable object. */
8b93c638 1839static int
b2c2bd75 1840varobj_value_is_changeable_p (struct varobj *var)
8b93c638
JM
1841{
1842 int r;
1843 struct type *type;
1844
1845 if (CPLUS_FAKE_CHILD (var))
1846 return 0;
1847
6e2a9270 1848 type = get_value_type (var);
8b93c638
JM
1849
1850 switch (TYPE_CODE (type))
1851 {
72330bd6
AC
1852 case TYPE_CODE_STRUCT:
1853 case TYPE_CODE_UNION:
1854 case TYPE_CODE_ARRAY:
1855 r = 0;
1856 break;
8b93c638 1857
72330bd6
AC
1858 default:
1859 r = 1;
8b93c638
JM
1860 }
1861
1862 return r;
1863}
1864
2024f65a
VP
1865/* Given the value and the type of a variable object,
1866 adjust the value and type to those necessary
1867 for getting children of the variable object.
1868 This includes dereferencing top-level references
1869 to all types and dereferencing pointers to
1870 structures.
1871
1872 Both TYPE and *TYPE should be non-null. VALUE
1873 can be null if we want to only translate type.
1874 *VALUE can be null as well -- if the parent
02142340
VP
1875 value is not known.
1876
1877 If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
1878 depending on whether pointer was deferenced
1879 in this function. */
2024f65a
VP
1880static void
1881adjust_value_for_child_access (struct value **value,
02142340
VP
1882 struct type **type,
1883 int *was_ptr)
2024f65a
VP
1884{
1885 gdb_assert (type && *type);
1886
02142340
VP
1887 if (was_ptr)
1888 *was_ptr = 0;
1889
2024f65a
VP
1890 *type = check_typedef (*type);
1891
1892 /* The type of value stored in varobj, that is passed
1893 to us, is already supposed to be
1894 reference-stripped. */
1895
1896 gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
1897
1898 /* Pointers to structures are treated just like
1899 structures when accessing children. Don't
1900 dererences pointers to other types. */
1901 if (TYPE_CODE (*type) == TYPE_CODE_PTR)
1902 {
1903 struct type *target_type = get_target_type (*type);
1904 if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
1905 || TYPE_CODE (target_type) == TYPE_CODE_UNION)
1906 {
1907 if (value && *value)
3f4178d6
DJ
1908 {
1909 int success = gdb_value_ind (*value, value);
1910 if (!success)
1911 *value = NULL;
1912 }
2024f65a 1913 *type = target_type;
02142340
VP
1914 if (was_ptr)
1915 *was_ptr = 1;
2024f65a
VP
1916 }
1917 }
1918
1919 /* The 'get_target_type' function calls check_typedef on
1920 result, so we can immediately check type code. No
1921 need to call check_typedef here. */
1922}
1923
8b93c638
JM
1924/* C */
1925static int
fba45db2 1926c_number_of_children (struct varobj *var)
8b93c638 1927{
2024f65a
VP
1928 struct type *type = get_value_type (var);
1929 int children = 0;
8b93c638 1930 struct type *target;
8b93c638 1931
02142340 1932 adjust_value_for_child_access (NULL, &type, NULL);
8b93c638 1933 target = get_target_type (type);
8b93c638
JM
1934
1935 switch (TYPE_CODE (type))
1936 {
1937 case TYPE_CODE_ARRAY:
1938 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
72330bd6 1939 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED)
8b93c638
JM
1940 children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
1941 else
74a44383
DJ
1942 /* If we don't know how many elements there are, don't display
1943 any. */
1944 children = 0;
8b93c638
JM
1945 break;
1946
1947 case TYPE_CODE_STRUCT:
1948 case TYPE_CODE_UNION:
1949 children = TYPE_NFIELDS (type);
1950 break;
1951
1952 case TYPE_CODE_PTR:
2024f65a
VP
1953 /* The type here is a pointer to non-struct. Typically, pointers
1954 have one child, except for function ptrs, which have no children,
1955 and except for void*, as we don't know what to show.
1956
0755e6c1
FN
1957 We can show char* so we allow it to be dereferenced. If you decide
1958 to test for it, please mind that a little magic is necessary to
1959 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
1960 TYPE_NAME == "char" */
2024f65a
VP
1961 if (TYPE_CODE (target) == TYPE_CODE_FUNC
1962 || TYPE_CODE (target) == TYPE_CODE_VOID)
1963 children = 0;
1964 else
1965 children = 1;
8b93c638
JM
1966 break;
1967
1968 default:
1969 /* Other types have no children */
1970 break;
1971 }
1972
1973 return children;
1974}
1975
1976static char *
fba45db2 1977c_name_of_variable (struct varobj *parent)
8b93c638
JM
1978{
1979 return savestring (parent->name, strlen (parent->name));
1980}
1981
bbec2603
VP
1982/* Return the value of element TYPE_INDEX of a structure
1983 value VALUE. VALUE's type should be a structure,
1984 or union, or a typedef to struct/union.
1985
1986 Returns NULL if getting the value fails. Never throws. */
1987static struct value *
1988value_struct_element_index (struct value *value, int type_index)
8b93c638 1989{
bbec2603
VP
1990 struct value *result = NULL;
1991 volatile struct gdb_exception e;
8b93c638 1992
bbec2603
VP
1993 struct type *type = value_type (value);
1994 type = check_typedef (type);
1995
1996 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
1997 || TYPE_CODE (type) == TYPE_CODE_UNION);
8b93c638 1998
bbec2603
VP
1999 TRY_CATCH (e, RETURN_MASK_ERROR)
2000 {
2001 if (TYPE_FIELD_STATIC (type, type_index))
2002 result = value_static_field (type, type_index);
2003 else
2004 result = value_primitive_field (value, 0, type_index, type);
2005 }
2006 if (e.reason < 0)
2007 {
2008 return NULL;
2009 }
2010 else
2011 {
2012 return result;
2013 }
2014}
2015
2016/* Obtain the information about child INDEX of the variable
2017 object PARENT.
2018 If CNAME is not null, sets *CNAME to the name of the child relative
2019 to the parent.
2020 If CVALUE is not null, sets *CVALUE to the value of the child.
2021 If CTYPE is not null, sets *CTYPE to the type of the child.
2022
2023 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
2024 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
2025 to NULL. */
2026static void
2027c_describe_child (struct varobj *parent, int index,
02142340
VP
2028 char **cname, struct value **cvalue, struct type **ctype,
2029 char **cfull_expression)
bbec2603
VP
2030{
2031 struct value *value = parent->value;
2024f65a 2032 struct type *type = get_value_type (parent);
02142340
VP
2033 char *parent_expression = NULL;
2034 int was_ptr;
bbec2603
VP
2035
2036 if (cname)
2037 *cname = NULL;
2038 if (cvalue)
2039 *cvalue = NULL;
2040 if (ctype)
2041 *ctype = NULL;
02142340
VP
2042 if (cfull_expression)
2043 {
2044 *cfull_expression = NULL;
2045 parent_expression = varobj_get_path_expr (parent);
2046 }
2047 adjust_value_for_child_access (&value, &type, &was_ptr);
bbec2603 2048
8b93c638
JM
2049 switch (TYPE_CODE (type))
2050 {
2051 case TYPE_CODE_ARRAY:
bbec2603
VP
2052 if (cname)
2053 *cname = xstrprintf ("%d", index
2054 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)));
2055
2056 if (cvalue && value)
2057 {
2058 int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
2059 struct value *indval =
2060 value_from_longest (builtin_type_int, (LONGEST) real_index);
2061 gdb_value_subscript (value, indval, cvalue);
2062 }
2063
2064 if (ctype)
2065 *ctype = get_target_type (type);
2066
02142340
VP
2067 if (cfull_expression)
2068 *cfull_expression = xstrprintf ("(%s)[%d]", parent_expression,
2069 index
2070 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)));
2071
2072
8b93c638
JM
2073 break;
2074
2075 case TYPE_CODE_STRUCT:
2076 case TYPE_CODE_UNION:
bbec2603
VP
2077 if (cname)
2078 {
2079 char *string = TYPE_FIELD_NAME (type, index);
2080 *cname = savestring (string, strlen (string));
2081 }
2082
2083 if (cvalue && value)
2084 {
2085 /* For C, varobj index is the same as type index. */
2086 *cvalue = value_struct_element_index (value, index);
2087 }
2088
2089 if (ctype)
2090 *ctype = TYPE_FIELD_TYPE (type, index);
2091
02142340
VP
2092 if (cfull_expression)
2093 {
2094 char *join = was_ptr ? "->" : ".";
2095 *cfull_expression = xstrprintf ("(%s)%s%s", parent_expression, join,
2096 TYPE_FIELD_NAME (type, index));
2097 }
2098
8b93c638
JM
2099 break;
2100
2101 case TYPE_CODE_PTR:
bbec2603
VP
2102 if (cname)
2103 *cname = xstrprintf ("*%s", parent->name);
8b93c638 2104
bbec2603 2105 if (cvalue && value)
3f4178d6
DJ
2106 {
2107 int success = gdb_value_ind (value, cvalue);
2108 if (!success)
2109 *cvalue = NULL;
2110 }
bbec2603 2111
2024f65a
VP
2112 /* Don't use get_target_type because it calls
2113 check_typedef and here, we want to show the true
2114 declared type of the variable. */
bbec2603 2115 if (ctype)
2024f65a 2116 *ctype = TYPE_TARGET_TYPE (type);
02142340
VP
2117
2118 if (cfull_expression)
2119 *cfull_expression = xstrprintf ("*(%s)", parent_expression);
bbec2603 2120
8b93c638
JM
2121 break;
2122
2123 default:
2124 /* This should not happen */
bbec2603
VP
2125 if (cname)
2126 *cname = xstrdup ("???");
02142340
VP
2127 if (cfull_expression)
2128 *cfull_expression = xstrdup ("???");
bbec2603 2129 /* Don't set value and type, we don't know then. */
8b93c638 2130 }
bbec2603 2131}
8b93c638 2132
bbec2603
VP
2133static char *
2134c_name_of_child (struct varobj *parent, int index)
2135{
2136 char *name;
02142340 2137 c_describe_child (parent, index, &name, NULL, NULL, NULL);
8b93c638
JM
2138 return name;
2139}
2140
02142340
VP
2141static char *
2142c_path_expr_of_child (struct varobj *child)
2143{
2144 c_describe_child (child->parent, child->index, NULL, NULL, NULL,
2145 &child->path_expr);
2146 return child->path_expr;
2147}
2148
30b28db1 2149static struct value *
fba45db2 2150c_value_of_root (struct varobj **var_handle)
8b93c638 2151{
5e572bb4 2152 struct value *new_val = NULL;
73a93a32 2153 struct varobj *var = *var_handle;
8b93c638
JM
2154 struct frame_info *fi;
2155 int within_scope;
2156
73a93a32 2157 /* Only root variables can be updated... */
b2c2bd75 2158 if (!is_root_p (var))
73a93a32
JI
2159 /* Not a root var */
2160 return NULL;
2161
72330bd6 2162
8b93c638 2163 /* Determine whether the variable is still around. */
b20d8971 2164 if (var->root->valid_block == NULL || var->root->use_selected_frame)
8b93c638
JM
2165 within_scope = 1;
2166 else
2167 {
e64d9b3d 2168 fi = frame_find_by_id (var->root->frame);
8b93c638
JM
2169 within_scope = fi != NULL;
2170 /* FIXME: select_frame could fail */
d2353924
NR
2171 if (fi)
2172 {
2173 CORE_ADDR pc = get_frame_pc (fi);
2174 if (pc < BLOCK_START (var->root->valid_block) ||
2175 pc >= BLOCK_END (var->root->valid_block))
2176 within_scope = 0;
2d43bda2
NR
2177 else
2178 select_frame (fi);
d2353924 2179 }
8b93c638 2180 }
72330bd6 2181
8b93c638
JM
2182 if (within_scope)
2183 {
73a93a32 2184 /* We need to catch errors here, because if evaluate
85d93f1d
VP
2185 expression fails we want to just return NULL. */
2186 gdb_evaluate_expression (var->root->exp, &new_val);
8b93c638
JM
2187 return new_val;
2188 }
2189
2190 return NULL;
2191}
2192
30b28db1 2193static struct value *
fba45db2 2194c_value_of_child (struct varobj *parent, int index)
8b93c638 2195{
bbec2603 2196 struct value *value = NULL;
02142340 2197 c_describe_child (parent, index, NULL, &value, NULL, NULL);
8b93c638
JM
2198
2199 return value;
2200}
2201
2202static struct type *
fba45db2 2203c_type_of_child (struct varobj *parent, int index)
8b93c638 2204{
bbec2603 2205 struct type *type = NULL;
02142340 2206 c_describe_child (parent, index, NULL, NULL, &type, NULL);
8b93c638
JM
2207 return type;
2208}
2209
8b93c638 2210static char *
fba45db2 2211c_value_of_variable (struct varobj *var)
8b93c638 2212{
14b3d9c9
JB
2213 /* BOGUS: if val_print sees a struct/class, or a reference to one,
2214 it will print out its children instead of "{...}". So we need to
2215 catch that case explicitly. */
2216 struct type *type = get_type (var);
e64d9b3d 2217
14b3d9c9
JB
2218 /* Strip top-level references. */
2219 while (TYPE_CODE (type) == TYPE_CODE_REF)
2220 type = check_typedef (TYPE_TARGET_TYPE (type));
2221
2222 switch (TYPE_CODE (type))
8b93c638
JM
2223 {
2224 case TYPE_CODE_STRUCT:
2225 case TYPE_CODE_UNION:
2226 return xstrdup ("{...}");
2227 /* break; */
2228
2229 case TYPE_CODE_ARRAY:
2230 {
e64d9b3d 2231 char *number;
b435e160 2232 number = xstrprintf ("[%d]", var->num_children);
e64d9b3d 2233 return (number);
8b93c638
JM
2234 }
2235 /* break; */
2236
2237 default:
2238 {
575bbeb6
KS
2239 if (var->value == NULL)
2240 {
2241 /* This can happen if we attempt to get the value of a struct
2242 member when the parent is an invalid pointer. This is an
2243 error condition, so we should tell the caller. */
2244 return NULL;
2245 }
2246 else
2247 {
25d5ea92
VP
2248 if (var->not_fetched && value_lazy (var->value))
2249 /* Frozen variable and no value yet. We don't
2250 implicitly fetch the value. MI response will
2251 use empty string for the value, which is OK. */
2252 return NULL;
2253
b2c2bd75 2254 gdb_assert (varobj_value_is_changeable_p (var));
acd65feb 2255 gdb_assert (!value_lazy (var->value));
c39c8256 2256 return xstrdup (var->print_value);
85265413 2257 }
e64d9b3d 2258 }
8b93c638
JM
2259 }
2260}
2261\f
2262
2263/* C++ */
2264
2265static int
fba45db2 2266cplus_number_of_children (struct varobj *var)
8b93c638
JM
2267{
2268 struct type *type;
2269 int children, dont_know;
2270
2271 dont_know = 1;
2272 children = 0;
2273
2274 if (!CPLUS_FAKE_CHILD (var))
2275 {
2024f65a 2276 type = get_value_type (var);
02142340 2277 adjust_value_for_child_access (NULL, &type, NULL);
8b93c638
JM
2278
2279 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
72330bd6 2280 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
8b93c638
JM
2281 {
2282 int kids[3];
2283
2284 cplus_class_num_children (type, kids);
2285 if (kids[v_public] != 0)
2286 children++;
2287 if (kids[v_private] != 0)
2288 children++;
2289 if (kids[v_protected] != 0)
2290 children++;
2291
2292 /* Add any baseclasses */
2293 children += TYPE_N_BASECLASSES (type);
2294 dont_know = 0;
2295
2296 /* FIXME: save children in var */
2297 }
2298 }
2299 else
2300 {
2301 int kids[3];
2302
2024f65a 2303 type = get_value_type (var->parent);
02142340 2304 adjust_value_for_child_access (NULL, &type, NULL);
8b93c638
JM
2305
2306 cplus_class_num_children (type, kids);
6e382aa3 2307 if (strcmp (var->name, "public") == 0)
8b93c638 2308 children = kids[v_public];
6e382aa3 2309 else if (strcmp (var->name, "private") == 0)
8b93c638
JM
2310 children = kids[v_private];
2311 else
2312 children = kids[v_protected];
2313 dont_know = 0;
2314 }
2315
2316 if (dont_know)
2317 children = c_number_of_children (var);
2318
2319 return children;
2320}
2321
2322/* Compute # of public, private, and protected variables in this class.
2323 That means we need to descend into all baseclasses and find out
2324 how many are there, too. */
2325static void
1669605f 2326cplus_class_num_children (struct type *type, int children[3])
8b93c638
JM
2327{
2328 int i;
2329
2330 children[v_public] = 0;
2331 children[v_private] = 0;
2332 children[v_protected] = 0;
2333
2334 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
2335 {
2336 /* If we have a virtual table pointer, omit it. */
72330bd6 2337 if (TYPE_VPTR_BASETYPE (type) == type && TYPE_VPTR_FIELDNO (type) == i)
8b93c638
JM
2338 continue;
2339
2340 if (TYPE_FIELD_PROTECTED (type, i))
2341 children[v_protected]++;
2342 else if (TYPE_FIELD_PRIVATE (type, i))
2343 children[v_private]++;
2344 else
2345 children[v_public]++;
2346 }
2347}
2348
2349static char *
fba45db2 2350cplus_name_of_variable (struct varobj *parent)
8b93c638
JM
2351{
2352 return c_name_of_variable (parent);
2353}
2354
2024f65a
VP
2355enum accessibility { private_field, protected_field, public_field };
2356
2357/* Check if field INDEX of TYPE has the specified accessibility.
2358 Return 0 if so and 1 otherwise. */
2359static int
2360match_accessibility (struct type *type, int index, enum accessibility acc)
8b93c638 2361{
2024f65a
VP
2362 if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
2363 return 1;
2364 else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
2365 return 1;
2366 else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
2367 && !TYPE_FIELD_PROTECTED (type, index))
2368 return 1;
2369 else
2370 return 0;
2371}
2372
2373static void
2374cplus_describe_child (struct varobj *parent, int index,
02142340
VP
2375 char **cname, struct value **cvalue, struct type **ctype,
2376 char **cfull_expression)
2024f65a 2377{
348144ba 2378 char *name = NULL;
2024f65a 2379 struct value *value;
8b93c638 2380 struct type *type;
02142340
VP
2381 int was_ptr;
2382 char *parent_expression = NULL;
8b93c638 2383
2024f65a
VP
2384 if (cname)
2385 *cname = NULL;
2386 if (cvalue)
2387 *cvalue = NULL;
2388 if (ctype)
2389 *ctype = NULL;
02142340
VP
2390 if (cfull_expression)
2391 *cfull_expression = NULL;
2024f65a 2392
8b93c638
JM
2393 if (CPLUS_FAKE_CHILD (parent))
2394 {
2024f65a
VP
2395 value = parent->parent->value;
2396 type = get_value_type (parent->parent);
02142340
VP
2397 if (cfull_expression)
2398 parent_expression = varobj_get_path_expr (parent->parent);
8b93c638
JM
2399 }
2400 else
2024f65a
VP
2401 {
2402 value = parent->value;
2403 type = get_value_type (parent);
02142340
VP
2404 if (cfull_expression)
2405 parent_expression = varobj_get_path_expr (parent);
2024f65a 2406 }
8b93c638 2407
02142340 2408 adjust_value_for_child_access (&value, &type, &was_ptr);
2024f65a
VP
2409
2410 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
3f4178d6 2411 || TYPE_CODE (type) == TYPE_CODE_UNION)
8b93c638 2412 {
02142340 2413 char *join = was_ptr ? "->" : ".";
8b93c638
JM
2414 if (CPLUS_FAKE_CHILD (parent))
2415 {
6e382aa3
JJ
2416 /* The fields of the class type are ordered as they
2417 appear in the class. We are given an index for a
2418 particular access control type ("public","protected",
2419 or "private"). We must skip over fields that don't
2420 have the access control we are looking for to properly
2421 find the indexed field. */
2422 int type_index = TYPE_N_BASECLASSES (type);
2024f65a 2423 enum accessibility acc = public_field;
6e382aa3 2424 if (strcmp (parent->name, "private") == 0)
2024f65a 2425 acc = private_field;
6e382aa3 2426 else if (strcmp (parent->name, "protected") == 0)
2024f65a
VP
2427 acc = protected_field;
2428
2429 while (index >= 0)
6e382aa3 2430 {
2024f65a
VP
2431 if (TYPE_VPTR_BASETYPE (type) == type
2432 && type_index == TYPE_VPTR_FIELDNO (type))
2433 ; /* ignore vptr */
2434 else if (match_accessibility (type, type_index, acc))
6e382aa3
JJ
2435 --index;
2436 ++type_index;
6e382aa3 2437 }
2024f65a
VP
2438 --type_index;
2439
2440 if (cname)
2441 *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
2442
2443 if (cvalue && value)
2444 *cvalue = value_struct_element_index (value, type_index);
2445
2446 if (ctype)
2447 *ctype = TYPE_FIELD_TYPE (type, type_index);
02142340
VP
2448
2449 if (cfull_expression)
2450 *cfull_expression = xstrprintf ("((%s)%s%s)", parent_expression,
2451 join,
2452 TYPE_FIELD_NAME (type, type_index));
2024f65a
VP
2453 }
2454 else if (index < TYPE_N_BASECLASSES (type))
2455 {
2456 /* This is a baseclass. */
2457 if (cname)
2458 *cname = xstrdup (TYPE_FIELD_NAME (type, index));
2459
2460 if (cvalue && value)
6e382aa3 2461 {
2024f65a 2462 *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
02142340 2463 release_value (*cvalue);
6e382aa3
JJ
2464 }
2465
2024f65a
VP
2466 if (ctype)
2467 {
2468 *ctype = TYPE_FIELD_TYPE (type, index);
2469 }
02142340
VP
2470
2471 if (cfull_expression)
2472 {
2473 char *ptr = was_ptr ? "*" : "";
2474 /* Cast the parent to the base' type. Note that in gdb,
2475 expression like
2476 (Base1)d
2477 will create an lvalue, for all appearences, so we don't
2478 need to use more fancy:
2479 *(Base1*)(&d)
2480 construct. */
2481 *cfull_expression = xstrprintf ("(%s(%s%s) %s)",
2482 ptr,
2483 TYPE_FIELD_NAME (type, index),
2484 ptr,
2485 parent_expression);
2486 }
8b93c638 2487 }
8b93c638
JM
2488 else
2489 {
348144ba 2490 char *access = NULL;
6e382aa3 2491 int children[3];
2024f65a 2492 cplus_class_num_children (type, children);
6e382aa3 2493
8b93c638 2494 /* Everything beyond the baseclasses can
6e382aa3
JJ
2495 only be "public", "private", or "protected"
2496
2497 The special "fake" children are always output by varobj in
2498 this order. So if INDEX == 2, it MUST be "protected". */
8b93c638
JM
2499 index -= TYPE_N_BASECLASSES (type);
2500 switch (index)
2501 {
2502 case 0:
6e382aa3 2503 if (children[v_public] > 0)
2024f65a 2504 access = "public";
6e382aa3 2505 else if (children[v_private] > 0)
2024f65a 2506 access = "private";
6e382aa3 2507 else
2024f65a 2508 access = "protected";
6e382aa3 2509 break;
8b93c638 2510 case 1:
6e382aa3 2511 if (children[v_public] > 0)
8b93c638 2512 {
6e382aa3 2513 if (children[v_private] > 0)
2024f65a 2514 access = "private";
6e382aa3 2515 else
2024f65a 2516 access = "protected";
8b93c638 2517 }
6e382aa3 2518 else if (children[v_private] > 0)
2024f65a 2519 access = "protected";
6e382aa3 2520 break;
8b93c638 2521 case 2:
6e382aa3 2522 /* Must be protected */
2024f65a 2523 access = "protected";
6e382aa3 2524 break;
8b93c638
JM
2525 default:
2526 /* error! */
2527 break;
2528 }
348144ba
MS
2529
2530 gdb_assert (access);
2024f65a
VP
2531 if (cname)
2532 *cname = xstrdup (access);
8b93c638 2533
02142340 2534 /* Value and type and full expression are null here. */
2024f65a 2535 }
8b93c638 2536 }
8b93c638
JM
2537 else
2538 {
02142340 2539 c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression);
2024f65a
VP
2540 }
2541}
8b93c638 2542
2024f65a
VP
2543static char *
2544cplus_name_of_child (struct varobj *parent, int index)
2545{
2546 char *name = NULL;
02142340 2547 cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
8b93c638
JM
2548 return name;
2549}
2550
02142340
VP
2551static char *
2552cplus_path_expr_of_child (struct varobj *child)
2553{
2554 cplus_describe_child (child->parent, child->index, NULL, NULL, NULL,
2555 &child->path_expr);
2556 return child->path_expr;
2557}
2558
30b28db1 2559static struct value *
fba45db2 2560cplus_value_of_root (struct varobj **var_handle)
8b93c638 2561{
73a93a32 2562 return c_value_of_root (var_handle);
8b93c638
JM
2563}
2564
30b28db1 2565static struct value *
fba45db2 2566cplus_value_of_child (struct varobj *parent, int index)
8b93c638 2567{
2024f65a 2568 struct value *value = NULL;
02142340 2569 cplus_describe_child (parent, index, NULL, &value, NULL, NULL);
8b93c638
JM
2570 return value;
2571}
2572
2573static struct type *
fba45db2 2574cplus_type_of_child (struct varobj *parent, int index)
8b93c638 2575{
2024f65a 2576 struct type *type = NULL;
02142340 2577 cplus_describe_child (parent, index, NULL, NULL, &type, NULL);
8b93c638
JM
2578 return type;
2579}
2580
8b93c638 2581static char *
fba45db2 2582cplus_value_of_variable (struct varobj *var)
8b93c638
JM
2583{
2584
2585 /* If we have one of our special types, don't print out
2586 any value. */
2587 if (CPLUS_FAKE_CHILD (var))
2588 return xstrdup ("");
2589
2590 return c_value_of_variable (var);
2591}
2592\f
2593/* Java */
2594
2595static int
fba45db2 2596java_number_of_children (struct varobj *var)
8b93c638
JM
2597{
2598 return cplus_number_of_children (var);
2599}
2600
2601static char *
fba45db2 2602java_name_of_variable (struct varobj *parent)
8b93c638
JM
2603{
2604 char *p, *name;
2605
2606 name = cplus_name_of_variable (parent);
2607 /* If the name has "-" in it, it is because we
2608 needed to escape periods in the name... */
2609 p = name;
2610
2611 while (*p != '\000')
2612 {
2613 if (*p == '-')
2614 *p = '.';
2615 p++;
2616 }
2617
2618 return name;
2619}
2620
2621static char *
fba45db2 2622java_name_of_child (struct varobj *parent, int index)
8b93c638
JM
2623{
2624 char *name, *p;
2625
2626 name = cplus_name_of_child (parent, index);
2627 /* Escape any periods in the name... */
2628 p = name;
2629
2630 while (*p != '\000')
2631 {
2632 if (*p == '.')
2633 *p = '-';
2634 p++;
2635 }
2636
2637 return name;
2638}
2639
02142340
VP
2640static char *
2641java_path_expr_of_child (struct varobj *child)
2642{
2643 return NULL;
2644}
2645
30b28db1 2646static struct value *
fba45db2 2647java_value_of_root (struct varobj **var_handle)
8b93c638 2648{
73a93a32 2649 return cplus_value_of_root (var_handle);
8b93c638
JM
2650}
2651
30b28db1 2652static struct value *
fba45db2 2653java_value_of_child (struct varobj *parent, int index)
8b93c638
JM
2654{
2655 return cplus_value_of_child (parent, index);
2656}
2657
2658static struct type *
fba45db2 2659java_type_of_child (struct varobj *parent, int index)
8b93c638
JM
2660{
2661 return cplus_type_of_child (parent, index);
2662}
2663
8b93c638 2664static char *
fba45db2 2665java_value_of_variable (struct varobj *var)
8b93c638
JM
2666{
2667 return cplus_value_of_variable (var);
2668}
2669\f
2670extern void _initialize_varobj (void);
2671void
2672_initialize_varobj (void)
2673{
2674 int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
2675
2676 varobj_table = xmalloc (sizeof_table);
2677 memset (varobj_table, 0, sizeof_table);
2678
85c07804
AC
2679 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance,
2680 &varobjdebug, _("\
2681Set varobj debugging."), _("\
2682Show varobj debugging."), _("\
2683When non-zero, varobj debugging is enabled."),
2684 NULL,
920d2a44 2685 show_varobjdebug,
85c07804 2686 &setlist, &showlist);
8b93c638 2687}
8756216b
DP
2688
2689/* Invalidate the varobjs that are tied to locals and re-create the ones that
2690 are defined on globals.
2691 Invalidated varobjs will be always printed in_scope="invalid". */
2692void
2693varobj_invalidate (void)
2694{
2695 struct varobj **all_rootvarobj;
2696 struct varobj **varp;
2697
2698 if (varobj_list (&all_rootvarobj) > 0)
2699 {
2700 varp = all_rootvarobj;
2701 while (*varp != NULL)
2702 {
2703 /* global var must be re-evaluated. */
2704 if ((*varp)->root->valid_block == NULL)
2705 {
2706 struct varobj *tmp_var;
2707
2708 /* Try to create a varobj with same expression. If we succeed replace
2709 the old varobj, otherwise invalidate it. */
2710 tmp_var = varobj_create (NULL, (*varp)->name, (CORE_ADDR) 0, USE_CURRENT_FRAME);
2711 if (tmp_var != NULL)
2712 {
2713 tmp_var->obj_name = xstrdup ((*varp)->obj_name);
2714 varobj_delete (*varp, NULL, 0);
2715 install_variable (tmp_var);
2716 }
2717 else
2718 (*varp)->root->is_valid = 0;
2719 }
2720 else /* locals must be invalidated. */
2721 (*varp)->root->is_valid = 0;
2722
2723 varp++;
2724 }
2725 xfree (all_rootvarobj);
2726 }
2727 return;
2728}
This page took 0.910689 seconds and 4 git commands to generate.