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