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