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