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