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