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