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