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