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