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