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