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