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