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