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