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