fix typo in previous commit
[deliverable/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;
e55dccf0 436 struct value *value = NULL;
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))
206415a3 443 fi = deprecated_safe_get_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);
206415a3 490 old_fi = get_selected_frame (NULL);
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 497 if (!gdb_evaluate_expression (var->root->exp, &value))
e55dccf0
VP
498 {
499 /* Error getting the value. Try to at least get the
500 right type. */
501 struct value *type_only_value = evaluate_type (var->root->exp);
502 var->type = value_type (type_only_value);
503 }
504 else
505 var->type = value_type (value);
acd65feb 506
acd65feb 507 install_new_value (var, value, 1 /* Initial assignment */);
8b93c638
JM
508
509 /* Set language info */
510 lang = variable_language (var);
d5d6fca5 511 var->root->lang = &languages[lang];
8b93c638
JM
512
513 /* Set ourselves as our root */
514 var->root->rootvar = var;
515
516 /* Reset the selected frame */
517 if (fi != NULL)
0f7d239c 518 select_frame (old_fi);
8b93c638
JM
519 }
520
73a93a32
JI
521 /* If the variable object name is null, that means this
522 is a temporary variable, so don't install it. */
523
524 if ((var != NULL) && (objname != NULL))
8b93c638
JM
525 {
526 var->obj_name = savestring (objname, strlen (objname));
527
528 /* If a varobj name is duplicated, the install will fail so
529 we must clenup */
530 if (!install_variable (var))
531 {
532 do_cleanups (old_chain);
533 return NULL;
534 }
535 }
536
537 discard_cleanups (old_chain);
538 return var;
539}
540
541/* Generates an unique name that can be used for a varobj */
542
543char *
544varobj_gen_name (void)
545{
546 static int id = 0;
e64d9b3d 547 char *obj_name;
8b93c638
JM
548
549 /* generate a name for this object */
550 id++;
b435e160 551 obj_name = xstrprintf ("var%d", id);
8b93c638 552
e64d9b3d 553 return obj_name;
8b93c638
JM
554}
555
556/* Given an "objname", returns the pointer to the corresponding varobj
557 or NULL if not found */
558
559struct varobj *
560varobj_get_handle (char *objname)
561{
562 struct vlist *cv;
563 const char *chp;
564 unsigned int index = 0;
565 unsigned int i = 1;
566
567 for (chp = objname; *chp; chp++)
568 {
569 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
570 }
571
572 cv = *(varobj_table + index);
573 while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
574 cv = cv->next;
575
576 if (cv == NULL)
8a3fe4f8 577 error (_("Variable object not found"));
8b93c638
JM
578
579 return cv->var;
580}
581
582/* Given the handle, return the name of the object */
583
584char *
585varobj_get_objname (struct varobj *var)
586{
587 return var->obj_name;
588}
589
590/* Given the handle, return the expression represented by the object */
591
592char *
593varobj_get_expression (struct varobj *var)
594{
595 return name_of_variable (var);
596}
597
598/* Deletes a varobj and all its children if only_children == 0,
599 otherwise deletes only the children; returns a malloc'ed list of all the
600 (malloc'ed) names of the variables that have been deleted (NULL terminated) */
601
602int
603varobj_delete (struct varobj *var, char ***dellist, int only_children)
604{
605 int delcount;
606 int mycount;
607 struct cpstack *result = NULL;
608 char **cp;
609
610 /* Initialize a stack for temporary results */
611 cppush (&result, NULL);
612
613 if (only_children)
614 /* Delete only the variable children */
615 delcount = delete_variable (&result, var, 1 /* only the children */ );
616 else
617 /* Delete the variable and all its children */
618 delcount = delete_variable (&result, var, 0 /* parent+children */ );
619
620 /* We may have been asked to return a list of what has been deleted */
621 if (dellist != NULL)
622 {
623 *dellist = xmalloc ((delcount + 1) * sizeof (char *));
624
625 cp = *dellist;
626 mycount = delcount;
627 *cp = cppop (&result);
628 while ((*cp != NULL) && (mycount > 0))
629 {
630 mycount--;
631 cp++;
632 *cp = cppop (&result);
633 }
634
635 if (mycount || (*cp != NULL))
8a3fe4f8 636 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
72330bd6 637 mycount);
8b93c638
JM
638 }
639
640 return delcount;
641}
642
643/* Set/Get variable object display format */
644
645enum varobj_display_formats
646varobj_set_display_format (struct varobj *var,
647 enum varobj_display_formats format)
648{
649 switch (format)
650 {
651 case FORMAT_NATURAL:
652 case FORMAT_BINARY:
653 case FORMAT_DECIMAL:
654 case FORMAT_HEXADECIMAL:
655 case FORMAT_OCTAL:
656 var->format = format;
657 break;
658
659 default:
660 var->format = variable_default_display (var);
661 }
662
663 return var->format;
664}
665
666enum varobj_display_formats
667varobj_get_display_format (struct varobj *var)
668{
669 return var->format;
670}
671
672int
673varobj_get_num_children (struct varobj *var)
674{
675 if (var->num_children == -1)
676 var->num_children = number_of_children (var);
677
678 return var->num_children;
679}
680
681/* Creates a list of the immediate children of a variable object;
682 the return code is the number of such children or -1 on error */
683
684int
685varobj_list_children (struct varobj *var, struct varobj ***childlist)
686{
687 struct varobj *child;
688 char *name;
689 int i;
690
691 /* sanity check: have we been passed a pointer? */
692 if (childlist == NULL)
693 return -1;
694
695 *childlist = NULL;
696
697 if (var->num_children == -1)
698 var->num_children = number_of_children (var);
699
74a44383
DJ
700 /* If that failed, give up. */
701 if (var->num_children == -1)
702 return -1;
703
28335dcc
VP
704 /* If we're called when the list of children is not yet initialized,
705 allocate enough elements in it. */
706 while (VEC_length (varobj_p, var->children) < var->num_children)
707 VEC_safe_push (varobj_p, var->children, NULL);
708
8b93c638
JM
709 /* List of children */
710 *childlist = xmalloc ((var->num_children + 1) * sizeof (struct varobj *));
711
712 for (i = 0; i < var->num_children; i++)
713 {
28335dcc
VP
714 varobj_p existing;
715
8b93c638
JM
716 /* Mark as the end in case we bail out */
717 *((*childlist) + i) = NULL;
718
28335dcc
VP
719 existing = VEC_index (varobj_p, var->children, i);
720
721 if (existing == NULL)
722 {
723 /* Either it's the first call to varobj_list_children for
724 this variable object, and the child was never created,
725 or it was explicitly deleted by the client. */
726 name = name_of_child (var, i);
727 existing = create_child (var, i, name);
728 VEC_replace (varobj_p, var->children, i, existing);
729 }
8b93c638 730
28335dcc 731 *((*childlist) + i) = existing;
8b93c638
JM
732 }
733
734 /* End of list is marked by a NULL pointer */
735 *((*childlist) + i) = NULL;
736
737 return var->num_children;
738}
739
740/* Obtain the type of an object Variable as a string similar to the one gdb
741 prints on the console */
742
743char *
744varobj_get_type (struct varobj *var)
745{
30b28db1 746 struct value *val;
8b93c638
JM
747 struct cleanup *old_chain;
748 struct ui_file *stb;
749 char *thetype;
750 long length;
751
752 /* For the "fake" variables, do not return a type. (It's type is
8756216b
DP
753 NULL, too.)
754 Do not return a type for invalid variables as well. */
755 if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
8b93c638
JM
756 return NULL;
757
758 stb = mem_fileopen ();
759 old_chain = make_cleanup_ui_file_delete (stb);
760
30b28db1 761 /* To print the type, we simply create a zero ``struct value *'' and
8b93c638
JM
762 cast it to our type. We then typeprint this variable. */
763 val = value_zero (var->type, not_lval);
df407dfe 764 type_print (value_type (val), "", stb, -1);
8b93c638
JM
765
766 thetype = ui_file_xstrdup (stb, &length);
767 do_cleanups (old_chain);
768 return thetype;
769}
770
1ecb4ee0
DJ
771/* Obtain the type of an object variable. */
772
773struct type *
774varobj_get_gdb_type (struct varobj *var)
775{
776 return var->type;
777}
778
8b93c638
JM
779enum varobj_languages
780varobj_get_language (struct varobj *var)
781{
782 return variable_language (var);
783}
784
785int
786varobj_get_attributes (struct varobj *var)
787{
788 int attributes = 0;
789
8756216b 790 if (var->root->is_valid && variable_editable (var))
8b93c638
JM
791 /* FIXME: define masks for attributes */
792 attributes |= 0x00000001; /* Editable */
793
794 return attributes;
795}
796
797char *
798varobj_get_value (struct varobj *var)
799{
800 return my_value_of_variable (var);
801}
802
803/* Set the value of an object variable (if it is editable) to the
804 value of the given expression */
805/* Note: Invokes functions that can call error() */
806
807int
808varobj_set_value (struct varobj *var, char *expression)
809{
30b28db1 810 struct value *val;
8b93c638 811 int offset = 0;
a6c442d8 812 int error = 0;
8b93c638
JM
813
814 /* The argument "expression" contains the variable's new value.
815 We need to first construct a legal expression for this -- ugh! */
816 /* Does this cover all the bases? */
817 struct expression *exp;
30b28db1 818 struct value *value;
8b93c638
JM
819 int saved_input_radix = input_radix;
820
b20d8971 821 if (var->value != NULL && variable_editable (var))
8b93c638
JM
822 {
823 char *s = expression;
824 int i;
8b93c638
JM
825
826 input_radix = 10; /* ALWAYS reset to decimal temporarily */
7a24eb7c 827 exp = parse_exp_1 (&s, 0, 0);
8b93c638
JM
828 if (!gdb_evaluate_expression (exp, &value))
829 {
830 /* We cannot proceed without a valid expression. */
8038e1e2 831 xfree (exp);
8b93c638
JM
832 return 0;
833 }
834
acd65feb 835 /* All types that are editable must also be changeable. */
b2c2bd75 836 gdb_assert (varobj_value_is_changeable_p (var));
acd65feb
VP
837
838 /* The value of a changeable variable object must not be lazy. */
839 gdb_assert (!value_lazy (var->value));
840
841 /* Need to coerce the input. We want to check if the
842 value of the variable object will be different
843 after assignment, and the first thing value_assign
844 does is coerce the input.
845 For example, if we are assigning an array to a pointer variable we
846 should compare the pointer with the the array's address, not with the
847 array's content. */
848 value = coerce_array (value);
849
acd65feb
VP
850 /* The new value may be lazy. gdb_value_assign, or
851 rather value_contents, will take care of this.
852 If fetching of the new value will fail, gdb_value_assign
853 with catch the exception. */
575bbeb6 854 if (!gdb_value_assign (var->value, value, &val))
8a1a0112 855 return 0;
b26ed50d 856
ae097835
VP
857 /* If the value has changed, record it, so that next -var-update can
858 report this change. If a variable had a value of '1', we've set it
859 to '333' and then set again to '1', when -var-update will report this
860 variable as changed -- because the first assignment has set the
861 'updated' flag. There's no need to optimize that, because return value
862 of -var-update should be considered an approximation. */
863 var->updated = install_new_value (var, val, 0 /* Compare values. */);
8b93c638
JM
864 input_radix = saved_input_radix;
865 return 1;
866 }
867
868 return 0;
869}
870
871/* Returns a malloc'ed list with all root variable objects */
872int
873varobj_list (struct varobj ***varlist)
874{
875 struct varobj **cv;
876 struct varobj_root *croot;
877 int mycount = rootcount;
878
879 /* Alloc (rootcount + 1) entries for the result */
880 *varlist = xmalloc ((rootcount + 1) * sizeof (struct varobj *));
881
882 cv = *varlist;
883 croot = rootlist;
884 while ((croot != NULL) && (mycount > 0))
885 {
886 *cv = croot->rootvar;
887 mycount--;
888 cv++;
889 croot = croot->next;
890 }
891 /* Mark the end of the list */
892 *cv = NULL;
893
894 if (mycount || (croot != NULL))
72330bd6
AC
895 warning
896 ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)",
897 rootcount, mycount);
8b93c638
JM
898
899 return rootcount;
900}
901
acd65feb
VP
902/* Assign a new value to a variable object. If INITIAL is non-zero,
903 this is the first assignement after the variable object was just
904 created, or changed type. In that case, just assign the value
905 and return 0.
906 Otherwise, assign the value and if type_changeable returns non-zero,
907 find if the new value is different from the current value.
b26ed50d
VP
908 Return 1 if so, and 0 if the values are equal.
909
910 The VALUE parameter should not be released -- the function will
911 take care of releasing it when needed. */
acd65feb
VP
912static int
913install_new_value (struct varobj *var, struct value *value, int initial)
914{
915 int changeable;
916 int need_to_fetch;
917 int changed = 0;
918
acd65feb
VP
919 /* We need to know the varobj's type to decide if the value should
920 be fetched or not. C++ fake children (public/protected/private) don't have
921 a type. */
922 gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
b2c2bd75 923 changeable = varobj_value_is_changeable_p (var);
acd65feb
VP
924 need_to_fetch = changeable;
925
b26ed50d
VP
926 /* We are not interested in the address of references, and given
927 that in C++ a reference is not rebindable, it cannot
928 meaningfully change. So, get hold of the real value. */
929 if (value)
930 {
931 value = coerce_ref (value);
932 release_value (value);
933 }
934
acd65feb
VP
935 if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
936 /* For unions, we need to fetch the value implicitly because
937 of implementation of union member fetch. When gdb
938 creates a value for a field and the value of the enclosing
939 structure is not lazy, it immediately copies the necessary
940 bytes from the enclosing values. If the enclosing value is
941 lazy, the call to value_fetch_lazy on the field will read
942 the data from memory. For unions, that means we'll read the
943 same memory more than once, which is not desirable. So
944 fetch now. */
945 need_to_fetch = 1;
946
947 /* The new value might be lazy. If the type is changeable,
948 that is we'll be comparing values of this type, fetch the
949 value now. Otherwise, on the next update the old value
950 will be lazy, which means we've lost that old value. */
951 if (need_to_fetch && value && value_lazy (value))
952 {
953 if (!gdb_value_fetch_lazy (value))
954 {
acd65feb
VP
955 /* Set the value to NULL, so that for the next -var-update,
956 we don't try to compare the new value with this value,
957 that we couldn't even read. */
958 value = NULL;
959 }
acd65feb
VP
960 }
961
962 /* If the type is changeable, compare the old and the new values.
963 If this is the initial assignment, we don't have any old value
964 to compare with. */
7af9851d 965 if (initial && changeable)
85265413
NR
966 var->print_value = value_get_print_value (value, var->format);
967 else if (changeable)
acd65feb
VP
968 {
969 /* If the value of the varobj was changed by -var-set-value, then the
970 value in the varobj and in the target is the same. However, that value
971 is different from the value that the varobj had after the previous
57e66780 972 -var-update. So need to the varobj as changed. */
acd65feb 973 if (var->updated)
57e66780
DJ
974 {
975 xfree (var->print_value);
976 var->print_value = value_get_print_value (value, var->format);
977 changed = 1;
978 }
acd65feb
VP
979 else
980 {
981 /* Try to compare the values. That requires that both
982 values are non-lazy. */
983
984 /* Quick comparison of NULL values. */
985 if (var->value == NULL && value == NULL)
986 /* Equal. */
987 ;
988 else if (var->value == NULL || value == NULL)
57e66780
DJ
989 {
990 xfree (var->print_value);
991 var->print_value = value_get_print_value (value, var->format);
992 changed = 1;
993 }
acd65feb
VP
994 else
995 {
85265413 996 char *print_value;
acd65feb
VP
997 gdb_assert (!value_lazy (var->value));
998 gdb_assert (!value_lazy (value));
85265413
NR
999 print_value = value_get_print_value (value, var->format);
1000
57e66780 1001 gdb_assert (var->print_value != NULL && print_value != NULL);
85265413
NR
1002 if (strcmp (var->print_value, print_value) != 0)
1003 {
1004 xfree (var->print_value);
1005 var->print_value = print_value;
1006 changed = 1;
1007 }
1008 else
1009 xfree (print_value);
acd65feb
VP
1010 }
1011 }
1012 }
85265413 1013
acd65feb
VP
1014 /* We must always keep the new value, since children depend on it. */
1015 if (var->value != NULL)
1016 value_free (var->value);
1017 var->value = value;
1018 var->updated = 0;
85265413 1019
b26ed50d 1020 gdb_assert (!var->value || value_type (var->value));
acd65feb
VP
1021
1022 return changed;
1023}
acd65feb 1024
8b93c638
JM
1025/* Update the values for a variable and its children. This is a
1026 two-pronged attack. First, re-parse the value for the root's
1027 expression to see if it's changed. Then go all the way
1028 through its children, reconstructing them and noting if they've
1029 changed.
8756216b
DP
1030 Return value:
1031 < 0 for error values, see varobj.h.
1032 Otherwise it is the number of children + parent changed.
8b93c638 1033
705da579
KS
1034 Only root variables can be updated...
1035
1036 NOTE: This function may delete the caller's varobj. If it
8756216b
DP
1037 returns TYPE_CHANGED, then it has done this and VARP will be modified
1038 to point to the new varobj. */
8b93c638
JM
1039
1040int
705da579 1041varobj_update (struct varobj **varp, struct varobj ***changelist)
8b93c638
JM
1042{
1043 int changed = 0;
73a93a32 1044 int type_changed;
8b93c638
JM
1045 int i;
1046 int vleft;
8b93c638
JM
1047 struct varobj *v;
1048 struct varobj **cv;
2c67cb8b 1049 struct varobj **templist = NULL;
30b28db1 1050 struct value *new;
28335dcc
VP
1051 VEC (varobj_p) *stack = NULL;
1052 VEC (varobj_p) *result = NULL;
e64d9b3d
MH
1053 struct frame_id old_fid;
1054 struct frame_info *fi;
8b93c638 1055
8756216b 1056 /* sanity check: have we been passed a pointer? */
a1f42e84 1057 gdb_assert (changelist);
8b93c638 1058
b2c2bd75 1059 if (!is_root_p (*varp))
a1f42e84 1060 error (_("Only root variables can be updated"));
8756216b
DP
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. */
206415a3 1067 old_fid = get_frame_id (deprecated_safe_get_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
93b979d6
NR
1149 VEC_free (varobj_p, stack);
1150 VEC_free (varobj_p, result);
1151
73a93a32 1152 if (type_changed)
8756216b 1153 return TYPE_CHANGED;
73a93a32
JI
1154 else
1155 return changed;
8b93c638
JM
1156}
1157\f
1158
1159/* Helper functions */
1160
1161/*
1162 * Variable object construction/destruction
1163 */
1164
1165static int
fba45db2
KB
1166delete_variable (struct cpstack **resultp, struct varobj *var,
1167 int only_children_p)
8b93c638
JM
1168{
1169 int delcount = 0;
1170
1171 delete_variable_1 (resultp, &delcount, var,
1172 only_children_p, 1 /* remove_from_parent_p */ );
1173
1174 return delcount;
1175}
1176
1177/* Delete the variable object VAR and its children */
1178/* IMPORTANT NOTE: If we delete a variable which is a child
1179 and the parent is not removed we dump core. It must be always
1180 initially called with remove_from_parent_p set */
1181static void
72330bd6
AC
1182delete_variable_1 (struct cpstack **resultp, int *delcountp,
1183 struct varobj *var, int only_children_p,
1184 int remove_from_parent_p)
8b93c638 1185{
28335dcc 1186 int i;
8b93c638
JM
1187
1188 /* Delete any children of this variable, too. */
28335dcc
VP
1189 for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
1190 {
1191 varobj_p child = VEC_index (varobj_p, var->children, i);
8b93c638 1192 if (!remove_from_parent_p)
28335dcc
VP
1193 child->parent = NULL;
1194 delete_variable_1 (resultp, delcountp, child, 0, only_children_p);
8b93c638 1195 }
28335dcc 1196 VEC_free (varobj_p, var->children);
8b93c638
JM
1197
1198 /* if we were called to delete only the children we are done here */
1199 if (only_children_p)
1200 return;
1201
1202 /* Otherwise, add it to the list of deleted ones and proceed to do so */
73a93a32
JI
1203 /* If the name is null, this is a temporary variable, that has not
1204 yet been installed, don't report it, it belongs to the caller... */
1205 if (var->obj_name != NULL)
8b93c638 1206 {
5b616ba1 1207 cppush (resultp, xstrdup (var->obj_name));
8b93c638
JM
1208 *delcountp = *delcountp + 1;
1209 }
1210
1211 /* If this variable has a parent, remove it from its parent's list */
1212 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1213 (as indicated by remove_from_parent_p) we don't bother doing an
1214 expensive list search to find the element to remove when we are
1215 discarding the list afterwards */
72330bd6 1216 if ((remove_from_parent_p) && (var->parent != NULL))
8b93c638 1217 {
28335dcc 1218 VEC_replace (varobj_p, var->parent->children, var->index, NULL);
8b93c638 1219 }
72330bd6 1220
73a93a32
JI
1221 if (var->obj_name != NULL)
1222 uninstall_variable (var);
8b93c638
JM
1223
1224 /* Free memory associated with this variable */
1225 free_variable (var);
1226}
1227
1228/* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1229static int
fba45db2 1230install_variable (struct varobj *var)
8b93c638
JM
1231{
1232 struct vlist *cv;
1233 struct vlist *newvl;
1234 const char *chp;
1235 unsigned int index = 0;
1236 unsigned int i = 1;
1237
1238 for (chp = var->obj_name; *chp; chp++)
1239 {
1240 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1241 }
1242
1243 cv = *(varobj_table + index);
1244 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1245 cv = cv->next;
1246
1247 if (cv != NULL)
8a3fe4f8 1248 error (_("Duplicate variable object name"));
8b93c638
JM
1249
1250 /* Add varobj to hash table */
1251 newvl = xmalloc (sizeof (struct vlist));
1252 newvl->next = *(varobj_table + index);
1253 newvl->var = var;
1254 *(varobj_table + index) = newvl;
1255
1256 /* If root, add varobj to root list */
b2c2bd75 1257 if (is_root_p (var))
8b93c638
JM
1258 {
1259 /* Add to list of root variables */
1260 if (rootlist == NULL)
1261 var->root->next = NULL;
1262 else
1263 var->root->next = rootlist;
1264 rootlist = var->root;
1265 rootcount++;
1266 }
1267
1268 return 1; /* OK */
1269}
1270
1271/* Unistall the object VAR. */
1272static void
fba45db2 1273uninstall_variable (struct varobj *var)
8b93c638
JM
1274{
1275 struct vlist *cv;
1276 struct vlist *prev;
1277 struct varobj_root *cr;
1278 struct varobj_root *prer;
1279 const char *chp;
1280 unsigned int index = 0;
1281 unsigned int i = 1;
1282
1283 /* Remove varobj from hash table */
1284 for (chp = var->obj_name; *chp; chp++)
1285 {
1286 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1287 }
1288
1289 cv = *(varobj_table + index);
1290 prev = NULL;
1291 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1292 {
1293 prev = cv;
1294 cv = cv->next;
1295 }
1296
1297 if (varobjdebug)
1298 fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
1299
1300 if (cv == NULL)
1301 {
72330bd6
AC
1302 warning
1303 ("Assertion failed: Could not find variable object \"%s\" to delete",
1304 var->obj_name);
8b93c638
JM
1305 return;
1306 }
1307
1308 if (prev == NULL)
1309 *(varobj_table + index) = cv->next;
1310 else
1311 prev->next = cv->next;
1312
b8c9b27d 1313 xfree (cv);
8b93c638
JM
1314
1315 /* If root, remove varobj from root list */
b2c2bd75 1316 if (is_root_p (var))
8b93c638
JM
1317 {
1318 /* Remove from list of root variables */
1319 if (rootlist == var->root)
1320 rootlist = var->root->next;
1321 else
1322 {
1323 prer = NULL;
1324 cr = rootlist;
1325 while ((cr != NULL) && (cr->rootvar != var))
1326 {
1327 prer = cr;
1328 cr = cr->next;
1329 }
1330 if (cr == NULL)
1331 {
72330bd6
AC
1332 warning
1333 ("Assertion failed: Could not find varobj \"%s\" in root list",
1334 var->obj_name);
8b93c638
JM
1335 return;
1336 }
1337 if (prer == NULL)
1338 rootlist = NULL;
1339 else
1340 prer->next = cr->next;
1341 }
1342 rootcount--;
1343 }
1344
1345}
1346
8b93c638
JM
1347/* Create and install a child of the parent of the given name */
1348static struct varobj *
fba45db2 1349create_child (struct varobj *parent, int index, char *name)
8b93c638
JM
1350{
1351 struct varobj *child;
1352 char *childs_name;
acd65feb 1353 struct value *value;
8b93c638
JM
1354
1355 child = new_variable ();
1356
1357 /* name is allocated by name_of_child */
1358 child->name = name;
1359 child->index = index;
acd65feb 1360 value = value_of_child (parent, index);
8b93c638
JM
1361 child->parent = parent;
1362 child->root = parent->root;
b435e160 1363 childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
8b93c638
JM
1364 child->obj_name = childs_name;
1365 install_variable (child);
1366
acd65feb
VP
1367 /* Compute the type of the child. Must do this before
1368 calling install_new_value. */
1369 if (value != NULL)
1370 /* If the child had no evaluation errors, var->value
1371 will be non-NULL and contain a valid type. */
1372 child->type = value_type (value);
1373 else
1374 /* Otherwise, we must compute the type. */
1375 child->type = (*child->root->lang->type_of_child) (child->parent,
1376 child->index);
1377 install_new_value (child, value, 1);
1378
8b93c638
JM
1379 return child;
1380}
8b93c638
JM
1381\f
1382
1383/*
1384 * Miscellaneous utility functions.
1385 */
1386
1387/* Allocate memory and initialize a new variable */
1388static struct varobj *
1389new_variable (void)
1390{
1391 struct varobj *var;
1392
1393 var = (struct varobj *) xmalloc (sizeof (struct varobj));
1394 var->name = NULL;
1395 var->obj_name = NULL;
1396 var->index = -1;
1397 var->type = NULL;
1398 var->value = NULL;
8b93c638
JM
1399 var->num_children = -1;
1400 var->parent = NULL;
1401 var->children = NULL;
1402 var->format = 0;
1403 var->root = NULL;
fb9b6b35 1404 var->updated = 0;
85265413 1405 var->print_value = NULL;
8b93c638
JM
1406
1407 return var;
1408}
1409
1410/* Allocate memory and initialize a new root variable */
1411static struct varobj *
1412new_root_variable (void)
1413{
1414 struct varobj *var = new_variable ();
1415 var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));;
1416 var->root->lang = NULL;
1417 var->root->exp = NULL;
1418 var->root->valid_block = NULL;
7a424e99 1419 var->root->frame = null_frame_id;
73a93a32 1420 var->root->use_selected_frame = 0;
8b93c638 1421 var->root->rootvar = NULL;
8756216b 1422 var->root->is_valid = 1;
8b93c638
JM
1423
1424 return var;
1425}
1426
1427/* Free any allocated memory associated with VAR. */
1428static void
fba45db2 1429free_variable (struct varobj *var)
8b93c638
JM
1430{
1431 /* Free the expression if this is a root variable. */
b2c2bd75 1432 if (is_root_p (var))
8b93c638 1433 {
96c1eda2 1434 free_current_contents (&var->root->exp);
8038e1e2 1435 xfree (var->root);
8b93c638
JM
1436 }
1437
8038e1e2
AC
1438 xfree (var->name);
1439 xfree (var->obj_name);
85265413 1440 xfree (var->print_value);
8038e1e2 1441 xfree (var);
8b93c638
JM
1442}
1443
74b7792f
AC
1444static void
1445do_free_variable_cleanup (void *var)
1446{
1447 free_variable (var);
1448}
1449
1450static struct cleanup *
1451make_cleanup_free_variable (struct varobj *var)
1452{
1453 return make_cleanup (do_free_variable_cleanup, var);
1454}
1455
6766a268
DJ
1456/* This returns the type of the variable. It also skips past typedefs
1457 to return the real type of the variable.
94b66fa7
KS
1458
1459 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1460 except within get_target_type and get_type. */
8b93c638 1461static struct type *
fba45db2 1462get_type (struct varobj *var)
8b93c638
JM
1463{
1464 struct type *type;
1465 type = var->type;
1466
6766a268
DJ
1467 if (type != NULL)
1468 type = check_typedef (type);
8b93c638
JM
1469
1470 return type;
1471}
1472
6e2a9270
VP
1473/* Return the type of the value that's stored in VAR,
1474 or that would have being stored there if the
1475 value were accessible.
1476
1477 This differs from VAR->type in that VAR->type is always
1478 the true type of the expession in the source language.
1479 The return value of this function is the type we're
1480 actually storing in varobj, and using for displaying
1481 the values and for comparing previous and new values.
1482
1483 For example, top-level references are always stripped. */
1484static struct type *
1485get_value_type (struct varobj *var)
1486{
1487 struct type *type;
1488
1489 if (var->value)
1490 type = value_type (var->value);
1491 else
1492 type = var->type;
1493
1494 type = check_typedef (type);
1495
1496 if (TYPE_CODE (type) == TYPE_CODE_REF)
1497 type = get_target_type (type);
1498
1499 type = check_typedef (type);
1500
1501 return type;
1502}
1503
8b93c638 1504/* This returns the target type (or NULL) of TYPE, also skipping
94b66fa7
KS
1505 past typedefs, just like get_type ().
1506
1507 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1508 except within get_target_type and get_type. */
8b93c638 1509static struct type *
fba45db2 1510get_target_type (struct type *type)
8b93c638
JM
1511{
1512 if (type != NULL)
1513 {
1514 type = TYPE_TARGET_TYPE (type);
6766a268
DJ
1515 if (type != NULL)
1516 type = check_typedef (type);
8b93c638
JM
1517 }
1518
1519 return type;
1520}
1521
1522/* What is the default display for this variable? We assume that
1523 everything is "natural". Any exceptions? */
1524static enum varobj_display_formats
fba45db2 1525variable_default_display (struct varobj *var)
8b93c638
JM
1526{
1527 return FORMAT_NATURAL;
1528}
1529
8b93c638
JM
1530/* FIXME: The following should be generic for any pointer */
1531static void
fba45db2 1532cppush (struct cpstack **pstack, char *name)
8b93c638
JM
1533{
1534 struct cpstack *s;
1535
1536 s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
1537 s->name = name;
1538 s->next = *pstack;
1539 *pstack = s;
1540}
1541
1542/* FIXME: The following should be generic for any pointer */
1543static char *
fba45db2 1544cppop (struct cpstack **pstack)
8b93c638
JM
1545{
1546 struct cpstack *s;
1547 char *v;
1548
1549 if ((*pstack)->name == NULL && (*pstack)->next == NULL)
1550 return NULL;
1551
1552 s = *pstack;
1553 v = s->name;
1554 *pstack = (*pstack)->next;
b8c9b27d 1555 xfree (s);
8b93c638
JM
1556
1557 return v;
1558}
1559\f
1560/*
1561 * Language-dependencies
1562 */
1563
1564/* Common entry points */
1565
1566/* Get the language of variable VAR. */
1567static enum varobj_languages
fba45db2 1568variable_language (struct varobj *var)
8b93c638
JM
1569{
1570 enum varobj_languages lang;
1571
1572 switch (var->root->exp->language_defn->la_language)
1573 {
1574 default:
1575 case language_c:
1576 lang = vlang_c;
1577 break;
1578 case language_cplus:
1579 lang = vlang_cplus;
1580 break;
1581 case language_java:
1582 lang = vlang_java;
1583 break;
1584 }
1585
1586 return lang;
1587}
1588
1589/* Return the number of children for a given variable.
1590 The result of this function is defined by the language
1591 implementation. The number of children returned by this function
1592 is the number of children that the user will see in the variable
1593 display. */
1594static int
fba45db2 1595number_of_children (struct varobj *var)
8b93c638
JM
1596{
1597 return (*var->root->lang->number_of_children) (var);;
1598}
1599
1600/* What is the expression for the root varobj VAR? Returns a malloc'd string. */
1601static char *
fba45db2 1602name_of_variable (struct varobj *var)
8b93c638
JM
1603{
1604 return (*var->root->lang->name_of_variable) (var);
1605}
1606
1607/* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
1608static char *
fba45db2 1609name_of_child (struct varobj *var, int index)
8b93c638
JM
1610{
1611 return (*var->root->lang->name_of_child) (var, index);
1612}
1613
30b28db1 1614/* What is the ``struct value *'' of the root variable VAR?
73a93a32
JI
1615 TYPE_CHANGED controls what to do if the type of a
1616 use_selected_frame = 1 variable changes. On input,
1617 TYPE_CHANGED = 1 means discard the old varobj, and replace
1618 it with this one. TYPE_CHANGED = 0 means leave it around.
1619 NB: In both cases, var_handle will point to the new varobj,
1620 so if you use TYPE_CHANGED = 0, you will have to stash the
1621 old varobj pointer away somewhere before calling this.
1622 On return, TYPE_CHANGED will be 1 if the type has changed, and
1623 0 otherwise. */
30b28db1 1624static struct value *
fba45db2 1625value_of_root (struct varobj **var_handle, int *type_changed)
8b93c638 1626{
73a93a32
JI
1627 struct varobj *var;
1628
1629 if (var_handle == NULL)
1630 return NULL;
1631
1632 var = *var_handle;
1633
1634 /* This should really be an exception, since this should
1635 only get called with a root variable. */
1636
b2c2bd75 1637 if (!is_root_p (var))
73a93a32
JI
1638 return NULL;
1639
1640 if (var->root->use_selected_frame)
1641 {
1642 struct varobj *tmp_var;
1643 char *old_type, *new_type;
1644 old_type = varobj_get_type (var);
1645 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
1646 USE_SELECTED_FRAME);
1647 if (tmp_var == NULL)
1648 {
1649 return NULL;
1650 }
1651 new_type = varobj_get_type (tmp_var);
72330bd6 1652 if (strcmp (old_type, new_type) == 0)
73a93a32
JI
1653 {
1654 varobj_delete (tmp_var, NULL, 0);
1655 *type_changed = 0;
1656 }
1657 else
1658 {
1659 if (*type_changed)
1660 {
72330bd6 1661 tmp_var->obj_name =
73a93a32 1662 savestring (var->obj_name, strlen (var->obj_name));
f7635dd9 1663 varobj_delete (var, NULL, 0);
73a93a32
JI
1664 }
1665 else
1666 {
72330bd6 1667 tmp_var->obj_name = varobj_gen_name ();
73a93a32
JI
1668 }
1669 install_variable (tmp_var);
1670 *var_handle = tmp_var;
705da579 1671 var = *var_handle;
73a93a32
JI
1672 *type_changed = 1;
1673 }
1674 }
1675 else
1676 {
1677 *type_changed = 0;
1678 }
1679
1680 return (*var->root->lang->value_of_root) (var_handle);
8b93c638
JM
1681}
1682
30b28db1
AC
1683/* What is the ``struct value *'' for the INDEX'th child of PARENT? */
1684static struct value *
fba45db2 1685value_of_child (struct varobj *parent, int index)
8b93c638 1686{
30b28db1 1687 struct value *value;
8b93c638
JM
1688
1689 value = (*parent->root->lang->value_of_child) (parent, index);
1690
8b93c638
JM
1691 return value;
1692}
1693
8b93c638
JM
1694/* Is this variable editable? Use the variable's type to make
1695 this determination. */
1696static int
fba45db2 1697variable_editable (struct varobj *var)
8b93c638
JM
1698{
1699 return (*var->root->lang->variable_editable) (var);
1700}
1701
1702/* GDB already has a command called "value_of_variable". Sigh. */
1703static char *
fba45db2 1704my_value_of_variable (struct varobj *var)
8b93c638 1705{
8756216b
DP
1706 if (var->root->is_valid)
1707 return (*var->root->lang->value_of_variable) (var);
1708 else
1709 return NULL;
8b93c638
JM
1710}
1711
85265413
NR
1712static char *
1713value_get_print_value (struct value *value, enum varobj_display_formats format)
1714{
1715 long dummy;
57e66780
DJ
1716 struct ui_file *stb;
1717 struct cleanup *old_chain;
85265413 1718 char *thevalue;
57e66780
DJ
1719
1720 if (value == NULL)
1721 return NULL;
1722
1723 stb = mem_fileopen ();
1724 old_chain = make_cleanup_ui_file_delete (stb);
1725
85265413
NR
1726 common_val_print (value, stb, format_code[(int) format], 1, 0, 0);
1727 thevalue = ui_file_xstrdup (stb, &dummy);
57e66780 1728
85265413
NR
1729 do_cleanups (old_chain);
1730 return thevalue;
1731}
1732
acd65feb
VP
1733/* Return non-zero if changes in value of VAR
1734 must be detected and reported by -var-update.
1735 Return zero is -var-update should never report
1736 changes of such values. This makes sense for structures
1737 (since the changes in children values will be reported separately),
1738 or for artifical objects (like 'public' pseudo-field in C++).
1739
1740 Return value of 0 means that gdb need not call value_fetch_lazy
1741 for the value of this variable object. */
8b93c638 1742static int
b2c2bd75 1743varobj_value_is_changeable_p (struct varobj *var)
8b93c638
JM
1744{
1745 int r;
1746 struct type *type;
1747
1748 if (CPLUS_FAKE_CHILD (var))
1749 return 0;
1750
6e2a9270 1751 type = get_value_type (var);
8b93c638
JM
1752
1753 switch (TYPE_CODE (type))
1754 {
72330bd6
AC
1755 case TYPE_CODE_STRUCT:
1756 case TYPE_CODE_UNION:
1757 case TYPE_CODE_ARRAY:
1758 r = 0;
1759 break;
8b93c638 1760
72330bd6
AC
1761 default:
1762 r = 1;
8b93c638
JM
1763 }
1764
1765 return r;
1766}
1767
2024f65a
VP
1768/* Given the value and the type of a variable object,
1769 adjust the value and type to those necessary
1770 for getting children of the variable object.
1771 This includes dereferencing top-level references
1772 to all types and dereferencing pointers to
1773 structures.
1774
1775 Both TYPE and *TYPE should be non-null. VALUE
1776 can be null if we want to only translate type.
1777 *VALUE can be null as well -- if the parent
1778 value is not known. */
1779static void
1780adjust_value_for_child_access (struct value **value,
1781 struct type **type)
1782{
1783 gdb_assert (type && *type);
1784
1785 *type = check_typedef (*type);
1786
1787 /* The type of value stored in varobj, that is passed
1788 to us, is already supposed to be
1789 reference-stripped. */
1790
1791 gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
1792
1793 /* Pointers to structures are treated just like
1794 structures when accessing children. Don't
1795 dererences pointers to other types. */
1796 if (TYPE_CODE (*type) == TYPE_CODE_PTR)
1797 {
1798 struct type *target_type = get_target_type (*type);
1799 if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
1800 || TYPE_CODE (target_type) == TYPE_CODE_UNION)
1801 {
1802 if (value && *value)
1803 gdb_value_ind (*value, value);
1804 *type = target_type;
1805 }
1806 }
1807
1808 /* The 'get_target_type' function calls check_typedef on
1809 result, so we can immediately check type code. No
1810 need to call check_typedef here. */
1811}
1812
8b93c638
JM
1813/* C */
1814static int
fba45db2 1815c_number_of_children (struct varobj *var)
8b93c638 1816{
2024f65a
VP
1817 struct type *type = get_value_type (var);
1818 int children = 0;
8b93c638 1819 struct type *target;
8b93c638 1820
2024f65a 1821 adjust_value_for_child_access (NULL, &type);
8b93c638 1822 target = get_target_type (type);
8b93c638
JM
1823
1824 switch (TYPE_CODE (type))
1825 {
1826 case TYPE_CODE_ARRAY:
1827 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
72330bd6 1828 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED)
8b93c638
JM
1829 children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
1830 else
74a44383
DJ
1831 /* If we don't know how many elements there are, don't display
1832 any. */
1833 children = 0;
8b93c638
JM
1834 break;
1835
1836 case TYPE_CODE_STRUCT:
1837 case TYPE_CODE_UNION:
1838 children = TYPE_NFIELDS (type);
1839 break;
1840
1841 case TYPE_CODE_PTR:
2024f65a
VP
1842 /* The type here is a pointer to non-struct. Typically, pointers
1843 have one child, except for function ptrs, which have no children,
1844 and except for void*, as we don't know what to show.
1845
0755e6c1
FN
1846 We can show char* so we allow it to be dereferenced. If you decide
1847 to test for it, please mind that a little magic is necessary to
1848 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
1849 TYPE_NAME == "char" */
2024f65a
VP
1850 if (TYPE_CODE (target) == TYPE_CODE_FUNC
1851 || TYPE_CODE (target) == TYPE_CODE_VOID)
1852 children = 0;
1853 else
1854 children = 1;
8b93c638
JM
1855 break;
1856
1857 default:
1858 /* Other types have no children */
1859 break;
1860 }
1861
1862 return children;
1863}
1864
1865static char *
fba45db2 1866c_name_of_variable (struct varobj *parent)
8b93c638
JM
1867{
1868 return savestring (parent->name, strlen (parent->name));
1869}
1870
bbec2603
VP
1871/* Return the value of element TYPE_INDEX of a structure
1872 value VALUE. VALUE's type should be a structure,
1873 or union, or a typedef to struct/union.
1874
1875 Returns NULL if getting the value fails. Never throws. */
1876static struct value *
1877value_struct_element_index (struct value *value, int type_index)
8b93c638 1878{
bbec2603
VP
1879 struct value *result = NULL;
1880 volatile struct gdb_exception e;
8b93c638 1881
bbec2603
VP
1882 struct type *type = value_type (value);
1883 type = check_typedef (type);
1884
1885 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
1886 || TYPE_CODE (type) == TYPE_CODE_UNION);
8b93c638 1887
bbec2603
VP
1888 TRY_CATCH (e, RETURN_MASK_ERROR)
1889 {
1890 if (TYPE_FIELD_STATIC (type, type_index))
1891 result = value_static_field (type, type_index);
1892 else
1893 result = value_primitive_field (value, 0, type_index, type);
1894 }
1895 if (e.reason < 0)
1896 {
1897 return NULL;
1898 }
1899 else
1900 {
1901 return result;
1902 }
1903}
1904
1905/* Obtain the information about child INDEX of the variable
1906 object PARENT.
1907 If CNAME is not null, sets *CNAME to the name of the child relative
1908 to the parent.
1909 If CVALUE is not null, sets *CVALUE to the value of the child.
1910 If CTYPE is not null, sets *CTYPE to the type of the child.
1911
1912 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
1913 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
1914 to NULL. */
1915static void
1916c_describe_child (struct varobj *parent, int index,
1917 char **cname, struct value **cvalue, struct type **ctype)
1918{
1919 struct value *value = parent->value;
2024f65a 1920 struct type *type = get_value_type (parent);
bbec2603
VP
1921
1922 if (cname)
1923 *cname = NULL;
1924 if (cvalue)
1925 *cvalue = NULL;
1926 if (ctype)
1927 *ctype = NULL;
1928
2024f65a 1929 adjust_value_for_child_access (&value, &type);
bbec2603 1930
8b93c638
JM
1931 switch (TYPE_CODE (type))
1932 {
1933 case TYPE_CODE_ARRAY:
bbec2603
VP
1934 if (cname)
1935 *cname = xstrprintf ("%d", index
1936 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)));
1937
1938 if (cvalue && value)
1939 {
1940 int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
1941 struct value *indval =
1942 value_from_longest (builtin_type_int, (LONGEST) real_index);
1943 gdb_value_subscript (value, indval, cvalue);
1944 }
1945
1946 if (ctype)
1947 *ctype = get_target_type (type);
1948
8b93c638
JM
1949 break;
1950
1951 case TYPE_CODE_STRUCT:
1952 case TYPE_CODE_UNION:
bbec2603
VP
1953 if (cname)
1954 {
1955 char *string = TYPE_FIELD_NAME (type, index);
1956 *cname = savestring (string, strlen (string));
1957 }
1958
1959 if (cvalue && value)
1960 {
1961 /* For C, varobj index is the same as type index. */
1962 *cvalue = value_struct_element_index (value, index);
1963 }
1964
1965 if (ctype)
1966 *ctype = TYPE_FIELD_TYPE (type, index);
1967
8b93c638
JM
1968 break;
1969
1970 case TYPE_CODE_PTR:
bbec2603
VP
1971 if (cname)
1972 *cname = xstrprintf ("*%s", parent->name);
8b93c638 1973
bbec2603
VP
1974 if (cvalue && value)
1975 gdb_value_ind (value, cvalue);
1976
2024f65a
VP
1977 /* Don't use get_target_type because it calls
1978 check_typedef and here, we want to show the true
1979 declared type of the variable. */
bbec2603 1980 if (ctype)
2024f65a 1981 *ctype = TYPE_TARGET_TYPE (type);
bbec2603 1982
8b93c638
JM
1983 break;
1984
1985 default:
1986 /* This should not happen */
bbec2603
VP
1987 if (cname)
1988 *cname = xstrdup ("???");
1989 /* Don't set value and type, we don't know then. */
8b93c638 1990 }
bbec2603 1991}
8b93c638 1992
bbec2603
VP
1993static char *
1994c_name_of_child (struct varobj *parent, int index)
1995{
1996 char *name;
1997 c_describe_child (parent, index, &name, NULL, NULL);
8b93c638
JM
1998 return name;
1999}
2000
30b28db1 2001static struct value *
fba45db2 2002c_value_of_root (struct varobj **var_handle)
8b93c638 2003{
5e572bb4 2004 struct value *new_val = NULL;
73a93a32 2005 struct varobj *var = *var_handle;
8b93c638
JM
2006 struct frame_info *fi;
2007 int within_scope;
2008
73a93a32 2009 /* Only root variables can be updated... */
b2c2bd75 2010 if (!is_root_p (var))
73a93a32
JI
2011 /* Not a root var */
2012 return NULL;
2013
72330bd6 2014
8b93c638 2015 /* Determine whether the variable is still around. */
b20d8971 2016 if (var->root->valid_block == NULL || var->root->use_selected_frame)
8b93c638
JM
2017 within_scope = 1;
2018 else
2019 {
e64d9b3d 2020 fi = frame_find_by_id (var->root->frame);
8b93c638
JM
2021 within_scope = fi != NULL;
2022 /* FIXME: select_frame could fail */
d2353924
NR
2023 if (fi)
2024 {
2025 CORE_ADDR pc = get_frame_pc (fi);
2026 if (pc < BLOCK_START (var->root->valid_block) ||
2027 pc >= BLOCK_END (var->root->valid_block))
2028 within_scope = 0;
2d43bda2
NR
2029 else
2030 select_frame (fi);
d2353924 2031 }
8b93c638 2032 }
72330bd6 2033
8b93c638
JM
2034 if (within_scope)
2035 {
73a93a32 2036 /* We need to catch errors here, because if evaluate
85d93f1d
VP
2037 expression fails we want to just return NULL. */
2038 gdb_evaluate_expression (var->root->exp, &new_val);
8b93c638
JM
2039 return new_val;
2040 }
2041
2042 return NULL;
2043}
2044
30b28db1 2045static struct value *
fba45db2 2046c_value_of_child (struct varobj *parent, int index)
8b93c638 2047{
bbec2603
VP
2048 struct value *value = NULL;
2049 c_describe_child (parent, index, NULL, &value, NULL);
8b93c638
JM
2050
2051 return value;
2052}
2053
2054static struct type *
fba45db2 2055c_type_of_child (struct varobj *parent, int index)
8b93c638 2056{
bbec2603
VP
2057 struct type *type = NULL;
2058 c_describe_child (parent, index, NULL, NULL, &type);
8b93c638
JM
2059 return type;
2060}
2061
2062static int
fba45db2 2063c_variable_editable (struct varobj *var)
8b93c638 2064{
6e2a9270 2065 switch (TYPE_CODE (get_value_type (var)))
8b93c638
JM
2066 {
2067 case TYPE_CODE_STRUCT:
2068 case TYPE_CODE_UNION:
2069 case TYPE_CODE_ARRAY:
2070 case TYPE_CODE_FUNC:
8b93c638
JM
2071 case TYPE_CODE_METHOD:
2072 return 0;
2073 break;
2074
2075 default:
2076 return 1;
2077 break;
2078 }
2079}
2080
2081static char *
fba45db2 2082c_value_of_variable (struct varobj *var)
8b93c638 2083{
14b3d9c9
JB
2084 /* BOGUS: if val_print sees a struct/class, or a reference to one,
2085 it will print out its children instead of "{...}". So we need to
2086 catch that case explicitly. */
2087 struct type *type = get_type (var);
e64d9b3d 2088
14b3d9c9
JB
2089 /* Strip top-level references. */
2090 while (TYPE_CODE (type) == TYPE_CODE_REF)
2091 type = check_typedef (TYPE_TARGET_TYPE (type));
2092
2093 switch (TYPE_CODE (type))
8b93c638
JM
2094 {
2095 case TYPE_CODE_STRUCT:
2096 case TYPE_CODE_UNION:
2097 return xstrdup ("{...}");
2098 /* break; */
2099
2100 case TYPE_CODE_ARRAY:
2101 {
e64d9b3d 2102 char *number;
b435e160 2103 number = xstrprintf ("[%d]", var->num_children);
e64d9b3d 2104 return (number);
8b93c638
JM
2105 }
2106 /* break; */
2107
2108 default:
2109 {
575bbeb6
KS
2110 if (var->value == NULL)
2111 {
2112 /* This can happen if we attempt to get the value of a struct
2113 member when the parent is an invalid pointer. This is an
2114 error condition, so we should tell the caller. */
2115 return NULL;
2116 }
2117 else
2118 {
b2c2bd75 2119 gdb_assert (varobj_value_is_changeable_p (var));
acd65feb 2120 gdb_assert (!value_lazy (var->value));
85265413
NR
2121 return value_get_print_value (var->value, var->format);
2122 }
e64d9b3d 2123 }
8b93c638
JM
2124 }
2125}
2126\f
2127
2128/* C++ */
2129
2130static int
fba45db2 2131cplus_number_of_children (struct varobj *var)
8b93c638
JM
2132{
2133 struct type *type;
2134 int children, dont_know;
2135
2136 dont_know = 1;
2137 children = 0;
2138
2139 if (!CPLUS_FAKE_CHILD (var))
2140 {
2024f65a
VP
2141 type = get_value_type (var);
2142 adjust_value_for_child_access (NULL, &type);
8b93c638
JM
2143
2144 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
72330bd6 2145 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
8b93c638
JM
2146 {
2147 int kids[3];
2148
2149 cplus_class_num_children (type, kids);
2150 if (kids[v_public] != 0)
2151 children++;
2152 if (kids[v_private] != 0)
2153 children++;
2154 if (kids[v_protected] != 0)
2155 children++;
2156
2157 /* Add any baseclasses */
2158 children += TYPE_N_BASECLASSES (type);
2159 dont_know = 0;
2160
2161 /* FIXME: save children in var */
2162 }
2163 }
2164 else
2165 {
2166 int kids[3];
2167
2024f65a
VP
2168 type = get_value_type (var->parent);
2169 adjust_value_for_child_access (NULL, &type);
8b93c638
JM
2170
2171 cplus_class_num_children (type, kids);
6e382aa3 2172 if (strcmp (var->name, "public") == 0)
8b93c638 2173 children = kids[v_public];
6e382aa3 2174 else if (strcmp (var->name, "private") == 0)
8b93c638
JM
2175 children = kids[v_private];
2176 else
2177 children = kids[v_protected];
2178 dont_know = 0;
2179 }
2180
2181 if (dont_know)
2182 children = c_number_of_children (var);
2183
2184 return children;
2185}
2186
2187/* Compute # of public, private, and protected variables in this class.
2188 That means we need to descend into all baseclasses and find out
2189 how many are there, too. */
2190static void
1669605f 2191cplus_class_num_children (struct type *type, int children[3])
8b93c638
JM
2192{
2193 int i;
2194
2195 children[v_public] = 0;
2196 children[v_private] = 0;
2197 children[v_protected] = 0;
2198
2199 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
2200 {
2201 /* If we have a virtual table pointer, omit it. */
72330bd6 2202 if (TYPE_VPTR_BASETYPE (type) == type && TYPE_VPTR_FIELDNO (type) == i)
8b93c638
JM
2203 continue;
2204
2205 if (TYPE_FIELD_PROTECTED (type, i))
2206 children[v_protected]++;
2207 else if (TYPE_FIELD_PRIVATE (type, i))
2208 children[v_private]++;
2209 else
2210 children[v_public]++;
2211 }
2212}
2213
2214static char *
fba45db2 2215cplus_name_of_variable (struct varobj *parent)
8b93c638
JM
2216{
2217 return c_name_of_variable (parent);
2218}
2219
2024f65a
VP
2220enum accessibility { private_field, protected_field, public_field };
2221
2222/* Check if field INDEX of TYPE has the specified accessibility.
2223 Return 0 if so and 1 otherwise. */
2224static int
2225match_accessibility (struct type *type, int index, enum accessibility acc)
8b93c638 2226{
2024f65a
VP
2227 if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
2228 return 1;
2229 else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
2230 return 1;
2231 else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
2232 && !TYPE_FIELD_PROTECTED (type, index))
2233 return 1;
2234 else
2235 return 0;
2236}
2237
2238static void
2239cplus_describe_child (struct varobj *parent, int index,
2240 char **cname, struct value **cvalue, struct type **ctype)
2241{
2242 char *name = 0;
2243 struct value *value;
8b93c638 2244 struct type *type;
8b93c638 2245
2024f65a
VP
2246 if (cname)
2247 *cname = NULL;
2248 if (cvalue)
2249 *cvalue = NULL;
2250 if (ctype)
2251 *ctype = NULL;
2252
2253
8b93c638
JM
2254 if (CPLUS_FAKE_CHILD (parent))
2255 {
2024f65a
VP
2256 value = parent->parent->value;
2257 type = get_value_type (parent->parent);
8b93c638
JM
2258 }
2259 else
2024f65a
VP
2260 {
2261 value = parent->value;
2262 type = get_value_type (parent);
2263 }
8b93c638 2264
2024f65a
VP
2265 adjust_value_for_child_access (&value, &type);
2266
2267 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
2268 || TYPE_CODE (type) == TYPE_CODE_STRUCT)
8b93c638 2269 {
8b93c638
JM
2270 if (CPLUS_FAKE_CHILD (parent))
2271 {
6e382aa3
JJ
2272 /* The fields of the class type are ordered as they
2273 appear in the class. We are given an index for a
2274 particular access control type ("public","protected",
2275 or "private"). We must skip over fields that don't
2276 have the access control we are looking for to properly
2277 find the indexed field. */
2278 int type_index = TYPE_N_BASECLASSES (type);
2024f65a 2279 enum accessibility acc = public_field;
6e382aa3 2280 if (strcmp (parent->name, "private") == 0)
2024f65a 2281 acc = private_field;
6e382aa3 2282 else if (strcmp (parent->name, "protected") == 0)
2024f65a
VP
2283 acc = protected_field;
2284
2285 while (index >= 0)
6e382aa3 2286 {
2024f65a
VP
2287 if (TYPE_VPTR_BASETYPE (type) == type
2288 && type_index == TYPE_VPTR_FIELDNO (type))
2289 ; /* ignore vptr */
2290 else if (match_accessibility (type, type_index, acc))
6e382aa3
JJ
2291 --index;
2292 ++type_index;
6e382aa3 2293 }
2024f65a
VP
2294 --type_index;
2295
2296 if (cname)
2297 *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
2298
2299 if (cvalue && value)
2300 *cvalue = value_struct_element_index (value, type_index);
2301
2302 if (ctype)
2303 *ctype = TYPE_FIELD_TYPE (type, type_index);
2304 }
2305 else if (index < TYPE_N_BASECLASSES (type))
2306 {
2307 /* This is a baseclass. */
2308 if (cname)
2309 *cname = xstrdup (TYPE_FIELD_NAME (type, index));
2310
2311 if (cvalue && value)
6e382aa3 2312 {
2024f65a 2313 *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
6e382aa3
JJ
2314 }
2315
2024f65a
VP
2316 if (ctype)
2317 {
2318 *ctype = TYPE_FIELD_TYPE (type, index);
2319 }
8b93c638 2320 }
8b93c638
JM
2321 else
2322 {
2024f65a 2323 char *access = 0;
6e382aa3 2324 int children[3];
2024f65a 2325 cplus_class_num_children (type, children);
6e382aa3 2326
8b93c638 2327 /* Everything beyond the baseclasses can
6e382aa3
JJ
2328 only be "public", "private", or "protected"
2329
2330 The special "fake" children are always output by varobj in
2331 this order. So if INDEX == 2, it MUST be "protected". */
8b93c638
JM
2332 index -= TYPE_N_BASECLASSES (type);
2333 switch (index)
2334 {
2335 case 0:
6e382aa3 2336 if (children[v_public] > 0)
2024f65a 2337 access = "public";
6e382aa3 2338 else if (children[v_private] > 0)
2024f65a 2339 access = "private";
6e382aa3 2340 else
2024f65a 2341 access = "protected";
6e382aa3 2342 break;
8b93c638 2343 case 1:
6e382aa3 2344 if (children[v_public] > 0)
8b93c638 2345 {
6e382aa3 2346 if (children[v_private] > 0)
2024f65a 2347 access = "private";
6e382aa3 2348 else
2024f65a 2349 access = "protected";
8b93c638 2350 }
6e382aa3 2351 else if (children[v_private] > 0)
2024f65a 2352 access = "protected";
6e382aa3 2353 break;
8b93c638 2354 case 2:
6e382aa3 2355 /* Must be protected */
2024f65a 2356 access = "protected";
6e382aa3 2357 break;
8b93c638
JM
2358 default:
2359 /* error! */
2360 break;
2361 }
2024f65a
VP
2362
2363 if (cname)
2364 *cname = xstrdup (access);
8b93c638 2365
2024f65a
VP
2366 /* Value and type are null here. */
2367 }
8b93c638 2368 }
8b93c638
JM
2369 else
2370 {
2024f65a
VP
2371 c_describe_child (parent, index, cname, cvalue, ctype);
2372 }
2373}
8b93c638 2374
2024f65a
VP
2375static char *
2376cplus_name_of_child (struct varobj *parent, int index)
2377{
2378 char *name = NULL;
2379 cplus_describe_child (parent, index, &name, NULL, NULL);
8b93c638
JM
2380 return name;
2381}
2382
30b28db1 2383static struct value *
fba45db2 2384cplus_value_of_root (struct varobj **var_handle)
8b93c638 2385{
73a93a32 2386 return c_value_of_root (var_handle);
8b93c638
JM
2387}
2388
30b28db1 2389static struct value *
fba45db2 2390cplus_value_of_child (struct varobj *parent, int index)
8b93c638 2391{
2024f65a
VP
2392 struct value *value = NULL;
2393 cplus_describe_child (parent, index, NULL, &value, NULL);
8b93c638
JM
2394 return value;
2395}
2396
2397static struct type *
fba45db2 2398cplus_type_of_child (struct varobj *parent, int index)
8b93c638 2399{
2024f65a
VP
2400 struct type *type = NULL;
2401 cplus_describe_child (parent, index, NULL, NULL, &type);
8b93c638
JM
2402 return type;
2403}
2404
2405static int
fba45db2 2406cplus_variable_editable (struct varobj *var)
8b93c638
JM
2407{
2408 if (CPLUS_FAKE_CHILD (var))
2409 return 0;
2410
2411 return c_variable_editable (var);
2412}
2413
2414static char *
fba45db2 2415cplus_value_of_variable (struct varobj *var)
8b93c638
JM
2416{
2417
2418 /* If we have one of our special types, don't print out
2419 any value. */
2420 if (CPLUS_FAKE_CHILD (var))
2421 return xstrdup ("");
2422
2423 return c_value_of_variable (var);
2424}
2425\f
2426/* Java */
2427
2428static int
fba45db2 2429java_number_of_children (struct varobj *var)
8b93c638
JM
2430{
2431 return cplus_number_of_children (var);
2432}
2433
2434static char *
fba45db2 2435java_name_of_variable (struct varobj *parent)
8b93c638
JM
2436{
2437 char *p, *name;
2438
2439 name = cplus_name_of_variable (parent);
2440 /* If the name has "-" in it, it is because we
2441 needed to escape periods in the name... */
2442 p = name;
2443
2444 while (*p != '\000')
2445 {
2446 if (*p == '-')
2447 *p = '.';
2448 p++;
2449 }
2450
2451 return name;
2452}
2453
2454static char *
fba45db2 2455java_name_of_child (struct varobj *parent, int index)
8b93c638
JM
2456{
2457 char *name, *p;
2458
2459 name = cplus_name_of_child (parent, index);
2460 /* Escape any periods in the name... */
2461 p = name;
2462
2463 while (*p != '\000')
2464 {
2465 if (*p == '.')
2466 *p = '-';
2467 p++;
2468 }
2469
2470 return name;
2471}
2472
30b28db1 2473static struct value *
fba45db2 2474java_value_of_root (struct varobj **var_handle)
8b93c638 2475{
73a93a32 2476 return cplus_value_of_root (var_handle);
8b93c638
JM
2477}
2478
30b28db1 2479static struct value *
fba45db2 2480java_value_of_child (struct varobj *parent, int index)
8b93c638
JM
2481{
2482 return cplus_value_of_child (parent, index);
2483}
2484
2485static struct type *
fba45db2 2486java_type_of_child (struct varobj *parent, int index)
8b93c638
JM
2487{
2488 return cplus_type_of_child (parent, index);
2489}
2490
2491static int
fba45db2 2492java_variable_editable (struct varobj *var)
8b93c638
JM
2493{
2494 return cplus_variable_editable (var);
2495}
2496
2497static char *
fba45db2 2498java_value_of_variable (struct varobj *var)
8b93c638
JM
2499{
2500 return cplus_value_of_variable (var);
2501}
2502\f
2503extern void _initialize_varobj (void);
2504void
2505_initialize_varobj (void)
2506{
2507 int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
2508
2509 varobj_table = xmalloc (sizeof_table);
2510 memset (varobj_table, 0, sizeof_table);
2511
85c07804
AC
2512 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance,
2513 &varobjdebug, _("\
2514Set varobj debugging."), _("\
2515Show varobj debugging."), _("\
2516When non-zero, varobj debugging is enabled."),
2517 NULL,
920d2a44 2518 show_varobjdebug,
85c07804 2519 &setlist, &showlist);
8b93c638 2520}
8756216b
DP
2521
2522/* Invalidate the varobjs that are tied to locals and re-create the ones that
2523 are defined on globals.
2524 Invalidated varobjs will be always printed in_scope="invalid". */
2525void
2526varobj_invalidate (void)
2527{
2528 struct varobj **all_rootvarobj;
2529 struct varobj **varp;
2530
2531 if (varobj_list (&all_rootvarobj) > 0)
2532 {
2533 varp = all_rootvarobj;
2534 while (*varp != NULL)
2535 {
2536 /* global var must be re-evaluated. */
2537 if ((*varp)->root->valid_block == NULL)
2538 {
2539 struct varobj *tmp_var;
2540
2541 /* Try to create a varobj with same expression. If we succeed replace
2542 the old varobj, otherwise invalidate it. */
2543 tmp_var = varobj_create (NULL, (*varp)->name, (CORE_ADDR) 0, USE_CURRENT_FRAME);
2544 if (tmp_var != NULL)
2545 {
2546 tmp_var->obj_name = xstrdup ((*varp)->obj_name);
2547 varobj_delete (*varp, NULL, 0);
2548 install_variable (tmp_var);
2549 }
2550 else
2551 (*varp)->root->is_valid = 0;
2552 }
2553 else /* locals must be invalidated. */
2554 (*varp)->root->is_valid = 0;
2555
2556 varp++;
2557 }
2558 xfree (all_rootvarobj);
2559 }
2560 return;
2561}
This page took 0.781145 seconds and 4 git commands to generate.