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