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