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