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