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