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