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