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