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