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