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