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