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