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