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