]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/varobj.c
* arch-utils.c (selected_byte_order): Return target_byte_order_user.
[thirdparty/binutils-gdb.git] / gdb / varobj.c
CommitLineData
8b93c638 1/* Implementation of the GDB variable objects API.
bc8332bb 2
0fb0cc75
JB
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 2009 Free Software Foundation, Inc.
8b93c638
JM
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
a9762ec7 8 the Free Software Foundation; either version 3 of the License, or
8b93c638
JM
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
a9762ec7 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
8b93c638
JM
18
19#include "defs.h"
a6c442d8 20#include "exceptions.h"
8b93c638
JM
21#include "value.h"
22#include "expression.h"
23#include "frame.h"
8b93c638
JM
24#include "language.h"
25#include "wrapper.h"
26#include "gdbcmd.h"
d2353924 27#include "block.h"
79a45b7d 28#include "valprint.h"
a6c442d8
MK
29
30#include "gdb_assert.h"
b66d6d2e 31#include "gdb_string.h"
8b93c638
JM
32
33#include "varobj.h"
28335dcc 34#include "vec.h"
6208b47d
VP
35#include "gdbthread.h"
36#include "inferior.h"
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 {
50810684 2266 struct gdbarch *gdbarch = get_type_arch (value_type (value));
b6313243 2267 make_cleanup (xfree, thevalue);
50810684 2268 LA_PRINT_STRING (stb, builtin_type (gdbarch)->builtin_char,
b6313243
TT
2269 (gdb_byte *) thevalue, strlen (thevalue),
2270 0, &opts);
2271 }
2272 else
2273 common_val_print (value, stb, 0, &opts, current_language);
85265413 2274 thevalue = ui_file_xstrdup (stb, &dummy);
57e66780 2275
85265413
NR
2276 do_cleanups (old_chain);
2277 return thevalue;
2278}
2279
340a7723
NR
2280int
2281varobj_editable_p (struct varobj *var)
2282{
2283 struct type *type;
2284 struct value *value;
2285
2286 if (!(var->root->is_valid && var->value && VALUE_LVAL (var->value)))
2287 return 0;
2288
2289 type = get_value_type (var);
2290
2291 switch (TYPE_CODE (type))
2292 {
2293 case TYPE_CODE_STRUCT:
2294 case TYPE_CODE_UNION:
2295 case TYPE_CODE_ARRAY:
2296 case TYPE_CODE_FUNC:
2297 case TYPE_CODE_METHOD:
2298 return 0;
2299 break;
2300
2301 default:
2302 return 1;
2303 break;
2304 }
2305}
2306
acd65feb
VP
2307/* Return non-zero if changes in value of VAR
2308 must be detected and reported by -var-update.
2309 Return zero is -var-update should never report
2310 changes of such values. This makes sense for structures
2311 (since the changes in children values will be reported separately),
2312 or for artifical objects (like 'public' pseudo-field in C++).
2313
2314 Return value of 0 means that gdb need not call value_fetch_lazy
2315 for the value of this variable object. */
8b93c638 2316static int
b2c2bd75 2317varobj_value_is_changeable_p (struct varobj *var)
8b93c638
JM
2318{
2319 int r;
2320 struct type *type;
2321
2322 if (CPLUS_FAKE_CHILD (var))
2323 return 0;
2324
6e2a9270 2325 type = get_value_type (var);
8b93c638
JM
2326
2327 switch (TYPE_CODE (type))
2328 {
72330bd6
AC
2329 case TYPE_CODE_STRUCT:
2330 case TYPE_CODE_UNION:
2331 case TYPE_CODE_ARRAY:
2332 r = 0;
2333 break;
8b93c638 2334
72330bd6
AC
2335 default:
2336 r = 1;
8b93c638
JM
2337 }
2338
2339 return r;
2340}
2341
5a413362
VP
2342/* Return 1 if that varobj is floating, that is is always evaluated in the
2343 selected frame, and not bound to thread/frame. Such variable objects
2344 are created using '@' as frame specifier to -var-create. */
2345int
2346varobj_floating_p (struct varobj *var)
2347{
2348 return var->root->floating;
2349}
2350
2024f65a
VP
2351/* Given the value and the type of a variable object,
2352 adjust the value and type to those necessary
2353 for getting children of the variable object.
2354 This includes dereferencing top-level references
2355 to all types and dereferencing pointers to
2356 structures.
2357
2358 Both TYPE and *TYPE should be non-null. VALUE
2359 can be null if we want to only translate type.
2360 *VALUE can be null as well -- if the parent
02142340
VP
2361 value is not known.
2362
2363 If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
b6313243 2364 depending on whether pointer was dereferenced
02142340 2365 in this function. */
2024f65a
VP
2366static void
2367adjust_value_for_child_access (struct value **value,
02142340
VP
2368 struct type **type,
2369 int *was_ptr)
2024f65a
VP
2370{
2371 gdb_assert (type && *type);
2372
02142340
VP
2373 if (was_ptr)
2374 *was_ptr = 0;
2375
2024f65a
VP
2376 *type = check_typedef (*type);
2377
2378 /* The type of value stored in varobj, that is passed
2379 to us, is already supposed to be
2380 reference-stripped. */
2381
2382 gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
2383
2384 /* Pointers to structures are treated just like
2385 structures when accessing children. Don't
2386 dererences pointers to other types. */
2387 if (TYPE_CODE (*type) == TYPE_CODE_PTR)
2388 {
2389 struct type *target_type = get_target_type (*type);
2390 if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
2391 || TYPE_CODE (target_type) == TYPE_CODE_UNION)
2392 {
2393 if (value && *value)
3f4178d6
DJ
2394 {
2395 int success = gdb_value_ind (*value, value);
2396 if (!success)
2397 *value = NULL;
2398 }
2024f65a 2399 *type = target_type;
02142340
VP
2400 if (was_ptr)
2401 *was_ptr = 1;
2024f65a
VP
2402 }
2403 }
2404
2405 /* The 'get_target_type' function calls check_typedef on
2406 result, so we can immediately check type code. No
2407 need to call check_typedef here. */
2408}
2409
8b93c638
JM
2410/* C */
2411static int
fba45db2 2412c_number_of_children (struct varobj *var)
8b93c638 2413{
2024f65a
VP
2414 struct type *type = get_value_type (var);
2415 int children = 0;
8b93c638 2416 struct type *target;
8b93c638 2417
02142340 2418 adjust_value_for_child_access (NULL, &type, NULL);
8b93c638 2419 target = get_target_type (type);
8b93c638
JM
2420
2421 switch (TYPE_CODE (type))
2422 {
2423 case TYPE_CODE_ARRAY:
2424 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
d78df370 2425 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
8b93c638
JM
2426 children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
2427 else
74a44383
DJ
2428 /* If we don't know how many elements there are, don't display
2429 any. */
2430 children = 0;
8b93c638
JM
2431 break;
2432
2433 case TYPE_CODE_STRUCT:
2434 case TYPE_CODE_UNION:
2435 children = TYPE_NFIELDS (type);
2436 break;
2437
2438 case TYPE_CODE_PTR:
2024f65a
VP
2439 /* The type here is a pointer to non-struct. Typically, pointers
2440 have one child, except for function ptrs, which have no children,
2441 and except for void*, as we don't know what to show.
2442
0755e6c1
FN
2443 We can show char* so we allow it to be dereferenced. If you decide
2444 to test for it, please mind that a little magic is necessary to
2445 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
2446 TYPE_NAME == "char" */
2024f65a
VP
2447 if (TYPE_CODE (target) == TYPE_CODE_FUNC
2448 || TYPE_CODE (target) == TYPE_CODE_VOID)
2449 children = 0;
2450 else
2451 children = 1;
8b93c638
JM
2452 break;
2453
2454 default:
2455 /* Other types have no children */
2456 break;
2457 }
2458
2459 return children;
2460}
2461
2462static char *
fba45db2 2463c_name_of_variable (struct varobj *parent)
8b93c638 2464{
1b36a34b 2465 return xstrdup (parent->name);
8b93c638
JM
2466}
2467
bbec2603
VP
2468/* Return the value of element TYPE_INDEX of a structure
2469 value VALUE. VALUE's type should be a structure,
2470 or union, or a typedef to struct/union.
2471
2472 Returns NULL if getting the value fails. Never throws. */
2473static struct value *
2474value_struct_element_index (struct value *value, int type_index)
8b93c638 2475{
bbec2603
VP
2476 struct value *result = NULL;
2477 volatile struct gdb_exception e;
8b93c638 2478
bbec2603
VP
2479 struct type *type = value_type (value);
2480 type = check_typedef (type);
2481
2482 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
2483 || TYPE_CODE (type) == TYPE_CODE_UNION);
8b93c638 2484
bbec2603
VP
2485 TRY_CATCH (e, RETURN_MASK_ERROR)
2486 {
d6a843b5 2487 if (field_is_static (&TYPE_FIELD (type, type_index)))
bbec2603
VP
2488 result = value_static_field (type, type_index);
2489 else
2490 result = value_primitive_field (value, 0, type_index, type);
2491 }
2492 if (e.reason < 0)
2493 {
2494 return NULL;
2495 }
2496 else
2497 {
2498 return result;
2499 }
2500}
2501
2502/* Obtain the information about child INDEX of the variable
2503 object PARENT.
2504 If CNAME is not null, sets *CNAME to the name of the child relative
2505 to the parent.
2506 If CVALUE is not null, sets *CVALUE to the value of the child.
2507 If CTYPE is not null, sets *CTYPE to the type of the child.
2508
2509 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
2510 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
2511 to NULL. */
2512static void
2513c_describe_child (struct varobj *parent, int index,
02142340
VP
2514 char **cname, struct value **cvalue, struct type **ctype,
2515 char **cfull_expression)
bbec2603
VP
2516{
2517 struct value *value = parent->value;
2024f65a 2518 struct type *type = get_value_type (parent);
02142340
VP
2519 char *parent_expression = NULL;
2520 int was_ptr;
bbec2603
VP
2521
2522 if (cname)
2523 *cname = NULL;
2524 if (cvalue)
2525 *cvalue = NULL;
2526 if (ctype)
2527 *ctype = NULL;
02142340
VP
2528 if (cfull_expression)
2529 {
2530 *cfull_expression = NULL;
2531 parent_expression = varobj_get_path_expr (parent);
2532 }
2533 adjust_value_for_child_access (&value, &type, &was_ptr);
bbec2603 2534
8b93c638
JM
2535 switch (TYPE_CODE (type))
2536 {
2537 case TYPE_CODE_ARRAY:
bbec2603
VP
2538 if (cname)
2539 *cname = xstrprintf ("%d", index
2540 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)));
2541
2542 if (cvalue && value)
2543 {
2544 int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
2497b498 2545 gdb_value_subscript (value, real_index, cvalue);
bbec2603
VP
2546 }
2547
2548 if (ctype)
2549 *ctype = get_target_type (type);
2550
02142340
VP
2551 if (cfull_expression)
2552 *cfull_expression = xstrprintf ("(%s)[%d]", parent_expression,
2553 index
2554 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)));
2555
2556
8b93c638
JM
2557 break;
2558
2559 case TYPE_CODE_STRUCT:
2560 case TYPE_CODE_UNION:
bbec2603 2561 if (cname)
1b36a34b 2562 *cname = xstrdup (TYPE_FIELD_NAME (type, index));
bbec2603
VP
2563
2564 if (cvalue && value)
2565 {
2566 /* For C, varobj index is the same as type index. */
2567 *cvalue = value_struct_element_index (value, index);
2568 }
2569
2570 if (ctype)
2571 *ctype = TYPE_FIELD_TYPE (type, index);
2572
02142340
VP
2573 if (cfull_expression)
2574 {
2575 char *join = was_ptr ? "->" : ".";
2576 *cfull_expression = xstrprintf ("(%s)%s%s", parent_expression, join,
2577 TYPE_FIELD_NAME (type, index));
2578 }
2579
8b93c638
JM
2580 break;
2581
2582 case TYPE_CODE_PTR:
bbec2603
VP
2583 if (cname)
2584 *cname = xstrprintf ("*%s", parent->name);
8b93c638 2585
bbec2603 2586 if (cvalue && value)
3f4178d6
DJ
2587 {
2588 int success = gdb_value_ind (value, cvalue);
2589 if (!success)
2590 *cvalue = NULL;
2591 }
bbec2603 2592
2024f65a
VP
2593 /* Don't use get_target_type because it calls
2594 check_typedef and here, we want to show the true
2595 declared type of the variable. */
bbec2603 2596 if (ctype)
2024f65a 2597 *ctype = TYPE_TARGET_TYPE (type);
02142340
VP
2598
2599 if (cfull_expression)
2600 *cfull_expression = xstrprintf ("*(%s)", parent_expression);
bbec2603 2601
8b93c638
JM
2602 break;
2603
2604 default:
2605 /* This should not happen */
bbec2603
VP
2606 if (cname)
2607 *cname = xstrdup ("???");
02142340
VP
2608 if (cfull_expression)
2609 *cfull_expression = xstrdup ("???");
bbec2603 2610 /* Don't set value and type, we don't know then. */
8b93c638 2611 }
bbec2603 2612}
8b93c638 2613
bbec2603
VP
2614static char *
2615c_name_of_child (struct varobj *parent, int index)
2616{
2617 char *name;
02142340 2618 c_describe_child (parent, index, &name, NULL, NULL, NULL);
8b93c638
JM
2619 return name;
2620}
2621
02142340
VP
2622static char *
2623c_path_expr_of_child (struct varobj *child)
2624{
2625 c_describe_child (child->parent, child->index, NULL, NULL, NULL,
2626 &child->path_expr);
2627 return child->path_expr;
2628}
2629
c5b48eac
VP
2630/* If frame associated with VAR can be found, switch
2631 to it and return 1. Otherwise, return 0. */
2632static int
2633check_scope (struct varobj *var)
2634{
2635 struct frame_info *fi;
2636 int scope;
2637
2638 fi = frame_find_by_id (var->root->frame);
2639 scope = fi != NULL;
2640
2641 if (fi)
2642 {
2643 CORE_ADDR pc = get_frame_pc (fi);
2644 if (pc < BLOCK_START (var->root->valid_block) ||
2645 pc >= BLOCK_END (var->root->valid_block))
2646 scope = 0;
2647 else
2648 select_frame (fi);
2649 }
2650 return scope;
2651}
2652
30b28db1 2653static struct value *
fba45db2 2654c_value_of_root (struct varobj **var_handle)
8b93c638 2655{
5e572bb4 2656 struct value *new_val = NULL;
73a93a32 2657 struct varobj *var = *var_handle;
8b93c638 2658 struct frame_info *fi;
c5b48eac 2659 int within_scope = 0;
6208b47d
VP
2660 struct cleanup *back_to;
2661
73a93a32 2662 /* Only root variables can be updated... */
b2c2bd75 2663 if (!is_root_p (var))
73a93a32
JI
2664 /* Not a root var */
2665 return NULL;
2666
4f8d22e3 2667 back_to = make_cleanup_restore_current_thread ();
72330bd6 2668
8b93c638 2669 /* Determine whether the variable is still around. */
a5defcdc 2670 if (var->root->valid_block == NULL || var->root->floating)
8b93c638 2671 within_scope = 1;
c5b48eac
VP
2672 else if (var->root->thread_id == 0)
2673 {
2674 /* The program was single-threaded when the variable object was
2675 created. Technically, it's possible that the program became
2676 multi-threaded since then, but we don't support such
2677 scenario yet. */
2678 within_scope = check_scope (var);
2679 }
8b93c638
JM
2680 else
2681 {
c5b48eac
VP
2682 ptid_t ptid = thread_id_to_pid (var->root->thread_id);
2683 if (in_thread_list (ptid))
d2353924 2684 {
c5b48eac
VP
2685 switch_to_thread (ptid);
2686 within_scope = check_scope (var);
2687 }
8b93c638 2688 }
72330bd6 2689
8b93c638
JM
2690 if (within_scope)
2691 {
73a93a32 2692 /* We need to catch errors here, because if evaluate
85d93f1d
VP
2693 expression fails we want to just return NULL. */
2694 gdb_evaluate_expression (var->root->exp, &new_val);
8b93c638
JM
2695 return new_val;
2696 }
2697
6208b47d
VP
2698 do_cleanups (back_to);
2699
8b93c638
JM
2700 return NULL;
2701}
2702
30b28db1 2703static struct value *
fba45db2 2704c_value_of_child (struct varobj *parent, int index)
8b93c638 2705{
bbec2603 2706 struct value *value = NULL;
02142340 2707 c_describe_child (parent, index, NULL, &value, NULL, NULL);
8b93c638
JM
2708
2709 return value;
2710}
2711
2712static struct type *
fba45db2 2713c_type_of_child (struct varobj *parent, int index)
8b93c638 2714{
bbec2603 2715 struct type *type = NULL;
02142340 2716 c_describe_child (parent, index, NULL, NULL, &type, NULL);
8b93c638
JM
2717 return type;
2718}
2719
8b93c638 2720static char *
de051565 2721c_value_of_variable (struct varobj *var, enum varobj_display_formats format)
8b93c638 2722{
14b3d9c9
JB
2723 /* BOGUS: if val_print sees a struct/class, or a reference to one,
2724 it will print out its children instead of "{...}". So we need to
2725 catch that case explicitly. */
2726 struct type *type = get_type (var);
e64d9b3d 2727
b6313243
TT
2728 /* If we have a custom formatter, return whatever string it has
2729 produced. */
2730 if (var->pretty_printer && var->print_value)
2731 return xstrdup (var->print_value);
2732
14b3d9c9
JB
2733 /* Strip top-level references. */
2734 while (TYPE_CODE (type) == TYPE_CODE_REF)
2735 type = check_typedef (TYPE_TARGET_TYPE (type));
2736
2737 switch (TYPE_CODE (type))
8b93c638
JM
2738 {
2739 case TYPE_CODE_STRUCT:
2740 case TYPE_CODE_UNION:
2741 return xstrdup ("{...}");
2742 /* break; */
2743
2744 case TYPE_CODE_ARRAY:
2745 {
e64d9b3d 2746 char *number;
b435e160 2747 number = xstrprintf ("[%d]", var->num_children);
e64d9b3d 2748 return (number);
8b93c638
JM
2749 }
2750 /* break; */
2751
2752 default:
2753 {
575bbeb6
KS
2754 if (var->value == NULL)
2755 {
2756 /* This can happen if we attempt to get the value of a struct
2757 member when the parent is an invalid pointer. This is an
2758 error condition, so we should tell the caller. */
2759 return NULL;
2760 }
2761 else
2762 {
25d5ea92
VP
2763 if (var->not_fetched && value_lazy (var->value))
2764 /* Frozen variable and no value yet. We don't
2765 implicitly fetch the value. MI response will
2766 use empty string for the value, which is OK. */
2767 return NULL;
2768
b2c2bd75 2769 gdb_assert (varobj_value_is_changeable_p (var));
acd65feb 2770 gdb_assert (!value_lazy (var->value));
de051565
MK
2771
2772 /* If the specified format is the current one,
2773 we can reuse print_value */
2774 if (format == var->format)
2775 return xstrdup (var->print_value);
2776 else
b6313243
TT
2777 return value_get_print_value (var->value, format,
2778 var->pretty_printer);
85265413 2779 }
e64d9b3d 2780 }
8b93c638
JM
2781 }
2782}
2783\f
2784
2785/* C++ */
2786
2787static int
fba45db2 2788cplus_number_of_children (struct varobj *var)
8b93c638
JM
2789{
2790 struct type *type;
2791 int children, dont_know;
2792
2793 dont_know = 1;
2794 children = 0;
2795
2796 if (!CPLUS_FAKE_CHILD (var))
2797 {
2024f65a 2798 type = get_value_type (var);
02142340 2799 adjust_value_for_child_access (NULL, &type, NULL);
8b93c638
JM
2800
2801 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
72330bd6 2802 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
8b93c638
JM
2803 {
2804 int kids[3];
2805
2806 cplus_class_num_children (type, kids);
2807 if (kids[v_public] != 0)
2808 children++;
2809 if (kids[v_private] != 0)
2810 children++;
2811 if (kids[v_protected] != 0)
2812 children++;
2813
2814 /* Add any baseclasses */
2815 children += TYPE_N_BASECLASSES (type);
2816 dont_know = 0;
2817
2818 /* FIXME: save children in var */
2819 }
2820 }
2821 else
2822 {
2823 int kids[3];
2824
2024f65a 2825 type = get_value_type (var->parent);
02142340 2826 adjust_value_for_child_access (NULL, &type, NULL);
8b93c638
JM
2827
2828 cplus_class_num_children (type, kids);
6e382aa3 2829 if (strcmp (var->name, "public") == 0)
8b93c638 2830 children = kids[v_public];
6e382aa3 2831 else if (strcmp (var->name, "private") == 0)
8b93c638
JM
2832 children = kids[v_private];
2833 else
2834 children = kids[v_protected];
2835 dont_know = 0;
2836 }
2837
2838 if (dont_know)
2839 children = c_number_of_children (var);
2840
2841 return children;
2842}
2843
2844/* Compute # of public, private, and protected variables in this class.
2845 That means we need to descend into all baseclasses and find out
2846 how many are there, too. */
2847static void
1669605f 2848cplus_class_num_children (struct type *type, int children[3])
8b93c638
JM
2849{
2850 int i;
2851
2852 children[v_public] = 0;
2853 children[v_private] = 0;
2854 children[v_protected] = 0;
2855
2856 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
2857 {
2858 /* If we have a virtual table pointer, omit it. */
72330bd6 2859 if (TYPE_VPTR_BASETYPE (type) == type && TYPE_VPTR_FIELDNO (type) == i)
8b93c638
JM
2860 continue;
2861
2862 if (TYPE_FIELD_PROTECTED (type, i))
2863 children[v_protected]++;
2864 else if (TYPE_FIELD_PRIVATE (type, i))
2865 children[v_private]++;
2866 else
2867 children[v_public]++;
2868 }
2869}
2870
2871static char *
fba45db2 2872cplus_name_of_variable (struct varobj *parent)
8b93c638
JM
2873{
2874 return c_name_of_variable (parent);
2875}
2876
2024f65a
VP
2877enum accessibility { private_field, protected_field, public_field };
2878
2879/* Check if field INDEX of TYPE has the specified accessibility.
2880 Return 0 if so and 1 otherwise. */
2881static int
2882match_accessibility (struct type *type, int index, enum accessibility acc)
8b93c638 2883{
2024f65a
VP
2884 if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
2885 return 1;
2886 else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
2887 return 1;
2888 else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
2889 && !TYPE_FIELD_PROTECTED (type, index))
2890 return 1;
2891 else
2892 return 0;
2893}
2894
2895static void
2896cplus_describe_child (struct varobj *parent, int index,
02142340
VP
2897 char **cname, struct value **cvalue, struct type **ctype,
2898 char **cfull_expression)
2024f65a 2899{
348144ba 2900 char *name = NULL;
2024f65a 2901 struct value *value;
8b93c638 2902 struct type *type;
02142340
VP
2903 int was_ptr;
2904 char *parent_expression = NULL;
8b93c638 2905
2024f65a
VP
2906 if (cname)
2907 *cname = NULL;
2908 if (cvalue)
2909 *cvalue = NULL;
2910 if (ctype)
2911 *ctype = NULL;
02142340
VP
2912 if (cfull_expression)
2913 *cfull_expression = NULL;
2024f65a 2914
8b93c638
JM
2915 if (CPLUS_FAKE_CHILD (parent))
2916 {
2024f65a
VP
2917 value = parent->parent->value;
2918 type = get_value_type (parent->parent);
02142340
VP
2919 if (cfull_expression)
2920 parent_expression = varobj_get_path_expr (parent->parent);
8b93c638
JM
2921 }
2922 else
2024f65a
VP
2923 {
2924 value = parent->value;
2925 type = get_value_type (parent);
02142340
VP
2926 if (cfull_expression)
2927 parent_expression = varobj_get_path_expr (parent);
2024f65a 2928 }
8b93c638 2929
02142340 2930 adjust_value_for_child_access (&value, &type, &was_ptr);
2024f65a
VP
2931
2932 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
3f4178d6 2933 || TYPE_CODE (type) == TYPE_CODE_UNION)
8b93c638 2934 {
02142340 2935 char *join = was_ptr ? "->" : ".";
8b93c638
JM
2936 if (CPLUS_FAKE_CHILD (parent))
2937 {
6e382aa3
JJ
2938 /* The fields of the class type are ordered as they
2939 appear in the class. We are given an index for a
2940 particular access control type ("public","protected",
2941 or "private"). We must skip over fields that don't
2942 have the access control we are looking for to properly
2943 find the indexed field. */
2944 int type_index = TYPE_N_BASECLASSES (type);
2024f65a 2945 enum accessibility acc = public_field;
6e382aa3 2946 if (strcmp (parent->name, "private") == 0)
2024f65a 2947 acc = private_field;
6e382aa3 2948 else if (strcmp (parent->name, "protected") == 0)
2024f65a
VP
2949 acc = protected_field;
2950
2951 while (index >= 0)
6e382aa3 2952 {
2024f65a
VP
2953 if (TYPE_VPTR_BASETYPE (type) == type
2954 && type_index == TYPE_VPTR_FIELDNO (type))
2955 ; /* ignore vptr */
2956 else if (match_accessibility (type, type_index, acc))
6e382aa3
JJ
2957 --index;
2958 ++type_index;
6e382aa3 2959 }
2024f65a
VP
2960 --type_index;
2961
2962 if (cname)
2963 *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
2964
2965 if (cvalue && value)
2966 *cvalue = value_struct_element_index (value, type_index);
2967
2968 if (ctype)
2969 *ctype = TYPE_FIELD_TYPE (type, type_index);
02142340
VP
2970
2971 if (cfull_expression)
2972 *cfull_expression = xstrprintf ("((%s)%s%s)", parent_expression,
2973 join,
2974 TYPE_FIELD_NAME (type, type_index));
2024f65a
VP
2975 }
2976 else if (index < TYPE_N_BASECLASSES (type))
2977 {
2978 /* This is a baseclass. */
2979 if (cname)
2980 *cname = xstrdup (TYPE_FIELD_NAME (type, index));
2981
2982 if (cvalue && value)
6e382aa3 2983 {
2024f65a 2984 *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
02142340 2985 release_value (*cvalue);
6e382aa3
JJ
2986 }
2987
2024f65a
VP
2988 if (ctype)
2989 {
2990 *ctype = TYPE_FIELD_TYPE (type, index);
2991 }
02142340
VP
2992
2993 if (cfull_expression)
2994 {
2995 char *ptr = was_ptr ? "*" : "";
2996 /* Cast the parent to the base' type. Note that in gdb,
2997 expression like
2998 (Base1)d
2999 will create an lvalue, for all appearences, so we don't
3000 need to use more fancy:
3001 *(Base1*)(&d)
3002 construct. */
3003 *cfull_expression = xstrprintf ("(%s(%s%s) %s)",
3004 ptr,
3005 TYPE_FIELD_NAME (type, index),
3006 ptr,
3007 parent_expression);
3008 }
8b93c638 3009 }
8b93c638
JM
3010 else
3011 {
348144ba 3012 char *access = NULL;
6e382aa3 3013 int children[3];
2024f65a 3014 cplus_class_num_children (type, children);
6e382aa3 3015
8b93c638 3016 /* Everything beyond the baseclasses can
6e382aa3
JJ
3017 only be "public", "private", or "protected"
3018
3019 The special "fake" children are always output by varobj in
3020 this order. So if INDEX == 2, it MUST be "protected". */
8b93c638
JM
3021 index -= TYPE_N_BASECLASSES (type);
3022 switch (index)
3023 {
3024 case 0:
6e382aa3 3025 if (children[v_public] > 0)
2024f65a 3026 access = "public";
6e382aa3 3027 else if (children[v_private] > 0)
2024f65a 3028 access = "private";
6e382aa3 3029 else
2024f65a 3030 access = "protected";
6e382aa3 3031 break;
8b93c638 3032 case 1:
6e382aa3 3033 if (children[v_public] > 0)
8b93c638 3034 {
6e382aa3 3035 if (children[v_private] > 0)
2024f65a 3036 access = "private";
6e382aa3 3037 else
2024f65a 3038 access = "protected";
8b93c638 3039 }
6e382aa3 3040 else if (children[v_private] > 0)
2024f65a 3041 access = "protected";
6e382aa3 3042 break;
8b93c638 3043 case 2:
6e382aa3 3044 /* Must be protected */
2024f65a 3045 access = "protected";
6e382aa3 3046 break;
8b93c638
JM
3047 default:
3048 /* error! */
3049 break;
3050 }
348144ba
MS
3051
3052 gdb_assert (access);
2024f65a
VP
3053 if (cname)
3054 *cname = xstrdup (access);
8b93c638 3055
02142340 3056 /* Value and type and full expression are null here. */
2024f65a 3057 }
8b93c638 3058 }
8b93c638
JM
3059 else
3060 {
02142340 3061 c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression);
2024f65a
VP
3062 }
3063}
8b93c638 3064
2024f65a
VP
3065static char *
3066cplus_name_of_child (struct varobj *parent, int index)
3067{
3068 char *name = NULL;
02142340 3069 cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
8b93c638
JM
3070 return name;
3071}
3072
02142340
VP
3073static char *
3074cplus_path_expr_of_child (struct varobj *child)
3075{
3076 cplus_describe_child (child->parent, child->index, NULL, NULL, NULL,
3077 &child->path_expr);
3078 return child->path_expr;
3079}
3080
30b28db1 3081static struct value *
fba45db2 3082cplus_value_of_root (struct varobj **var_handle)
8b93c638 3083{
73a93a32 3084 return c_value_of_root (var_handle);
8b93c638
JM
3085}
3086
30b28db1 3087static struct value *
fba45db2 3088cplus_value_of_child (struct varobj *parent, int index)
8b93c638 3089{
2024f65a 3090 struct value *value = NULL;
02142340 3091 cplus_describe_child (parent, index, NULL, &value, NULL, NULL);
8b93c638
JM
3092 return value;
3093}
3094
3095static struct type *
fba45db2 3096cplus_type_of_child (struct varobj *parent, int index)
8b93c638 3097{
2024f65a 3098 struct type *type = NULL;
02142340 3099 cplus_describe_child (parent, index, NULL, NULL, &type, NULL);
8b93c638
JM
3100 return type;
3101}
3102
8b93c638 3103static char *
de051565 3104cplus_value_of_variable (struct varobj *var, enum varobj_display_formats format)
8b93c638
JM
3105{
3106
3107 /* If we have one of our special types, don't print out
3108 any value. */
3109 if (CPLUS_FAKE_CHILD (var))
3110 return xstrdup ("");
3111
de051565 3112 return c_value_of_variable (var, format);
8b93c638
JM
3113}
3114\f
3115/* Java */
3116
3117static int
fba45db2 3118java_number_of_children (struct varobj *var)
8b93c638
JM
3119{
3120 return cplus_number_of_children (var);
3121}
3122
3123static char *
fba45db2 3124java_name_of_variable (struct varobj *parent)
8b93c638
JM
3125{
3126 char *p, *name;
3127
3128 name = cplus_name_of_variable (parent);
3129 /* If the name has "-" in it, it is because we
3130 needed to escape periods in the name... */
3131 p = name;
3132
3133 while (*p != '\000')
3134 {
3135 if (*p == '-')
3136 *p = '.';
3137 p++;
3138 }
3139
3140 return name;
3141}
3142
3143static char *
fba45db2 3144java_name_of_child (struct varobj *parent, int index)
8b93c638
JM
3145{
3146 char *name, *p;
3147
3148 name = cplus_name_of_child (parent, index);
3149 /* Escape any periods in the name... */
3150 p = name;
3151
3152 while (*p != '\000')
3153 {
3154 if (*p == '.')
3155 *p = '-';
3156 p++;
3157 }
3158
3159 return name;
3160}
3161
02142340
VP
3162static char *
3163java_path_expr_of_child (struct varobj *child)
3164{
3165 return NULL;
3166}
3167
30b28db1 3168static struct value *
fba45db2 3169java_value_of_root (struct varobj **var_handle)
8b93c638 3170{
73a93a32 3171 return cplus_value_of_root (var_handle);
8b93c638
JM
3172}
3173
30b28db1 3174static struct value *
fba45db2 3175java_value_of_child (struct varobj *parent, int index)
8b93c638
JM
3176{
3177 return cplus_value_of_child (parent, index);
3178}
3179
3180static struct type *
fba45db2 3181java_type_of_child (struct varobj *parent, int index)
8b93c638
JM
3182{
3183 return cplus_type_of_child (parent, index);
3184}
3185
8b93c638 3186static char *
de051565 3187java_value_of_variable (struct varobj *var, enum varobj_display_formats format)
8b93c638 3188{
de051565 3189 return cplus_value_of_variable (var, format);
8b93c638
JM
3190}
3191\f
3192extern void _initialize_varobj (void);
3193void
3194_initialize_varobj (void)
3195{
3196 int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
3197
3198 varobj_table = xmalloc (sizeof_table);
3199 memset (varobj_table, 0, sizeof_table);
3200
85c07804
AC
3201 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance,
3202 &varobjdebug, _("\
3203Set varobj debugging."), _("\
3204Show varobj debugging."), _("\
3205When non-zero, varobj debugging is enabled."),
3206 NULL,
920d2a44 3207 show_varobjdebug,
85c07804 3208 &setlist, &showlist);
8b93c638 3209}
8756216b
DP
3210
3211/* Invalidate the varobjs that are tied to locals and re-create the ones that
3212 are defined on globals.
3213 Invalidated varobjs will be always printed in_scope="invalid". */
2dbd25e5 3214
8756216b
DP
3215void
3216varobj_invalidate (void)
3217{
3218 struct varobj **all_rootvarobj;
3219 struct varobj **varp;
3220
3221 if (varobj_list (&all_rootvarobj) > 0)
2dbd25e5
JK
3222 {
3223 varp = all_rootvarobj;
3224 while (*varp != NULL)
3225 {
3226 /* Floating varobjs are reparsed on each stop, so we don't care if
f4a34a08
JK
3227 the presently parsed expression refers to something that's gone.
3228 */
2dbd25e5
JK
3229 if ((*varp)->root->floating)
3230 continue;
3231
3232 /* global var must be re-evaluated. */
3233 if ((*varp)->root->valid_block == NULL)
3234 {
3235 struct varobj *tmp_var;
3236
f4a34a08
JK
3237 /* Try to create a varobj with same expression. If we succeed
3238 replace the old varobj, otherwise invalidate it. */
3239 tmp_var = varobj_create (NULL, (*varp)->name, (CORE_ADDR) 0,
3240 USE_CURRENT_FRAME);
2dbd25e5
JK
3241 if (tmp_var != NULL)
3242 {
3243 tmp_var->obj_name = xstrdup ((*varp)->obj_name);
3244 varobj_delete (*varp, NULL, 0);
3245 install_variable (tmp_var);
3246 }
3247 else
3248 (*varp)->root->is_valid = 0;
3249 }
3250 else /* locals must be invalidated. */
3251 (*varp)->root->is_valid = 0;
3252
3253 varp++;
3254 }
3255 }
f7545552 3256 xfree (all_rootvarobj);
8756216b
DP
3257 return;
3258}