Remove varobj_clear_saved_item
[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];
11106495
TT
628 bool type_updated = update_type_if_necessary (existing,
629 item->value.get ());
bf8793bb 630
8264ba82
AG
631 if (type_updated)
632 {
0604393c
SM
633 if (type_changed != NULL)
634 type_changed->push_back (existing);
8264ba82 635 }
11106495 636 if (install_new_value (existing, item->value.get (), 0))
0cc7d26f 637 {
0604393c
SM
638 if (!type_updated && changed != NULL)
639 changed->push_back (existing);
0cc7d26f 640 }
0604393c
SM
641 else if (!type_updated && unchanged != NULL)
642 unchanged->push_back (existing);
0cc7d26f
TT
643 }
644}
645
576ea091
YQ
646#if HAVE_PYTHON
647
4c37490d 648static bool
b09e2c59 649dynamic_varobj_has_child_method (const struct varobj *var)
0cc7d26f 650{
bb5ce47a 651 PyObject *printer = var->dynamic->pretty_printer;
0cc7d26f 652
0646da15 653 if (!gdb_python_initialized)
4c37490d 654 return false;
0646da15 655
bde7b3e3
TT
656 gdbpy_enter_varobj enter_py (var);
657 return PyObject_HasAttr (printer, gdbpy_children_cst);
0cc7d26f 658}
576ea091 659#endif
0cc7d26f 660
e5250216
YQ
661/* A factory for creating dynamic varobj's iterators. Returns an
662 iterator object suitable for iterating over VAR's children. */
663
24fd95b4 664static std::unique_ptr<varobj_iter>
e5250216
YQ
665varobj_get_iterator (struct varobj *var)
666{
576ea091 667#if HAVE_PYTHON
e5250216
YQ
668 if (var->dynamic->pretty_printer)
669 return py_varobj_get_iterator (var, var->dynamic->pretty_printer);
576ea091 670#endif
e5250216
YQ
671
672 gdb_assert_not_reached (_("\
673requested an iterator from a non-dynamic varobj"));
674}
675
4c37490d 676static bool
b6313243 677update_dynamic_varobj_children (struct varobj *var,
0604393c
SM
678 std::vector<varobj *> *changed,
679 std::vector<varobj *> *type_changed,
680 std::vector<varobj *> *newobj,
681 std::vector<varobj *> *unchanged,
4c37490d
SM
682 bool *cchanged,
683 bool update_children,
0cc7d26f
TT
684 int from,
685 int to)
b6313243 686{
b6313243 687 int i;
b6313243 688
4c37490d 689 *cchanged = false;
b6313243 690
bb5ce47a 691 if (update_children || var->dynamic->child_iter == NULL)
b6313243 692 {
e5250216 693 var->dynamic->child_iter = varobj_get_iterator (var);
446d2c03 694 var->dynamic->saved_item.reset (nullptr);
b6313243 695
e5250216 696 i = 0;
b6313243 697
bb5ce47a 698 if (var->dynamic->child_iter == NULL)
4c37490d 699 return false;
b6313243 700 }
0cc7d26f 701 else
ddf0ea08 702 i = var->children.size ();
b6313243 703
0cc7d26f
TT
704 /* We ask for one extra child, so that MI can report whether there
705 are more children. */
706 for (; to < 0 || i < to + 1; ++i)
b6313243 707 {
60ee72f6 708 std::unique_ptr<varobj_item> item;
b6313243 709
0cc7d26f 710 /* See if there was a leftover from last time. */
827f100c 711 if (var->dynamic->saved_item != NULL)
74462664 712 item = std::move (var->dynamic->saved_item);
0cc7d26f 713 else
11106495 714 item = var->dynamic->child_iter->next ();
b6313243 715
e5250216
YQ
716 if (item == NULL)
717 {
718 /* Iteration is done. Remove iterator from VAR. */
24fd95b4 719 var->dynamic->child_iter.reset (nullptr);
e5250216
YQ
720 break;
721 }
0cc7d26f
TT
722 /* We don't want to push the extra child on any report list. */
723 if (to < 0 || i < to)
b6313243 724 {
4c37490d 725 bool can_mention = from < 0 || i >= from;
0cc7d26f 726
0cc7d26f 727 install_dynamic_child (var, can_mention ? changed : NULL,
8264ba82 728 can_mention ? type_changed : NULL,
fe978cb0 729 can_mention ? newobj : NULL,
0cc7d26f 730 can_mention ? unchanged : NULL,
5e5ac9a5 731 can_mention ? cchanged : NULL, i,
60ee72f6 732 item.get ());
b6313243 733 }
0cc7d26f 734 else
b6313243 735 {
74462664 736 var->dynamic->saved_item = std::move (item);
b6313243 737
0cc7d26f
TT
738 /* We want to truncate the child list just before this
739 element. */
740 break;
741 }
b6313243
TT
742 }
743
ddf0ea08 744 if (i < var->children.size ())
b6313243 745 {
4c37490d 746 *cchanged = true;
ddf0ea08
SM
747 for (int j = i; j < var->children.size (); ++j)
748 varobj_delete (var->children[j], 0);
749
750 var->children.resize (i);
b6313243 751 }
0cc7d26f
TT
752
753 /* If there are fewer children than requested, note that the list of
754 children changed. */
ddf0ea08 755 if (to >= 0 && var->children.size () < to)
4c37490d 756 *cchanged = true;
0cc7d26f 757
ddf0ea08 758 var->num_children = var->children.size ();
b6313243 759
4c37490d 760 return true;
b6313243 761}
25d5ea92 762
8b93c638
JM
763int
764varobj_get_num_children (struct varobj *var)
765{
766 if (var->num_children == -1)
b6313243 767 {
31f628ae 768 if (varobj_is_dynamic_p (var))
0cc7d26f 769 {
4c37490d 770 bool dummy;
0cc7d26f
TT
771
772 /* If we have a dynamic varobj, don't report -1 children.
773 So, try to fetch some children first. */
8264ba82 774 update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL, &dummy,
4c37490d 775 false, 0, 0);
0cc7d26f
TT
776 }
777 else
b6313243
TT
778 var->num_children = number_of_children (var);
779 }
8b93c638 780
0cc7d26f 781 return var->num_children >= 0 ? var->num_children : 0;
8b93c638
JM
782}
783
784/* Creates a list of the immediate children of a variable object;
581e13c1 785 the return code is the number of such children or -1 on error. */
8b93c638 786
ddf0ea08 787const std::vector<varobj *> &
0cc7d26f 788varobj_list_children (struct varobj *var, int *from, int *to)
8b93c638 789{
bd046f64 790 var->dynamic->children_requested = true;
b6313243 791
31f628ae 792 if (varobj_is_dynamic_p (var))
0cc7d26f 793 {
4c37490d
SM
794 bool children_changed;
795
b6313243
TT
796 /* This, in theory, can result in the number of children changing without
797 frontend noticing. But well, calling -var-list-children on the same
798 varobj twice is not something a sane frontend would do. */
8264ba82 799 update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL,
4c37490d 800 &children_changed, false, 0, *to);
99ad9427 801 varobj_restrict_range (var->children, from, to);
0cc7d26f
TT
802 return var->children;
803 }
8b93c638 804
8b93c638
JM
805 if (var->num_children == -1)
806 var->num_children = number_of_children (var);
807
74a44383
DJ
808 /* If that failed, give up. */
809 if (var->num_children == -1)
d56d46f5 810 return var->children;
74a44383 811
28335dcc
VP
812 /* If we're called when the list of children is not yet initialized,
813 allocate enough elements in it. */
ddf0ea08
SM
814 while (var->children.size () < var->num_children)
815 var->children.push_back (NULL);
28335dcc 816
ddf0ea08 817 for (int i = 0; i < var->num_children; i++)
8b93c638 818 {
ddf0ea08 819 if (var->children[i] == NULL)
28335dcc
VP
820 {
821 /* Either it's the first call to varobj_list_children for
822 this variable object, and the child was never created,
823 or it was explicitly deleted by the client. */
2f408ecb 824 std::string name = name_of_child (var, i);
ddf0ea08 825 var->children[i] = create_child (var, i, name);
28335dcc 826 }
8b93c638
JM
827 }
828
99ad9427 829 varobj_restrict_range (var->children, from, to);
d56d46f5 830 return var->children;
8b93c638
JM
831}
832
b6313243 833static struct varobj *
5a2e0d6e 834varobj_add_child (struct varobj *var, struct varobj_item *item)
b6313243 835{
ddf0ea08
SM
836 varobj *v = create_child_with_value (var, var->children.size (), item);
837
838 var->children.push_back (v);
a109c7c1 839
b6313243
TT
840 return v;
841}
842
8b93c638 843/* Obtain the type of an object Variable as a string similar to the one gdb
afa269ae
SM
844 prints on the console. The caller is responsible for freeing the string.
845 */
8b93c638 846
2f408ecb 847std::string
8b93c638
JM
848varobj_get_type (struct varobj *var)
849{
8ab91b96 850 /* For the "fake" variables, do not return a type. (Its type is
8756216b
DP
851 NULL, too.)
852 Do not return a type for invalid variables as well. */
853 if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid)
2f408ecb 854 return std::string ();
8b93c638 855
1a4300e9 856 return type_to_string (var->type);
8b93c638
JM
857}
858
1ecb4ee0
DJ
859/* Obtain the type of an object variable. */
860
861struct type *
b09e2c59 862varobj_get_gdb_type (const struct varobj *var)
1ecb4ee0
DJ
863{
864 return var->type;
865}
866
85254831
KS
867/* Is VAR a path expression parent, i.e., can it be used to construct
868 a valid path expression? */
869
4c37490d 870static bool
b09e2c59 871is_path_expr_parent (const struct varobj *var)
85254831 872{
9a9a7608
AB
873 gdb_assert (var->root->lang_ops->is_path_expr_parent != NULL);
874 return var->root->lang_ops->is_path_expr_parent (var);
875}
85254831 876
9a9a7608
AB
877/* Is VAR a path expression parent, i.e., can it be used to construct
878 a valid path expression? By default we assume any VAR can be a path
879 parent. */
85254831 880
4c37490d 881bool
b09e2c59 882varobj_default_is_path_expr_parent (const struct varobj *var)
9a9a7608 883{
4c37490d 884 return true;
85254831
KS
885}
886
887/* Return the path expression parent for VAR. */
888
c1cc6152
SM
889const struct varobj *
890varobj_get_path_expr_parent (const struct varobj *var)
85254831 891{
c1cc6152 892 const struct varobj *parent = var;
85254831
KS
893
894 while (!is_root_p (parent) && !is_path_expr_parent (parent))
895 parent = parent->parent;
896
5abe0f0c
JV
897 /* Computation of full rooted expression for children of dynamic
898 varobjs is not supported. */
899 if (varobj_is_dynamic_p (parent))
900 error (_("Invalid variable object (child of a dynamic varobj)"));
901
85254831
KS
902 return parent;
903}
904
02142340
VP
905/* Return a pointer to the full rooted expression of varobj VAR.
906 If it has not been computed yet, compute it. */
2f408ecb
PA
907
908const char *
c1cc6152 909varobj_get_path_expr (const struct varobj *var)
02142340 910{
2f408ecb 911 if (var->path_expr.empty ())
02142340
VP
912 {
913 /* For root varobjs, we initialize path_expr
914 when creating varobj, so here it should be
915 child varobj. */
c1cc6152 916 struct varobj *mutable_var = (struct varobj *) var;
02142340 917 gdb_assert (!is_root_p (var));
2568868e 918
c1cc6152 919 mutable_var->path_expr = (*var->root->lang_ops->path_expr_of_child) (var);
02142340 920 }
2568868e 921
2f408ecb 922 return var->path_expr.c_str ();
02142340
VP
923}
924
fa4d0c40 925const struct language_defn *
b09e2c59 926varobj_get_language (const struct varobj *var)
8b93c638 927{
fa4d0c40 928 return var->root->exp->language_defn;
8b93c638
JM
929}
930
931int
b09e2c59 932varobj_get_attributes (const struct varobj *var)
8b93c638
JM
933{
934 int attributes = 0;
935
340a7723 936 if (varobj_editable_p (var))
581e13c1 937 /* FIXME: define masks for attributes. */
8b93c638
JM
938 attributes |= 0x00000001; /* Editable */
939
940 return attributes;
941}
942
cde5ef40
YQ
943/* Return true if VAR is a dynamic varobj. */
944
4c37490d 945bool
b09e2c59 946varobj_is_dynamic_p (const struct varobj *var)
0cc7d26f 947{
bb5ce47a 948 return var->dynamic->pretty_printer != NULL;
0cc7d26f
TT
949}
950
2f408ecb 951std::string
de051565
MK
952varobj_get_formatted_value (struct varobj *var,
953 enum varobj_display_formats format)
954{
955 return my_value_of_variable (var, format);
956}
957
2f408ecb 958std::string
8b93c638
JM
959varobj_get_value (struct varobj *var)
960{
de051565 961 return my_value_of_variable (var, var->format);
8b93c638
JM
962}
963
964/* Set the value of an object variable (if it is editable) to the
581e13c1
MS
965 value of the given expression. */
966/* Note: Invokes functions that can call error(). */
8b93c638 967
4c37490d 968bool
2f408ecb 969varobj_set_value (struct varobj *var, const char *expression)
8b93c638 970{
34365054 971 struct value *val = NULL; /* Initialize to keep gcc happy. */
8b93c638 972 /* The argument "expression" contains the variable's new value.
581e13c1
MS
973 We need to first construct a legal expression for this -- ugh! */
974 /* Does this cover all the bases? */
34365054 975 struct value *value = NULL; /* Initialize to keep gcc happy. */
8b93c638 976 int saved_input_radix = input_radix;
bbc13ae3 977 const char *s = expression;
8b93c638 978
340a7723 979 gdb_assert (varobj_editable_p (var));
8b93c638 980
581e13c1 981 input_radix = 10; /* ALWAYS reset to decimal temporarily. */
4d01a485 982 expression_up exp = parse_exp_1 (&s, 0, 0, 0);
a70b8144 983 try
8e7b59a5 984 {
4d01a485 985 value = evaluate_expression (exp.get ());
8e7b59a5
KS
986 }
987
230d2906 988 catch (const gdb_exception_error &except)
340a7723 989 {
581e13c1 990 /* We cannot proceed without a valid expression. */
4c37490d 991 return false;
8b93c638
JM
992 }
993
340a7723
NR
994 /* All types that are editable must also be changeable. */
995 gdb_assert (varobj_value_is_changeable_p (var));
996
997 /* The value of a changeable variable object must not be lazy. */
b4d61099 998 gdb_assert (!value_lazy (var->value.get ()));
340a7723
NR
999
1000 /* Need to coerce the input. We want to check if the
1001 value of the variable object will be different
1002 after assignment, and the first thing value_assign
1003 does is coerce the input.
1004 For example, if we are assigning an array to a pointer variable we
b021a221 1005 should compare the pointer with the array's address, not with the
340a7723
NR
1006 array's content. */
1007 value = coerce_array (value);
1008
8e7b59a5
KS
1009 /* The new value may be lazy. value_assign, or
1010 rather value_contents, will take care of this. */
a70b8144 1011 try
8e7b59a5 1012 {
b4d61099 1013 val = value_assign (var->value.get (), value);
8e7b59a5
KS
1014 }
1015
230d2906 1016 catch (const gdb_exception_error &except)
492d29ea 1017 {
4c37490d 1018 return false;
492d29ea 1019 }
8e7b59a5 1020
340a7723
NR
1021 /* If the value has changed, record it, so that next -var-update can
1022 report this change. If a variable had a value of '1', we've set it
1023 to '333' and then set again to '1', when -var-update will report this
1024 variable as changed -- because the first assignment has set the
1025 'updated' flag. There's no need to optimize that, because return value
1026 of -var-update should be considered an approximation. */
4c37490d 1027 var->updated = install_new_value (var, val, false /* Compare values. */);
340a7723 1028 input_radix = saved_input_radix;
4c37490d 1029 return true;
8b93c638
JM
1030}
1031
0cc7d26f
TT
1032#if HAVE_PYTHON
1033
1034/* A helper function to install a constructor function and visualizer
bb5ce47a 1035 in a varobj_dynamic. */
0cc7d26f
TT
1036
1037static void
bb5ce47a 1038install_visualizer (struct varobj_dynamic *var, PyObject *constructor,
0cc7d26f
TT
1039 PyObject *visualizer)
1040{
1041 Py_XDECREF (var->constructor);
1042 var->constructor = constructor;
1043
1044 Py_XDECREF (var->pretty_printer);
1045 var->pretty_printer = visualizer;
1046
24fd95b4 1047 var->child_iter.reset (nullptr);
0cc7d26f
TT
1048}
1049
1050/* Install the default visualizer for VAR. */
1051
1052static void
1053install_default_visualizer (struct varobj *var)
1054{
d65aec65
PM
1055 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1056 if (CPLUS_FAKE_CHILD (var))
1057 return;
1058
0cc7d26f
TT
1059 if (pretty_printing)
1060 {
a31abe80 1061 gdbpy_ref<> pretty_printer;
0cc7d26f 1062
b4d61099 1063 if (var->value != nullptr)
0cc7d26f 1064 {
b4d61099 1065 pretty_printer = gdbpy_get_varobj_pretty_printer (var->value.get ());
a31abe80 1066 if (pretty_printer == nullptr)
0cc7d26f
TT
1067 {
1068 gdbpy_print_stack ();
1069 error (_("Cannot instantiate printer for default visualizer"));
1070 }
1071 }
a31abe80 1072
0cc7d26f 1073 if (pretty_printer == Py_None)
895dafa6 1074 pretty_printer.reset (nullptr);
0cc7d26f 1075
a31abe80 1076 install_visualizer (var->dynamic, NULL, pretty_printer.release ());
0cc7d26f
TT
1077 }
1078}
1079
1080/* Instantiate and install a visualizer for VAR using CONSTRUCTOR to
1081 make a new object. */
1082
1083static void
1084construct_visualizer (struct varobj *var, PyObject *constructor)
1085{
1086 PyObject *pretty_printer;
1087
d65aec65
PM
1088 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */
1089 if (CPLUS_FAKE_CHILD (var))
1090 return;
1091
0cc7d26f
TT
1092 Py_INCREF (constructor);
1093 if (constructor == Py_None)
1094 pretty_printer = NULL;
1095 else
1096 {
b4d61099
TT
1097 pretty_printer = instantiate_pretty_printer (constructor,
1098 var->value.get ());
0cc7d26f
TT
1099 if (! pretty_printer)
1100 {
1101 gdbpy_print_stack ();
1102 Py_DECREF (constructor);
1103 constructor = Py_None;
1104 Py_INCREF (constructor);
1105 }
1106
1107 if (pretty_printer == Py_None)
1108 {
1109 Py_DECREF (pretty_printer);
1110 pretty_printer = NULL;
1111 }
1112 }
1113
bb5ce47a 1114 install_visualizer (var->dynamic, constructor, pretty_printer);
0cc7d26f
TT
1115}
1116
1117#endif /* HAVE_PYTHON */
1118
1119/* A helper function for install_new_value. This creates and installs
1120 a visualizer for VAR, if appropriate. */
1121
1122static void
1123install_new_value_visualizer (struct varobj *var)
1124{
1125#if HAVE_PYTHON
1126 /* If the constructor is None, then we want the raw value. If VAR
1127 does not have a value, just skip this. */
0646da15
TT
1128 if (!gdb_python_initialized)
1129 return;
1130
bb5ce47a 1131 if (var->dynamic->constructor != Py_None && var->value != NULL)
0cc7d26f 1132 {
bde7b3e3 1133 gdbpy_enter_varobj enter_py (var);
0cc7d26f 1134
bb5ce47a 1135 if (var->dynamic->constructor == NULL)
0cc7d26f
TT
1136 install_default_visualizer (var);
1137 else
bb5ce47a 1138 construct_visualizer (var, var->dynamic->constructor);
0cc7d26f
TT
1139 }
1140#else
1141 /* Do nothing. */
1142#endif
1143}
1144
8264ba82
AG
1145/* When using RTTI to determine variable type it may be changed in runtime when
1146 the variable value is changed. This function checks whether type of varobj
1147 VAR will change when a new value NEW_VALUE is assigned and if it is so
1148 updates the type of VAR. */
1149
4c37490d 1150static bool
8264ba82
AG
1151update_type_if_necessary (struct varobj *var, struct value *new_value)
1152{
1153 if (new_value)
1154 {
1155 struct value_print_options opts;
1156
1157 get_user_print_options (&opts);
1158 if (opts.objectprint)
1159 {
2f408ecb
PA
1160 struct type *new_type = value_actual_type (new_value, 0, 0);
1161 std::string new_type_str = type_to_string (new_type);
1162 std::string curr_type_str = varobj_get_type (var);
8264ba82 1163
2f408ecb
PA
1164 /* Did the type name change? */
1165 if (curr_type_str != new_type_str)
8264ba82
AG
1166 {
1167 var->type = new_type;
1168
1169 /* This information may be not valid for a new type. */
30914ca8 1170 varobj_delete (var, 1);
ddf0ea08 1171 var->children.clear ();
8264ba82 1172 var->num_children = -1;
4c37490d 1173 return true;
8264ba82
AG
1174 }
1175 }
1176 }
1177
4c37490d 1178 return false;
8264ba82
AG
1179}
1180
4c37490d
SM
1181/* Assign a new value to a variable object. If INITIAL is true,
1182 this is the first assignment after the variable object was just
acd65feb 1183 created, or changed type. In that case, just assign the value
4c37490d
SM
1184 and return false.
1185 Otherwise, assign the new value, and return true if the value is
1186 different from the current one, false otherwise. The comparison is
581e13c1
MS
1187 done on textual representation of value. Therefore, some types
1188 need not be compared. E.g. for structures the reported value is
1189 always "{...}", so no comparison is necessary here. If the old
4c37490d 1190 value was NULL and new one is not, or vice versa, we always return true.
b26ed50d
VP
1191
1192 The VALUE parameter should not be released -- the function will
1193 take care of releasing it when needed. */
4c37490d
SM
1194static bool
1195install_new_value (struct varobj *var, struct value *value, bool initial)
acd65feb 1196{
4c37490d
SM
1197 bool changeable;
1198 bool need_to_fetch;
1199 bool changed = false;
1200 bool intentionally_not_fetched = false;
acd65feb 1201
acd65feb 1202 /* We need to know the varobj's type to decide if the value should
3e43a32a 1203 be fetched or not. C++ fake children (public/protected/private)
581e13c1 1204 don't have a type. */
acd65feb 1205 gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
b2c2bd75 1206 changeable = varobj_value_is_changeable_p (var);
b6313243
TT
1207
1208 /* If the type has custom visualizer, we consider it to be always
581e13c1 1209 changeable. FIXME: need to make sure this behaviour will not
b6313243 1210 mess up read-sensitive values. */
bb5ce47a 1211 if (var->dynamic->pretty_printer != NULL)
4c37490d 1212 changeable = true;
b6313243 1213
acd65feb
VP
1214 need_to_fetch = changeable;
1215
b26ed50d
VP
1216 /* We are not interested in the address of references, and given
1217 that in C++ a reference is not rebindable, it cannot
1218 meaningfully change. So, get hold of the real value. */
1219 if (value)
0cc7d26f 1220 value = coerce_ref (value);
b26ed50d 1221
78134374 1222 if (var->type && var->type->code () == TYPE_CODE_UNION)
acd65feb
VP
1223 /* For unions, we need to fetch the value implicitly because
1224 of implementation of union member fetch. When gdb
1225 creates a value for a field and the value of the enclosing
1226 structure is not lazy, it immediately copies the necessary
1227 bytes from the enclosing values. If the enclosing value is
1228 lazy, the call to value_fetch_lazy on the field will read
1229 the data from memory. For unions, that means we'll read the
1230 same memory more than once, which is not desirable. So
1231 fetch now. */
4c37490d 1232 need_to_fetch = true;
acd65feb
VP
1233
1234 /* The new value might be lazy. If the type is changeable,
1235 that is we'll be comparing values of this type, fetch the
1236 value now. Otherwise, on the next update the old value
1237 will be lazy, which means we've lost that old value. */
1238 if (need_to_fetch && value && value_lazy (value))
1239 {
c1cc6152 1240 const struct varobj *parent = var->parent;
4c37490d 1241 bool frozen = var->frozen;
a109c7c1 1242
25d5ea92
VP
1243 for (; !frozen && parent; parent = parent->parent)
1244 frozen |= parent->frozen;
1245
1246 if (frozen && initial)
1247 {
1248 /* For variables that are frozen, or are children of frozen
1249 variables, we don't do fetch on initial assignment.
30baf67b 1250 For non-initial assignment we do the fetch, since it means we're
25d5ea92 1251 explicitly asked to compare the new value with the old one. */
4c37490d 1252 intentionally_not_fetched = true;
25d5ea92 1253 }
8e7b59a5 1254 else
acd65feb 1255 {
8e7b59a5 1256
a70b8144 1257 try
8e7b59a5
KS
1258 {
1259 value_fetch_lazy (value);
1260 }
1261
230d2906 1262 catch (const gdb_exception_error &except)
8e7b59a5
KS
1263 {
1264 /* Set the value to NULL, so that for the next -var-update,
1265 we don't try to compare the new value with this value,
1266 that we couldn't even read. */
1267 value = NULL;
1268 }
acd65feb 1269 }
acd65feb
VP
1270 }
1271
e848a8a5
TT
1272 /* Get a reference now, before possibly passing it to any Python
1273 code that might release it. */
b4d61099 1274 value_ref_ptr value_holder;
e848a8a5 1275 if (value != NULL)
bbfa6f00 1276 value_holder = value_ref_ptr::new_reference (value);
b6313243 1277
7a4d50bf
VP
1278 /* Below, we'll be comparing string rendering of old and new
1279 values. Don't get string rendering if the value is
1280 lazy -- if it is, the code above has decided that the value
1281 should not be fetched. */
2f408ecb 1282 std::string print_value;
bb5ce47a
YQ
1283 if (value != NULL && !value_lazy (value)
1284 && var->dynamic->pretty_printer == NULL)
99ad9427 1285 print_value = varobj_value_get_print_value (value, var->format, var);
7a4d50bf 1286
acd65feb
VP
1287 /* If the type is changeable, compare the old and the new values.
1288 If this is the initial assignment, we don't have any old value
1289 to compare with. */
7a4d50bf 1290 if (!initial && changeable)
acd65feb 1291 {
3e43a32a
MS
1292 /* If the value of the varobj was changed by -var-set-value,
1293 then the value in the varobj and in the target is the same.
1294 However, that value is different from the value that the
581e13c1 1295 varobj had after the previous -var-update. So need to the
3e43a32a 1296 varobj as changed. */
acd65feb 1297 if (var->updated)
4c37490d 1298 changed = true;
bb5ce47a 1299 else if (var->dynamic->pretty_printer == NULL)
acd65feb
VP
1300 {
1301 /* Try to compare the values. That requires that both
1302 values are non-lazy. */
b4d61099 1303 if (var->not_fetched && value_lazy (var->value.get ()))
25d5ea92
VP
1304 {
1305 /* This is a frozen varobj and the value was never read.
1306 Presumably, UI shows some "never read" indicator.
1307 Now that we've fetched the real value, we need to report
1308 this varobj as changed so that UI can show the real
1309 value. */
4c37490d 1310 changed = true;
25d5ea92 1311 }
dda83cd7 1312 else if (var->value == NULL && value == NULL)
581e13c1 1313 /* Equal. */
acd65feb
VP
1314 ;
1315 else if (var->value == NULL || value == NULL)
57e66780 1316 {
4c37490d 1317 changed = true;
57e66780 1318 }
acd65feb
VP
1319 else
1320 {
b4d61099 1321 gdb_assert (!value_lazy (var->value.get ()));
acd65feb 1322 gdb_assert (!value_lazy (value));
85265413 1323
2f408ecb
PA
1324 gdb_assert (!var->print_value.empty () && !print_value.empty ());
1325 if (var->print_value != print_value)
4c37490d 1326 changed = true;
acd65feb
VP
1327 }
1328 }
1329 }
85265413 1330
ee342b23
VP
1331 if (!initial && !changeable)
1332 {
1333 /* For values that are not changeable, we don't compare the values.
1334 However, we want to notice if a value was not NULL and now is NULL,
1335 or vise versa, so that we report when top-level varobjs come in scope
1336 and leave the scope. */
1337 changed = (var->value != NULL) != (value != NULL);
1338 }
1339
acd65feb 1340 /* We must always keep the new value, since children depend on it. */
b4d61099 1341 var->value = value_holder;
25d5ea92 1342 if (value && value_lazy (value) && intentionally_not_fetched)
4c37490d 1343 var->not_fetched = true;
25d5ea92 1344 else
4c37490d
SM
1345 var->not_fetched = false;
1346 var->updated = false;
85265413 1347
0cc7d26f
TT
1348 install_new_value_visualizer (var);
1349
1350 /* If we installed a pretty-printer, re-compare the printed version
1351 to see if the variable changed. */
bb5ce47a 1352 if (var->dynamic->pretty_printer != NULL)
0cc7d26f 1353 {
b4d61099
TT
1354 print_value = varobj_value_get_print_value (var->value.get (),
1355 var->format, var);
2f408ecb
PA
1356 if ((var->print_value.empty () && !print_value.empty ())
1357 || (!var->print_value.empty () && print_value.empty ())
1358 || (!var->print_value.empty () && !print_value.empty ()
1359 && var->print_value != print_value))
4c37490d 1360 changed = true;
0cc7d26f 1361 }
0cc7d26f
TT
1362 var->print_value = print_value;
1363
b4d61099 1364 gdb_assert (var->value == nullptr || value_type (var->value.get ()));
acd65feb
VP
1365
1366 return changed;
1367}
acd65feb 1368
0cc7d26f
TT
1369/* Return the requested range for a varobj. VAR is the varobj. FROM
1370 and TO are out parameters; *FROM and *TO will be set to the
1371 selected sub-range of VAR. If no range was selected using
1372 -var-set-update-range, then both will be -1. */
1373void
b09e2c59 1374varobj_get_child_range (const struct varobj *var, int *from, int *to)
b6313243 1375{
0cc7d26f
TT
1376 *from = var->from;
1377 *to = var->to;
b6313243
TT
1378}
1379
0cc7d26f
TT
1380/* Set the selected sub-range of children of VAR to start at index
1381 FROM and end at index TO. If either FROM or TO is less than zero,
1382 this is interpreted as a request for all children. */
1383void
1384varobj_set_child_range (struct varobj *var, int from, int to)
b6313243 1385{
0cc7d26f
TT
1386 var->from = from;
1387 var->to = to;
b6313243
TT
1388}
1389
1390void
1391varobj_set_visualizer (struct varobj *var, const char *visualizer)
1392{
1393#if HAVE_PYTHON
bde7b3e3 1394 PyObject *mainmod;
b6313243 1395
0646da15
TT
1396 if (!gdb_python_initialized)
1397 return;
1398
bde7b3e3 1399 gdbpy_enter_varobj enter_py (var);
b6313243
TT
1400
1401 mainmod = PyImport_AddModule ("__main__");
7c66fffc
TT
1402 gdbpy_ref<> globals
1403 = gdbpy_ref<>::new_reference (PyModule_GetDict (mainmod));
7780f186
TT
1404 gdbpy_ref<> constructor (PyRun_String (visualizer, Py_eval_input,
1405 globals.get (), globals.get ()));
b6313243 1406
bde7b3e3 1407 if (constructor == NULL)
b6313243
TT
1408 {
1409 gdbpy_print_stack ();
da1f2771 1410 error (_("Could not evaluate visualizer expression: %s"), visualizer);
b6313243
TT
1411 }
1412
bde7b3e3 1413 construct_visualizer (var, constructor.get ());
b6313243 1414
0cc7d26f 1415 /* If there are any children now, wipe them. */
30914ca8 1416 varobj_delete (var, 1 /* children only */);
0cc7d26f 1417 var->num_children = -1;
b6313243 1418#else
da1f2771 1419 error (_("Python support required"));
b6313243
TT
1420#endif
1421}
1422
7a290c40 1423/* If NEW_VALUE is the new value of the given varobj (var), return
4c37490d 1424 true if var has mutated. In other words, if the type of
7a290c40
JB
1425 the new value is different from the type of the varobj's old
1426 value.
1427
1428 NEW_VALUE may be NULL, if the varobj is now out of scope. */
1429
4c37490d 1430static bool
b09e2c59 1431varobj_value_has_mutated (const struct varobj *var, struct value *new_value,
7a290c40
JB
1432 struct type *new_type)
1433{
1434 /* If we haven't previously computed the number of children in var,
1435 it does not matter from the front-end's perspective whether
1436 the type has mutated or not. For all intents and purposes,
1437 it has not mutated. */
1438 if (var->num_children < 0)
4c37490d 1439 return false;
7a290c40 1440
4c37490d 1441 if (var->root->lang_ops->value_has_mutated != NULL)
8776cfe9
JB
1442 {
1443 /* The varobj module, when installing new values, explicitly strips
1444 references, saying that we're not interested in those addresses.
1445 But detection of mutation happens before installing the new
1446 value, so our value may be a reference that we need to strip
1447 in order to remain consistent. */
1448 if (new_value != NULL)
1449 new_value = coerce_ref (new_value);
1450 return var->root->lang_ops->value_has_mutated (var, new_value, new_type);
1451 }
7a290c40 1452 else
4c37490d 1453 return false;
7a290c40
JB
1454}
1455
8b93c638
JM
1456/* Update the values for a variable and its children. This is a
1457 two-pronged attack. First, re-parse the value for the root's
1458 expression to see if it's changed. Then go all the way
1459 through its children, reconstructing them and noting if they've
1460 changed.
1461
4c37490d 1462 The IS_EXPLICIT parameter specifies if this call is result
25d5ea92 1463 of MI request to update this specific variable, or
581e13c1 1464 result of implicit -var-update *. For implicit request, we don't
25d5ea92 1465 update frozen variables.
705da579 1466
581e13c1 1467 NOTE: This function may delete the caller's varobj. If it
8756216b
DP
1468 returns TYPE_CHANGED, then it has done this and VARP will be modified
1469 to point to the new varobj. */
8b93c638 1470
0604393c 1471std::vector<varobj_update_result>
4c37490d 1472varobj_update (struct varobj **varp, bool is_explicit)
8b93c638 1473{
4c37490d 1474 bool type_changed = false;
fe978cb0 1475 struct value *newobj;
0604393c
SM
1476 std::vector<varobj_update_result> stack;
1477 std::vector<varobj_update_result> result;
8b93c638 1478
25d5ea92
VP
1479 /* Frozen means frozen -- we don't check for any change in
1480 this varobj, including its going out of scope, or
1481 changing type. One use case for frozen varobjs is
1482 retaining previously evaluated expressions, and we don't
1483 want them to be reevaluated at all. */
fe978cb0 1484 if (!is_explicit && (*varp)->frozen)
f7f9ae2c 1485 return result;
8756216b
DP
1486
1487 if (!(*varp)->root->is_valid)
f7f9ae2c 1488 {
0604393c 1489 result.emplace_back (*varp, VAROBJ_INVALID);
f7f9ae2c
VP
1490 return result;
1491 }
8b93c638 1492
25d5ea92 1493 if ((*varp)->root->rootvar == *varp)
ae093f96 1494 {
0604393c 1495 varobj_update_result r (*varp);
f7f9ae2c 1496
581e13c1 1497 /* Update the root variable. value_of_root can return NULL
25d5ea92 1498 if the variable is no longer around, i.e. we stepped out of
581e13c1 1499 the frame in which a local existed. We are letting the
25d5ea92
VP
1500 value_of_root variable dispose of the varobj if the type
1501 has changed. */
fe978cb0 1502 newobj = value_of_root (varp, &type_changed);
4c37490d
SM
1503 if (update_type_if_necessary (*varp, newobj))
1504 type_changed = true;
f7f9ae2c 1505 r.varobj = *varp;
f7f9ae2c 1506 r.type_changed = type_changed;
fe978cb0 1507 if (install_new_value ((*varp), newobj, type_changed))
4c37490d 1508 r.changed = true;
ea56f9c2 1509
fe978cb0 1510 if (newobj == NULL)
f7f9ae2c 1511 r.status = VAROBJ_NOT_IN_SCOPE;
4c37490d 1512 r.value_installed = true;
f7f9ae2c
VP
1513
1514 if (r.status == VAROBJ_NOT_IN_SCOPE)
b6313243 1515 {
0b4bc29a 1516 if (r.type_changed || r.changed)
0604393c
SM
1517 result.push_back (std::move (r));
1518
b6313243
TT
1519 return result;
1520 }
a109c7c1 1521
0604393c 1522 stack.push_back (std::move (r));
b20d8971 1523 }
0604393c
SM
1524 else
1525 stack.emplace_back (*varp);
8b93c638 1526
8756216b 1527 /* Walk through the children, reconstructing them all. */
0604393c 1528 while (!stack.empty ())
8b93c638 1529 {
0604393c
SM
1530 varobj_update_result r = std::move (stack.back ());
1531 stack.pop_back ();
b6313243
TT
1532 struct varobj *v = r.varobj;
1533
b6313243
TT
1534 /* Update this variable, unless it's a root, which is already
1535 updated. */
1536 if (!r.value_installed)
7a290c40
JB
1537 {
1538 struct type *new_type;
1539
fe978cb0 1540 newobj = value_of_child (v->parent, v->index);
4c37490d
SM
1541 if (update_type_if_necessary (v, newobj))
1542 r.type_changed = true;
fe978cb0
PA
1543 if (newobj)
1544 new_type = value_type (newobj);
7a290c40 1545 else
ca20d462 1546 new_type = v->root->lang_ops->type_of_child (v->parent, v->index);
7a290c40 1547
fe978cb0 1548 if (varobj_value_has_mutated (v, newobj, new_type))
7a290c40
JB
1549 {
1550 /* The children are no longer valid; delete them now.
dda83cd7 1551 Report the fact that its type changed as well. */
30914ca8 1552 varobj_delete (v, 1 /* only_children */);
7a290c40
JB
1553 v->num_children = -1;
1554 v->to = -1;
1555 v->from = -1;
1556 v->type = new_type;
4c37490d 1557 r.type_changed = true;
7a290c40
JB
1558 }
1559
fe978cb0 1560 if (install_new_value (v, newobj, r.type_changed))
b6313243 1561 {
4c37490d
SM
1562 r.changed = true;
1563 v->updated = false;
b6313243
TT
1564 }
1565 }
1566
31f628ae
YQ
1567 /* We probably should not get children of a dynamic varobj, but
1568 for which -var-list-children was never invoked. */
1569 if (varobj_is_dynamic_p (v))
b6313243 1570 {
b926417a 1571 std::vector<varobj *> changed, type_changed_vec, unchanged, newobj_vec;
4c37490d 1572 bool children_changed = false;
b6313243
TT
1573
1574 if (v->frozen)
1575 continue;
1576
bd046f64 1577 if (!v->dynamic->children_requested)
0cc7d26f 1578 {
4c37490d 1579 bool dummy;
0cc7d26f
TT
1580
1581 /* If we initially did not have potential children, but
1582 now we do, consider the varobj as changed.
1583 Otherwise, if children were never requested, consider
1584 it as unchanged -- presumably, such varobj is not yet
1585 expanded in the UI, so we need not bother getting
1586 it. */
1587 if (!varobj_has_more (v, 0))
1588 {
8264ba82 1589 update_dynamic_varobj_children (v, NULL, NULL, NULL, NULL,
4c37490d 1590 &dummy, false, 0, 0);
0cc7d26f 1591 if (varobj_has_more (v, 0))
4c37490d 1592 r.changed = true;
0cc7d26f
TT
1593 }
1594
1595 if (r.changed)
0604393c 1596 result.push_back (std::move (r));
0cc7d26f
TT
1597
1598 continue;
1599 }
1600
4c37490d 1601 /* If update_dynamic_varobj_children returns false, then we have
b6313243 1602 a non-conforming pretty-printer, so we skip it. */
b926417a
TT
1603 if (update_dynamic_varobj_children (v, &changed, &type_changed_vec,
1604 &newobj_vec,
1605 &unchanged, &children_changed,
1606 true, v->from, v->to))
b6313243 1607 {
b926417a 1608 if (children_changed || !newobj_vec.empty ())
b6313243 1609 {
4c37490d 1610 r.children_changed = true;
b926417a 1611 r.newobj = std::move (newobj_vec);
b6313243 1612 }
0cc7d26f
TT
1613 /* Push in reverse order so that the first child is
1614 popped from the work stack first, and so will be
1615 added to result first. This does not affect
1616 correctness, just "nicer". */
b926417a 1617 for (int i = type_changed_vec.size () - 1; i >= 0; --i)
8264ba82 1618 {
b926417a 1619 varobj_update_result item (type_changed_vec[i]);
8264ba82
AG
1620
1621 /* Type may change only if value was changed. */
b926417a
TT
1622 item.changed = true;
1623 item.type_changed = true;
1624 item.value_installed = true;
0604393c 1625
b926417a 1626 stack.push_back (std::move (item));
8264ba82 1627 }
0604393c 1628 for (int i = changed.size () - 1; i >= 0; --i)
b6313243 1629 {
b926417a 1630 varobj_update_result item (changed[i]);
a109c7c1 1631
b926417a
TT
1632 item.changed = true;
1633 item.value_installed = true;
0604393c 1634
b926417a 1635 stack.push_back (std::move (item));
b6313243 1636 }
0604393c
SM
1637 for (int i = unchanged.size () - 1; i >= 0; --i)
1638 {
1639 if (!unchanged[i]->frozen)
1640 {
b926417a 1641 varobj_update_result item (unchanged[i]);
0604393c 1642
b926417a 1643 item.value_installed = true;
0cc7d26f 1644
b926417a 1645 stack.push_back (std::move (item));
0604393c
SM
1646 }
1647 }
1648 if (r.changed || r.children_changed)
1649 result.push_back (std::move (r));
0cc7d26f 1650
b6313243
TT
1651 continue;
1652 }
1653 }
28335dcc
VP
1654
1655 /* Push any children. Use reverse order so that the first
1656 child is popped from the work stack first, and so
1657 will be added to result first. This does not
1658 affect correctness, just "nicer". */
0604393c 1659 for (int i = v->children.size () - 1; i >= 0; --i)
8b93c638 1660 {
ddf0ea08 1661 varobj *c = v->children[i];
a109c7c1 1662
28335dcc 1663 /* Child may be NULL if explicitly deleted by -var-delete. */
25d5ea92 1664 if (c != NULL && !c->frozen)
0604393c 1665 stack.emplace_back (c);
8b93c638 1666 }
b6313243
TT
1667
1668 if (r.changed || r.type_changed)
0604393c 1669 result.push_back (std::move (r));
8b93c638
JM
1670 }
1671
f7f9ae2c 1672 return result;
8b93c638 1673}
8b93c638
JM
1674
1675/* Helper functions */
1676
1677/*
1678 * Variable object construction/destruction
1679 */
1680
1681static int
4c37490d 1682delete_variable (struct varobj *var, bool only_children_p)
8b93c638
JM
1683{
1684 int delcount = 0;
1685
30914ca8 1686 delete_variable_1 (&delcount, var, only_children_p,
4c37490d 1687 true /* remove_from_parent_p */ );
8b93c638
JM
1688
1689 return delcount;
1690}
1691
581e13c1 1692/* Delete the variable object VAR and its children. */
8b93c638
JM
1693/* IMPORTANT NOTE: If we delete a variable which is a child
1694 and the parent is not removed we dump core. It must be always
581e13c1 1695 initially called with remove_from_parent_p set. */
8b93c638 1696static void
4c37490d
SM
1697delete_variable_1 (int *delcountp, struct varobj *var, bool only_children_p,
1698 bool remove_from_parent_p)
8b93c638 1699{
581e13c1 1700 /* Delete any children of this variable, too. */
ddf0ea08 1701 for (varobj *child : var->children)
28335dcc 1702 {
214270ab
VP
1703 if (!child)
1704 continue;
ddf0ea08 1705
8b93c638 1706 if (!remove_from_parent_p)
28335dcc 1707 child->parent = NULL;
ddf0ea08 1708
4c37490d 1709 delete_variable_1 (delcountp, child, false, only_children_p);
8b93c638 1710 }
ddf0ea08 1711 var->children.clear ();
8b93c638 1712
581e13c1 1713 /* if we were called to delete only the children we are done here. */
8b93c638
JM
1714 if (only_children_p)
1715 return;
1716
581e13c1 1717 /* Otherwise, add it to the list of deleted ones and proceed to do so. */
2f408ecb 1718 /* If the name is empty, this is a temporary variable, that has not
581e13c1 1719 yet been installed, don't report it, it belongs to the caller... */
2f408ecb 1720 if (!var->obj_name.empty ())
8b93c638 1721 {
8b93c638
JM
1722 *delcountp = *delcountp + 1;
1723 }
1724
581e13c1 1725 /* If this variable has a parent, remove it from its parent's list. */
8b93c638
JM
1726 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1727 (as indicated by remove_from_parent_p) we don't bother doing an
1728 expensive list search to find the element to remove when we are
581e13c1 1729 discarding the list afterwards. */
72330bd6 1730 if ((remove_from_parent_p) && (var->parent != NULL))
ddf0ea08 1731 var->parent->children[var->index] = NULL;
72330bd6 1732
2f408ecb 1733 if (!var->obj_name.empty ())
73a93a32 1734 uninstall_variable (var);
8b93c638 1735
581e13c1 1736 /* Free memory associated with this variable. */
9e5b9d2b 1737 delete var;
8b93c638
JM
1738}
1739
581e13c1 1740/* Install the given variable VAR with the object name VAR->OBJ_NAME. */
4c37490d 1741static bool
fba45db2 1742install_variable (struct varobj *var)
8b93c638 1743{
2c1413a9
TT
1744 hashval_t hash = htab_hash_string (var->obj_name.c_str ());
1745 void **slot = htab_find_slot_with_hash (varobj_table,
1746 var->obj_name.c_str (),
1747 hash, INSERT);
1748 if (*slot != nullptr)
8a3fe4f8 1749 error (_("Duplicate variable object name"));
8b93c638 1750
581e13c1 1751 /* Add varobj to hash table. */
2c1413a9 1752 *slot = var;
8b93c638 1753
581e13c1 1754 /* If root, add varobj to root list. */
b2c2bd75 1755 if (is_root_p (var))
76deb5d9 1756 rootlist.push_front (var->root);
8b93c638 1757
4c37490d 1758 return true; /* OK */
8b93c638
JM
1759}
1760
405feb71 1761/* Uninstall the object VAR. */
8b93c638 1762static void
fba45db2 1763uninstall_variable (struct varobj *var)
8b93c638 1764{
2c1413a9
TT
1765 hashval_t hash = htab_hash_string (var->obj_name.c_str ());
1766 htab_remove_elt_with_hash (varobj_table, var->obj_name.c_str (), hash);
8b93c638
JM
1767
1768 if (varobjdebug)
2f408ecb 1769 fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name.c_str ());
8b93c638 1770
581e13c1 1771 /* If root, remove varobj from root list. */
b2c2bd75 1772 if (is_root_p (var))
8b93c638 1773 {
76deb5d9
TT
1774 auto iter = std::find (rootlist.begin (), rootlist.end (), var->root);
1775 rootlist.erase (iter);
8b93c638 1776 }
8b93c638
JM
1777}
1778
837ce252
SM
1779/* Create and install a child of the parent of the given name.
1780
1781 The created VAROBJ takes ownership of the allocated NAME. */
1782
8b93c638 1783static struct varobj *
2f408ecb 1784create_child (struct varobj *parent, int index, std::string &name)
b6313243 1785{
5a2e0d6e
YQ
1786 struct varobj_item item;
1787
2f408ecb 1788 std::swap (item.name, name);
11106495 1789 item.value = release_value (value_of_child (parent, index));
5a2e0d6e
YQ
1790
1791 return create_child_with_value (parent, index, &item);
b6313243
TT
1792}
1793
1794static struct varobj *
5a2e0d6e
YQ
1795create_child_with_value (struct varobj *parent, int index,
1796 struct varobj_item *item)
8b93c638 1797{
9e5b9d2b 1798 varobj *child = new varobj (parent->root);
8b93c638 1799
5e5ac9a5 1800 /* NAME is allocated by caller. */
2f408ecb 1801 std::swap (child->name, item->name);
8b93c638 1802 child->index = index;
8b93c638 1803 child->parent = parent;
85254831 1804
99ad9427 1805 if (varobj_is_anonymous_child (child))
2f408ecb
PA
1806 child->obj_name = string_printf ("%s.%d_anonymous",
1807 parent->obj_name.c_str (), index);
85254831 1808 else
2f408ecb
PA
1809 child->obj_name = string_printf ("%s.%s",
1810 parent->obj_name.c_str (),
1811 child->name.c_str ());
85254831 1812
8b93c638
JM
1813 install_variable (child);
1814
acd65feb
VP
1815 /* Compute the type of the child. Must do this before
1816 calling install_new_value. */
5a2e0d6e 1817 if (item->value != NULL)
acd65feb 1818 /* If the child had no evaluation errors, var->value
581e13c1 1819 will be non-NULL and contain a valid type. */
11106495 1820 child->type = value_actual_type (item->value.get (), 0, NULL);
acd65feb 1821 else
581e13c1 1822 /* Otherwise, we must compute the type. */
ca20d462
YQ
1823 child->type = (*child->root->lang_ops->type_of_child) (child->parent,
1824 child->index);
11106495 1825 install_new_value (child, item->value.get (), 1);
acd65feb 1826
8b93c638
JM
1827 return child;
1828}
8b93c638
JM
1829\f
1830
1831/*
1832 * Miscellaneous utility functions.
1833 */
1834
581e13c1 1835/* Allocate memory and initialize a new variable. */
9e5b9d2b
SM
1836varobj::varobj (varobj_root *root_)
1837: root (root_), dynamic (new varobj_dynamic)
8b93c638 1838{
8b93c638
JM
1839}
1840
581e13c1 1841/* Free any allocated memory associated with VAR. */
9e5b9d2b
SM
1842
1843varobj::~varobj ()
8b93c638 1844{
9e5b9d2b
SM
1845 varobj *var = this;
1846
d452c4bc 1847#if HAVE_PYTHON
bb5ce47a 1848 if (var->dynamic->pretty_printer != NULL)
d452c4bc 1849 {
bde7b3e3 1850 gdbpy_enter_varobj enter_py (var);
bb5ce47a
YQ
1851
1852 Py_XDECREF (var->dynamic->constructor);
1853 Py_XDECREF (var->dynamic->pretty_printer);
d452c4bc
UW
1854 }
1855#endif
1856
b2c2bd75 1857 if (is_root_p (var))
4d01a485 1858 delete var->root;
8b93c638 1859
9e5b9d2b 1860 delete var->dynamic;
74b7792f
AC
1861}
1862
6e2a9270
VP
1863/* Return the type of the value that's stored in VAR,
1864 or that would have being stored there if the
581e13c1 1865 value were accessible.
6e2a9270
VP
1866
1867 This differs from VAR->type in that VAR->type is always
85102364 1868 the true type of the expression in the source language.
6e2a9270
VP
1869 The return value of this function is the type we're
1870 actually storing in varobj, and using for displaying
1871 the values and for comparing previous and new values.
1872
1873 For example, top-level references are always stripped. */
99ad9427 1874struct type *
b09e2c59 1875varobj_get_value_type (const struct varobj *var)
6e2a9270
VP
1876{
1877 struct type *type;
1878
b4d61099
TT
1879 if (var->value != nullptr)
1880 type = value_type (var->value.get ());
6e2a9270
VP
1881 else
1882 type = var->type;
1883
1884 type = check_typedef (type);
1885
aa006118 1886 if (TYPE_IS_REFERENCE (type))
6e2a9270
VP
1887 type = get_target_type (type);
1888
1889 type = check_typedef (type);
1890
1891 return type;
1892}
1893
8b93c638 1894/* What is the default display for this variable? We assume that
581e13c1 1895 everything is "natural". Any exceptions? */
8b93c638 1896static enum varobj_display_formats
fba45db2 1897variable_default_display (struct varobj *var)
8b93c638
JM
1898{
1899 return FORMAT_NATURAL;
1900}
1901
8b93c638
JM
1902/*
1903 * Language-dependencies
1904 */
1905
1906/* Common entry points */
1907
8b93c638
JM
1908/* Return the number of children for a given variable.
1909 The result of this function is defined by the language
581e13c1 1910 implementation. The number of children returned by this function
8b93c638 1911 is the number of children that the user will see in the variable
581e13c1 1912 display. */
8b93c638 1913static int
b09e2c59 1914number_of_children (const struct varobj *var)
8b93c638 1915{
ca20d462 1916 return (*var->root->lang_ops->number_of_children) (var);
8b93c638
JM
1917}
1918
2f408ecb
PA
1919/* What is the expression for the root varobj VAR? */
1920
1921static std::string
b09e2c59 1922name_of_variable (const struct varobj *var)
8b93c638 1923{
ca20d462 1924 return (*var->root->lang_ops->name_of_variable) (var);
8b93c638
JM
1925}
1926
2f408ecb
PA
1927/* What is the name of the INDEX'th child of VAR? */
1928
1929static std::string
fba45db2 1930name_of_child (struct varobj *var, int index)
8b93c638 1931{
ca20d462 1932 return (*var->root->lang_ops->name_of_child) (var, index);
8b93c638
JM
1933}
1934
2213e2be 1935/* If frame associated with VAR can be found, switch
4c37490d 1936 to it and return true. Otherwise, return false. */
2213e2be 1937
4c37490d 1938static bool
b09e2c59 1939check_scope (const struct varobj *var)
2213e2be
YQ
1940{
1941 struct frame_info *fi;
4c37490d 1942 bool scope;
2213e2be
YQ
1943
1944 fi = frame_find_by_id (var->root->frame);
1945 scope = fi != NULL;
1946
1947 if (fi)
1948 {
1949 CORE_ADDR pc = get_frame_pc (fi);
1950
1951 if (pc < BLOCK_START (var->root->valid_block) ||
1952 pc >= BLOCK_END (var->root->valid_block))
4c37490d 1953 scope = false;
2213e2be
YQ
1954 else
1955 select_frame (fi);
1956 }
1957 return scope;
1958}
1959
1960/* Helper function to value_of_root. */
1961
1962static struct value *
1963value_of_root_1 (struct varobj **var_handle)
1964{
1965 struct value *new_val = NULL;
1966 struct varobj *var = *var_handle;
4c37490d 1967 bool within_scope = false;
2213e2be
YQ
1968
1969 /* Only root variables can be updated... */
1970 if (!is_root_p (var))
1971 /* Not a root var. */
1972 return NULL;
1973
5ed8105e 1974 scoped_restore_current_thread restore_thread;
2213e2be
YQ
1975
1976 /* Determine whether the variable is still around. */
1977 if (var->root->valid_block == NULL || var->root->floating)
4c37490d 1978 within_scope = true;
2213e2be
YQ
1979 else if (var->root->thread_id == 0)
1980 {
1981 /* The program was single-threaded when the variable object was
1982 created. Technically, it's possible that the program became
1983 multi-threaded since then, but we don't support such
1984 scenario yet. */
1985 within_scope = check_scope (var);
1986 }
1987 else
1988 {
00431a78 1989 thread_info *thread = find_thread_global_id (var->root->thread_id);
5d5658a1 1990
00431a78 1991 if (thread != NULL)
2213e2be 1992 {
00431a78 1993 switch_to_thread (thread);
2213e2be
YQ
1994 within_scope = check_scope (var);
1995 }
1996 }
1997
1998 if (within_scope)
1999 {
2213e2be
YQ
2000
2001 /* We need to catch errors here, because if evaluate
dda83cd7 2002 expression fails we want to just return NULL. */
a70b8144 2003 try
2213e2be 2004 {
4d01a485 2005 new_val = evaluate_expression (var->root->exp.get ());
2213e2be 2006 }
230d2906 2007 catch (const gdb_exception_error &except)
492d29ea
PA
2008 {
2009 }
2213e2be
YQ
2010 }
2011
2213e2be
YQ
2012 return new_val;
2013}
2014
a5defcdc
VP
2015/* What is the ``struct value *'' of the root variable VAR?
2016 For floating variable object, evaluation can get us a value
2017 of different type from what is stored in varobj already. In
2018 that case:
2019 - *type_changed will be set to 1
2020 - old varobj will be freed, and new one will be
2021 created, with the same name.
2022 - *var_handle will be set to the new varobj
2023 Otherwise, *type_changed will be set to 0. */
30b28db1 2024static struct value *
4c37490d 2025value_of_root (struct varobj **var_handle, bool *type_changed)
8b93c638 2026{
73a93a32
JI
2027 struct varobj *var;
2028
2029 if (var_handle == NULL)
2030 return NULL;
2031
2032 var = *var_handle;
2033
2034 /* This should really be an exception, since this should
581e13c1 2035 only get called with a root variable. */
73a93a32 2036
b2c2bd75 2037 if (!is_root_p (var))
73a93a32
JI
2038 return NULL;
2039
a5defcdc 2040 if (var->root->floating)
73a93a32
JI
2041 {
2042 struct varobj *tmp_var;
6225abfa 2043
2f408ecb 2044 tmp_var = varobj_create (NULL, var->name.c_str (), (CORE_ADDR) 0,
73a93a32
JI
2045 USE_SELECTED_FRAME);
2046 if (tmp_var == NULL)
2047 {
2048 return NULL;
2049 }
2f408ecb
PA
2050 std::string old_type = varobj_get_type (var);
2051 std::string new_type = varobj_get_type (tmp_var);
2052 if (old_type == new_type)
73a93a32 2053 {
fcacd99f
VP
2054 /* The expression presently stored inside var->root->exp
2055 remembers the locations of local variables relatively to
2056 the frame where the expression was created (in DWARF location
2057 button, for example). Naturally, those locations are not
2058 correct in other frames, so update the expression. */
2059
4d01a485 2060 std::swap (var->root->exp, tmp_var->root->exp);
fcacd99f 2061
30914ca8 2062 varobj_delete (tmp_var, 0);
73a93a32
JI
2063 *type_changed = 0;
2064 }
2065 else
2066 {
2f408ecb 2067 tmp_var->obj_name = var->obj_name;
0cc7d26f
TT
2068 tmp_var->from = var->from;
2069 tmp_var->to = var->to;
30914ca8 2070 varobj_delete (var, 0);
a5defcdc 2071
73a93a32
JI
2072 install_variable (tmp_var);
2073 *var_handle = tmp_var;
705da579 2074 var = *var_handle;
4c37490d 2075 *type_changed = true;
73a93a32
JI
2076 }
2077 }
2078 else
2079 {
2080 *type_changed = 0;
2081 }
2082
7a290c40
JB
2083 {
2084 struct value *value;
2085
2213e2be 2086 value = value_of_root_1 (var_handle);
7a290c40
JB
2087 if (var->value == NULL || value == NULL)
2088 {
2089 /* For root varobj-s, a NULL value indicates a scoping issue.
2090 So, nothing to do in terms of checking for mutations. */
2091 }
2092 else if (varobj_value_has_mutated (var, value, value_type (value)))
2093 {
2094 /* The type has mutated, so the children are no longer valid.
2095 Just delete them, and tell our caller that the type has
2096 changed. */
30914ca8 2097 varobj_delete (var, 1 /* only_children */);
7a290c40
JB
2098 var->num_children = -1;
2099 var->to = -1;
2100 var->from = -1;
4c37490d 2101 *type_changed = true;
7a290c40
JB
2102 }
2103 return value;
2104 }
8b93c638
JM
2105}
2106
581e13c1 2107/* What is the ``struct value *'' for the INDEX'th child of PARENT? */
30b28db1 2108static struct value *
c1cc6152 2109value_of_child (const struct varobj *parent, int index)
8b93c638 2110{
30b28db1 2111 struct value *value;
8b93c638 2112
ca20d462 2113 value = (*parent->root->lang_ops->value_of_child) (parent, index);
8b93c638 2114
8b93c638
JM
2115 return value;
2116}
2117
581e13c1 2118/* GDB already has a command called "value_of_variable". Sigh. */
2f408ecb 2119static std::string
de051565 2120my_value_of_variable (struct varobj *var, enum varobj_display_formats format)
8b93c638 2121{
8756216b 2122 if (var->root->is_valid)
0cc7d26f 2123 {
bb5ce47a 2124 if (var->dynamic->pretty_printer != NULL)
b4d61099
TT
2125 return varobj_value_get_print_value (var->value.get (), var->format,
2126 var);
ca20d462 2127 return (*var->root->lang_ops->value_of_variable) (var, format);
0cc7d26f 2128 }
8756216b 2129 else
2f408ecb 2130 return std::string ();
8b93c638
JM
2131}
2132
99ad9427
YQ
2133void
2134varobj_formatted_print_options (struct value_print_options *opts,
2135 enum varobj_display_formats format)
2136{
2137 get_formatted_print_options (opts, format_code[(int) format]);
2138 opts->deref_ref = 0;
0625771b 2139 opts->raw = !pretty_printing;
99ad9427
YQ
2140}
2141
2f408ecb 2142std::string
99ad9427
YQ
2143varobj_value_get_print_value (struct value *value,
2144 enum varobj_display_formats format,
b09e2c59 2145 const struct varobj *var)
85265413 2146{
79a45b7d 2147 struct value_print_options opts;
be759fcf
PM
2148 struct type *type = NULL;
2149 long len = 0;
1eba6383 2150 gdb::unique_xmalloc_ptr<char> encoding;
3a182a69
JK
2151 /* Initialize it just to avoid a GCC false warning. */
2152 CORE_ADDR str_addr = 0;
4c37490d 2153 bool string_print = false;
57e66780
DJ
2154
2155 if (value == NULL)
2f408ecb 2156 return std::string ();
57e66780 2157
d7e74731 2158 string_file stb;
2f408ecb
PA
2159 std::string thevalue;
2160
b6313243 2161#if HAVE_PYTHON
0646da15
TT
2162 if (gdb_python_initialized)
2163 {
bb5ce47a 2164 PyObject *value_formatter = var->dynamic->pretty_printer;
d452c4bc 2165
68cdc557 2166 gdbpy_enter_varobj enter_py (var);
09ca9e2e 2167
0646da15
TT
2168 if (value_formatter)
2169 {
2170 /* First check to see if we have any children at all. If so,
2171 we simply return {...}. */
2172 if (dynamic_varobj_has_child_method (var))
d7e74731 2173 return "{...}";
b6313243 2174
0646da15
TT
2175 if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst))
2176 {
2177 struct value *replacement;
0646da15 2178
a5c5eda7
SM
2179 gdbpy_ref<> output = apply_varobj_pretty_printer (value_formatter,
2180 &replacement,
2181 &stb);
0646da15
TT
2182
2183 /* If we have string like output ... */
68cdc557 2184 if (output != NULL)
0646da15 2185 {
0646da15
TT
2186 /* If this is a lazy string, extract it. For lazy
2187 strings we always print as a string, so set
2188 string_print. */
68cdc557 2189 if (gdbpy_is_lazy_string (output.get ()))
0646da15 2190 {
68cdc557
TT
2191 gdbpy_extract_lazy_string (output.get (), &str_addr,
2192 &type, &len, &encoding);
4c37490d 2193 string_print = true;
0646da15
TT
2194 }
2195 else
2196 {
2197 /* If it is a regular (non-lazy) string, extract
2198 it and copy the contents into THEVALUE. If the
2199 hint says to print it as a string, set
2200 string_print. Otherwise just return the extracted
2201 string as a value. */
2202
9b972014 2203 gdb::unique_xmalloc_ptr<char> s
68cdc557 2204 = python_string_to_target_string (output.get ());
0646da15
TT
2205
2206 if (s)
2207 {
e3821cca 2208 struct gdbarch *gdbarch;
0646da15 2209
9b972014
TT
2210 gdb::unique_xmalloc_ptr<char> hint
2211 = gdbpy_get_display_hint (value_formatter);
0646da15
TT
2212 if (hint)
2213 {
9b972014 2214 if (!strcmp (hint.get (), "string"))
4c37490d 2215 string_print = true;
0646da15
TT
2216 }
2217
9b972014 2218 thevalue = std::string (s.get ());
2f408ecb 2219 len = thevalue.size ();
e3821cca 2220 gdbarch = get_type_arch (value_type (value));
0646da15 2221 type = builtin_type (gdbarch)->builtin_char;
0646da15
TT
2222
2223 if (!string_print)
d7e74731 2224 return thevalue;
0646da15
TT
2225 }
2226 else
2227 gdbpy_print_stack ();
2228 }
2229 }
2230 /* If the printer returned a replacement value, set VALUE
2231 to REPLACEMENT. If there is not a replacement value,
2232 just use the value passed to this function. */
2233 if (replacement)
2234 value = replacement;
2235 }
2236 }
2237 }
b6313243
TT
2238#endif
2239
99ad9427 2240 varobj_formatted_print_options (&opts, format);
00bd41d6
PM
2241
2242 /* If the THEVALUE has contents, it is a regular string. */
2f408ecb 2243 if (!thevalue.empty ())
d7e74731 2244 LA_PRINT_STRING (&stb, type, (gdb_byte *) thevalue.c_str (),
1eba6383 2245 len, encoding.get (), 0, &opts);
09ca9e2e 2246 else if (string_print)
00bd41d6
PM
2247 /* Otherwise, if string_print is set, and it is not a regular
2248 string, it is a lazy string. */
d7e74731 2249 val_print_string (type, encoding.get (), str_addr, len, &stb, &opts);
b6313243 2250 else
00bd41d6 2251 /* All other cases. */
d7e74731 2252 common_val_print (value, &stb, 0, &opts, current_language);
57e66780 2253
d7e74731 2254 return std::move (stb.string ());
85265413
NR
2255}
2256
4c37490d 2257bool
b09e2c59 2258varobj_editable_p (const struct varobj *var)
340a7723
NR
2259{
2260 struct type *type;
340a7723 2261
b4d61099
TT
2262 if (!(var->root->is_valid && var->value != nullptr
2263 && VALUE_LVAL (var->value.get ())))
4c37490d 2264 return false;
340a7723 2265
99ad9427 2266 type = varobj_get_value_type (var);
340a7723 2267
78134374 2268 switch (type->code ())
340a7723
NR
2269 {
2270 case TYPE_CODE_STRUCT:
2271 case TYPE_CODE_UNION:
2272 case TYPE_CODE_ARRAY:
2273 case TYPE_CODE_FUNC:
2274 case TYPE_CODE_METHOD:
4c37490d 2275 return false;
340a7723
NR
2276 break;
2277
2278 default:
4c37490d 2279 return true;
340a7723
NR
2280 break;
2281 }
2282}
2283
d32cafc7 2284/* Call VAR's value_is_changeable_p language-specific callback. */
acd65feb 2285
4c37490d 2286bool
b09e2c59 2287varobj_value_is_changeable_p (const struct varobj *var)
8b93c638 2288{
ca20d462 2289 return var->root->lang_ops->value_is_changeable_p (var);
8b93c638
JM
2290}
2291
4c37490d 2292/* Return true if that varobj is floating, that is is always evaluated in the
5a413362
VP
2293 selected frame, and not bound to thread/frame. Such variable objects
2294 are created using '@' as frame specifier to -var-create. */
4c37490d 2295bool
b09e2c59 2296varobj_floating_p (const struct varobj *var)
5a413362
VP
2297{
2298 return var->root->floating;
2299}
2300
d32cafc7
JB
2301/* Implement the "value_is_changeable_p" varobj callback for most
2302 languages. */
2303
4c37490d 2304bool
b09e2c59 2305varobj_default_value_is_changeable_p (const struct varobj *var)
d32cafc7 2306{
4c37490d 2307 bool r;
d32cafc7
JB
2308 struct type *type;
2309
2310 if (CPLUS_FAKE_CHILD (var))
4c37490d 2311 return false;
d32cafc7 2312
99ad9427 2313 type = varobj_get_value_type (var);
d32cafc7 2314
78134374 2315 switch (type->code ())
d32cafc7
JB
2316 {
2317 case TYPE_CODE_STRUCT:
2318 case TYPE_CODE_UNION:
2319 case TYPE_CODE_ARRAY:
4c37490d 2320 r = false;
d32cafc7
JB
2321 break;
2322
2323 default:
4c37490d 2324 r = true;
d32cafc7
JB
2325 }
2326
2327 return r;
2328}
2329
d8f168dd
TT
2330/* Iterate all the existing _root_ VAROBJs and call the FUNC callback
2331 for each one. */
54333c3b
JK
2332
2333void
d8f168dd 2334all_root_varobjs (gdb::function_view<void (struct varobj *var)> func)
54333c3b 2335{
54333c3b 2336 /* Iterate "safely" - handle if the callee deletes its passed VAROBJ. */
76deb5d9
TT
2337 auto iter = rootlist.begin ();
2338 auto end = rootlist.end ();
2339 while (iter != end)
54333c3b 2340 {
76deb5d9 2341 auto self = iter++;
d8f168dd 2342 func ((*self)->rootvar);
54333c3b
JK
2343 }
2344}
8756216b 2345
54333c3b 2346/* Invalidate varobj VAR if it is tied to locals and re-create it if it is
4e969b4f
AB
2347 defined on globals. It is a helper for varobj_invalidate.
2348
2349 This function is called after changing the symbol file, in this case the
2350 pointers to "struct type" stored by the varobj are no longer valid. All
2351 varobj must be either re-evaluated, or marked as invalid here. */
2dbd25e5 2352
54333c3b 2353static void
d8f168dd 2354varobj_invalidate_iter (struct varobj *var)
8756216b 2355{
4e969b4f
AB
2356 /* global and floating var must be re-evaluated. */
2357 if (var->root->floating || var->root->valid_block == NULL)
2dbd25e5 2358 {
54333c3b 2359 struct varobj *tmp_var;
2dbd25e5 2360
54333c3b
JK
2361 /* Try to create a varobj with same expression. If we succeed
2362 replace the old varobj, otherwise invalidate it. */
2f408ecb 2363 tmp_var = varobj_create (NULL, var->name.c_str (), (CORE_ADDR) 0,
54333c3b
JK
2364 USE_CURRENT_FRAME);
2365 if (tmp_var != NULL)
2366 {
2f408ecb 2367 tmp_var->obj_name = var->obj_name;
30914ca8 2368 varobj_delete (var, 0);
54333c3b 2369 install_variable (tmp_var);
2dbd25e5 2370 }
54333c3b 2371 else
4c37490d 2372 var->root->is_valid = false;
2dbd25e5 2373 }
54333c3b 2374 else /* locals must be invalidated. */
4c37490d 2375 var->root->is_valid = false;
54333c3b
JK
2376}
2377
2378/* Invalidate the varobjs that are tied to locals and re-create the ones that
2379 are defined on globals.
2380 Invalidated varobjs will be always printed in_scope="invalid". */
2381
2382void
2383varobj_invalidate (void)
2384{
d8f168dd 2385 all_root_varobjs (varobj_invalidate_iter);
8756216b 2386}
481695ed 2387
2c1413a9
TT
2388/* A hash function for a varobj. */
2389
2390static hashval_t
2391hash_varobj (const void *a)
2392{
2393 const varobj *obj = (const varobj *) a;
2394 return htab_hash_string (obj->obj_name.c_str ());
2395}
2396
2397/* A hash table equality function for varobjs. */
2398
2399static int
2400eq_varobj_and_string (const void *a, const void *b)
2401{
2402 const varobj *obj = (const varobj *) a;
2403 const char *name = (const char *) b;
2404
2405 return obj->obj_name == name;
2406}
2407
6c265988 2408void _initialize_varobj ();
1c3569d4 2409void
6c265988 2410_initialize_varobj ()
1c3569d4 2411{
2c1413a9
TT
2412 varobj_table = htab_create_alloc (5, hash_varobj, eq_varobj_and_string,
2413 nullptr, xcalloc, xfree);
1c3569d4
MR
2414
2415 add_setshow_zuinteger_cmd ("varobj", class_maintenance,
2416 &varobjdebug,
2417 _("Set varobj debugging."),
2418 _("Show varobj debugging."),
2419 _("When non-zero, varobj debugging is enabled."),
2420 NULL, show_varobjdebug,
2421 &setdebuglist, &showdebuglist);
2422}
This page took 2.931313 seconds and 4 git commands to generate.