daily update
[deliverable/binutils-gdb.git] / gdb / varobj.c
CommitLineData
8b93c638 1/* Implementation of the GDB variable objects API.
bc8332bb 2
8acc9f48 3 Copyright (C) 1999-2013 Free Software Foundation, Inc.
8b93c638
JM
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
a9762ec7 7 the Free Software Foundation; either version 3 of the License, or
8b93c638
JM
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
a9762ec7 16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
8b93c638
JM
17
18#include "defs.h"
a6c442d8 19#include "exceptions.h"
8b93c638
JM
20#include "value.h"
21#include "expression.h"
22#include "frame.h"
8b93c638 23#include "language.h"
8b93c638 24#include "gdbcmd.h"
d2353924 25#include "block.h"
79a45b7d 26#include "valprint.h"
a6c442d8
MK
27
28#include "gdb_assert.h"
b66d6d2e 29#include "gdb_string.h"
0cc7d26f 30#include "gdb_regex.h"
8b93c638
JM
31
32#include "varobj.h"
28335dcc 33#include "vec.h"
6208b47d
VP
34#include "gdbthread.h"
35#include "inferior.h"
181875a4
JB
36#include "ada-varobj.h"
37#include "ada-lang.h"
8b93c638 38
b6313243
TT
39#if HAVE_PYTHON
40#include "python/python.h"
41#include "python/python-internal.h"
50389644
PA
42#else
43typedef int PyObject;
b6313243
TT
44#endif
45
85254831
KS
46/* The names of varobjs representing anonymous structs or unions. */
47#define ANONYMOUS_STRUCT_NAME _("<anonymous struct>")
48#define ANONYMOUS_UNION_NAME _("<anonymous union>")
49
8b93c638
JM
50/* Non-zero if we want to see trace of varobj level stuff. */
51
ccce17b0 52unsigned int varobjdebug = 0;
920d2a44
AC
53static void
54show_varobjdebug (struct ui_file *file, int from_tty,
55 struct cmd_list_element *c, const char *value)
56{
57 fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
58}
8b93c638 59
581e13c1 60/* String representations of gdb's format codes. */
8b93c638 61char *varobj_format_string[] =
72330bd6 62 { "natural", "binary", "decimal", "hexadecimal", "octal" };
8b93c638 63
581e13c1 64/* String representations of gdb's known languages. */
c573f273 65char *varobj_language_string[] = { "C", "C++", "Java" };
8b93c638 66
0cc7d26f
TT
67/* True if we want to allow Python-based pretty-printing. */
68static int pretty_printing = 0;
69
70void
71varobj_enable_pretty_printing (void)
72{
73 pretty_printing = 1;
74}
75
8b93c638
JM
76/* Data structures */
77
78/* Every root variable has one of these structures saved in its
581e13c1 79 varobj. Members which must be free'd are noted. */
8b93c638 80struct varobj_root
72330bd6 81{
8b93c638 82
581e13c1 83 /* Alloc'd expression for this parent. */
72330bd6 84 struct expression *exp;
8b93c638 85
581e13c1 86 /* Block for which this expression is valid. */
270140bd 87 const struct block *valid_block;
8b93c638 88
44a67aa7
VP
89 /* The frame for this expression. This field is set iff valid_block is
90 not NULL. */
e64d9b3d 91 struct frame_id frame;
8b93c638 92
c5b48eac 93 /* The thread ID that this varobj_root belong to. This field
581e13c1 94 is only valid if valid_block is not NULL.
c5b48eac
VP
95 When not 0, indicates which thread 'frame' belongs to.
96 When 0, indicates that the thread list was empty when the varobj_root
97 was created. */
98 int thread_id;
99
a5defcdc
VP
100 /* If 1, the -var-update always recomputes the value in the
101 current thread and frame. Otherwise, variable object is
581e13c1 102 always updated in the specific scope/thread/frame. */
a5defcdc 103 int floating;
73a93a32 104
8756216b
DP
105 /* Flag that indicates validity: set to 0 when this varobj_root refers
106 to symbols that do not exist anymore. */
107 int is_valid;
108
581e13c1 109 /* Language info for this variable and its children. */
72330bd6 110 struct language_specific *lang;
8b93c638 111
581e13c1 112 /* The varobj for this root node. */
72330bd6 113 struct varobj *rootvar;
8b93c638 114
72330bd6
AC
115 /* Next root variable */
116 struct varobj_root *next;
117};
8b93c638
JM
118
119/* Every variable in the system has a structure of this type defined
581e13c1
MS
120 for it. This structure holds all information necessary to manipulate
121 a particular object variable. Members which must be freed are noted. */
8b93c638 122struct varobj
72330bd6 123{
8b93c638 124
581e13c1 125 /* Alloc'd name of the variable for this object. If this variable is a
72330bd6 126 child, then this name will be the child's source name.
581e13c1
MS
127 (bar, not foo.bar). */
128 /* NOTE: This is the "expression". */
72330bd6 129 char *name;
8b93c638 130
02142340
VP
131 /* Alloc'd expression for this child. Can be used to create a
132 root variable corresponding to this child. */
133 char *path_expr;
134
581e13c1
MS
135 /* The alloc'd name for this variable's object. This is here for
136 convenience when constructing this object's children. */
72330bd6 137 char *obj_name;
8b93c638 138
581e13c1 139 /* Index of this variable in its parent or -1. */
72330bd6 140 int index;
8b93c638 141
202ddcaa
VP
142 /* The type of this variable. This can be NULL
143 for artifial variable objects -- currently, the "accessibility"
144 variable objects in C++. */
72330bd6 145 struct type *type;
8b93c638 146
b20d8971
VP
147 /* The value of this expression or subexpression. A NULL value
148 indicates there was an error getting this value.
b2c2bd75
VP
149 Invariant: if varobj_value_is_changeable_p (this) is non-zero,
150 the value is either NULL, or not lazy. */
30b28db1 151 struct value *value;
8b93c638 152
581e13c1 153 /* The number of (immediate) children this variable has. */
72330bd6 154 int num_children;
8b93c638 155
581e13c1 156 /* If this object is a child, this points to its immediate parent. */
72330bd6 157 struct varobj *parent;
8b93c638 158
28335dcc
VP
159 /* Children of this object. */
160 VEC (varobj_p) *children;
8b93c638 161
b6313243
TT
162 /* Whether the children of this varobj were requested. This field is
163 used to decide if dynamic varobj should recompute their children.
164 In the event that the frontend never asked for the children, we
165 can avoid that. */
166 int children_requested;
167
581e13c1
MS
168 /* Description of the root variable. Points to root variable for
169 children. */
72330bd6 170 struct varobj_root *root;
8b93c638 171
581e13c1 172 /* The format of the output for this object. */
72330bd6 173 enum varobj_display_formats format;
fb9b6b35 174
581e13c1 175 /* Was this variable updated via a varobj_set_value operation. */
fb9b6b35 176 int updated;
85265413
NR
177
178 /* Last print value. */
179 char *print_value;
25d5ea92
VP
180
181 /* Is this variable frozen. Frozen variables are never implicitly
182 updated by -var-update *
183 or -var-update <direct-or-indirect-parent>. */
184 int frozen;
185
186 /* Is the value of this variable intentionally not fetched? It is
187 not fetched if either the variable is frozen, or any parents is
188 frozen. */
189 int not_fetched;
b6313243 190
0cc7d26f
TT
191 /* Sub-range of children which the MI consumer has requested. If
192 FROM < 0 or TO < 0, means that all children have been
193 requested. */
194 int from;
195 int to;
196
197 /* The pretty-printer constructor. If NULL, then the default
198 pretty-printer will be looked up. If None, then no
199 pretty-printer will be installed. */
200 PyObject *constructor;
201
b6313243
TT
202 /* The pretty-printer that has been constructed. If NULL, then a
203 new printer object is needed, and one will be constructed. */
204 PyObject *pretty_printer;
0cc7d26f
TT
205
206 /* The iterator returned by the printer's 'children' method, or NULL
207 if not available. */
208 PyObject *child_iter;
209
210 /* We request one extra item from the iterator, so that we can
211 report to the caller whether there are more items than we have
212 already reported. However, we don't want to install this value
213 when we read it, because that will mess up future updates. So,
214 we stash it here instead. */
215 PyObject *saved_item;
72330bd6 216};
8b93c638 217
8b93c638 218struct cpstack
72330bd6
AC
219{
220 char *name;
221 struct cpstack *next;
222};
8b93c638
JM
223
224/* A list of varobjs */
225
226struct vlist
72330bd6
AC
227{
228 struct varobj *var;
229 struct vlist *next;
230};
8b93c638
JM
231
232/* Private function prototypes */
233
581e13c1 234/* Helper functions for the above subcommands. */
8b93c638 235
a14ed312 236static int delete_variable (struct cpstack **, struct varobj *, int);
8b93c638 237
a14ed312
KB
238static void delete_variable_1 (struct cpstack **, int *,
239 struct varobj *, int, int);
8b93c638 240
a14ed312 241static int install_variable (struct varobj *);
8b93c638 242
a14ed312 243static void uninstall_variable (struct varobj *);
8b93c638 244
a14ed312 245static struct varobj *create_child (struct varobj *, int, char *);
8b93c638 246
b6313243
TT
247static struct varobj *
248create_child_with_value (struct varobj *parent, int index, const char *name,
249 struct value *value);
250
8b93c638
JM
251/* Utility routines */
252
a14ed312 253static struct varobj *new_variable (void);
8b93c638 254
a14ed312 255static struct varobj *new_root_variable (void);
8b93c638 256
a14ed312 257static void free_variable (struct varobj *var);
8b93c638 258
74b7792f
AC
259static struct cleanup *make_cleanup_free_variable (struct varobj *var);
260
a14ed312 261static struct type *get_type (struct varobj *var);
8b93c638 262
6e2a9270
VP
263static struct type *get_value_type (struct varobj *var);
264
a14ed312 265static struct type *get_target_type (struct type *);
8b93c638 266
a14ed312 267static enum varobj_display_formats variable_default_display (struct varobj *);
8b93c638 268
a14ed312 269static void cppush (struct cpstack **pstack, char *name);
8b93c638 270
a14ed312 271static char *cppop (struct cpstack **pstack);
8b93c638 272
8264ba82
AG
273static int update_type_if_necessary (struct varobj *var,
274 struct value *new_value);
275
acd65feb
VP
276static int install_new_value (struct varobj *var, struct value *value,
277 int initial);
278
581e13c1 279/* Language-specific routines. */
8b93c638 280
a14ed312 281static enum varobj_languages variable_language (struct varobj *var);
8b93c638 282
a14ed312 283static int number_of_children (struct varobj *);
8b93c638 284
a14ed312 285static char *name_of_variable (struct varobj *);
8b93c638 286
a14ed312 287static char *name_of_child (struct varobj *, int);
8b93c638 288
30b28db1 289static struct value *value_of_root (struct varobj **var_handle, int *);
8b93c638 290
30b28db1 291static struct value *value_of_child (struct varobj *parent, int index);
8b93c638 292
de051565
MK
293static char *my_value_of_variable (struct varobj *var,
294 enum varobj_display_formats format);
8b93c638 295
85265413 296static char *value_get_print_value (struct value *value,
b6313243 297 enum varobj_display_formats format,
d452c4bc 298 struct varobj *var);
85265413 299
b2c2bd75
VP
300static int varobj_value_is_changeable_p (struct varobj *var);
301
302static int is_root_p (struct varobj *var);
8b93c638 303
d8b65138
JK
304#if HAVE_PYTHON
305
9a1edae6
PM
306static struct varobj *varobj_add_child (struct varobj *var,
307 const char *name,
308 struct value *value);
b6313243 309
d8b65138
JK
310#endif /* HAVE_PYTHON */
311
d32cafc7
JB
312static int default_value_is_changeable_p (struct varobj *var);
313
8b93c638
JM
314/* C implementation */
315
a14ed312 316static int c_number_of_children (struct varobj *var);
8b93c638 317
a14ed312 318static char *c_name_of_variable (struct varobj *parent);
8b93c638 319
a14ed312 320static char *c_name_of_child (struct varobj *parent, int index);
8b93c638 321
02142340
VP
322static char *c_path_expr_of_child (struct varobj *child);
323
30b28db1 324static struct value *c_value_of_child (struct varobj *parent, int index);
8b93c638 325
a14ed312 326static struct type *c_type_of_child (struct varobj *parent, int index);
8b93c638 327
de051565
MK
328static char *c_value_of_variable (struct varobj *var,
329 enum varobj_display_formats format);
8b93c638
JM
330
331/* C++ implementation */
332
a14ed312 333static int cplus_number_of_children (struct varobj *var);
8b93c638 334
a14ed312 335static void cplus_class_num_children (struct type *type, int children[3]);
8b93c638 336
a14ed312 337static char *cplus_name_of_variable (struct varobj *parent);
8b93c638 338
a14ed312 339static char *cplus_name_of_child (struct varobj *parent, int index);
8b93c638 340
02142340
VP
341static char *cplus_path_expr_of_child (struct varobj *child);
342
30b28db1 343static struct value *cplus_value_of_child (struct varobj *parent, int index);
8b93c638 344
a14ed312 345static struct type *cplus_type_of_child (struct varobj *parent, int index);
8b93c638 346
de051565
MK
347static char *cplus_value_of_variable (struct varobj *var,
348 enum varobj_display_formats format);
8b93c638
JM
349
350/* Java implementation */
351
a14ed312 352static int java_number_of_children (struct varobj *var);
8b93c638 353
a14ed312 354static char *java_name_of_variable (struct varobj *parent);
8b93c638 355
a14ed312 356static char *java_name_of_child (struct varobj *parent, int index);
8b93c638 357
02142340
VP
358static char *java_path_expr_of_child (struct varobj *child);
359
30b28db1 360static struct value *java_value_of_child (struct varobj *parent, int index);
8b93c638 361
a14ed312 362static struct type *java_type_of_child (struct varobj *parent, int index);
8b93c638 363
de051565
MK
364static char *java_value_of_variable (struct varobj *var,
365 enum varobj_display_formats format);
8b93c638 366
40591b7d
JCD
367/* Ada implementation */
368
369static int ada_number_of_children (struct varobj *var);
370
371static char *ada_name_of_variable (struct varobj *parent);
372
373static char *ada_name_of_child (struct varobj *parent, int index);
374
375static char *ada_path_expr_of_child (struct varobj *child);
376
40591b7d
JCD
377static struct value *ada_value_of_child (struct varobj *parent, int index);
378
379static struct type *ada_type_of_child (struct varobj *parent, int index);
380
381static char *ada_value_of_variable (struct varobj *var,
382 enum varobj_display_formats format);
383
d32cafc7
JB
384static int ada_value_is_changeable_p (struct varobj *var);
385
7a290c40
JB
386static int ada_value_has_mutated (struct varobj *var, struct value *new_val,
387 struct type *new_type);
388
8b93c638
JM
389/* The language specific vector */
390
391struct language_specific
72330bd6 392{
581e13c1 393 /* The number of children of PARENT. */
72330bd6 394 int (*number_of_children) (struct varobj * parent);
8b93c638 395
581e13c1 396 /* The name (expression) of a root varobj. */
72330bd6 397 char *(*name_of_variable) (struct varobj * parent);
8b93c638 398
581e13c1 399 /* The name of the INDEX'th child of PARENT. */
72330bd6 400 char *(*name_of_child) (struct varobj * parent, int index);
8b93c638 401
02142340
VP
402 /* Returns the rooted expression of CHILD, which is a variable
403 obtain that has some parent. */
404 char *(*path_expr_of_child) (struct varobj * child);
405
581e13c1 406 /* The ``struct value *'' of the INDEX'th child of PARENT. */
30b28db1 407 struct value *(*value_of_child) (struct varobj * parent, int index);
8b93c638 408
581e13c1 409 /* The type of the INDEX'th child of PARENT. */
72330bd6 410 struct type *(*type_of_child) (struct varobj * parent, int index);
8b93c638 411
581e13c1 412 /* The current value of VAR. */
de051565
MK
413 char *(*value_of_variable) (struct varobj * var,
414 enum varobj_display_formats format);
7a290c40 415
d32cafc7
JB
416 /* Return non-zero if changes in value of VAR must be detected and
417 reported by -var-update. Return zero if -var-update should never
418 report changes of such values. This makes sense for structures
419 (since the changes in children values will be reported separately),
420 or for artifical objects (like 'public' pseudo-field in C++).
421
422 Return value of 0 means that gdb need not call value_fetch_lazy
423 for the value of this variable object. */
424 int (*value_is_changeable_p) (struct varobj *var);
425
7a290c40
JB
426 /* Return nonzero if the type of VAR has mutated.
427
428 VAR's value is still the varobj's previous value, while NEW_VALUE
429 is VAR's new value and NEW_TYPE is the var's new type. NEW_VALUE
430 may be NULL indicating that there is no value available (the varobj
431 may be out of scope, of may be the child of a null pointer, for
432 instance). NEW_TYPE, on the other hand, must never be NULL.
433
434 This function should also be able to assume that var's number of
435 children is set (not < 0).
436
437 Languages where types do not mutate can set this to NULL. */
438 int (*value_has_mutated) (struct varobj *var, struct value *new_value,
439 struct type *new_type);
72330bd6 440};
8b93c638 441
581e13c1 442/* Array of known source language routines. */
d5d6fca5 443static struct language_specific languages[vlang_end] = {
8b93c638
JM
444 /* C */
445 {
72330bd6
AC
446 c_number_of_children,
447 c_name_of_variable,
448 c_name_of_child,
02142340 449 c_path_expr_of_child,
72330bd6
AC
450 c_value_of_child,
451 c_type_of_child,
7a290c40 452 c_value_of_variable,
d32cafc7 453 default_value_is_changeable_p,
7a290c40 454 NULL /* value_has_mutated */}
8b93c638
JM
455 ,
456 /* C++ */
457 {
72330bd6
AC
458 cplus_number_of_children,
459 cplus_name_of_variable,
460 cplus_name_of_child,
02142340 461 cplus_path_expr_of_child,
72330bd6
AC
462 cplus_value_of_child,
463 cplus_type_of_child,
7a290c40 464 cplus_value_of_variable,
d32cafc7 465 default_value_is_changeable_p,
7a290c40 466 NULL /* value_has_mutated */}
8b93c638
JM
467 ,
468 /* Java */
469 {
72330bd6
AC
470 java_number_of_children,
471 java_name_of_variable,
472 java_name_of_child,
02142340 473 java_path_expr_of_child,
72330bd6
AC
474 java_value_of_child,
475 java_type_of_child,
7a290c40 476 java_value_of_variable,
d32cafc7 477 default_value_is_changeable_p,
7a290c40 478 NULL /* value_has_mutated */},
40591b7d
JCD
479 /* Ada */
480 {
40591b7d
JCD
481 ada_number_of_children,
482 ada_name_of_variable,
483 ada_name_of_child,
484 ada_path_expr_of_child,
40591b7d
JCD
485 ada_value_of_child,
486 ada_type_of_child,
7a290c40 487 ada_value_of_variable,
d32cafc7 488 ada_value_is_changeable_p,
7a290c40 489 ada_value_has_mutated}
8b93c638
JM
490};
491
581e13c1 492/* A little convenience enum for dealing with C++/Java. */
8b93c638 493enum vsections
72330bd6
AC
494{
495 v_public = 0, v_private, v_protected
496};
8b93c638
JM
497
498/* Private data */
499
581e13c1 500/* Mappings of varobj_display_formats enums to gdb's format codes. */
72330bd6 501static int format_code[] = { 0, 't', 'd', 'x', 'o' };
8b93c638 502
581e13c1 503/* Header of the list of root variable objects. */
8b93c638 504static struct varobj_root *rootlist;
8b93c638 505
581e13c1
MS
506/* Prime number indicating the number of buckets in the hash table. */
507/* A prime large enough to avoid too many colisions. */
8b93c638
JM
508#define VAROBJ_TABLE_SIZE 227
509
581e13c1 510/* Pointer to the varobj hash table (built at run time). */
8b93c638
JM
511static struct vlist **varobj_table;
512
581e13c1 513/* Is the variable X one of our "fake" children? */
8b93c638
JM
514#define CPLUS_FAKE_CHILD(x) \
515((x) != NULL && (x)->type == NULL && (x)->value == NULL)
516\f
517
518/* API Implementation */
b2c2bd75
VP
519static int
520is_root_p (struct varobj *var)
521{
522 return (var->root->rootvar == var);
523}
8b93c638 524
d452c4bc
UW
525#ifdef HAVE_PYTHON
526/* Helper function to install a Python environment suitable for
527 use during operations on VAR. */
70221824 528static struct cleanup *
d452c4bc
UW
529varobj_ensure_python_env (struct varobj *var)
530{
531 return ensure_python_env (var->root->exp->gdbarch,
532 var->root->exp->language_defn);
533}
534#endif
535
581e13c1 536/* Creates a varobj (not its children). */
8b93c638 537
7d8547c9
AC
538/* Return the full FRAME which corresponds to the given CORE_ADDR
539 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */
540
541static struct frame_info *
542find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
543{
544 struct frame_info *frame = NULL;
545
546 if (frame_addr == (CORE_ADDR) 0)
547 return NULL;
548
9d49bdc2
PA
549 for (frame = get_current_frame ();
550 frame != NULL;
551 frame = get_prev_frame (frame))
7d8547c9 552 {
1fac167a
UW
553 /* The CORE_ADDR we get as argument was parsed from a string GDB
554 output as $fp. This output got truncated to gdbarch_addr_bit.
555 Truncate the frame base address in the same manner before
556 comparing it against our argument. */
557 CORE_ADDR frame_base = get_frame_base_address (frame);
558 int addr_bit = gdbarch_addr_bit (get_frame_arch (frame));
a109c7c1 559
1fac167a
UW
560 if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT))
561 frame_base &= ((CORE_ADDR) 1 << addr_bit) - 1;
562
563 if (frame_base == frame_addr)
7d8547c9
AC
564 return frame;
565 }
9d49bdc2
PA
566
567 return NULL;
7d8547c9
AC
568}
569
8b93c638
JM
570struct varobj *
571varobj_create (char *objname,
72330bd6 572 char *expression, CORE_ADDR frame, enum varobj_type type)
8b93c638
JM
573{
574 struct varobj *var;
8b93c638
JM
575 struct cleanup *old_chain;
576
581e13c1 577 /* Fill out a varobj structure for the (root) variable being constructed. */
8b93c638 578 var = new_root_variable ();
74b7792f 579 old_chain = make_cleanup_free_variable (var);
8b93c638
JM
580
581 if (expression != NULL)
582 {
e4195b40 583 struct frame_info *fi;
35633fef 584 struct frame_id old_id = null_frame_id;
e4195b40 585 struct block *block;
bbc13ae3 586 const char *p;
8b93c638 587 enum varobj_languages lang;
e55dccf0 588 struct value *value = NULL;
8e7b59a5 589 volatile struct gdb_exception except;
1bb9788d 590 CORE_ADDR pc;
8b93c638 591
9d49bdc2
PA
592 /* Parse and evaluate the expression, filling in as much of the
593 variable's data as possible. */
594
595 if (has_stack_frames ())
596 {
581e13c1 597 /* Allow creator to specify context of variable. */
9d49bdc2
PA
598 if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
599 fi = get_selected_frame (NULL);
600 else
601 /* FIXME: cagney/2002-11-23: This code should be doing a
602 lookup using the frame ID and not just the frame's
603 ``address''. This, of course, means an interface
604 change. However, with out that interface change ISAs,
605 such as the ia64 with its two stacks, won't work.
606 Similar goes for the case where there is a frameless
607 function. */
608 fi = find_frame_addr_in_frame_chain (frame);
609 }
8b93c638 610 else
9d49bdc2 611 fi = NULL;
8b93c638 612
581e13c1 613 /* frame = -2 means always use selected frame. */
73a93a32 614 if (type == USE_SELECTED_FRAME)
a5defcdc 615 var->root->floating = 1;
73a93a32 616
1bb9788d 617 pc = 0;
8b93c638
JM
618 block = NULL;
619 if (fi != NULL)
1bb9788d
TT
620 {
621 block = get_frame_block (fi, 0);
622 pc = get_frame_pc (fi);
623 }
8b93c638
JM
624
625 p = expression;
626 innermost_block = NULL;
73a93a32 627 /* Wrap the call to parse expression, so we can
581e13c1 628 return a sensible error. */
8e7b59a5
KS
629 TRY_CATCH (except, RETURN_MASK_ERROR)
630 {
1bb9788d 631 var->root->exp = parse_exp_1 (&p, pc, block, 0);
8e7b59a5
KS
632 }
633
634 if (except.reason < 0)
73a93a32 635 {
f748fb40 636 do_cleanups (old_chain);
73a93a32
JI
637 return NULL;
638 }
8b93c638 639
581e13c1 640 /* Don't allow variables to be created for types. */
608b4967
TT
641 if (var->root->exp->elts[0].opcode == OP_TYPE
642 || var->root->exp->elts[0].opcode == OP_TYPEOF
643 || var->root->exp->elts[0].opcode == OP_DECLTYPE)
8b93c638
JM
644 {
645 do_cleanups (old_chain);
bc8332bb
AC
646 fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
647 " as an expression.\n");
8b93c638
JM
648 return NULL;
649 }
650
651 var->format = variable_default_display (var);
652 var->root->valid_block = innermost_block;
1b36a34b 653 var->name = xstrdup (expression);
02142340 654 /* For a root var, the name and the expr are the same. */
1b36a34b 655 var->path_expr = xstrdup (expression);
8b93c638
JM
656
657 /* When the frame is different from the current frame,
658 we must select the appropriate frame before parsing
659 the expression, otherwise the value will not be current.
581e13c1 660 Since select_frame is so benign, just call it for all cases. */
4e22772d 661 if (innermost_block)
8b93c638 662 {
4e22772d
JK
663 /* User could specify explicit FRAME-ADDR which was not found but
664 EXPRESSION is frame specific and we would not be able to evaluate
665 it correctly next time. With VALID_BLOCK set we must also set
666 FRAME and THREAD_ID. */
667 if (fi == NULL)
668 error (_("Failed to find the specified frame"));
669
7a424e99 670 var->root->frame = get_frame_id (fi);
c5b48eac 671 var->root->thread_id = pid_to_thread_id (inferior_ptid);
35633fef 672 old_id = get_frame_id (get_selected_frame (NULL));
c5b48eac 673 select_frame (fi);
8b93c638
JM
674 }
675
340a7723 676 /* We definitely need to catch errors here.
8b93c638 677 If evaluate_expression succeeds we got the value we wanted.
581e13c1 678 But if it fails, we still go on with a call to evaluate_type(). */
8e7b59a5
KS
679 TRY_CATCH (except, RETURN_MASK_ERROR)
680 {
681 value = evaluate_expression (var->root->exp);
682 }
683
684 if (except.reason < 0)
e55dccf0
VP
685 {
686 /* Error getting the value. Try to at least get the
687 right type. */
688 struct value *type_only_value = evaluate_type (var->root->exp);
a109c7c1 689
e55dccf0
VP
690 var->type = value_type (type_only_value);
691 }
8264ba82
AG
692 else
693 {
694 int real_type_found = 0;
695
696 var->type = value_actual_type (value, 0, &real_type_found);
697 if (real_type_found)
698 value = value_cast (var->type, value);
699 }
acd65feb 700
8b93c638
JM
701 /* Set language info */
702 lang = variable_language (var);
d5d6fca5 703 var->root->lang = &languages[lang];
8b93c638 704
d32cafc7
JB
705 install_new_value (var, value, 1 /* Initial assignment */);
706
581e13c1 707 /* Set ourselves as our root. */
8b93c638
JM
708 var->root->rootvar = var;
709
581e13c1 710 /* Reset the selected frame. */
35633fef
JK
711 if (frame_id_p (old_id))
712 select_frame (frame_find_by_id (old_id));
8b93c638
JM
713 }
714
73a93a32 715 /* If the variable object name is null, that means this
581e13c1 716 is a temporary variable, so don't install it. */
73a93a32
JI
717
718 if ((var != NULL) && (objname != NULL))
8b93c638 719 {
1b36a34b 720 var->obj_name = xstrdup (objname);
8b93c638
JM
721
722 /* If a varobj name is duplicated, the install will fail so
581e13c1 723 we must cleanup. */
8b93c638
JM
724 if (!install_variable (var))
725 {
726 do_cleanups (old_chain);
727 return NULL;
728 }
729 }
730
731 discard_cleanups (old_chain);
732 return var;
733}
734
581e13c1 735/* Generates an unique name that can be used for a varobj. */
8b93c638
JM
736
737char *
738varobj_gen_name (void)
739{
740 static int id = 0;
e64d9b3d 741 char *obj_name;
8b93c638 742
581e13c1 743 /* Generate a name for this object. */
8b93c638 744 id++;
b435e160 745 obj_name = xstrprintf ("var%d", id);
8b93c638 746
e64d9b3d 747 return obj_name;
8b93c638
JM
748}
749
61d8f275
JK
750/* Given an OBJNAME, returns the pointer to the corresponding varobj. Call
751 error if OBJNAME cannot be found. */
8b93c638
JM
752
753struct varobj *
754varobj_get_handle (char *objname)
755{
756 struct vlist *cv;
757 const char *chp;
758 unsigned int index = 0;
759 unsigned int i = 1;
760
761 for (chp = objname; *chp; chp++)
762 {
763 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
764 }
765
766 cv = *(varobj_table + index);
767 while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
768 cv = cv->next;
769
770 if (cv == NULL)
8a3fe4f8 771 error (_("Variable object not found"));
8b93c638
JM
772
773 return cv->var;
774}
775
581e13c1 776/* Given the handle, return the name of the object. */
8b93c638
JM
777
778char *
779varobj_get_objname (struct varobj *var)
780{
781 return var->obj_name;
782}
783
581e13c1 784/* Given the handle, return the expression represented by the object. */
8b93c638
JM
785
786char *
787varobj_get_expression (struct varobj *var)
788{
789 return name_of_variable (var);
790}
791
792/* Deletes a varobj and all its children if only_children == 0,
3e43a32a
MS
793 otherwise deletes only the children; returns a malloc'ed list of
794 all the (malloc'ed) names of the variables that have been deleted
581e13c1 795 (NULL terminated). */
8b93c638
JM
796
797int
798varobj_delete (struct varobj *var, char ***dellist, int only_children)
799{
800 int delcount;
801 int mycount;
802 struct cpstack *result = NULL;
803 char **cp;
804
581e13c1 805 /* Initialize a stack for temporary results. */
8b93c638
JM
806 cppush (&result, NULL);
807
808 if (only_children)
581e13c1 809 /* Delete only the variable children. */
8b93c638
JM
810 delcount = delete_variable (&result, var, 1 /* only the children */ );
811 else
581e13c1 812 /* Delete the variable and all its children. */
8b93c638
JM
813 delcount = delete_variable (&result, var, 0 /* parent+children */ );
814
581e13c1 815 /* We may have been asked to return a list of what has been deleted. */
8b93c638
JM
816 if (dellist != NULL)
817 {
818 *dellist = xmalloc ((delcount + 1) * sizeof (char *));
819
820 cp = *dellist;
821 mycount = delcount;
822 *cp = cppop (&result);
823 while ((*cp != NULL) && (mycount > 0))
824 {
825 mycount--;
826 cp++;
827 *cp = cppop (&result);
828 }
829
830 if (mycount || (*cp != NULL))
8a3fe4f8 831 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
72330bd6 832 mycount);
8b93c638
JM
833 }
834
835 return delcount;
836}
837
d8b65138
JK
838#if HAVE_PYTHON
839
b6313243
TT
840/* Convenience function for varobj_set_visualizer. Instantiate a
841 pretty-printer for a given value. */
842static PyObject *
843instantiate_pretty_printer (PyObject *constructor, struct value *value)
844{
b6313243
TT
845 PyObject *val_obj = NULL;
846 PyObject *printer;
b6313243 847
b6313243 848 val_obj = value_to_value_object (value);
b6313243
TT
849 if (! val_obj)
850 return NULL;
851
852 printer = PyObject_CallFunctionObjArgs (constructor, val_obj, NULL);
853 Py_DECREF (val_obj);
854 return printer;
b6313243
TT
855}
856
d8b65138
JK
857#endif
858
581e13c1 859/* Set/Get variable object display format. */
8b93c638
JM
860
861enum varobj_display_formats
862varobj_set_display_format (struct varobj *var,
863 enum varobj_display_formats format)
864{
865 switch (format)
866 {
867 case FORMAT_NATURAL:
868 case FORMAT_BINARY:
869 case FORMAT_DECIMAL:
870 case FORMAT_HEXADECIMAL:
871 case FORMAT_OCTAL:
872 var->format = format;
873 break;
874
875 default:
876 var->format = variable_default_display (var);
877 }
878
ae7d22a6
VP
879 if (varobj_value_is_changeable_p (var)
880 && var->value && !value_lazy (var->value))
881 {
6c761d9c 882 xfree (var->print_value);
d452c4bc 883 var->print_value = value_get_print_value (var->value, var->format, var);
ae7d22a6
VP
884 }
885
8b93c638
JM
886 return var->format;
887}
888
889enum varobj_display_formats
890varobj_get_display_format (struct varobj *var)
891{
892 return var->format;
893}
894
b6313243
TT
895char *
896varobj_get_display_hint (struct varobj *var)
897{
898 char *result = NULL;
899
900#if HAVE_PYTHON
0646da15
TT
901 struct cleanup *back_to;
902
903 if (!gdb_python_initialized)
904 return NULL;
905
906 back_to = varobj_ensure_python_env (var);
d452c4bc 907
b6313243
TT
908 if (var->pretty_printer)
909 result = gdbpy_get_display_hint (var->pretty_printer);
d452c4bc
UW
910
911 do_cleanups (back_to);
b6313243
TT
912#endif
913
914 return result;
915}
916
0cc7d26f
TT
917/* Return true if the varobj has items after TO, false otherwise. */
918
919int
920varobj_has_more (struct varobj *var, int to)
921{
922 if (VEC_length (varobj_p, var->children) > to)
923 return 1;
924 return ((to == -1 || VEC_length (varobj_p, var->children) == to)
925 && var->saved_item != NULL);
926}
927
c5b48eac
VP
928/* If the variable object is bound to a specific thread, that
929 is its evaluation can always be done in context of a frame
930 inside that thread, returns GDB id of the thread -- which
581e13c1 931 is always positive. Otherwise, returns -1. */
c5b48eac
VP
932int
933varobj_get_thread_id (struct varobj *var)
934{
935 if (var->root->valid_block && var->root->thread_id > 0)
936 return var->root->thread_id;
937 else
938 return -1;
939}
940
25d5ea92
VP
941void
942varobj_set_frozen (struct varobj *var, int frozen)
943{
944 /* When a variable is unfrozen, we don't fetch its value.
945 The 'not_fetched' flag remains set, so next -var-update
946 won't complain.
947
948 We don't fetch the value, because for structures the client
949 should do -var-update anyway. It would be bad to have different
950 client-size logic for structure and other types. */
951 var->frozen = frozen;
952}
953
954int
955varobj_get_frozen (struct varobj *var)
956{
957 return var->frozen;
958}
959
0cc7d26f
TT
960/* A helper function that restricts a range to what is actually
961 available in a VEC. This follows the usual rules for the meaning
962 of FROM and TO -- if either is negative, the entire range is
963 used. */
964
965static void
966restrict_range (VEC (varobj_p) *children, int *from, int *to)
967{
968 if (*from < 0 || *to < 0)
969 {
970 *from = 0;
971 *to = VEC_length (varobj_p, children);
972 }
973 else
974 {
975 if (*from > VEC_length (varobj_p, children))
976 *from = VEC_length (varobj_p, children);
977 if (*to > VEC_length (varobj_p, children))
978 *to = VEC_length (varobj_p, children);
979 if (*from > *to)
980 *from = *to;
981 }
982}
983
d8b65138
JK
984#if HAVE_PYTHON
985
0cc7d26f
TT
986/* A helper for update_dynamic_varobj_children that installs a new
987 child when needed. */
988
989static void
990install_dynamic_child (struct varobj *var,
991 VEC (varobj_p) **changed,
8264ba82 992 VEC (varobj_p) **type_changed,
0cc7d26f
TT
993 VEC (varobj_p) **new,
994 VEC (varobj_p) **unchanged,
995 int *cchanged,
996 int index,
997 const char *name,
998 struct value *value)
999{
1000 if (VEC_length (varobj_p, var->children) < index + 1)
1001 {
1002 /* There's no child yet. */
1003 struct varobj *child = varobj_add_child (var, name, value);
a109c7c1 1004
0cc7d26f
TT
1005 if (new)
1006 {
1007 VEC_safe_push (varobj_p, *new, child);
1008 *cchanged = 1;
1009 }
1010 }
bf8793bb 1011 else
0cc7d26f
TT
1012 {
1013 varobj_p existing = VEC_index (varobj_p, var->children, index);
8264ba82 1014 int type_updated = update_type_if_necessary (existing, value);
bf8793bb 1015
8264ba82
AG
1016 if (type_updated)
1017 {
1018 if (type_changed)
1019 VEC_safe_push (varobj_p, *type_changed, existing);
1020 }
0cc7d26f
TT
1021 if (install_new_value (existing, value, 0))
1022 {
8264ba82 1023 if (!type_updated && changed)
0cc7d26f
TT
1024 VEC_safe_push (varobj_p, *changed, existing);
1025 }
8264ba82 1026 else if (!type_updated && unchanged)
0cc7d26f
TT
1027 VEC_safe_push (varobj_p, *unchanged, existing);
1028 }
1029}
1030
0cc7d26f
TT
1031static int
1032dynamic_varobj_has_child_method (struct varobj *var)
1033{
1034 struct cleanup *back_to;
1035 PyObject *printer = var->pretty_printer;
1036 int result;
1037
0646da15
TT
1038 if (!gdb_python_initialized)
1039 return 0;
1040
0cc7d26f
TT
1041 back_to = varobj_ensure_python_env (var);
1042 result = PyObject_HasAttr (printer, gdbpy_children_cst);
1043 do_cleanups (back_to);
1044 return result;
1045}
1046
1047#endif
1048
b6313243
TT
1049static int
1050update_dynamic_varobj_children (struct varobj *var,
1051 VEC (varobj_p) **changed,
8264ba82 1052 VEC (varobj_p) **type_changed,
0cc7d26f
TT
1053 VEC (varobj_p) **new,
1054 VEC (varobj_p) **unchanged,
1055 int *cchanged,
1056 int update_children,
1057 int from,
1058 int to)
b6313243
TT
1059{
1060#if HAVE_PYTHON
b6313243
TT
1061 struct cleanup *back_to;
1062 PyObject *children;
b6313243 1063 int i;
b6313243 1064 PyObject *printer = var->pretty_printer;
b6313243 1065
0646da15
TT
1066 if (!gdb_python_initialized)
1067 return 0;
1068
d452c4bc 1069 back_to = varobj_ensure_python_env (var);
b6313243
TT
1070
1071 *cchanged = 0;
1072 if (!PyObject_HasAttr (printer, gdbpy_children_cst))
1073 {
1074 do_cleanups (back_to);
1075 return 0;
1076 }
1077
0cc7d26f 1078 if (update_children || !var->child_iter)
b6313243 1079 {
0cc7d26f
TT
1080 children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
1081 NULL);
b6313243 1082
0cc7d26f
TT
1083 if (!children)
1084 {
1085 gdbpy_print_stack ();
1086 error (_("Null value returned for children"));
1087 }
b6313243 1088
0cc7d26f 1089 make_cleanup_py_decref (children);
b6313243 1090
0cc7d26f
TT
1091 Py_XDECREF (var->child_iter);
1092 var->child_iter = PyObject_GetIter (children);
1093 if (!var->child_iter)
1094 {
1095 gdbpy_print_stack ();
1096 error (_("Could not get children iterator"));
1097 }
1098
1099 Py_XDECREF (var->saved_item);
1100 var->saved_item = NULL;
1101
1102 i = 0;
b6313243 1103 }
0cc7d26f
TT
1104 else
1105 i = VEC_length (varobj_p, var->children);
b6313243 1106
0cc7d26f
TT
1107 /* We ask for one extra child, so that MI can report whether there
1108 are more children. */
1109 for (; to < 0 || i < to + 1; ++i)
b6313243 1110 {
0cc7d26f 1111 PyObject *item;
a4c8e806 1112 int force_done = 0;
b6313243 1113
0cc7d26f
TT
1114 /* See if there was a leftover from last time. */
1115 if (var->saved_item)
1116 {
1117 item = var->saved_item;
1118 var->saved_item = NULL;
1119 }
1120 else
1121 item = PyIter_Next (var->child_iter);
b6313243 1122
0cc7d26f 1123 if (!item)
a4c8e806
TT
1124 {
1125 /* Normal end of iteration. */
1126 if (!PyErr_Occurred ())
1127 break;
1128
1129 /* If we got a memory error, just use the text as the
1130 item. */
1131 if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
1132 {
1133 PyObject *type, *value, *trace;
1134 char *name_str, *value_str;
1135
1136 PyErr_Fetch (&type, &value, &trace);
1137 value_str = gdbpy_exception_to_string (type, value);
1138 Py_XDECREF (type);
1139 Py_XDECREF (value);
1140 Py_XDECREF (trace);
1141 if (!value_str)
1142 {
1143 gdbpy_print_stack ();
1144 break;
1145 }
1146
1147 name_str = xstrprintf ("<error at %d>", i);
1148 item = Py_BuildValue ("(ss)", name_str, value_str);
1149 xfree (name_str);
1150 xfree (value_str);
1151 if (!item)
1152 {
1153 gdbpy_print_stack ();
1154 break;
1155 }
1156
1157 force_done = 1;
1158 }
1159 else
1160 {
1161 /* Any other kind of error. */
1162 gdbpy_print_stack ();
1163 break;
1164 }
1165 }
b6313243 1166
0cc7d26f
TT
1167 /* We don't want to push the extra child on any report list. */
1168 if (to < 0 || i < to)
b6313243 1169 {
0cc7d26f 1170 PyObject *py_v;
ddd49eee 1171 const char *name;
0cc7d26f
TT
1172 struct value *v;
1173 struct cleanup *inner;
1174 int can_mention = from < 0 || i >= from;
1175
1176 inner = make_cleanup_py_decref (item);
1177
1178 if (!PyArg_ParseTuple (item, "sO", &name, &py_v))
a4c8e806
TT
1179 {
1180 gdbpy_print_stack ();
1181 error (_("Invalid item from the child list"));
1182 }
0cc7d26f
TT
1183
1184 v = convert_value_from_python (py_v);
8dc78533
JK
1185 if (v == NULL)
1186 gdbpy_print_stack ();
0cc7d26f 1187 install_dynamic_child (var, can_mention ? changed : NULL,
8264ba82 1188 can_mention ? type_changed : NULL,
0cc7d26f
TT
1189 can_mention ? new : NULL,
1190 can_mention ? unchanged : NULL,
1191 can_mention ? cchanged : NULL, i, name, v);
1192 do_cleanups (inner);
b6313243 1193 }
0cc7d26f 1194 else
b6313243 1195 {
0cc7d26f
TT
1196 Py_XDECREF (var->saved_item);
1197 var->saved_item = item;
b6313243 1198
0cc7d26f
TT
1199 /* We want to truncate the child list just before this
1200 element. */
1201 break;
1202 }
a4c8e806
TT
1203
1204 if (force_done)
1205 break;
b6313243
TT
1206 }
1207
1208 if (i < VEC_length (varobj_p, var->children))
1209 {
0cc7d26f 1210 int j;
a109c7c1 1211
0cc7d26f
TT
1212 *cchanged = 1;
1213 for (j = i; j < VEC_length (varobj_p, var->children); ++j)
1214 varobj_delete (VEC_index (varobj_p, var->children, j), NULL, 0);
1215 VEC_truncate (varobj_p, var->children, i);
b6313243 1216 }
0cc7d26f
TT
1217
1218 /* If there are fewer children than requested, note that the list of
1219 children changed. */
1220 if (to >= 0 && VEC_length (varobj_p, var->children) < to)
1221 *cchanged = 1;
1222
b6313243
TT
1223 var->num_children = VEC_length (varobj_p, var->children);
1224
1225 do_cleanups (back_to);
1226
b6313243
TT
1227 return 1;
1228#else
9e77999c 1229 gdb_assert_not_reached ("should never be called if Python is not enabled");
b6313243
TT
1230#endif
1231}
25d5ea92 1232
8b93c638
JM
1233int
1234varobj_get_num_children (struct varobj *var)
1235{
1236 if (var->num_children == -1)
b6313243 1237 {
0cc7d26f
TT
1238 if (var->pretty_printer)
1239 {
1240 int dummy;
1241
1242 /* If we have a dynamic varobj, don't report -1 children.
1243 So, try to fetch some children first. */
8264ba82 1244 update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL, &dummy,
0cc7d26f
TT
1245 0, 0, 0);
1246 }
1247 else
b6313243
TT
1248 var->num_children = number_of_children (var);
1249 }
8b93c638 1250
0cc7d26f 1251 return var->num_children >= 0 ? var->num_children : 0;
8b93c638
JM
1252}
1253
1254/* Creates a list of the immediate children of a variable object;
581e13c1 1255 the return code is the number of such children or -1 on error. */
8b93c638 1256
d56d46f5 1257VEC (varobj_p)*
0cc7d26f 1258varobj_list_children (struct varobj *var, int *from, int *to)
8b93c638 1259{
8b93c638 1260 char *name;
b6313243
TT
1261 int i, children_changed;
1262
1263 var->children_requested = 1;
1264
0cc7d26f
TT
1265 if (var->pretty_printer)
1266 {
b6313243
TT
1267 /* This, in theory, can result in the number of children changing without
1268 frontend noticing. But well, calling -var-list-children on the same
1269 varobj twice is not something a sane frontend would do. */
8264ba82
AG
1270 update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL,
1271 &children_changed, 0, 0, *to);
0cc7d26f
TT
1272 restrict_range (var->children, from, to);
1273 return var->children;
1274 }
8b93c638 1275
8b93c638
JM
1276 if (var->num_children == -1)
1277 var->num_children = number_of_children (var);
1278
74a44383
DJ
1279 /* If that failed, give up. */
1280 if (var->num_children == -1)
d56d46f5 1281 return var->children;
74a44383 1282
28335dcc
VP
1283 /* If we're called when the list of children is not yet initialized,
1284 allocate enough elements in it. */
1285 while (VEC_length (varobj_p, var->children) < var->num_children)
1286 VEC_safe_push (varobj_p, var->children, NULL);
1287
8b93c638
JM
1288 for (i = 0; i < var->num_children; i++)
1289 {
d56d46f5 1290 varobj_p existing = VEC_index (varobj_p, var->children, i);
28335dcc
VP
1291
1292 if (existing == NULL)
1293 {
1294 /* Either it's the first call to varobj_list_children for
1295 this variable object, and the child was never created,
1296 or it was explicitly deleted by the client. */
1297 name = name_of_child (var, i);
1298 existing = create_child (var, i, name);
1299 VEC_replace (varobj_p, var->children, i, existing);
1300 }
8b93c638
JM
1301 }
1302
0cc7d26f 1303 restrict_range (var->children, from, to);
d56d46f5 1304 return var->children;
8b93c638
JM
1305}
1306
d8b65138
JK
1307#if HAVE_PYTHON
1308
b6313243
TT
1309static struct varobj *
1310varobj_add_child (struct varobj *var, const char *name, struct value *value)
1311{
1312 varobj_p v = create_child_with_value (var,
1313 VEC_length (varobj_p, var->children),
1314 name, value);
a109c7c1 1315
b6313243 1316 VEC_safe_push (varobj_p, var->children, v);
b6313243
TT
1317 return v;
1318}
1319
d8b65138
JK
1320#endif /* HAVE_PYTHON */
1321
8b93c638 1322/* Obtain the type of an object Variable as a string similar to the one gdb
581e13c1 1323 prints on the console. */
8b93c638
JM
1324
1325char *
1326varobj_get_type (struct varobj *var)
1327{
581e13c1 1328 /* For the "fake" variables, do not return a type. (It's type is
8756216b
DP
1329 NULL, too.)
1330 Do not return a type for invalid variables as well. */
1331 if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
8b93c638
JM
1332 return NULL;
1333
1a4300e9 1334 return type_to_string (var->type);
8b93c638
JM
1335}
1336
1ecb4ee0
DJ
1337/* Obtain the type of an object variable. */
1338
1339struct type *
1340varobj_get_gdb_type (struct varobj *var)
1341{
1342 return var->type;
1343}
1344
85254831
KS
1345/* Is VAR a path expression parent, i.e., can it be used to construct
1346 a valid path expression? */
1347
1348static int
1349is_path_expr_parent (struct varobj *var)
1350{
1351 struct type *type;
1352
1353 /* "Fake" children are not path_expr parents. */
1354 if (CPLUS_FAKE_CHILD (var))
1355 return 0;
1356
1357 type = get_value_type (var);
1358
1359 /* Anonymous unions and structs are also not path_expr parents. */
1360 return !((TYPE_CODE (type) == TYPE_CODE_STRUCT
1361 || TYPE_CODE (type) == TYPE_CODE_UNION)
1362 && TYPE_NAME (type) == NULL);
1363}
1364
1365/* Return the path expression parent for VAR. */
1366
1367static struct varobj *
1368get_path_expr_parent (struct varobj *var)
1369{
1370 struct varobj *parent = var;
1371
1372 while (!is_root_p (parent) && !is_path_expr_parent (parent))
1373 parent = parent->parent;
1374
1375 return parent;
1376}
1377
02142340
VP
1378/* Return a pointer to the full rooted expression of varobj VAR.
1379 If it has not been computed yet, compute it. */
1380char *
1381varobj_get_path_expr (struct varobj *var)
1382{
1383 if (var->path_expr != NULL)
1384 return var->path_expr;
1385 else
1386 {
1387 /* For root varobjs, we initialize path_expr
1388 when creating varobj, so here it should be
1389 child varobj. */
1390 gdb_assert (!is_root_p (var));
1391 return (*var->root->lang->path_expr_of_child) (var);
1392 }
1393}
1394
8b93c638
JM
1395enum varobj_languages
1396varobj_get_language (struct varobj *var)
1397{
1398 return variable_language (var);
1399}
1400
1401int
1402varobj_get_attributes (struct varobj *var)
1403{
1404 int attributes = 0;
1405
340a7723 1406 if (varobj_editable_p (var))
581e13c1 1407 /* FIXME: define masks for attributes. */
8b93c638
JM
1408 attributes |= 0x00000001; /* Editable */
1409
1410 return attributes;
1411}
1412
0cc7d26f
TT
1413int
1414varobj_pretty_printed_p (struct varobj *var)
1415{
1416 return var->pretty_printer != NULL;
1417}
1418
de051565
MK
1419char *
1420varobj_get_formatted_value (struct varobj *var,
1421 enum varobj_display_formats format)
1422{
1423 return my_value_of_variable (var, format);
1424}
1425
8b93c638
JM
1426char *
1427varobj_get_value (struct varobj *var)
1428{
de051565 1429 return my_value_of_variable (var, var->format);
8b93c638
JM
1430}
1431
1432/* Set the value of an object variable (if it is editable) to the
581e13c1
MS
1433 value of the given expression. */
1434/* Note: Invokes functions that can call error(). */
8b93c638
JM
1435
1436int
1437varobj_set_value (struct varobj *var, char *expression)
1438{
34365054 1439 struct value *val = NULL; /* Initialize to keep gcc happy. */
8b93c638 1440 /* The argument "expression" contains the variable's new value.
581e13c1
MS
1441 We need to first construct a legal expression for this -- ugh! */
1442 /* Does this cover all the bases? */
8b93c638 1443 struct expression *exp;
34365054 1444 struct value *value = NULL; /* Initialize to keep gcc happy. */
8b93c638 1445 int saved_input_radix = input_radix;
bbc13ae3 1446 const char *s = expression;
8e7b59a5 1447 volatile struct gdb_exception except;
8b93c638 1448
340a7723 1449 gdb_assert (varobj_editable_p (var));
8b93c638 1450
581e13c1 1451 input_radix = 10; /* ALWAYS reset to decimal temporarily. */
1bb9788d 1452 exp = parse_exp_1 (&s, 0, 0, 0);
8e7b59a5
KS
1453 TRY_CATCH (except, RETURN_MASK_ERROR)
1454 {
1455 value = evaluate_expression (exp);
1456 }
1457
1458 if (except.reason < 0)
340a7723 1459 {
581e13c1 1460 /* We cannot proceed without a valid expression. */
340a7723
NR
1461 xfree (exp);
1462 return 0;
8b93c638
JM
1463 }
1464
340a7723
NR
1465 /* All types that are editable must also be changeable. */
1466 gdb_assert (varobj_value_is_changeable_p (var));
1467
1468 /* The value of a changeable variable object must not be lazy. */
1469 gdb_assert (!value_lazy (var->value));
1470
1471 /* Need to coerce the input. We want to check if the
1472 value of the variable object will be different
1473 after assignment, and the first thing value_assign
1474 does is coerce the input.
1475 For example, if we are assigning an array to a pointer variable we
b021a221 1476 should compare the pointer with the array's address, not with the
340a7723
NR
1477 array's content. */
1478 value = coerce_array (value);
1479
8e7b59a5
KS
1480 /* The new value may be lazy. value_assign, or
1481 rather value_contents, will take care of this. */
1482 TRY_CATCH (except, RETURN_MASK_ERROR)
1483 {
1484 val = value_assign (var->value, value);
1485 }
1486
1487 if (except.reason < 0)
340a7723 1488 return 0;
8e7b59a5 1489
340a7723
NR
1490 /* If the value has changed, record it, so that next -var-update can
1491 report this change. If a variable had a value of '1', we've set it
1492 to '333' and then set again to '1', when -var-update will report this
1493 variable as changed -- because the first assignment has set the
1494 'updated' flag. There's no need to optimize that, because return value
1495 of -var-update should be considered an approximation. */
581e13c1 1496 var->updated = install_new_value (var, val, 0 /* Compare values. */);
340a7723
NR
1497 input_radix = saved_input_radix;
1498 return 1;
8b93c638
JM
1499}
1500
0cc7d26f
TT
1501#if HAVE_PYTHON
1502
1503/* A helper function to install a constructor function and visualizer
1504 in a varobj. */
1505
1506static void
1507install_visualizer (struct varobj *var, PyObject *constructor,
1508 PyObject *visualizer)
1509{
1510 Py_XDECREF (var->constructor);
1511 var->constructor = constructor;
1512
1513 Py_XDECREF (var->pretty_printer);
1514 var->pretty_printer = visualizer;
1515
1516 Py_XDECREF (var->child_iter);
1517 var->child_iter = NULL;
1518}
1519
1520/* Install the default visualizer for VAR. */
1521
1522static void
1523install_default_visualizer (struct varobj *var)
1524{
d65aec65
PM
1525 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1526 if (CPLUS_FAKE_CHILD (var))
1527 return;
1528
0cc7d26f
TT
1529 if (pretty_printing)
1530 {
1531 PyObject *pretty_printer = NULL;
1532
1533 if (var->value)
1534 {
1535 pretty_printer = gdbpy_get_varobj_pretty_printer (var->value);
1536 if (! pretty_printer)
1537 {
1538 gdbpy_print_stack ();
1539 error (_("Cannot instantiate printer for default visualizer"));
1540 }
1541 }
1542
1543 if (pretty_printer == Py_None)
1544 {
1545 Py_DECREF (pretty_printer);
1546 pretty_printer = NULL;
1547 }
1548
1549 install_visualizer (var, NULL, pretty_printer);
1550 }
1551}
1552
1553/* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
1554 make a new object. */
1555
1556static void
1557construct_visualizer (struct varobj *var, PyObject *constructor)
1558{
1559 PyObject *pretty_printer;
1560
d65aec65
PM
1561 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1562 if (CPLUS_FAKE_CHILD (var))
1563 return;
1564
0cc7d26f
TT
1565 Py_INCREF (constructor);
1566 if (constructor == Py_None)
1567 pretty_printer = NULL;
1568 else
1569 {
1570 pretty_printer = instantiate_pretty_printer (constructor, var->value);
1571 if (! pretty_printer)
1572 {
1573 gdbpy_print_stack ();
1574 Py_DECREF (constructor);
1575 constructor = Py_None;
1576 Py_INCREF (constructor);
1577 }
1578
1579 if (pretty_printer == Py_None)
1580 {
1581 Py_DECREF (pretty_printer);
1582 pretty_printer = NULL;
1583 }
1584 }
1585
1586 install_visualizer (var, constructor, pretty_printer);
1587}
1588
1589#endif /* HAVE_PYTHON */
1590
1591/* A helper function for install_new_value. This creates and installs
1592 a visualizer for VAR, if appropriate. */
1593
1594static void
1595install_new_value_visualizer (struct varobj *var)
1596{
1597#if HAVE_PYTHON
1598 /* If the constructor is None, then we want the raw value. If VAR
1599 does not have a value, just skip this. */
0646da15
TT
1600 if (!gdb_python_initialized)
1601 return;
1602
0cc7d26f
TT
1603 if (var->constructor != Py_None && var->value)
1604 {
1605 struct cleanup *cleanup;
0cc7d26f
TT
1606
1607 cleanup = varobj_ensure_python_env (var);
1608
1609 if (!var->constructor)
1610 install_default_visualizer (var);
1611 else
1612 construct_visualizer (var, var->constructor);
1613
1614 do_cleanups (cleanup);
1615 }
1616#else
1617 /* Do nothing. */
1618#endif
1619}
1620
8264ba82
AG
1621/* When using RTTI to determine variable type it may be changed in runtime when
1622 the variable value is changed. This function checks whether type of varobj
1623 VAR will change when a new value NEW_VALUE is assigned and if it is so
1624 updates the type of VAR. */
1625
1626static int
1627update_type_if_necessary (struct varobj *var, struct value *new_value)
1628{
1629 if (new_value)
1630 {
1631 struct value_print_options opts;
1632
1633 get_user_print_options (&opts);
1634 if (opts.objectprint)
1635 {
1636 struct type *new_type;
1637 char *curr_type_str, *new_type_str;
1638
1639 new_type = value_actual_type (new_value, 0, 0);
1640 new_type_str = type_to_string (new_type);
1641 curr_type_str = varobj_get_type (var);
1642 if (strcmp (curr_type_str, new_type_str) != 0)
1643 {
1644 var->type = new_type;
1645
1646 /* This information may be not valid for a new type. */
1647 varobj_delete (var, NULL, 1);
1648 VEC_free (varobj_p, var->children);
1649 var->num_children = -1;
1650 return 1;
1651 }
1652 }
1653 }
1654
1655 return 0;
1656}
1657
acd65feb
VP
1658/* Assign a new value to a variable object. If INITIAL is non-zero,
1659 this is the first assignement after the variable object was just
1660 created, or changed type. In that case, just assign the value
1661 and return 0.
581e13c1
MS
1662 Otherwise, assign the new value, and return 1 if the value is
1663 different from the current one, 0 otherwise. The comparison is
1664 done on textual representation of value. Therefore, some types
1665 need not be compared. E.g. for structures the reported value is
1666 always "{...}", so no comparison is necessary here. If the old
1667 value was NULL and new one is not, or vice versa, we always return 1.
b26ed50d
VP
1668
1669 The VALUE parameter should not be released -- the function will
1670 take care of releasing it when needed. */
acd65feb
VP
1671static int
1672install_new_value (struct varobj *var, struct value *value, int initial)
1673{
1674 int changeable;
1675 int need_to_fetch;
1676 int changed = 0;
25d5ea92 1677 int intentionally_not_fetched = 0;
7a4d50bf 1678 char *print_value = NULL;
acd65feb 1679
acd65feb 1680 /* We need to know the varobj's type to decide if the value should
3e43a32a 1681 be fetched or not. C++ fake children (public/protected/private)
581e13c1 1682 don't have a type. */
acd65feb 1683 gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
b2c2bd75 1684 changeable = varobj_value_is_changeable_p (var);
b6313243
TT
1685
1686 /* If the type has custom visualizer, we consider it to be always
581e13c1 1687 changeable. FIXME: need to make sure this behaviour will not
b6313243
TT
1688 mess up read-sensitive values. */
1689 if (var->pretty_printer)
1690 changeable = 1;
1691
acd65feb
VP
1692 need_to_fetch = changeable;
1693
b26ed50d
VP
1694 /* We are not interested in the address of references, and given
1695 that in C++ a reference is not rebindable, it cannot
1696 meaningfully change. So, get hold of the real value. */
1697 if (value)
0cc7d26f 1698 value = coerce_ref (value);
b26ed50d 1699
acd65feb
VP
1700 if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
1701 /* For unions, we need to fetch the value implicitly because
1702 of implementation of union member fetch. When gdb
1703 creates a value for a field and the value of the enclosing
1704 structure is not lazy, it immediately copies the necessary
1705 bytes from the enclosing values. If the enclosing value is
1706 lazy, the call to value_fetch_lazy on the field will read
1707 the data from memory. For unions, that means we'll read the
1708 same memory more than once, which is not desirable. So
1709 fetch now. */
1710 need_to_fetch = 1;
1711
1712 /* The new value might be lazy. If the type is changeable,
1713 that is we'll be comparing values of this type, fetch the
1714 value now. Otherwise, on the next update the old value
1715 will be lazy, which means we've lost that old value. */
1716 if (need_to_fetch && value && value_lazy (value))
1717 {
25d5ea92
VP
1718 struct varobj *parent = var->parent;
1719 int frozen = var->frozen;
a109c7c1 1720
25d5ea92
VP
1721 for (; !frozen && parent; parent = parent->parent)
1722 frozen |= parent->frozen;
1723
1724 if (frozen && initial)
1725 {
1726 /* For variables that are frozen, or are children of frozen
1727 variables, we don't do fetch on initial assignment.
1728 For non-initial assignemnt we do the fetch, since it means we're
1729 explicitly asked to compare the new value with the old one. */
1730 intentionally_not_fetched = 1;
1731 }
8e7b59a5 1732 else
acd65feb 1733 {
8e7b59a5
KS
1734 volatile struct gdb_exception except;
1735
1736 TRY_CATCH (except, RETURN_MASK_ERROR)
1737 {
1738 value_fetch_lazy (value);
1739 }
1740
1741 if (except.reason < 0)
1742 {
1743 /* Set the value to NULL, so that for the next -var-update,
1744 we don't try to compare the new value with this value,
1745 that we couldn't even read. */
1746 value = NULL;
1747 }
acd65feb 1748 }
acd65feb
VP
1749 }
1750
e848a8a5
TT
1751 /* Get a reference now, before possibly passing it to any Python
1752 code that might release it. */
1753 if (value != NULL)
1754 value_incref (value);
b6313243 1755
7a4d50bf
VP
1756 /* Below, we'll be comparing string rendering of old and new
1757 values. Don't get string rendering if the value is
1758 lazy -- if it is, the code above has decided that the value
1759 should not be fetched. */
0cc7d26f 1760 if (value && !value_lazy (value) && !var->pretty_printer)
d452c4bc 1761 print_value = value_get_print_value (value, var->format, var);
7a4d50bf 1762
acd65feb
VP
1763 /* If the type is changeable, compare the old and the new values.
1764 If this is the initial assignment, we don't have any old value
1765 to compare with. */
7a4d50bf 1766 if (!initial && changeable)
acd65feb 1767 {
3e43a32a
MS
1768 /* If the value of the varobj was changed by -var-set-value,
1769 then the value in the varobj and in the target is the same.
1770 However, that value is different from the value that the
581e13c1 1771 varobj had after the previous -var-update. So need to the
3e43a32a 1772 varobj as changed. */
acd65feb 1773 if (var->updated)
57e66780 1774 {
57e66780
DJ
1775 changed = 1;
1776 }
0cc7d26f 1777 else if (! var->pretty_printer)
acd65feb
VP
1778 {
1779 /* Try to compare the values. That requires that both
1780 values are non-lazy. */
25d5ea92
VP
1781 if (var->not_fetched && value_lazy (var->value))
1782 {
1783 /* This is a frozen varobj and the value was never read.
1784 Presumably, UI shows some "never read" indicator.
1785 Now that we've fetched the real value, we need to report
1786 this varobj as changed so that UI can show the real
1787 value. */
1788 changed = 1;
1789 }
1790 else if (var->value == NULL && value == NULL)
581e13c1 1791 /* Equal. */
acd65feb
VP
1792 ;
1793 else if (var->value == NULL || value == NULL)
57e66780 1794 {
57e66780
DJ
1795 changed = 1;
1796 }
acd65feb
VP
1797 else
1798 {
1799 gdb_assert (!value_lazy (var->value));
1800 gdb_assert (!value_lazy (value));
85265413 1801
57e66780 1802 gdb_assert (var->print_value != NULL && print_value != NULL);
85265413 1803 if (strcmp (var->print_value, print_value) != 0)
7a4d50bf 1804 changed = 1;
acd65feb
VP
1805 }
1806 }
1807 }
85265413 1808
ee342b23
VP
1809 if (!initial && !changeable)
1810 {
1811 /* For values that are not changeable, we don't compare the values.
1812 However, we want to notice if a value was not NULL and now is NULL,
1813 or vise versa, so that we report when top-level varobjs come in scope
1814 and leave the scope. */
1815 changed = (var->value != NULL) != (value != NULL);
1816 }
1817
acd65feb 1818 /* We must always keep the new value, since children depend on it. */
25d5ea92 1819 if (var->value != NULL && var->value != value)
acd65feb
VP
1820 value_free (var->value);
1821 var->value = value;
25d5ea92
VP
1822 if (value && value_lazy (value) && intentionally_not_fetched)
1823 var->not_fetched = 1;
1824 else
1825 var->not_fetched = 0;
acd65feb 1826 var->updated = 0;
85265413 1827
0cc7d26f
TT
1828 install_new_value_visualizer (var);
1829
1830 /* If we installed a pretty-printer, re-compare the printed version
1831 to see if the variable changed. */
1832 if (var->pretty_printer)
1833 {
1834 xfree (print_value);
1835 print_value = value_get_print_value (var->value, var->format, var);
e8f781e2
TT
1836 if ((var->print_value == NULL && print_value != NULL)
1837 || (var->print_value != NULL && print_value == NULL)
1838 || (var->print_value != NULL && print_value != NULL
1839 && strcmp (var->print_value, print_value) != 0))
0cc7d26f
TT
1840 changed = 1;
1841 }
1842 if (var->print_value)
1843 xfree (var->print_value);
1844 var->print_value = print_value;
1845
b26ed50d 1846 gdb_assert (!var->value || value_type (var->value));
acd65feb
VP
1847
1848 return changed;
1849}
acd65feb 1850
0cc7d26f
TT
1851/* Return the requested range for a varobj. VAR is the varobj. FROM
1852 and TO are out parameters; *FROM and *TO will be set to the
1853 selected sub-range of VAR. If no range was selected using
1854 -var-set-update-range, then both will be -1. */
1855void
1856varobj_get_child_range (struct varobj *var, int *from, int *to)
b6313243 1857{
0cc7d26f
TT
1858 *from = var->from;
1859 *to = var->to;
b6313243
TT
1860}
1861
0cc7d26f
TT
1862/* Set the selected sub-range of children of VAR to start at index
1863 FROM and end at index TO. If either FROM or TO is less than zero,
1864 this is interpreted as a request for all children. */
1865void
1866varobj_set_child_range (struct varobj *var, int from, int to)
b6313243 1867{
0cc7d26f
TT
1868 var->from = from;
1869 var->to = to;
b6313243
TT
1870}
1871
1872void
1873varobj_set_visualizer (struct varobj *var, const char *visualizer)
1874{
1875#if HAVE_PYTHON
34fa1d9d
MS
1876 PyObject *mainmod, *globals, *constructor;
1877 struct cleanup *back_to;
b6313243 1878
0646da15
TT
1879 if (!gdb_python_initialized)
1880 return;
1881
d452c4bc 1882 back_to = varobj_ensure_python_env (var);
b6313243
TT
1883
1884 mainmod = PyImport_AddModule ("__main__");
1885 globals = PyModule_GetDict (mainmod);
1886 Py_INCREF (globals);
1887 make_cleanup_py_decref (globals);
1888
1889 constructor = PyRun_String (visualizer, Py_eval_input, globals, globals);
b6313243 1890
0cc7d26f 1891 if (! constructor)
b6313243
TT
1892 {
1893 gdbpy_print_stack ();
da1f2771 1894 error (_("Could not evaluate visualizer expression: %s"), visualizer);
b6313243
TT
1895 }
1896
0cc7d26f
TT
1897 construct_visualizer (var, constructor);
1898 Py_XDECREF (constructor);
b6313243 1899
0cc7d26f
TT
1900 /* If there are any children now, wipe them. */
1901 varobj_delete (var, NULL, 1 /* children only */);
1902 var->num_children = -1;
b6313243
TT
1903
1904 do_cleanups (back_to);
1905#else
da1f2771 1906 error (_("Python support required"));
b6313243
TT
1907#endif
1908}
1909
7a290c40
JB
1910/* If NEW_VALUE is the new value of the given varobj (var), return
1911 non-zero if var has mutated. In other words, if the type of
1912 the new value is different from the type of the varobj's old
1913 value.
1914
1915 NEW_VALUE may be NULL, if the varobj is now out of scope. */
1916
1917static int
1918varobj_value_has_mutated (struct varobj *var, struct value *new_value,
1919 struct type *new_type)
1920{
1921 /* If we haven't previously computed the number of children in var,
1922 it does not matter from the front-end's perspective whether
1923 the type has mutated or not. For all intents and purposes,
1924 it has not mutated. */
1925 if (var->num_children < 0)
1926 return 0;
1927
1928 if (var->root->lang->value_has_mutated)
1929 return var->root->lang->value_has_mutated (var, new_value, new_type);
1930 else
1931 return 0;
1932}
1933
8b93c638
JM
1934/* Update the values for a variable and its children. This is a
1935 two-pronged attack. First, re-parse the value for the root's
1936 expression to see if it's changed. Then go all the way
1937 through its children, reconstructing them and noting if they've
1938 changed.
1939
25d5ea92
VP
1940 The EXPLICIT parameter specifies if this call is result
1941 of MI request to update this specific variable, or
581e13c1 1942 result of implicit -var-update *. For implicit request, we don't
25d5ea92 1943 update frozen variables.
705da579 1944
581e13c1 1945 NOTE: This function may delete the caller's varobj. If it
8756216b
DP
1946 returns TYPE_CHANGED, then it has done this and VARP will be modified
1947 to point to the new varobj. */
8b93c638 1948
1417b39d
JB
1949VEC(varobj_update_result) *
1950varobj_update (struct varobj **varp, int explicit)
8b93c638 1951{
25d5ea92 1952 int type_changed = 0;
8b93c638 1953 int i;
30b28db1 1954 struct value *new;
b6313243 1955 VEC (varobj_update_result) *stack = NULL;
f7f9ae2c 1956 VEC (varobj_update_result) *result = NULL;
8b93c638 1957
25d5ea92
VP
1958 /* Frozen means frozen -- we don't check for any change in
1959 this varobj, including its going out of scope, or
1960 changing type. One use case for frozen varobjs is
1961 retaining previously evaluated expressions, and we don't
1962 want them to be reevaluated at all. */
1963 if (!explicit && (*varp)->frozen)
f7f9ae2c 1964 return result;
8756216b
DP
1965
1966 if (!(*varp)->root->is_valid)
f7f9ae2c 1967 {
cfce2ea2 1968 varobj_update_result r = {0};
a109c7c1 1969
cfce2ea2 1970 r.varobj = *varp;
f7f9ae2c
VP
1971 r.status = VAROBJ_INVALID;
1972 VEC_safe_push (varobj_update_result, result, &r);
1973 return result;
1974 }
8b93c638 1975
25d5ea92 1976 if ((*varp)->root->rootvar == *varp)
ae093f96 1977 {
cfce2ea2 1978 varobj_update_result r = {0};
a109c7c1 1979
cfce2ea2 1980 r.varobj = *varp;
f7f9ae2c
VP
1981 r.status = VAROBJ_IN_SCOPE;
1982
581e13c1 1983 /* Update the root variable. value_of_root can return NULL
25d5ea92 1984 if the variable is no longer around, i.e. we stepped out of
581e13c1 1985 the frame in which a local existed. We are letting the
25d5ea92
VP
1986 value_of_root variable dispose of the varobj if the type
1987 has changed. */
25d5ea92 1988 new = value_of_root (varp, &type_changed);
8264ba82
AG
1989 if (update_type_if_necessary(*varp, new))
1990 type_changed = 1;
f7f9ae2c 1991 r.varobj = *varp;
f7f9ae2c 1992 r.type_changed = type_changed;
ea56f9c2 1993 if (install_new_value ((*varp), new, type_changed))
f7f9ae2c 1994 r.changed = 1;
ea56f9c2 1995
25d5ea92 1996 if (new == NULL)
f7f9ae2c 1997 r.status = VAROBJ_NOT_IN_SCOPE;
b6313243 1998 r.value_installed = 1;
f7f9ae2c
VP
1999
2000 if (r.status == VAROBJ_NOT_IN_SCOPE)
b6313243 2001 {
0b4bc29a
JK
2002 if (r.type_changed || r.changed)
2003 VEC_safe_push (varobj_update_result, result, &r);
b6313243
TT
2004 return result;
2005 }
2006
2007 VEC_safe_push (varobj_update_result, stack, &r);
2008 }
2009 else
2010 {
cfce2ea2 2011 varobj_update_result r = {0};
a109c7c1 2012
cfce2ea2 2013 r.varobj = *varp;
b6313243 2014 VEC_safe_push (varobj_update_result, stack, &r);
b20d8971 2015 }
8b93c638 2016
8756216b 2017 /* Walk through the children, reconstructing them all. */
b6313243 2018 while (!VEC_empty (varobj_update_result, stack))
8b93c638 2019 {
b6313243
TT
2020 varobj_update_result r = *(VEC_last (varobj_update_result, stack));
2021 struct varobj *v = r.varobj;
2022
2023 VEC_pop (varobj_update_result, stack);
2024
2025 /* Update this variable, unless it's a root, which is already
2026 updated. */
2027 if (!r.value_installed)
7a290c40
JB
2028 {
2029 struct type *new_type;
2030
b6313243 2031 new = value_of_child (v->parent, v->index);
8264ba82
AG
2032 if (update_type_if_necessary(v, new))
2033 r.type_changed = 1;
7a290c40
JB
2034 if (new)
2035 new_type = value_type (new);
2036 else
2037 new_type = v->root->lang->type_of_child (v->parent, v->index);
2038
2039 if (varobj_value_has_mutated (v, new, new_type))
2040 {
2041 /* The children are no longer valid; delete them now.
2042 Report the fact that its type changed as well. */
2043 varobj_delete (v, NULL, 1 /* only_children */);
2044 v->num_children = -1;
2045 v->to = -1;
2046 v->from = -1;
2047 v->type = new_type;
2048 r.type_changed = 1;
2049 }
2050
2051 if (install_new_value (v, new, r.type_changed))
b6313243
TT
2052 {
2053 r.changed = 1;
2054 v->updated = 0;
2055 }
2056 }
2057
2058 /* We probably should not get children of a varobj that has a
2059 pretty-printer, but for which -var-list-children was never
581e13c1 2060 invoked. */
b6313243
TT
2061 if (v->pretty_printer)
2062 {
8264ba82
AG
2063 VEC (varobj_p) *changed = 0, *type_changed = 0, *unchanged = 0;
2064 VEC (varobj_p) *new = 0;
26f9bcee 2065 int i, children_changed = 0;
b6313243
TT
2066
2067 if (v->frozen)
2068 continue;
2069
0cc7d26f
TT
2070 if (!v->children_requested)
2071 {
2072 int dummy;
2073
2074 /* If we initially did not have potential children, but
2075 now we do, consider the varobj as changed.
2076 Otherwise, if children were never requested, consider
2077 it as unchanged -- presumably, such varobj is not yet
2078 expanded in the UI, so we need not bother getting
2079 it. */
2080 if (!varobj_has_more (v, 0))
2081 {
8264ba82 2082 update_dynamic_varobj_children (v, NULL, NULL, NULL, NULL,
0cc7d26f
TT
2083 &dummy, 0, 0, 0);
2084 if (varobj_has_more (v, 0))
2085 r.changed = 1;
2086 }
2087
2088 if (r.changed)
2089 VEC_safe_push (varobj_update_result, result, &r);
2090
2091 continue;
2092 }
2093
b6313243
TT
2094 /* If update_dynamic_varobj_children returns 0, then we have
2095 a non-conforming pretty-printer, so we skip it. */
8264ba82
AG
2096 if (update_dynamic_varobj_children (v, &changed, &type_changed, &new,
2097 &unchanged, &children_changed, 1,
0cc7d26f 2098 v->from, v->to))
b6313243 2099 {
0cc7d26f 2100 if (children_changed || new)
b6313243 2101 {
0cc7d26f
TT
2102 r.children_changed = 1;
2103 r.new = new;
b6313243 2104 }
0cc7d26f
TT
2105 /* Push in reverse order so that the first child is
2106 popped from the work stack first, and so will be
2107 added to result first. This does not affect
2108 correctness, just "nicer". */
8264ba82
AG
2109 for (i = VEC_length (varobj_p, type_changed) - 1; i >= 0; --i)
2110 {
2111 varobj_p tmp = VEC_index (varobj_p, type_changed, i);
2112 varobj_update_result r = {0};
2113
2114 /* Type may change only if value was changed. */
2115 r.varobj = tmp;
2116 r.changed = 1;
2117 r.type_changed = 1;
2118 r.value_installed = 1;
2119 VEC_safe_push (varobj_update_result, stack, &r);
2120 }
0cc7d26f 2121 for (i = VEC_length (varobj_p, changed) - 1; i >= 0; --i)
b6313243 2122 {
0cc7d26f 2123 varobj_p tmp = VEC_index (varobj_p, changed, i);
cfce2ea2 2124 varobj_update_result r = {0};
a109c7c1 2125
cfce2ea2 2126 r.varobj = tmp;
0cc7d26f 2127 r.changed = 1;
b6313243
TT
2128 r.value_installed = 1;
2129 VEC_safe_push (varobj_update_result, stack, &r);
2130 }
0cc7d26f
TT
2131 for (i = VEC_length (varobj_p, unchanged) - 1; i >= 0; --i)
2132 {
2133 varobj_p tmp = VEC_index (varobj_p, unchanged, i);
a109c7c1 2134
0cc7d26f
TT
2135 if (!tmp->frozen)
2136 {
cfce2ea2 2137 varobj_update_result r = {0};
a109c7c1 2138
cfce2ea2 2139 r.varobj = tmp;
0cc7d26f
TT
2140 r.value_installed = 1;
2141 VEC_safe_push (varobj_update_result, stack, &r);
2142 }
2143 }
b6313243
TT
2144 if (r.changed || r.children_changed)
2145 VEC_safe_push (varobj_update_result, result, &r);
0cc7d26f 2146
8264ba82
AG
2147 /* Free CHANGED, TYPE_CHANGED and UNCHANGED, but not NEW,
2148 because NEW has been put into the result vector. */
0cc7d26f 2149 VEC_free (varobj_p, changed);
8264ba82 2150 VEC_free (varobj_p, type_changed);
0cc7d26f
TT
2151 VEC_free (varobj_p, unchanged);
2152
b6313243
TT
2153 continue;
2154 }
2155 }
28335dcc
VP
2156
2157 /* Push any children. Use reverse order so that the first
2158 child is popped from the work stack first, and so
2159 will be added to result first. This does not
2160 affect correctness, just "nicer". */
2161 for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i)
8b93c638 2162 {
28335dcc 2163 varobj_p c = VEC_index (varobj_p, v->children, i);
a109c7c1 2164
28335dcc 2165 /* Child may be NULL if explicitly deleted by -var-delete. */
25d5ea92 2166 if (c != NULL && !c->frozen)
28335dcc 2167 {
cfce2ea2 2168 varobj_update_result r = {0};
a109c7c1 2169
cfce2ea2 2170 r.varobj = c;
b6313243 2171 VEC_safe_push (varobj_update_result, stack, &r);
28335dcc 2172 }
8b93c638 2173 }
b6313243
TT
2174
2175 if (r.changed || r.type_changed)
2176 VEC_safe_push (varobj_update_result, result, &r);
8b93c638
JM
2177 }
2178
b6313243
TT
2179 VEC_free (varobj_update_result, stack);
2180
f7f9ae2c 2181 return result;
8b93c638
JM
2182}
2183\f
2184
2185/* Helper functions */
2186
2187/*
2188 * Variable object construction/destruction
2189 */
2190
2191static int
fba45db2
KB
2192delete_variable (struct cpstack **resultp, struct varobj *var,
2193 int only_children_p)
8b93c638
JM
2194{
2195 int delcount = 0;
2196
2197 delete_variable_1 (resultp, &delcount, var,
2198 only_children_p, 1 /* remove_from_parent_p */ );
2199
2200 return delcount;
2201}
2202
581e13c1 2203/* Delete the variable object VAR and its children. */
8b93c638
JM
2204/* IMPORTANT NOTE: If we delete a variable which is a child
2205 and the parent is not removed we dump core. It must be always
581e13c1 2206 initially called with remove_from_parent_p set. */
8b93c638 2207static void
72330bd6
AC
2208delete_variable_1 (struct cpstack **resultp, int *delcountp,
2209 struct varobj *var, int only_children_p,
2210 int remove_from_parent_p)
8b93c638 2211{
28335dcc 2212 int i;
8b93c638 2213
581e13c1 2214 /* Delete any children of this variable, too. */
28335dcc
VP
2215 for (i = 0; i < VEC_length (varobj_p, var->children); ++i)
2216 {
2217 varobj_p child = VEC_index (varobj_p, var->children, i);
a109c7c1 2218
214270ab
VP
2219 if (!child)
2220 continue;
8b93c638 2221 if (!remove_from_parent_p)
28335dcc
VP
2222 child->parent = NULL;
2223 delete_variable_1 (resultp, delcountp, child, 0, only_children_p);
8b93c638 2224 }
28335dcc 2225 VEC_free (varobj_p, var->children);
8b93c638 2226
581e13c1 2227 /* if we were called to delete only the children we are done here. */
8b93c638
JM
2228 if (only_children_p)
2229 return;
2230
581e13c1 2231 /* Otherwise, add it to the list of deleted ones and proceed to do so. */
73a93a32 2232 /* If the name is null, this is a temporary variable, that has not
581e13c1 2233 yet been installed, don't report it, it belongs to the caller... */
73a93a32 2234 if (var->obj_name != NULL)
8b93c638 2235 {
5b616ba1 2236 cppush (resultp, xstrdup (var->obj_name));
8b93c638
JM
2237 *delcountp = *delcountp + 1;
2238 }
2239
581e13c1 2240 /* If this variable has a parent, remove it from its parent's list. */
8b93c638
JM
2241 /* OPTIMIZATION: if the parent of this variable is also being deleted,
2242 (as indicated by remove_from_parent_p) we don't bother doing an
2243 expensive list search to find the element to remove when we are
581e13c1 2244 discarding the list afterwards. */
72330bd6 2245 if ((remove_from_parent_p) && (var->parent != NULL))
8b93c638 2246 {
28335dcc 2247 VEC_replace (varobj_p, var->parent->children, var->index, NULL);
8b93c638 2248 }
72330bd6 2249
73a93a32
JI
2250 if (var->obj_name != NULL)
2251 uninstall_variable (var);
8b93c638 2252
581e13c1 2253 /* Free memory associated with this variable. */
8b93c638
JM
2254 free_variable (var);
2255}
2256
581e13c1 2257/* Install the given variable VAR with the object name VAR->OBJ_NAME. */
8b93c638 2258static int
fba45db2 2259install_variable (struct varobj *var)
8b93c638
JM
2260{
2261 struct vlist *cv;
2262 struct vlist *newvl;
2263 const char *chp;
2264 unsigned int index = 0;
2265 unsigned int i = 1;
2266
2267 for (chp = var->obj_name; *chp; chp++)
2268 {
2269 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
2270 }
2271
2272 cv = *(varobj_table + index);
2273 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
2274 cv = cv->next;
2275
2276 if (cv != NULL)
8a3fe4f8 2277 error (_("Duplicate variable object name"));
8b93c638 2278
581e13c1 2279 /* Add varobj to hash table. */
8b93c638
JM
2280 newvl = xmalloc (sizeof (struct vlist));
2281 newvl->next = *(varobj_table + index);
2282 newvl->var = var;
2283 *(varobj_table + index) = newvl;
2284
581e13c1 2285 /* If root, add varobj to root list. */
b2c2bd75 2286 if (is_root_p (var))
8b93c638 2287 {
581e13c1 2288 /* Add to list of root variables. */
8b93c638
JM
2289 if (rootlist == NULL)
2290 var->root->next = NULL;
2291 else
2292 var->root->next = rootlist;
2293 rootlist = var->root;
8b93c638
JM
2294 }
2295
2296 return 1; /* OK */
2297}
2298
581e13c1 2299/* Unistall the object VAR. */
8b93c638 2300static void
fba45db2 2301uninstall_variable (struct varobj *var)
8b93c638
JM
2302{
2303 struct vlist *cv;
2304 struct vlist *prev;
2305 struct varobj_root *cr;
2306 struct varobj_root *prer;
2307 const char *chp;
2308 unsigned int index = 0;
2309 unsigned int i = 1;
2310
581e13c1 2311 /* Remove varobj from hash table. */
8b93c638
JM
2312 for (chp = var->obj_name; *chp; chp++)
2313 {
2314 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
2315 }
2316
2317 cv = *(varobj_table + index);
2318 prev = NULL;
2319 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
2320 {
2321 prev = cv;
2322 cv = cv->next;
2323 }
2324
2325 if (varobjdebug)
2326 fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
2327
2328 if (cv == NULL)
2329 {
72330bd6
AC
2330 warning
2331 ("Assertion failed: Could not find variable object \"%s\" to delete",
2332 var->obj_name);
8b93c638
JM
2333 return;
2334 }
2335
2336 if (prev == NULL)
2337 *(varobj_table + index) = cv->next;
2338 else
2339 prev->next = cv->next;
2340
b8c9b27d 2341 xfree (cv);
8b93c638 2342
581e13c1 2343 /* If root, remove varobj from root list. */
b2c2bd75 2344 if (is_root_p (var))
8b93c638 2345 {
581e13c1 2346 /* Remove from list of root variables. */
8b93c638
JM
2347 if (rootlist == var->root)
2348 rootlist = var->root->next;
2349 else
2350 {
2351 prer = NULL;
2352 cr = rootlist;
2353 while ((cr != NULL) && (cr->rootvar != var))
2354 {
2355 prer = cr;
2356 cr = cr->next;
2357 }
2358 if (cr == NULL)
2359 {
8f7e195f
JB
2360 warning (_("Assertion failed: Could not find "
2361 "varobj \"%s\" in root list"),
3e43a32a 2362 var->obj_name);
8b93c638
JM
2363 return;
2364 }
2365 if (prer == NULL)
2366 rootlist = NULL;
2367 else
2368 prer->next = cr->next;
2369 }
8b93c638
JM
2370 }
2371
2372}
2373
581e13c1 2374/* Create and install a child of the parent of the given name. */
8b93c638 2375static struct varobj *
fba45db2 2376create_child (struct varobj *parent, int index, char *name)
b6313243
TT
2377{
2378 return create_child_with_value (parent, index, name,
2379 value_of_child (parent, index));
2380}
2381
85254831
KS
2382/* Does CHILD represent a child with no name? This happens when
2383 the child is an anonmous struct or union and it has no field name
2384 in its parent variable.
2385
2386 This has already been determined by *_describe_child. The easiest
2387 thing to do is to compare the child's name with ANONYMOUS_*_NAME. */
2388
2389static int
2390is_anonymous_child (struct varobj *child)
2391{
2392 return (strcmp (child->name, ANONYMOUS_STRUCT_NAME) == 0
2393 || strcmp (child->name, ANONYMOUS_UNION_NAME) == 0);
2394}
2395
b6313243
TT
2396static struct varobj *
2397create_child_with_value (struct varobj *parent, int index, const char *name,
2398 struct value *value)
8b93c638
JM
2399{
2400 struct varobj *child;
2401 char *childs_name;
2402
2403 child = new_variable ();
2404
581e13c1 2405 /* Name is allocated by name_of_child. */
b6313243
TT
2406 /* FIXME: xstrdup should not be here. */
2407 child->name = xstrdup (name);
8b93c638 2408 child->index = index;
8b93c638
JM
2409 child->parent = parent;
2410 child->root = parent->root;
85254831
KS
2411
2412 if (is_anonymous_child (child))
2413 childs_name = xstrprintf ("%s.%d_anonymous", parent->obj_name, index);
2414 else
2415 childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
8b93c638 2416 child->obj_name = childs_name;
85254831 2417
8b93c638
JM
2418 install_variable (child);
2419
acd65feb
VP
2420 /* Compute the type of the child. Must do this before
2421 calling install_new_value. */
2422 if (value != NULL)
2423 /* If the child had no evaluation errors, var->value
581e13c1 2424 will be non-NULL and contain a valid type. */
8264ba82 2425 child->type = value_actual_type (value, 0, NULL);
acd65feb 2426 else
581e13c1 2427 /* Otherwise, we must compute the type. */
acd65feb
VP
2428 child->type = (*child->root->lang->type_of_child) (child->parent,
2429 child->index);
2430 install_new_value (child, value, 1);
2431
8b93c638
JM
2432 return child;
2433}
8b93c638
JM
2434\f
2435
2436/*
2437 * Miscellaneous utility functions.
2438 */
2439
581e13c1 2440/* Allocate memory and initialize a new variable. */
8b93c638
JM
2441static struct varobj *
2442new_variable (void)
2443{
2444 struct varobj *var;
2445
2446 var = (struct varobj *) xmalloc (sizeof (struct varobj));
2447 var->name = NULL;
02142340 2448 var->path_expr = NULL;
8b93c638
JM
2449 var->obj_name = NULL;
2450 var->index = -1;
2451 var->type = NULL;
2452 var->value = NULL;
8b93c638
JM
2453 var->num_children = -1;
2454 var->parent = NULL;
2455 var->children = NULL;
2456 var->format = 0;
2457 var->root = NULL;
fb9b6b35 2458 var->updated = 0;
85265413 2459 var->print_value = NULL;
25d5ea92
VP
2460 var->frozen = 0;
2461 var->not_fetched = 0;
b6313243 2462 var->children_requested = 0;
0cc7d26f
TT
2463 var->from = -1;
2464 var->to = -1;
2465 var->constructor = 0;
b6313243 2466 var->pretty_printer = 0;
0cc7d26f
TT
2467 var->child_iter = 0;
2468 var->saved_item = 0;
8b93c638
JM
2469
2470 return var;
2471}
2472
581e13c1 2473/* Allocate memory and initialize a new root variable. */
8b93c638
JM
2474static struct varobj *
2475new_root_variable (void)
2476{
2477 struct varobj *var = new_variable ();
a109c7c1 2478
3e43a32a 2479 var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));
8b93c638
JM
2480 var->root->lang = NULL;
2481 var->root->exp = NULL;
2482 var->root->valid_block = NULL;
7a424e99 2483 var->root->frame = null_frame_id;
a5defcdc 2484 var->root->floating = 0;
8b93c638 2485 var->root->rootvar = NULL;
8756216b 2486 var->root->is_valid = 1;
8b93c638
JM
2487
2488 return var;
2489}
2490
581e13c1 2491/* Free any allocated memory associated with VAR. */
8b93c638 2492static void
fba45db2 2493free_variable (struct varobj *var)
8b93c638 2494{
d452c4bc
UW
2495#if HAVE_PYTHON
2496 if (var->pretty_printer)
2497 {
2498 struct cleanup *cleanup = varobj_ensure_python_env (var);
0cc7d26f
TT
2499 Py_XDECREF (var->constructor);
2500 Py_XDECREF (var->pretty_printer);
2501 Py_XDECREF (var->child_iter);
2502 Py_XDECREF (var->saved_item);
d452c4bc
UW
2503 do_cleanups (cleanup);
2504 }
2505#endif
2506
36746093
JK
2507 value_free (var->value);
2508
581e13c1 2509 /* Free the expression if this is a root variable. */
b2c2bd75 2510 if (is_root_p (var))
8b93c638 2511 {
3038237c 2512 xfree (var->root->exp);
8038e1e2 2513 xfree (var->root);
8b93c638
JM
2514 }
2515
8038e1e2
AC
2516 xfree (var->name);
2517 xfree (var->obj_name);
85265413 2518 xfree (var->print_value);
02142340 2519 xfree (var->path_expr);
8038e1e2 2520 xfree (var);
8b93c638
JM
2521}
2522
74b7792f
AC
2523static void
2524do_free_variable_cleanup (void *var)
2525{
2526 free_variable (var);
2527}
2528
2529static struct cleanup *
2530make_cleanup_free_variable (struct varobj *var)
2531{
2532 return make_cleanup (do_free_variable_cleanup, var);
2533}
2534
581e13c1 2535/* This returns the type of the variable. It also skips past typedefs
6766a268 2536 to return the real type of the variable.
94b66fa7
KS
2537
2538 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
581e13c1 2539 except within get_target_type and get_type. */
8b93c638 2540static struct type *
fba45db2 2541get_type (struct varobj *var)
8b93c638
JM
2542{
2543 struct type *type;
8b93c638 2544
a109c7c1 2545 type = var->type;
6766a268
DJ
2546 if (type != NULL)
2547 type = check_typedef (type);
8b93c638
JM
2548
2549 return type;
2550}
2551
6e2a9270
VP
2552/* Return the type of the value that's stored in VAR,
2553 or that would have being stored there if the
581e13c1 2554 value were accessible.
6e2a9270
VP
2555
2556 This differs from VAR->type in that VAR->type is always
2557 the true type of the expession in the source language.
2558 The return value of this function is the type we're
2559 actually storing in varobj, and using for displaying
2560 the values and for comparing previous and new values.
2561
2562 For example, top-level references are always stripped. */
2563static struct type *
2564get_value_type (struct varobj *var)
2565{
2566 struct type *type;
2567
2568 if (var->value)
2569 type = value_type (var->value);
2570 else
2571 type = var->type;
2572
2573 type = check_typedef (type);
2574
2575 if (TYPE_CODE (type) == TYPE_CODE_REF)
2576 type = get_target_type (type);
2577
2578 type = check_typedef (type);
2579
2580 return type;
2581}
2582
8b93c638 2583/* This returns the target type (or NULL) of TYPE, also skipping
94b66fa7
KS
2584 past typedefs, just like get_type ().
2585
2586 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
581e13c1 2587 except within get_target_type and get_type. */
8b93c638 2588static struct type *
fba45db2 2589get_target_type (struct type *type)
8b93c638
JM
2590{
2591 if (type != NULL)
2592 {
2593 type = TYPE_TARGET_TYPE (type);
6766a268
DJ
2594 if (type != NULL)
2595 type = check_typedef (type);
8b93c638
JM
2596 }
2597
2598 return type;
2599}
2600
2601/* What is the default display for this variable? We assume that
581e13c1 2602 everything is "natural". Any exceptions? */
8b93c638 2603static enum varobj_display_formats
fba45db2 2604variable_default_display (struct varobj *var)
8b93c638
JM
2605{
2606 return FORMAT_NATURAL;
2607}
2608
581e13c1 2609/* FIXME: The following should be generic for any pointer. */
8b93c638 2610static void
fba45db2 2611cppush (struct cpstack **pstack, char *name)
8b93c638
JM
2612{
2613 struct cpstack *s;
2614
2615 s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
2616 s->name = name;
2617 s->next = *pstack;
2618 *pstack = s;
2619}
2620
581e13c1 2621/* FIXME: The following should be generic for any pointer. */
8b93c638 2622static char *
fba45db2 2623cppop (struct cpstack **pstack)
8b93c638
JM
2624{
2625 struct cpstack *s;
2626 char *v;
2627
2628 if ((*pstack)->name == NULL && (*pstack)->next == NULL)
2629 return NULL;
2630
2631 s = *pstack;
2632 v = s->name;
2633 *pstack = (*pstack)->next;
b8c9b27d 2634 xfree (s);
8b93c638
JM
2635
2636 return v;
2637}
2638\f
2639/*
2640 * Language-dependencies
2641 */
2642
2643/* Common entry points */
2644
581e13c1 2645/* Get the language of variable VAR. */
8b93c638 2646static enum varobj_languages
fba45db2 2647variable_language (struct varobj *var)
8b93c638
JM
2648{
2649 enum varobj_languages lang;
2650
2651 switch (var->root->exp->language_defn->la_language)
2652 {
2653 default:
2654 case language_c:
2655 lang = vlang_c;
2656 break;
2657 case language_cplus:
2658 lang = vlang_cplus;
2659 break;
2660 case language_java:
2661 lang = vlang_java;
2662 break;
40591b7d
JCD
2663 case language_ada:
2664 lang = vlang_ada;
2665 break;
8b93c638
JM
2666 }
2667
2668 return lang;
2669}
2670
2671/* Return the number of children for a given variable.
2672 The result of this function is defined by the language
581e13c1 2673 implementation. The number of children returned by this function
8b93c638 2674 is the number of children that the user will see in the variable
581e13c1 2675 display. */
8b93c638 2676static int
fba45db2 2677number_of_children (struct varobj *var)
8b93c638 2678{
82ae4854 2679 return (*var->root->lang->number_of_children) (var);
8b93c638
JM
2680}
2681
3e43a32a 2682/* What is the expression for the root varobj VAR? Returns a malloc'd
581e13c1 2683 string. */
8b93c638 2684static char *
fba45db2 2685name_of_variable (struct varobj *var)
8b93c638
JM
2686{
2687 return (*var->root->lang->name_of_variable) (var);
2688}
2689
3e43a32a 2690/* What is the name of the INDEX'th child of VAR? Returns a malloc'd
581e13c1 2691 string. */
8b93c638 2692static char *
fba45db2 2693name_of_child (struct varobj *var, int index)
8b93c638
JM
2694{
2695 return (*var->root->lang->name_of_child) (var, index);
2696}
2697
2213e2be
YQ
2698/* If frame associated with VAR can be found, switch
2699 to it and return 1. Otherwise, return 0. */
2700
2701static int
2702check_scope (struct varobj *var)
2703{
2704 struct frame_info *fi;
2705 int scope;
2706
2707 fi = frame_find_by_id (var->root->frame);
2708 scope = fi != NULL;
2709
2710 if (fi)
2711 {
2712 CORE_ADDR pc = get_frame_pc (fi);
2713
2714 if (pc < BLOCK_START (var->root->valid_block) ||
2715 pc >= BLOCK_END (var->root->valid_block))
2716 scope = 0;
2717 else
2718 select_frame (fi);
2719 }
2720 return scope;
2721}
2722
2723/* Helper function to value_of_root. */
2724
2725static struct value *
2726value_of_root_1 (struct varobj **var_handle)
2727{
2728 struct value *new_val = NULL;
2729 struct varobj *var = *var_handle;
2730 int within_scope = 0;
2731 struct cleanup *back_to;
2732
2733 /* Only root variables can be updated... */
2734 if (!is_root_p (var))
2735 /* Not a root var. */
2736 return NULL;
2737
2738 back_to = make_cleanup_restore_current_thread ();
2739
2740 /* Determine whether the variable is still around. */
2741 if (var->root->valid_block == NULL || var->root->floating)
2742 within_scope = 1;
2743 else if (var->root->thread_id == 0)
2744 {
2745 /* The program was single-threaded when the variable object was
2746 created. Technically, it's possible that the program became
2747 multi-threaded since then, but we don't support such
2748 scenario yet. */
2749 within_scope = check_scope (var);
2750 }
2751 else
2752 {
2753 ptid_t ptid = thread_id_to_pid (var->root->thread_id);
2754 if (in_thread_list (ptid))
2755 {
2756 switch_to_thread (ptid);
2757 within_scope = check_scope (var);
2758 }
2759 }
2760
2761 if (within_scope)
2762 {
2763 volatile struct gdb_exception except;
2764
2765 /* We need to catch errors here, because if evaluate
2766 expression fails we want to just return NULL. */
2767 TRY_CATCH (except, RETURN_MASK_ERROR)
2768 {
2769 new_val = evaluate_expression (var->root->exp);
2770 }
2771 }
2772
2773 do_cleanups (back_to);
2774
2775 return new_val;
2776}
2777
a5defcdc
VP
2778/* What is the ``struct value *'' of the root variable VAR?
2779 For floating variable object, evaluation can get us a value
2780 of different type from what is stored in varobj already. In
2781 that case:
2782 - *type_changed will be set to 1
2783 - old varobj will be freed, and new one will be
2784 created, with the same name.
2785 - *var_handle will be set to the new varobj
2786 Otherwise, *type_changed will be set to 0. */
30b28db1 2787static struct value *
fba45db2 2788value_of_root (struct varobj **var_handle, int *type_changed)
8b93c638 2789{
73a93a32
JI
2790 struct varobj *var;
2791
2792 if (var_handle == NULL)
2793 return NULL;
2794
2795 var = *var_handle;
2796
2797 /* This should really be an exception, since this should
581e13c1 2798 only get called with a root variable. */
73a93a32 2799
b2c2bd75 2800 if (!is_root_p (var))
73a93a32
JI
2801 return NULL;
2802
a5defcdc 2803 if (var->root->floating)
73a93a32
JI
2804 {
2805 struct varobj *tmp_var;
2806 char *old_type, *new_type;
6225abfa 2807
73a93a32
JI
2808 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
2809 USE_SELECTED_FRAME);
2810 if (tmp_var == NULL)
2811 {
2812 return NULL;
2813 }
6225abfa 2814 old_type = varobj_get_type (var);
73a93a32 2815 new_type = varobj_get_type (tmp_var);
72330bd6 2816 if (strcmp (old_type, new_type) == 0)
73a93a32 2817 {
fcacd99f
VP
2818 /* The expression presently stored inside var->root->exp
2819 remembers the locations of local variables relatively to
2820 the frame where the expression was created (in DWARF location
2821 button, for example). Naturally, those locations are not
2822 correct in other frames, so update the expression. */
2823
2824 struct expression *tmp_exp = var->root->exp;
a109c7c1 2825
fcacd99f
VP
2826 var->root->exp = tmp_var->root->exp;
2827 tmp_var->root->exp = tmp_exp;
2828
73a93a32
JI
2829 varobj_delete (tmp_var, NULL, 0);
2830 *type_changed = 0;
2831 }
2832 else
2833 {
1b36a34b 2834 tmp_var->obj_name = xstrdup (var->obj_name);
0cc7d26f
TT
2835 tmp_var->from = var->from;
2836 tmp_var->to = var->to;
a5defcdc
VP
2837 varobj_delete (var, NULL, 0);
2838
73a93a32
JI
2839 install_variable (tmp_var);
2840 *var_handle = tmp_var;
705da579 2841 var = *var_handle;
73a93a32
JI
2842 *type_changed = 1;
2843 }
74dddad3
MS
2844 xfree (old_type);
2845 xfree (new_type);
73a93a32
JI
2846 }
2847 else
2848 {
2849 *type_changed = 0;
2850 }
2851
7a290c40
JB
2852 {
2853 struct value *value;
2854
2213e2be 2855 value = value_of_root_1 (var_handle);
7a290c40
JB
2856 if (var->value == NULL || value == NULL)
2857 {
2858 /* For root varobj-s, a NULL value indicates a scoping issue.
2859 So, nothing to do in terms of checking for mutations. */
2860 }
2861 else if (varobj_value_has_mutated (var, value, value_type (value)))
2862 {
2863 /* The type has mutated, so the children are no longer valid.
2864 Just delete them, and tell our caller that the type has
2865 changed. */
2866 varobj_delete (var, NULL, 1 /* only_children */);
2867 var->num_children = -1;
2868 var->to = -1;
2869 var->from = -1;
2870 *type_changed = 1;
2871 }
2872 return value;
2873 }
8b93c638
JM
2874}
2875
581e13c1 2876/* What is the ``struct value *'' for the INDEX'th child of PARENT? */
30b28db1 2877static struct value *
fba45db2 2878value_of_child (struct varobj *parent, int index)
8b93c638 2879{
30b28db1 2880 struct value *value;
8b93c638
JM
2881
2882 value = (*parent->root->lang->value_of_child) (parent, index);
2883
8b93c638
JM
2884 return value;
2885}
2886
581e13c1 2887/* GDB already has a command called "value_of_variable". Sigh. */
8b93c638 2888static char *
de051565 2889my_value_of_variable (struct varobj *var, enum varobj_display_formats format)
8b93c638 2890{
8756216b 2891 if (var->root->is_valid)
0cc7d26f
TT
2892 {
2893 if (var->pretty_printer)
2894 return value_get_print_value (var->value, var->format, var);
2895 return (*var->root->lang->value_of_variable) (var, format);
2896 }
8756216b
DP
2897 else
2898 return NULL;
8b93c638
JM
2899}
2900
85265413 2901static char *
b6313243 2902value_get_print_value (struct value *value, enum varobj_display_formats format,
d452c4bc 2903 struct varobj *var)
85265413 2904{
57e66780 2905 struct ui_file *stb;
621c8364 2906 struct cleanup *old_chain;
ac91cd70 2907 char *thevalue = NULL;
79a45b7d 2908 struct value_print_options opts;
be759fcf
PM
2909 struct type *type = NULL;
2910 long len = 0;
2911 char *encoding = NULL;
2912 struct gdbarch *gdbarch = NULL;
3a182a69
JK
2913 /* Initialize it just to avoid a GCC false warning. */
2914 CORE_ADDR str_addr = 0;
09ca9e2e 2915 int string_print = 0;
57e66780
DJ
2916
2917 if (value == NULL)
2918 return NULL;
2919
621c8364
TT
2920 stb = mem_fileopen ();
2921 old_chain = make_cleanup_ui_file_delete (stb);
2922
be759fcf 2923 gdbarch = get_type_arch (value_type (value));
b6313243 2924#if HAVE_PYTHON
0646da15
TT
2925 if (gdb_python_initialized)
2926 {
2927 PyObject *value_formatter = var->pretty_printer;
d452c4bc 2928
0646da15 2929 varobj_ensure_python_env (var);
09ca9e2e 2930
0646da15
TT
2931 if (value_formatter)
2932 {
2933 /* First check to see if we have any children at all. If so,
2934 we simply return {...}. */
2935 if (dynamic_varobj_has_child_method (var))
2936 {
2937 do_cleanups (old_chain);
2938 return xstrdup ("{...}");
2939 }
b6313243 2940
0646da15
TT
2941 if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst))
2942 {
2943 struct value *replacement;
2944 PyObject *output = NULL;
2945
2946 output = apply_varobj_pretty_printer (value_formatter,
2947 &replacement,
2948 stb);
2949
2950 /* If we have string like output ... */
2951 if (output)
2952 {
2953 make_cleanup_py_decref (output);
2954
2955 /* If this is a lazy string, extract it. For lazy
2956 strings we always print as a string, so set
2957 string_print. */
2958 if (gdbpy_is_lazy_string (output))
2959 {
2960 gdbpy_extract_lazy_string (output, &str_addr, &type,
2961 &len, &encoding);
2962 make_cleanup (free_current_contents, &encoding);
2963 string_print = 1;
2964 }
2965 else
2966 {
2967 /* If it is a regular (non-lazy) string, extract
2968 it and copy the contents into THEVALUE. If the
2969 hint says to print it as a string, set
2970 string_print. Otherwise just return the extracted
2971 string as a value. */
2972
2973 char *s = python_string_to_target_string (output);
2974
2975 if (s)
2976 {
2977 char *hint;
2978
2979 hint = gdbpy_get_display_hint (value_formatter);
2980 if (hint)
2981 {
2982 if (!strcmp (hint, "string"))
2983 string_print = 1;
2984 xfree (hint);
2985 }
2986
2987 len = strlen (s);
2988 thevalue = xmemdup (s, len + 1, len + 1);
2989 type = builtin_type (gdbarch)->builtin_char;
2990 xfree (s);
2991
2992 if (!string_print)
2993 {
2994 do_cleanups (old_chain);
2995 return thevalue;
2996 }
2997
2998 make_cleanup (xfree, thevalue);
2999 }
3000 else
3001 gdbpy_print_stack ();
3002 }
3003 }
3004 /* If the printer returned a replacement value, set VALUE
3005 to REPLACEMENT. If there is not a replacement value,
3006 just use the value passed to this function. */
3007 if (replacement)
3008 value = replacement;
3009 }
3010 }
3011 }
b6313243
TT
3012#endif
3013
79a45b7d
TT
3014 get_formatted_print_options (&opts, format_code[(int) format]);
3015 opts.deref_ref = 0;
b6313243 3016 opts.raw = 1;
00bd41d6
PM
3017
3018 /* If the THEVALUE has contents, it is a regular string. */
b6313243 3019 if (thevalue)
ac91cd70 3020 LA_PRINT_STRING (stb, type, (gdb_byte *) thevalue, len, encoding, 0, &opts);
09ca9e2e 3021 else if (string_print)
00bd41d6
PM
3022 /* Otherwise, if string_print is set, and it is not a regular
3023 string, it is a lazy string. */
09ca9e2e 3024 val_print_string (type, encoding, str_addr, len, stb, &opts);
b6313243 3025 else
00bd41d6 3026 /* All other cases. */
b6313243 3027 common_val_print (value, stb, 0, &opts, current_language);
00bd41d6 3028
759ef836 3029 thevalue = ui_file_xstrdup (stb, NULL);
57e66780 3030
85265413
NR
3031 do_cleanups (old_chain);
3032 return thevalue;
3033}
3034
340a7723
NR
3035int
3036varobj_editable_p (struct varobj *var)
3037{
3038 struct type *type;
340a7723
NR
3039
3040 if (!(var->root->is_valid && var->value && VALUE_LVAL (var->value)))
3041 return 0;
3042
3043 type = get_value_type (var);
3044
3045 switch (TYPE_CODE (type))
3046 {
3047 case TYPE_CODE_STRUCT:
3048 case TYPE_CODE_UNION:
3049 case TYPE_CODE_ARRAY:
3050 case TYPE_CODE_FUNC:
3051 case TYPE_CODE_METHOD:
3052 return 0;
3053 break;
3054
3055 default:
3056 return 1;
3057 break;
3058 }
3059}
3060
d32cafc7 3061/* Call VAR's value_is_changeable_p language-specific callback. */
acd65feb 3062
8b93c638 3063static int
b2c2bd75 3064varobj_value_is_changeable_p (struct varobj *var)
8b93c638 3065{
d32cafc7 3066 return var->root->lang->value_is_changeable_p (var);
8b93c638
JM
3067}
3068
5a413362
VP
3069/* Return 1 if that varobj is floating, that is is always evaluated in the
3070 selected frame, and not bound to thread/frame. Such variable objects
3071 are created using '@' as frame specifier to -var-create. */
3072int
3073varobj_floating_p (struct varobj *var)
3074{
3075 return var->root->floating;
3076}
3077
2024f65a
VP
3078/* Given the value and the type of a variable object,
3079 adjust the value and type to those necessary
3080 for getting children of the variable object.
3081 This includes dereferencing top-level references
3082 to all types and dereferencing pointers to
581e13c1 3083 structures.
2024f65a 3084
8264ba82
AG
3085 If LOOKUP_ACTUAL_TYPE is set the enclosing type of the
3086 value will be fetched and if it differs from static type
3087 the value will be casted to it.
3088
581e13c1 3089 Both TYPE and *TYPE should be non-null. VALUE
2024f65a
VP
3090 can be null if we want to only translate type.
3091 *VALUE can be null as well -- if the parent
581e13c1 3092 value is not known.
02142340
VP
3093
3094 If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
b6313243 3095 depending on whether pointer was dereferenced
02142340 3096 in this function. */
2024f65a
VP
3097static void
3098adjust_value_for_child_access (struct value **value,
02142340 3099 struct type **type,
8264ba82
AG
3100 int *was_ptr,
3101 int lookup_actual_type)
2024f65a
VP
3102{
3103 gdb_assert (type && *type);
3104
02142340
VP
3105 if (was_ptr)
3106 *was_ptr = 0;
3107
2024f65a
VP
3108 *type = check_typedef (*type);
3109
3110 /* The type of value stored in varobj, that is passed
3111 to us, is already supposed to be
3112 reference-stripped. */
3113
3114 gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
3115
3116 /* Pointers to structures are treated just like
3117 structures when accessing children. Don't
3118 dererences pointers to other types. */
3119 if (TYPE_CODE (*type) == TYPE_CODE_PTR)
3120 {
3121 struct type *target_type = get_target_type (*type);
3122 if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
3123 || TYPE_CODE (target_type) == TYPE_CODE_UNION)
3124 {
3125 if (value && *value)
3f4178d6 3126 {
8e7b59a5 3127 volatile struct gdb_exception except;
a109c7c1 3128
8e7b59a5
KS
3129 TRY_CATCH (except, RETURN_MASK_ERROR)
3130 {
3131 *value = value_ind (*value);
3132 }
3133
3134 if (except.reason < 0)
3f4178d6
DJ
3135 *value = NULL;
3136 }
2024f65a 3137 *type = target_type;
02142340
VP
3138 if (was_ptr)
3139 *was_ptr = 1;
2024f65a
VP
3140 }
3141 }
3142
3143 /* The 'get_target_type' function calls check_typedef on
3144 result, so we can immediately check type code. No
3145 need to call check_typedef here. */
8264ba82
AG
3146
3147 /* Access a real type of the value (if necessary and possible). */
3148 if (value && *value && lookup_actual_type)
3149 {
3150 struct type *enclosing_type;
3151 int real_type_found = 0;
3152
3153 enclosing_type = value_actual_type (*value, 1, &real_type_found);
3154 if (real_type_found)
3155 {
3156 *type = enclosing_type;
3157 *value = value_cast (enclosing_type, *value);
3158 }
3159 }
2024f65a
VP
3160}
3161
d32cafc7
JB
3162/* Implement the "value_is_changeable_p" varobj callback for most
3163 languages. */
3164
3165static int
3166default_value_is_changeable_p (struct varobj *var)
3167{
3168 int r;
3169 struct type *type;
3170
3171 if (CPLUS_FAKE_CHILD (var))
3172 return 0;
3173
3174 type = get_value_type (var);
3175
3176 switch (TYPE_CODE (type))
3177 {
3178 case TYPE_CODE_STRUCT:
3179 case TYPE_CODE_UNION:
3180 case TYPE_CODE_ARRAY:
3181 r = 0;
3182 break;
3183
3184 default:
3185 r = 1;
3186 }
3187
3188 return r;
3189}
3190
8b93c638 3191/* C */
d32cafc7 3192
8b93c638 3193static int
fba45db2 3194c_number_of_children (struct varobj *var)
8b93c638 3195{
2024f65a
VP
3196 struct type *type = get_value_type (var);
3197 int children = 0;
8b93c638 3198 struct type *target;
8b93c638 3199
8264ba82 3200 adjust_value_for_child_access (NULL, &type, NULL, 0);
8b93c638 3201 target = get_target_type (type);
8b93c638
JM
3202
3203 switch (TYPE_CODE (type))
3204 {
3205 case TYPE_CODE_ARRAY:
3206 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
d78df370 3207 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
8b93c638
JM
3208 children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
3209 else
74a44383
DJ
3210 /* If we don't know how many elements there are, don't display
3211 any. */
3212 children = 0;
8b93c638
JM
3213 break;
3214
3215 case TYPE_CODE_STRUCT:
3216 case TYPE_CODE_UNION:
3217 children = TYPE_NFIELDS (type);
3218 break;
3219
3220 case TYPE_CODE_PTR:
581e13c1 3221 /* The type here is a pointer to non-struct. Typically, pointers
2024f65a
VP
3222 have one child, except for function ptrs, which have no children,
3223 and except for void*, as we don't know what to show.
3224
0755e6c1
FN
3225 We can show char* so we allow it to be dereferenced. If you decide
3226 to test for it, please mind that a little magic is necessary to
3227 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
581e13c1 3228 TYPE_NAME == "char". */
2024f65a
VP
3229 if (TYPE_CODE (target) == TYPE_CODE_FUNC
3230 || TYPE_CODE (target) == TYPE_CODE_VOID)
3231 children = 0;
3232 else
3233 children = 1;
8b93c638
JM
3234 break;
3235
3236 default:
581e13c1 3237 /* Other types have no children. */
8b93c638
JM
3238 break;
3239 }
3240
3241 return children;
3242}
3243
3244static char *
fba45db2 3245c_name_of_variable (struct varobj *parent)
8b93c638 3246{
1b36a34b 3247 return xstrdup (parent->name);
8b93c638
JM
3248}
3249
bbec2603
VP
3250/* Return the value of element TYPE_INDEX of a structure
3251 value VALUE. VALUE's type should be a structure,
581e13c1 3252 or union, or a typedef to struct/union.
bbec2603
VP
3253
3254 Returns NULL if getting the value fails. Never throws. */
3255static struct value *
3256value_struct_element_index (struct value *value, int type_index)
8b93c638 3257{
bbec2603
VP
3258 struct value *result = NULL;
3259 volatile struct gdb_exception e;
bbec2603 3260 struct type *type = value_type (value);
a109c7c1 3261
bbec2603
VP
3262 type = check_typedef (type);
3263
3264 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
3265 || TYPE_CODE (type) == TYPE_CODE_UNION);
8b93c638 3266
bbec2603
VP
3267 TRY_CATCH (e, RETURN_MASK_ERROR)
3268 {
d6a843b5 3269 if (field_is_static (&TYPE_FIELD (type, type_index)))
bbec2603
VP
3270 result = value_static_field (type, type_index);
3271 else
3272 result = value_primitive_field (value, 0, type_index, type);
3273 }
3274 if (e.reason < 0)
3275 {
3276 return NULL;
3277 }
3278 else
3279 {
3280 return result;
3281 }
3282}
3283
3284/* Obtain the information about child INDEX of the variable
581e13c1 3285 object PARENT.
bbec2603
VP
3286 If CNAME is not null, sets *CNAME to the name of the child relative
3287 to the parent.
3288 If CVALUE is not null, sets *CVALUE to the value of the child.
3289 If CTYPE is not null, sets *CTYPE to the type of the child.
3290
3291 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
3292 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
3293 to NULL. */
3294static void
3295c_describe_child (struct varobj *parent, int index,
02142340
VP
3296 char **cname, struct value **cvalue, struct type **ctype,
3297 char **cfull_expression)
bbec2603
VP
3298{
3299 struct value *value = parent->value;
2024f65a 3300 struct type *type = get_value_type (parent);
02142340
VP
3301 char *parent_expression = NULL;
3302 int was_ptr;
8e7b59a5 3303 volatile struct gdb_exception except;
bbec2603
VP
3304
3305 if (cname)
3306 *cname = NULL;
3307 if (cvalue)
3308 *cvalue = NULL;
3309 if (ctype)
3310 *ctype = NULL;
02142340
VP
3311 if (cfull_expression)
3312 {
3313 *cfull_expression = NULL;
85254831 3314 parent_expression = varobj_get_path_expr (get_path_expr_parent (parent));
02142340 3315 }
8264ba82 3316 adjust_value_for_child_access (&value, &type, &was_ptr, 0);
bbec2603 3317
8b93c638
JM
3318 switch (TYPE_CODE (type))
3319 {
3320 case TYPE_CODE_ARRAY:
bbec2603 3321 if (cname)
3e43a32a
MS
3322 *cname
3323 = xstrdup (int_string (index
3324 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
3325 10, 1, 0, 0));
bbec2603
VP
3326
3327 if (cvalue && value)
3328 {
3329 int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
a109c7c1 3330
8e7b59a5
KS
3331 TRY_CATCH (except, RETURN_MASK_ERROR)
3332 {
3333 *cvalue = value_subscript (value, real_index);
3334 }
bbec2603
VP
3335 }
3336
3337 if (ctype)
3338 *ctype = get_target_type (type);
3339
02142340 3340 if (cfull_expression)
43bbcdc2
PH
3341 *cfull_expression =
3342 xstrprintf ("(%s)[%s]", parent_expression,
3343 int_string (index
3344 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
3345 10, 1, 0, 0));
02142340
VP
3346
3347
8b93c638
JM
3348 break;
3349
3350 case TYPE_CODE_STRUCT:
3351 case TYPE_CODE_UNION:
85254831 3352 {
0d5cff50 3353 const char *field_name;
bbec2603 3354
85254831
KS
3355 /* If the type is anonymous and the field has no name,
3356 set an appropriate name. */
3357 field_name = TYPE_FIELD_NAME (type, index);
3358 if (field_name == NULL || *field_name == '\0')
3359 {
3360 if (cname)
3361 {
3362 if (TYPE_CODE (TYPE_FIELD_TYPE (type, index))
3363 == TYPE_CODE_STRUCT)
3364 *cname = xstrdup (ANONYMOUS_STRUCT_NAME);
3365 else
3366 *cname = xstrdup (ANONYMOUS_UNION_NAME);
3367 }
bbec2603 3368
85254831
KS
3369 if (cfull_expression)
3370 *cfull_expression = xstrdup ("");
3371 }
3372 else
3373 {
3374 if (cname)
3375 *cname = xstrdup (field_name);
bbec2603 3376
85254831
KS
3377 if (cfull_expression)
3378 {
3379 char *join = was_ptr ? "->" : ".";
a109c7c1 3380
85254831
KS
3381 *cfull_expression = xstrprintf ("(%s)%s%s", parent_expression,
3382 join, field_name);
3383 }
3384 }
02142340 3385
85254831
KS
3386 if (cvalue && value)
3387 {
3388 /* For C, varobj index is the same as type index. */
3389 *cvalue = value_struct_element_index (value, index);
3390 }
3391
3392 if (ctype)
3393 *ctype = TYPE_FIELD_TYPE (type, index);
3394 }
8b93c638
JM
3395 break;
3396
3397 case TYPE_CODE_PTR:
bbec2603
VP
3398 if (cname)
3399 *cname = xstrprintf ("*%s", parent->name);
8b93c638 3400
bbec2603 3401 if (cvalue && value)
3f4178d6 3402 {
8e7b59a5
KS
3403 TRY_CATCH (except, RETURN_MASK_ERROR)
3404 {
3405 *cvalue = value_ind (value);
3406 }
a109c7c1 3407
8e7b59a5 3408 if (except.reason < 0)
3f4178d6
DJ
3409 *cvalue = NULL;
3410 }
bbec2603 3411
2024f65a
VP
3412 /* Don't use get_target_type because it calls
3413 check_typedef and here, we want to show the true
3414 declared type of the variable. */
bbec2603 3415 if (ctype)
2024f65a 3416 *ctype = TYPE_TARGET_TYPE (type);
02142340
VP
3417
3418 if (cfull_expression)
3419 *cfull_expression = xstrprintf ("*(%s)", parent_expression);
bbec2603 3420
8b93c638
JM
3421 break;
3422
3423 default:
581e13c1 3424 /* This should not happen. */
bbec2603
VP
3425 if (cname)
3426 *cname = xstrdup ("???");
02142340
VP
3427 if (cfull_expression)
3428 *cfull_expression = xstrdup ("???");
581e13c1 3429 /* Don't set value and type, we don't know then. */
8b93c638 3430 }
bbec2603 3431}
8b93c638 3432
bbec2603
VP
3433static char *
3434c_name_of_child (struct varobj *parent, int index)
3435{
3436 char *name;
a109c7c1 3437
02142340 3438 c_describe_child (parent, index, &name, NULL, NULL, NULL);
8b93c638
JM
3439 return name;
3440}
3441
02142340
VP
3442static char *
3443c_path_expr_of_child (struct varobj *child)
3444{
3445 c_describe_child (child->parent, child->index, NULL, NULL, NULL,
3446 &child->path_expr);
3447 return child->path_expr;
3448}
3449
30b28db1 3450static struct value *
fba45db2 3451c_value_of_child (struct varobj *parent, int index)
8b93c638 3452{
bbec2603 3453 struct value *value = NULL;
8b93c638 3454
a109c7c1 3455 c_describe_child (parent, index, NULL, &value, NULL, NULL);
8b93c638
JM
3456 return value;
3457}
3458
3459static struct type *
fba45db2 3460c_type_of_child (struct varobj *parent, int index)
8b93c638 3461{
bbec2603 3462 struct type *type = NULL;
a109c7c1 3463
02142340 3464 c_describe_child (parent, index, NULL, NULL, &type, NULL);
8b93c638
JM
3465 return type;
3466}
3467
8b93c638 3468static char *
de051565 3469c_value_of_variable (struct varobj *var, enum varobj_display_formats format)
8b93c638 3470{
14b3d9c9
JB
3471 /* BOGUS: if val_print sees a struct/class, or a reference to one,
3472 it will print out its children instead of "{...}". So we need to
3473 catch that case explicitly. */
3474 struct type *type = get_type (var);
e64d9b3d 3475
581e13c1 3476 /* Strip top-level references. */
14b3d9c9
JB
3477 while (TYPE_CODE (type) == TYPE_CODE_REF)
3478 type = check_typedef (TYPE_TARGET_TYPE (type));
3479
3480 switch (TYPE_CODE (type))
8b93c638
JM
3481 {
3482 case TYPE_CODE_STRUCT:
3483 case TYPE_CODE_UNION:
3484 return xstrdup ("{...}");
3485 /* break; */
3486
3487 case TYPE_CODE_ARRAY:
3488 {
e64d9b3d 3489 char *number;
a109c7c1 3490
b435e160 3491 number = xstrprintf ("[%d]", var->num_children);
e64d9b3d 3492 return (number);
8b93c638
JM
3493 }
3494 /* break; */
3495
3496 default:
3497 {
575bbeb6
KS
3498 if (var->value == NULL)
3499 {
3500 /* This can happen if we attempt to get the value of a struct
581e13c1
MS
3501 member when the parent is an invalid pointer. This is an
3502 error condition, so we should tell the caller. */
575bbeb6
KS
3503 return NULL;
3504 }
3505 else
3506 {
25d5ea92
VP
3507 if (var->not_fetched && value_lazy (var->value))
3508 /* Frozen variable and no value yet. We don't
3509 implicitly fetch the value. MI response will
3510 use empty string for the value, which is OK. */
3511 return NULL;
3512
b2c2bd75 3513 gdb_assert (varobj_value_is_changeable_p (var));
acd65feb 3514 gdb_assert (!value_lazy (var->value));
de051565
MK
3515
3516 /* If the specified format is the current one,
581e13c1 3517 we can reuse print_value. */
de051565
MK
3518 if (format == var->format)
3519 return xstrdup (var->print_value);
3520 else
d452c4bc 3521 return value_get_print_value (var->value, format, var);
85265413 3522 }
e64d9b3d 3523 }
8b93c638
JM
3524 }
3525}
3526\f
3527
3528/* C++ */
3529
3530static int
fba45db2 3531cplus_number_of_children (struct varobj *var)
8b93c638 3532{
8264ba82 3533 struct value *value = NULL;
8b93c638
JM
3534 struct type *type;
3535 int children, dont_know;
8264ba82
AG
3536 int lookup_actual_type = 0;
3537 struct value_print_options opts;
8b93c638
JM
3538
3539 dont_know = 1;
3540 children = 0;
3541
8264ba82
AG
3542 get_user_print_options (&opts);
3543
8b93c638
JM
3544 if (!CPLUS_FAKE_CHILD (var))
3545 {
2024f65a 3546 type = get_value_type (var);
8264ba82
AG
3547
3548 /* It is necessary to access a real type (via RTTI). */
3549 if (opts.objectprint)
3550 {
3551 value = var->value;
3552 lookup_actual_type = (TYPE_CODE (var->type) == TYPE_CODE_REF
3553 || TYPE_CODE (var->type) == TYPE_CODE_PTR);
3554 }
3555 adjust_value_for_child_access (&value, &type, NULL, lookup_actual_type);
8b93c638
JM
3556
3557 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
72330bd6 3558 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
8b93c638
JM
3559 {
3560 int kids[3];
3561
3562 cplus_class_num_children (type, kids);
3563 if (kids[v_public] != 0)
3564 children++;
3565 if (kids[v_private] != 0)
3566 children++;
3567 if (kids[v_protected] != 0)
3568 children++;
3569
581e13c1 3570 /* Add any baseclasses. */
8b93c638
JM
3571 children += TYPE_N_BASECLASSES (type);
3572 dont_know = 0;
3573
581e13c1 3574 /* FIXME: save children in var. */
8b93c638
JM
3575 }
3576 }
3577 else
3578 {
3579 int kids[3];
3580
2024f65a 3581 type = get_value_type (var->parent);
8264ba82
AG
3582
3583 /* It is necessary to access a real type (via RTTI). */
3584 if (opts.objectprint)
3585 {
3586 struct varobj *parent = var->parent;
3587
3588 value = parent->value;
3589 lookup_actual_type = (TYPE_CODE (parent->type) == TYPE_CODE_REF
3590 || TYPE_CODE (parent->type) == TYPE_CODE_PTR);
3591 }
3592 adjust_value_for_child_access (&value, &type, NULL, lookup_actual_type);
8b93c638
JM
3593
3594 cplus_class_num_children (type, kids);
6e382aa3 3595 if (strcmp (var->name, "public") == 0)
8b93c638 3596 children = kids[v_public];
6e382aa3 3597 else if (strcmp (var->name, "private") == 0)
8b93c638
JM
3598 children = kids[v_private];
3599 else
3600 children = kids[v_protected];
3601 dont_know = 0;
3602 }
3603
3604 if (dont_know)
3605 children = c_number_of_children (var);
3606
3607 return children;
3608}
3609
3610/* Compute # of public, private, and protected variables in this class.
3611 That means we need to descend into all baseclasses and find out
581e13c1 3612 how many are there, too. */
8b93c638 3613static void
1669605f 3614cplus_class_num_children (struct type *type, int children[3])
8b93c638 3615{
d48cc9dd
DJ
3616 int i, vptr_fieldno;
3617 struct type *basetype = NULL;
8b93c638
JM
3618
3619 children[v_public] = 0;
3620 children[v_private] = 0;
3621 children[v_protected] = 0;
3622
d48cc9dd 3623 vptr_fieldno = get_vptr_fieldno (type, &basetype);
8b93c638
JM
3624 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
3625 {
d48cc9dd
DJ
3626 /* If we have a virtual table pointer, omit it. Even if virtual
3627 table pointers are not specifically marked in the debug info,
3628 they should be artificial. */
3629 if ((type == basetype && i == vptr_fieldno)
3630 || TYPE_FIELD_ARTIFICIAL (type, i))
8b93c638
JM
3631 continue;
3632
3633 if (TYPE_FIELD_PROTECTED (type, i))
3634 children[v_protected]++;
3635 else if (TYPE_FIELD_PRIVATE (type, i))
3636 children[v_private]++;
3637 else
3638 children[v_public]++;
3639 }
3640}
3641
3642static char *
fba45db2 3643cplus_name_of_variable (struct varobj *parent)
8b93c638
JM
3644{
3645 return c_name_of_variable (parent);
3646}
3647
2024f65a
VP
3648enum accessibility { private_field, protected_field, public_field };
3649
3650/* Check if field INDEX of TYPE has the specified accessibility.
3651 Return 0 if so and 1 otherwise. */
3652static int
3653match_accessibility (struct type *type, int index, enum accessibility acc)
8b93c638 3654{
2024f65a
VP
3655 if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
3656 return 1;
3657 else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
3658 return 1;
3659 else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
3660 && !TYPE_FIELD_PROTECTED (type, index))
3661 return 1;
3662 else
3663 return 0;
3664}
3665
3666static void
3667cplus_describe_child (struct varobj *parent, int index,
02142340
VP
3668 char **cname, struct value **cvalue, struct type **ctype,
3669 char **cfull_expression)
2024f65a 3670{
2024f65a 3671 struct value *value;
8b93c638 3672 struct type *type;
02142340 3673 int was_ptr;
8264ba82 3674 int lookup_actual_type = 0;
02142340 3675 char *parent_expression = NULL;
8264ba82
AG
3676 struct varobj *var;
3677 struct value_print_options opts;
8b93c638 3678
2024f65a
VP
3679 if (cname)
3680 *cname = NULL;
3681 if (cvalue)
3682 *cvalue = NULL;
3683 if (ctype)
3684 *ctype = NULL;
02142340
VP
3685 if (cfull_expression)
3686 *cfull_expression = NULL;
2024f65a 3687
8264ba82
AG
3688 get_user_print_options (&opts);
3689
3690 var = (CPLUS_FAKE_CHILD (parent)) ? parent->parent : parent;
3691 if (opts.objectprint)
3692 lookup_actual_type = (TYPE_CODE (var->type) == TYPE_CODE_REF
3693 || TYPE_CODE (var->type) == TYPE_CODE_PTR);
3694 value = var->value;
3695 type = get_value_type (var);
3696 if (cfull_expression)
3697 parent_expression = varobj_get_path_expr (get_path_expr_parent (var));
8b93c638 3698
8264ba82 3699 adjust_value_for_child_access (&value, &type, &was_ptr, lookup_actual_type);
2024f65a
VP
3700
3701 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
3f4178d6 3702 || TYPE_CODE (type) == TYPE_CODE_UNION)
8b93c638 3703 {
02142340 3704 char *join = was_ptr ? "->" : ".";
a109c7c1 3705
8b93c638
JM
3706 if (CPLUS_FAKE_CHILD (parent))
3707 {
6e382aa3
JJ
3708 /* The fields of the class type are ordered as they
3709 appear in the class. We are given an index for a
3710 particular access control type ("public","protected",
3711 or "private"). We must skip over fields that don't
3712 have the access control we are looking for to properly
581e13c1 3713 find the indexed field. */
6e382aa3 3714 int type_index = TYPE_N_BASECLASSES (type);
2024f65a 3715 enum accessibility acc = public_field;
d48cc9dd
DJ
3716 int vptr_fieldno;
3717 struct type *basetype = NULL;
0d5cff50 3718 const char *field_name;
d48cc9dd
DJ
3719
3720 vptr_fieldno = get_vptr_fieldno (type, &basetype);
6e382aa3 3721 if (strcmp (parent->name, "private") == 0)
2024f65a 3722 acc = private_field;
6e382aa3 3723 else if (strcmp (parent->name, "protected") == 0)
2024f65a
VP
3724 acc = protected_field;
3725
3726 while (index >= 0)
6e382aa3 3727 {
d48cc9dd
DJ
3728 if ((type == basetype && type_index == vptr_fieldno)
3729 || TYPE_FIELD_ARTIFICIAL (type, type_index))
2024f65a
VP
3730 ; /* ignore vptr */
3731 else if (match_accessibility (type, type_index, acc))
6e382aa3
JJ
3732 --index;
3733 ++type_index;
6e382aa3 3734 }
2024f65a
VP
3735 --type_index;
3736
85254831
KS
3737 /* If the type is anonymous and the field has no name,
3738 set an appopriate name. */
3739 field_name = TYPE_FIELD_NAME (type, type_index);
3740 if (field_name == NULL || *field_name == '\0')
3741 {
3742 if (cname)
3743 {
3744 if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
3745 == TYPE_CODE_STRUCT)
3746 *cname = xstrdup (ANONYMOUS_STRUCT_NAME);
3747 else if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
3748 == TYPE_CODE_UNION)
3749 *cname = xstrdup (ANONYMOUS_UNION_NAME);
3750 }
3751
3752 if (cfull_expression)
3753 *cfull_expression = xstrdup ("");
3754 }
3755 else
3756 {
3757 if (cname)
3758 *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
3759
3760 if (cfull_expression)
3761 *cfull_expression
3762 = xstrprintf ("((%s)%s%s)", parent_expression, join,
3763 field_name);
3764 }
2024f65a
VP
3765
3766 if (cvalue && value)
3767 *cvalue = value_struct_element_index (value, type_index);
3768
3769 if (ctype)
3770 *ctype = TYPE_FIELD_TYPE (type, type_index);
3771 }
3772 else if (index < TYPE_N_BASECLASSES (type))
3773 {
3774 /* This is a baseclass. */
3775 if (cname)
3776 *cname = xstrdup (TYPE_FIELD_NAME (type, index));
3777
3778 if (cvalue && value)
0cc7d26f 3779 *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
6e382aa3 3780
2024f65a
VP
3781 if (ctype)
3782 {
3783 *ctype = TYPE_FIELD_TYPE (type, index);
3784 }
02142340
VP
3785
3786 if (cfull_expression)
3787 {
3788 char *ptr = was_ptr ? "*" : "";
a109c7c1 3789
581e13c1 3790 /* Cast the parent to the base' type. Note that in gdb,
02142340
VP
3791 expression like
3792 (Base1)d
3793 will create an lvalue, for all appearences, so we don't
3794 need to use more fancy:
3795 *(Base1*)(&d)
0d932b2f
MK
3796 construct.
3797
3798 When we are in the scope of the base class or of one
3799 of its children, the type field name will be interpreted
3800 as a constructor, if it exists. Therefore, we must
3801 indicate that the name is a class name by using the
3802 'class' keyword. See PR mi/11912 */
3803 *cfull_expression = xstrprintf ("(%s(class %s%s) %s)",
02142340
VP
3804 ptr,
3805 TYPE_FIELD_NAME (type, index),
3806 ptr,
3807 parent_expression);
3808 }
8b93c638 3809 }
8b93c638
JM
3810 else
3811 {
348144ba 3812 char *access = NULL;
6e382aa3 3813 int children[3];
a109c7c1 3814
2024f65a 3815 cplus_class_num_children (type, children);
6e382aa3 3816
8b93c638 3817 /* Everything beyond the baseclasses can
6e382aa3
JJ
3818 only be "public", "private", or "protected"
3819
3820 The special "fake" children are always output by varobj in
581e13c1 3821 this order. So if INDEX == 2, it MUST be "protected". */
8b93c638
JM
3822 index -= TYPE_N_BASECLASSES (type);
3823 switch (index)
3824 {
3825 case 0:
6e382aa3 3826 if (children[v_public] > 0)
2024f65a 3827 access = "public";
6e382aa3 3828 else if (children[v_private] > 0)
2024f65a 3829 access = "private";
6e382aa3 3830 else
2024f65a 3831 access = "protected";
6e382aa3 3832 break;
8b93c638 3833 case 1:
6e382aa3 3834 if (children[v_public] > 0)
8b93c638 3835 {
6e382aa3 3836 if (children[v_private] > 0)
2024f65a 3837 access = "private";
6e382aa3 3838 else
2024f65a 3839 access = "protected";
8b93c638 3840 }
6e382aa3 3841 else if (children[v_private] > 0)
2024f65a 3842 access = "protected";
6e382aa3 3843 break;
8b93c638 3844 case 2:
581e13c1 3845 /* Must be protected. */
2024f65a 3846 access = "protected";
6e382aa3 3847 break;
8b93c638 3848 default:
581e13c1 3849 /* error! */
8b93c638
JM
3850 break;
3851 }
348144ba
MS
3852
3853 gdb_assert (access);
2024f65a
VP
3854 if (cname)
3855 *cname = xstrdup (access);
8b93c638 3856
02142340 3857 /* Value and type and full expression are null here. */
2024f65a 3858 }
8b93c638 3859 }
8b93c638
JM
3860 else
3861 {
02142340 3862 c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression);
2024f65a
VP
3863 }
3864}
8b93c638 3865
2024f65a
VP
3866static char *
3867cplus_name_of_child (struct varobj *parent, int index)
3868{
3869 char *name = NULL;
a109c7c1 3870
02142340 3871 cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
8b93c638
JM
3872 return name;
3873}
3874
02142340
VP
3875static char *
3876cplus_path_expr_of_child (struct varobj *child)
3877{
3878 cplus_describe_child (child->parent, child->index, NULL, NULL, NULL,
3879 &child->path_expr);
3880 return child->path_expr;
3881}
3882
30b28db1 3883static struct value *
fba45db2 3884cplus_value_of_child (struct varobj *parent, int index)
8b93c638 3885{
2024f65a 3886 struct value *value = NULL;
a109c7c1 3887
02142340 3888 cplus_describe_child (parent, index, NULL, &value, NULL, NULL);
8b93c638
JM
3889 return value;
3890}
3891
3892static struct type *
fba45db2 3893cplus_type_of_child (struct varobj *parent, int index)
8b93c638 3894{
2024f65a 3895 struct type *type = NULL;
a109c7c1 3896
02142340 3897 cplus_describe_child (parent, index, NULL, NULL, &type, NULL);
8b93c638
JM
3898 return type;
3899}
3900
8b93c638 3901static char *
a109c7c1
MS
3902cplus_value_of_variable (struct varobj *var,
3903 enum varobj_display_formats format)
8b93c638
JM
3904{
3905
3906 /* If we have one of our special types, don't print out
581e13c1 3907 any value. */
8b93c638
JM
3908 if (CPLUS_FAKE_CHILD (var))
3909 return xstrdup ("");
3910
de051565 3911 return c_value_of_variable (var, format);
8b93c638
JM
3912}
3913\f
3914/* Java */
3915
3916static int
fba45db2 3917java_number_of_children (struct varobj *var)
8b93c638
JM
3918{
3919 return cplus_number_of_children (var);
3920}
3921
3922static char *
fba45db2 3923java_name_of_variable (struct varobj *parent)
8b93c638
JM
3924{
3925 char *p, *name;
3926
3927 name = cplus_name_of_variable (parent);
3928 /* If the name has "-" in it, it is because we
581e13c1 3929 needed to escape periods in the name... */
8b93c638
JM
3930 p = name;
3931
3932 while (*p != '\000')
3933 {
3934 if (*p == '-')
3935 *p = '.';
3936 p++;
3937 }
3938
3939 return name;
3940}
3941
3942static char *
fba45db2 3943java_name_of_child (struct varobj *parent, int index)
8b93c638
JM
3944{
3945 char *name, *p;
3946
3947 name = cplus_name_of_child (parent, index);
581e13c1 3948 /* Escape any periods in the name... */
8b93c638
JM
3949 p = name;
3950
3951 while (*p != '\000')
3952 {
3953 if (*p == '.')
3954 *p = '-';
3955 p++;
3956 }
3957
3958 return name;
3959}
3960
02142340
VP
3961static char *
3962java_path_expr_of_child (struct varobj *child)
3963{
3964 return NULL;
3965}
3966
30b28db1 3967static struct value *
fba45db2 3968java_value_of_child (struct varobj *parent, int index)
8b93c638
JM
3969{
3970 return cplus_value_of_child (parent, index);
3971}
3972
3973static struct type *
fba45db2 3974java_type_of_child (struct varobj *parent, int index)
8b93c638
JM
3975{
3976 return cplus_type_of_child (parent, index);
3977}
3978
8b93c638 3979static char *
de051565 3980java_value_of_variable (struct varobj *var, enum varobj_display_formats format)
8b93c638 3981{
de051565 3982 return cplus_value_of_variable (var, format);
8b93c638 3983}
54333c3b 3984
40591b7d
JCD
3985/* Ada specific callbacks for VAROBJs. */
3986
3987static int
3988ada_number_of_children (struct varobj *var)
3989{
181875a4 3990 return ada_varobj_get_number_of_children (var->value, var->type);
40591b7d
JCD
3991}
3992
3993static char *
3994ada_name_of_variable (struct varobj *parent)
3995{
3996 return c_name_of_variable (parent);
3997}
3998
3999static char *
4000ada_name_of_child (struct varobj *parent, int index)
4001{
181875a4
JB
4002 return ada_varobj_get_name_of_child (parent->value, parent->type,
4003 parent->name, index);
40591b7d
JCD
4004}
4005
4006static char*
4007ada_path_expr_of_child (struct varobj *child)
4008{
181875a4
JB
4009 struct varobj *parent = child->parent;
4010 const char *parent_path_expr = varobj_get_path_expr (parent);
4011
4012 return ada_varobj_get_path_expr_of_child (parent->value,
4013 parent->type,
4014 parent->name,
4015 parent_path_expr,
4016 child->index);
40591b7d
JCD
4017}
4018
40591b7d
JCD
4019static struct value *
4020ada_value_of_child (struct varobj *parent, int index)
4021{
181875a4
JB
4022 return ada_varobj_get_value_of_child (parent->value, parent->type,
4023 parent->name, index);
40591b7d
JCD
4024}
4025
4026static struct type *
4027ada_type_of_child (struct varobj *parent, int index)
4028{
181875a4
JB
4029 return ada_varobj_get_type_of_child (parent->value, parent->type,
4030 index);
40591b7d
JCD
4031}
4032
4033static char *
4034ada_value_of_variable (struct varobj *var, enum varobj_display_formats format)
4035{
181875a4
JB
4036 struct value_print_options opts;
4037
4038 get_formatted_print_options (&opts, format_code[(int) format]);
4039 opts.deref_ref = 0;
4040 opts.raw = 1;
4041
4042 return ada_varobj_get_value_of_variable (var->value, var->type, &opts);
40591b7d
JCD
4043}
4044
d32cafc7
JB
4045/* Implement the "value_is_changeable_p" routine for Ada. */
4046
4047static int
4048ada_value_is_changeable_p (struct varobj *var)
4049{
4050 struct type *type = var->value ? value_type (var->value) : var->type;
4051
4052 if (ada_is_array_descriptor_type (type)
4053 && TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
4054 {
4055 /* This is in reality a pointer to an unconstrained array.
4056 its value is changeable. */
4057 return 1;
4058 }
4059
4060 if (ada_is_string_type (type))
4061 {
4062 /* We display the contents of the string in the array's
4063 "value" field. The contents can change, so consider
4064 that the array is changeable. */
4065 return 1;
4066 }
4067
4068 return default_value_is_changeable_p (var);
4069}
4070
7a290c40
JB
4071/* Implement the "value_has_mutated" routine for Ada. */
4072
4073static int
4074ada_value_has_mutated (struct varobj *var, struct value *new_val,
4075 struct type *new_type)
4076{
181875a4
JB
4077 int i;
4078 int from = -1;
4079 int to = -1;
4080
4081 /* If the number of fields have changed, then for sure the type
4082 has mutated. */
4083 if (ada_varobj_get_number_of_children (new_val, new_type)
4084 != var->num_children)
4085 return 1;
4086
4087 /* If the number of fields have remained the same, then we need
4088 to check the name of each field. If they remain the same,
4089 then chances are the type hasn't mutated. This is technically
4090 an incomplete test, as the child's type might have changed
4091 despite the fact that the name remains the same. But we'll
4092 handle this situation by saying that the child has mutated,
4093 not this value.
4094
4095 If only part (or none!) of the children have been fetched,
4096 then only check the ones we fetched. It does not matter
4097 to the frontend whether a child that it has not fetched yet
4098 has mutated or not. So just assume it hasn't. */
4099
4100 restrict_range (var->children, &from, &to);
4101 for (i = from; i < to; i++)
4102 if (strcmp (ada_varobj_get_name_of_child (new_val, new_type,
4103 var->name, i),
4104 VEC_index (varobj_p, var->children, i)->name) != 0)
4105 return 1;
4106
7a290c40
JB
4107 return 0;
4108}
4109
54333c3b
JK
4110/* Iterate all the existing _root_ VAROBJs and call the FUNC callback for them
4111 with an arbitrary caller supplied DATA pointer. */
4112
4113void
4114all_root_varobjs (void (*func) (struct varobj *var, void *data), void *data)
4115{
4116 struct varobj_root *var_root, *var_root_next;
4117
4118 /* Iterate "safely" - handle if the callee deletes its passed VAROBJ. */
4119
4120 for (var_root = rootlist; var_root != NULL; var_root = var_root_next)
4121 {
4122 var_root_next = var_root->next;
4123
4124 (*func) (var_root->rootvar, data);
4125 }
4126}
8b93c638
JM
4127\f
4128extern void _initialize_varobj (void);
4129void
4130_initialize_varobj (void)
4131{
4132 int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
4133
4134 varobj_table = xmalloc (sizeof_table);
4135 memset (varobj_table, 0, sizeof_table);
4136
ccce17b0
YQ
4137 add_setshow_zuinteger_cmd ("debugvarobj", class_maintenance,
4138 &varobjdebug,
4139 _("Set varobj debugging."),
4140 _("Show varobj debugging."),
4141 _("When non-zero, varobj debugging is enabled."),
4142 NULL, show_varobjdebug,
4143 &setlist, &showlist);
8b93c638 4144}
8756216b 4145
54333c3b 4146/* Invalidate varobj VAR if it is tied to locals and re-create it if it is
4e969b4f
AB
4147 defined on globals. It is a helper for varobj_invalidate.
4148
4149 This function is called after changing the symbol file, in this case the
4150 pointers to "struct type" stored by the varobj are no longer valid. All
4151 varobj must be either re-evaluated, or marked as invalid here. */
2dbd25e5 4152
54333c3b
JK
4153static void
4154varobj_invalidate_iter (struct varobj *var, void *unused)
8756216b 4155{
4e969b4f
AB
4156 /* global and floating var must be re-evaluated. */
4157 if (var->root->floating || var->root->valid_block == NULL)
2dbd25e5 4158 {
54333c3b 4159 struct varobj *tmp_var;
2dbd25e5 4160
54333c3b
JK
4161 /* Try to create a varobj with same expression. If we succeed
4162 replace the old varobj, otherwise invalidate it. */
4163 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
4164 USE_CURRENT_FRAME);
4165 if (tmp_var != NULL)
4166 {
4167 tmp_var->obj_name = xstrdup (var->obj_name);
4168 varobj_delete (var, NULL, 0);
4169 install_variable (tmp_var);
2dbd25e5 4170 }
54333c3b
JK
4171 else
4172 var->root->is_valid = 0;
2dbd25e5 4173 }
54333c3b
JK
4174 else /* locals must be invalidated. */
4175 var->root->is_valid = 0;
4176}
4177
4178/* Invalidate the varobjs that are tied to locals and re-create the ones that
4179 are defined on globals.
4180 Invalidated varobjs will be always printed in_scope="invalid". */
4181
4182void
4183varobj_invalidate (void)
4184{
4185 all_root_varobjs (varobj_invalidate_iter, NULL);
8756216b 4186}
This page took 2.700053 seconds and 4 git commands to generate.