Commit | Line | Data |
---|---|---|
8b93c638 | 1 | /* Implementation of the GDB variable objects API. |
bc8332bb | 2 | |
c5a57081 | 3 | Copyright (C) 1999-2012 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" | |
a6c442d8 | 19 | #include "exceptions.h" |
8b93c638 JM |
20 | #include "value.h" |
21 | #include "expression.h" | |
22 | #include "frame.h" | |
8b93c638 | 23 | #include "language.h" |
8b93c638 | 24 | #include "gdbcmd.h" |
d2353924 | 25 | #include "block.h" |
79a45b7d | 26 | #include "valprint.h" |
a6c442d8 MK |
27 | |
28 | #include "gdb_assert.h" | |
b66d6d2e | 29 | #include "gdb_string.h" |
0cc7d26f | 30 | #include "gdb_regex.h" |
8b93c638 JM |
31 | |
32 | #include "varobj.h" | |
28335dcc | 33 | #include "vec.h" |
6208b47d VP |
34 | #include "gdbthread.h" |
35 | #include "inferior.h" | |
181875a4 JB |
36 | #include "ada-varobj.h" |
37 | #include "ada-lang.h" | |
8b93c638 | 38 | |
b6313243 TT |
39 | #if HAVE_PYTHON |
40 | #include "python/python.h" | |
41 | #include "python/python-internal.h" | |
50389644 PA |
42 | #else |
43 | typedef int PyObject; | |
b6313243 TT |
44 | #endif |
45 | ||
85254831 KS |
46 | /* The names of varobjs representing anonymous structs or unions. */ |
47 | #define ANONYMOUS_STRUCT_NAME _("<anonymous struct>") | |
48 | #define ANONYMOUS_UNION_NAME _("<anonymous union>") | |
49 | ||
8b93c638 JM |
50 | /* Non-zero if we want to see trace of varobj level stuff. */ |
51 | ||
52 | int varobjdebug = 0; | |
920d2a44 AC |
53 | static void |
54 | show_varobjdebug (struct ui_file *file, int from_tty, | |
55 | struct cmd_list_element *c, const char *value) | |
56 | { | |
57 | fprintf_filtered (file, _("Varobj debugging is %s.\n"), value); | |
58 | } | |
8b93c638 | 59 | |
581e13c1 | 60 | /* String representations of gdb's format codes. */ |
8b93c638 | 61 | char *varobj_format_string[] = |
72330bd6 | 62 | { "natural", "binary", "decimal", "hexadecimal", "octal" }; |
8b93c638 | 63 | |
581e13c1 | 64 | /* String representations of gdb's known languages. */ |
72330bd6 | 65 | char *varobj_language_string[] = { "unknown", "C", "C++", "Java" }; |
8b93c638 | 66 | |
0cc7d26f TT |
67 | /* True if we want to allow Python-based pretty-printing. */ |
68 | static int pretty_printing = 0; | |
69 | ||
70 | void | |
71 | varobj_enable_pretty_printing (void) | |
72 | { | |
73 | pretty_printing = 1; | |
74 | } | |
75 | ||
8b93c638 JM |
76 | /* Data structures */ |
77 | ||
78 | /* Every root variable has one of these structures saved in its | |
581e13c1 | 79 | varobj. Members which must be free'd are noted. */ |
8b93c638 | 80 | struct varobj_root |
72330bd6 | 81 | { |
8b93c638 | 82 | |
581e13c1 | 83 | /* Alloc'd expression for this parent. */ |
72330bd6 | 84 | struct expression *exp; |
8b93c638 | 85 | |
581e13c1 | 86 | /* Block for which this expression is valid. */ |
72330bd6 | 87 | struct block *valid_block; |
8b93c638 | 88 | |
44a67aa7 VP |
89 | /* The frame for this expression. This field is set iff valid_block is |
90 | not NULL. */ | |
e64d9b3d | 91 | struct frame_id frame; |
8b93c638 | 92 | |
c5b48eac | 93 | /* The thread ID that this varobj_root belong to. This field |
581e13c1 | 94 | is only valid if valid_block is not NULL. |
c5b48eac VP |
95 | When not 0, indicates which thread 'frame' belongs to. |
96 | When 0, indicates that the thread list was empty when the varobj_root | |
97 | was created. */ | |
98 | int thread_id; | |
99 | ||
a5defcdc VP |
100 | /* If 1, the -var-update always recomputes the value in the |
101 | current thread and frame. Otherwise, variable object is | |
581e13c1 | 102 | always updated in the specific scope/thread/frame. */ |
a5defcdc | 103 | int floating; |
73a93a32 | 104 | |
8756216b DP |
105 | /* Flag that indicates validity: set to 0 when this varobj_root refers |
106 | to symbols that do not exist anymore. */ | |
107 | int is_valid; | |
108 | ||
581e13c1 | 109 | /* Language info for this variable and its children. */ |
72330bd6 | 110 | struct language_specific *lang; |
8b93c638 | 111 | |
581e13c1 | 112 | /* The varobj for this root node. */ |
72330bd6 | 113 | struct varobj *rootvar; |
8b93c638 | 114 | |
72330bd6 AC |
115 | /* Next root variable */ |
116 | struct varobj_root *next; | |
117 | }; | |
8b93c638 JM |
118 | |
119 | /* Every variable in the system has a structure of this type defined | |
581e13c1 MS |
120 | for it. This structure holds all information necessary to manipulate |
121 | a particular object variable. Members which must be freed are noted. */ | |
8b93c638 | 122 | struct varobj |
72330bd6 | 123 | { |
8b93c638 | 124 | |
581e13c1 | 125 | /* Alloc'd name of the variable for this object. If this variable is a |
72330bd6 | 126 | child, then this name will be the child's source name. |
581e13c1 MS |
127 | (bar, not foo.bar). */ |
128 | /* NOTE: This is the "expression". */ | |
72330bd6 | 129 | char *name; |
8b93c638 | 130 | |
02142340 VP |
131 | /* Alloc'd expression for this child. Can be used to create a |
132 | root variable corresponding to this child. */ | |
133 | char *path_expr; | |
134 | ||
581e13c1 MS |
135 | /* The alloc'd name for this variable's object. This is here for |
136 | convenience when constructing this object's children. */ | |
72330bd6 | 137 | char *obj_name; |
8b93c638 | 138 | |
581e13c1 | 139 | /* Index of this variable in its parent or -1. */ |
72330bd6 | 140 | int index; |
8b93c638 | 141 | |
202ddcaa VP |
142 | /* The type of this variable. This can be NULL |
143 | for artifial variable objects -- currently, the "accessibility" | |
144 | variable objects in C++. */ | |
72330bd6 | 145 | struct type *type; |
8b93c638 | 146 | |
b20d8971 VP |
147 | /* The value of this expression or subexpression. A NULL value |
148 | indicates there was an error getting this value. | |
b2c2bd75 VP |
149 | Invariant: if varobj_value_is_changeable_p (this) is non-zero, |
150 | the value is either NULL, or not lazy. */ | |
30b28db1 | 151 | struct value *value; |
8b93c638 | 152 | |
581e13c1 | 153 | /* The number of (immediate) children this variable has. */ |
72330bd6 | 154 | int num_children; |
8b93c638 | 155 | |
581e13c1 | 156 | /* If this object is a child, this points to its immediate parent. */ |
72330bd6 | 157 | struct varobj *parent; |
8b93c638 | 158 | |
28335dcc VP |
159 | /* Children of this object. */ |
160 | VEC (varobj_p) *children; | |
8b93c638 | 161 | |
b6313243 TT |
162 | /* Whether the children of this varobj were requested. This field is |
163 | used to decide if dynamic varobj should recompute their children. | |
164 | In the event that the frontend never asked for the children, we | |
165 | can avoid that. */ | |
166 | int children_requested; | |
167 | ||
581e13c1 MS |
168 | /* Description of the root variable. Points to root variable for |
169 | children. */ | |
72330bd6 | 170 | struct varobj_root *root; |
8b93c638 | 171 | |
581e13c1 | 172 | /* The format of the output for this object. */ |
72330bd6 | 173 | enum varobj_display_formats format; |
fb9b6b35 | 174 | |
581e13c1 | 175 | /* Was this variable updated via a varobj_set_value operation. */ |
fb9b6b35 | 176 | int updated; |
85265413 NR |
177 | |
178 | /* Last print value. */ | |
179 | char *print_value; | |
25d5ea92 VP |
180 | |
181 | /* Is this variable frozen. Frozen variables are never implicitly | |
182 | updated by -var-update * | |
183 | or -var-update <direct-or-indirect-parent>. */ | |
184 | int frozen; | |
185 | ||
186 | /* Is the value of this variable intentionally not fetched? It is | |
187 | not fetched if either the variable is frozen, or any parents is | |
188 | frozen. */ | |
189 | int not_fetched; | |
b6313243 | 190 | |
0cc7d26f TT |
191 | /* Sub-range of children which the MI consumer has requested. If |
192 | FROM < 0 or TO < 0, means that all children have been | |
193 | requested. */ | |
194 | int from; | |
195 | int to; | |
196 | ||
197 | /* The pretty-printer constructor. If NULL, then the default | |
198 | pretty-printer will be looked up. If None, then no | |
199 | pretty-printer will be installed. */ | |
200 | PyObject *constructor; | |
201 | ||
b6313243 TT |
202 | /* The pretty-printer that has been constructed. If NULL, then a |
203 | new printer object is needed, and one will be constructed. */ | |
204 | PyObject *pretty_printer; | |
0cc7d26f TT |
205 | |
206 | /* The iterator returned by the printer's 'children' method, or NULL | |
207 | if not available. */ | |
208 | PyObject *child_iter; | |
209 | ||
210 | /* We request one extra item from the iterator, so that we can | |
211 | report to the caller whether there are more items than we have | |
212 | already reported. However, we don't want to install this value | |
213 | when we read it, because that will mess up future updates. So, | |
214 | we stash it here instead. */ | |
215 | PyObject *saved_item; | |
72330bd6 | 216 | }; |
8b93c638 | 217 | |
8b93c638 | 218 | struct cpstack |
72330bd6 AC |
219 | { |
220 | char *name; | |
221 | struct cpstack *next; | |
222 | }; | |
8b93c638 JM |
223 | |
224 | /* A list of varobjs */ | |
225 | ||
226 | struct vlist | |
72330bd6 AC |
227 | { |
228 | struct varobj *var; | |
229 | struct vlist *next; | |
230 | }; | |
8b93c638 JM |
231 | |
232 | /* Private function prototypes */ | |
233 | ||
581e13c1 | 234 | /* Helper functions for the above subcommands. */ |
8b93c638 | 235 | |
a14ed312 | 236 | static int delete_variable (struct cpstack **, struct varobj *, int); |
8b93c638 | 237 | |
a14ed312 KB |
238 | static void delete_variable_1 (struct cpstack **, int *, |
239 | struct varobj *, int, int); | |
8b93c638 | 240 | |
a14ed312 | 241 | static int install_variable (struct varobj *); |
8b93c638 | 242 | |
a14ed312 | 243 | static void uninstall_variable (struct varobj *); |
8b93c638 | 244 | |
a14ed312 | 245 | static struct varobj *create_child (struct varobj *, int, char *); |
8b93c638 | 246 | |
b6313243 TT |
247 | static struct varobj * |
248 | create_child_with_value (struct varobj *parent, int index, const char *name, | |
249 | struct value *value); | |
250 | ||
8b93c638 JM |
251 | /* Utility routines */ |
252 | ||
a14ed312 | 253 | static struct varobj *new_variable (void); |
8b93c638 | 254 | |
a14ed312 | 255 | static struct varobj *new_root_variable (void); |
8b93c638 | 256 | |
a14ed312 | 257 | static void free_variable (struct varobj *var); |
8b93c638 | 258 | |
74b7792f AC |
259 | static struct cleanup *make_cleanup_free_variable (struct varobj *var); |
260 | ||
a14ed312 | 261 | static struct type *get_type (struct varobj *var); |
8b93c638 | 262 | |
6e2a9270 VP |
263 | static struct type *get_value_type (struct varobj *var); |
264 | ||
a14ed312 | 265 | static struct type *get_target_type (struct type *); |
8b93c638 | 266 | |
a14ed312 | 267 | static enum varobj_display_formats variable_default_display (struct varobj *); |
8b93c638 | 268 | |
a14ed312 | 269 | static void cppush (struct cpstack **pstack, char *name); |
8b93c638 | 270 | |
a14ed312 | 271 | static char *cppop (struct cpstack **pstack); |
8b93c638 | 272 | |
acd65feb VP |
273 | static int install_new_value (struct varobj *var, struct value *value, |
274 | int initial); | |
275 | ||
581e13c1 | 276 | /* Language-specific routines. */ |
8b93c638 | 277 | |
a14ed312 | 278 | static enum varobj_languages variable_language (struct varobj *var); |
8b93c638 | 279 | |
a14ed312 | 280 | static int number_of_children (struct varobj *); |
8b93c638 | 281 | |
a14ed312 | 282 | static char *name_of_variable (struct varobj *); |
8b93c638 | 283 | |
a14ed312 | 284 | static char *name_of_child (struct varobj *, int); |
8b93c638 | 285 | |
30b28db1 | 286 | static struct value *value_of_root (struct varobj **var_handle, int *); |
8b93c638 | 287 | |
30b28db1 | 288 | static struct value *value_of_child (struct varobj *parent, int index); |
8b93c638 | 289 | |
de051565 MK |
290 | static char *my_value_of_variable (struct varobj *var, |
291 | enum varobj_display_formats format); | |
8b93c638 | 292 | |
85265413 | 293 | static char *value_get_print_value (struct value *value, |
b6313243 | 294 | enum varobj_display_formats format, |
d452c4bc | 295 | struct varobj *var); |
85265413 | 296 | |
b2c2bd75 VP |
297 | static int varobj_value_is_changeable_p (struct varobj *var); |
298 | ||
299 | static int is_root_p (struct varobj *var); | |
8b93c638 | 300 | |
d8b65138 JK |
301 | #if HAVE_PYTHON |
302 | ||
9a1edae6 PM |
303 | static struct varobj *varobj_add_child (struct varobj *var, |
304 | const char *name, | |
305 | struct value *value); | |
b6313243 | 306 | |
d8b65138 JK |
307 | #endif /* HAVE_PYTHON */ |
308 | ||
d32cafc7 JB |
309 | static int default_value_is_changeable_p (struct varobj *var); |
310 | ||
8b93c638 JM |
311 | /* C implementation */ |
312 | ||
a14ed312 | 313 | static int c_number_of_children (struct varobj *var); |
8b93c638 | 314 | |
a14ed312 | 315 | static char *c_name_of_variable (struct varobj *parent); |
8b93c638 | 316 | |
a14ed312 | 317 | static char *c_name_of_child (struct varobj *parent, int index); |
8b93c638 | 318 | |
02142340 VP |
319 | static char *c_path_expr_of_child (struct varobj *child); |
320 | ||
30b28db1 | 321 | static struct value *c_value_of_root (struct varobj **var_handle); |
8b93c638 | 322 | |
30b28db1 | 323 | static struct value *c_value_of_child (struct varobj *parent, int index); |
8b93c638 | 324 | |
a14ed312 | 325 | static struct type *c_type_of_child (struct varobj *parent, int index); |
8b93c638 | 326 | |
de051565 MK |
327 | static char *c_value_of_variable (struct varobj *var, |
328 | enum varobj_display_formats format); | |
8b93c638 JM |
329 | |
330 | /* C++ implementation */ | |
331 | ||
a14ed312 | 332 | static int cplus_number_of_children (struct varobj *var); |
8b93c638 | 333 | |
a14ed312 | 334 | static void cplus_class_num_children (struct type *type, int children[3]); |
8b93c638 | 335 | |
a14ed312 | 336 | static char *cplus_name_of_variable (struct varobj *parent); |
8b93c638 | 337 | |
a14ed312 | 338 | static char *cplus_name_of_child (struct varobj *parent, int index); |
8b93c638 | 339 | |
02142340 VP |
340 | static char *cplus_path_expr_of_child (struct varobj *child); |
341 | ||
30b28db1 | 342 | static struct value *cplus_value_of_root (struct varobj **var_handle); |
8b93c638 | 343 | |
30b28db1 | 344 | static struct value *cplus_value_of_child (struct varobj *parent, int index); |
8b93c638 | 345 | |
a14ed312 | 346 | static struct type *cplus_type_of_child (struct varobj *parent, int index); |
8b93c638 | 347 | |
de051565 MK |
348 | static char *cplus_value_of_variable (struct varobj *var, |
349 | enum varobj_display_formats format); | |
8b93c638 JM |
350 | |
351 | /* Java implementation */ | |
352 | ||
a14ed312 | 353 | static int java_number_of_children (struct varobj *var); |
8b93c638 | 354 | |
a14ed312 | 355 | static char *java_name_of_variable (struct varobj *parent); |
8b93c638 | 356 | |
a14ed312 | 357 | static char *java_name_of_child (struct varobj *parent, int index); |
8b93c638 | 358 | |
02142340 VP |
359 | static char *java_path_expr_of_child (struct varobj *child); |
360 | ||
30b28db1 | 361 | static struct value *java_value_of_root (struct varobj **var_handle); |
8b93c638 | 362 | |
30b28db1 | 363 | static struct value *java_value_of_child (struct varobj *parent, int index); |
8b93c638 | 364 | |
a14ed312 | 365 | static struct type *java_type_of_child (struct varobj *parent, int index); |
8b93c638 | 366 | |
de051565 MK |
367 | static char *java_value_of_variable (struct varobj *var, |
368 | enum varobj_display_formats format); | |
8b93c638 | 369 | |
40591b7d JCD |
370 | /* Ada implementation */ |
371 | ||
372 | static int ada_number_of_children (struct varobj *var); | |
373 | ||
374 | static char *ada_name_of_variable (struct varobj *parent); | |
375 | ||
376 | static char *ada_name_of_child (struct varobj *parent, int index); | |
377 | ||
378 | static char *ada_path_expr_of_child (struct varobj *child); | |
379 | ||
380 | static struct value *ada_value_of_root (struct varobj **var_handle); | |
381 | ||
382 | static struct value *ada_value_of_child (struct varobj *parent, int index); | |
383 | ||
384 | static struct type *ada_type_of_child (struct varobj *parent, int index); | |
385 | ||
386 | static char *ada_value_of_variable (struct varobj *var, | |
387 | enum varobj_display_formats format); | |
388 | ||
d32cafc7 JB |
389 | static int ada_value_is_changeable_p (struct varobj *var); |
390 | ||
7a290c40 JB |
391 | static int ada_value_has_mutated (struct varobj *var, struct value *new_val, |
392 | struct type *new_type); | |
393 | ||
8b93c638 JM |
394 | /* The language specific vector */ |
395 | ||
396 | struct language_specific | |
72330bd6 | 397 | { |
8b93c638 | 398 | |
581e13c1 | 399 | /* The language of this variable. */ |
72330bd6 | 400 | enum varobj_languages language; |
8b93c638 | 401 | |
581e13c1 | 402 | /* The number of children of PARENT. */ |
72330bd6 | 403 | int (*number_of_children) (struct varobj * parent); |
8b93c638 | 404 | |
581e13c1 | 405 | /* The name (expression) of a root varobj. */ |
72330bd6 | 406 | char *(*name_of_variable) (struct varobj * parent); |
8b93c638 | 407 | |
581e13c1 | 408 | /* The name of the INDEX'th child of PARENT. */ |
72330bd6 | 409 | char *(*name_of_child) (struct varobj * parent, int index); |
8b93c638 | 410 | |
02142340 VP |
411 | /* Returns the rooted expression of CHILD, which is a variable |
412 | obtain that has some parent. */ | |
413 | char *(*path_expr_of_child) (struct varobj * child); | |
414 | ||
581e13c1 | 415 | /* The ``struct value *'' of the root variable ROOT. */ |
30b28db1 | 416 | struct value *(*value_of_root) (struct varobj ** root_handle); |
8b93c638 | 417 | |
581e13c1 | 418 | /* The ``struct value *'' of the INDEX'th child of PARENT. */ |
30b28db1 | 419 | struct value *(*value_of_child) (struct varobj * parent, int index); |
8b93c638 | 420 | |
581e13c1 | 421 | /* The type of the INDEX'th child of PARENT. */ |
72330bd6 | 422 | struct type *(*type_of_child) (struct varobj * parent, int index); |
8b93c638 | 423 | |
581e13c1 | 424 | /* The current value of VAR. */ |
de051565 MK |
425 | char *(*value_of_variable) (struct varobj * var, |
426 | enum varobj_display_formats format); | |
7a290c40 | 427 | |
d32cafc7 JB |
428 | /* Return non-zero if changes in value of VAR must be detected and |
429 | reported by -var-update. Return zero if -var-update should never | |
430 | report changes of such values. This makes sense for structures | |
431 | (since the changes in children values will be reported separately), | |
432 | or for artifical objects (like 'public' pseudo-field in C++). | |
433 | ||
434 | Return value of 0 means that gdb need not call value_fetch_lazy | |
435 | for the value of this variable object. */ | |
436 | int (*value_is_changeable_p) (struct varobj *var); | |
437 | ||
7a290c40 JB |
438 | /* Return nonzero if the type of VAR has mutated. |
439 | ||
440 | VAR's value is still the varobj's previous value, while NEW_VALUE | |
441 | is VAR's new value and NEW_TYPE is the var's new type. NEW_VALUE | |
442 | may be NULL indicating that there is no value available (the varobj | |
443 | may be out of scope, of may be the child of a null pointer, for | |
444 | instance). NEW_TYPE, on the other hand, must never be NULL. | |
445 | ||
446 | This function should also be able to assume that var's number of | |
447 | children is set (not < 0). | |
448 | ||
449 | Languages where types do not mutate can set this to NULL. */ | |
450 | int (*value_has_mutated) (struct varobj *var, struct value *new_value, | |
451 | struct type *new_type); | |
72330bd6 | 452 | }; |
8b93c638 | 453 | |
581e13c1 | 454 | /* Array of known source language routines. */ |
d5d6fca5 | 455 | static struct language_specific languages[vlang_end] = { |
581e13c1 | 456 | /* Unknown (try treating as C). */ |
8b93c638 | 457 | { |
72330bd6 AC |
458 | vlang_unknown, |
459 | c_number_of_children, | |
460 | c_name_of_variable, | |
461 | c_name_of_child, | |
02142340 | 462 | c_path_expr_of_child, |
72330bd6 AC |
463 | c_value_of_root, |
464 | c_value_of_child, | |
465 | c_type_of_child, | |
7a290c40 | 466 | c_value_of_variable, |
d32cafc7 | 467 | default_value_is_changeable_p, |
7a290c40 | 468 | NULL /* value_has_mutated */} |
8b93c638 JM |
469 | , |
470 | /* C */ | |
471 | { | |
72330bd6 AC |
472 | vlang_c, |
473 | c_number_of_children, | |
474 | c_name_of_variable, | |
475 | c_name_of_child, | |
02142340 | 476 | c_path_expr_of_child, |
72330bd6 AC |
477 | c_value_of_root, |
478 | c_value_of_child, | |
479 | c_type_of_child, | |
7a290c40 | 480 | c_value_of_variable, |
d32cafc7 | 481 | default_value_is_changeable_p, |
7a290c40 | 482 | NULL /* value_has_mutated */} |
8b93c638 JM |
483 | , |
484 | /* C++ */ | |
485 | { | |
72330bd6 AC |
486 | vlang_cplus, |
487 | cplus_number_of_children, | |
488 | cplus_name_of_variable, | |
489 | cplus_name_of_child, | |
02142340 | 490 | cplus_path_expr_of_child, |
72330bd6 AC |
491 | cplus_value_of_root, |
492 | cplus_value_of_child, | |
493 | cplus_type_of_child, | |
7a290c40 | 494 | cplus_value_of_variable, |
d32cafc7 | 495 | default_value_is_changeable_p, |
7a290c40 | 496 | NULL /* value_has_mutated */} |
8b93c638 JM |
497 | , |
498 | /* Java */ | |
499 | { | |
72330bd6 AC |
500 | vlang_java, |
501 | java_number_of_children, | |
502 | java_name_of_variable, | |
503 | java_name_of_child, | |
02142340 | 504 | java_path_expr_of_child, |
72330bd6 AC |
505 | java_value_of_root, |
506 | java_value_of_child, | |
507 | java_type_of_child, | |
7a290c40 | 508 | java_value_of_variable, |
d32cafc7 | 509 | default_value_is_changeable_p, |
7a290c40 | 510 | NULL /* value_has_mutated */}, |
40591b7d JCD |
511 | /* Ada */ |
512 | { | |
513 | vlang_ada, | |
514 | ada_number_of_children, | |
515 | ada_name_of_variable, | |
516 | ada_name_of_child, | |
517 | ada_path_expr_of_child, | |
518 | ada_value_of_root, | |
519 | ada_value_of_child, | |
520 | ada_type_of_child, | |
7a290c40 | 521 | ada_value_of_variable, |
d32cafc7 | 522 | ada_value_is_changeable_p, |
7a290c40 | 523 | ada_value_has_mutated} |
8b93c638 JM |
524 | }; |
525 | ||
581e13c1 | 526 | /* A little convenience enum for dealing with C++/Java. */ |
8b93c638 | 527 | enum vsections |
72330bd6 AC |
528 | { |
529 | v_public = 0, v_private, v_protected | |
530 | }; | |
8b93c638 JM |
531 | |
532 | /* Private data */ | |
533 | ||
581e13c1 | 534 | /* Mappings of varobj_display_formats enums to gdb's format codes. */ |
72330bd6 | 535 | static int format_code[] = { 0, 't', 'd', 'x', 'o' }; |
8b93c638 | 536 | |
581e13c1 | 537 | /* Header of the list of root variable objects. */ |
8b93c638 | 538 | static struct varobj_root *rootlist; |
8b93c638 | 539 | |
581e13c1 MS |
540 | /* Prime number indicating the number of buckets in the hash table. */ |
541 | /* A prime large enough to avoid too many colisions. */ | |
8b93c638 JM |
542 | #define VAROBJ_TABLE_SIZE 227 |
543 | ||
581e13c1 | 544 | /* Pointer to the varobj hash table (built at run time). */ |
8b93c638 JM |
545 | static struct vlist **varobj_table; |
546 | ||
581e13c1 | 547 | /* Is the variable X one of our "fake" children? */ |
8b93c638 JM |
548 | #define CPLUS_FAKE_CHILD(x) \ |
549 | ((x) != NULL && (x)->type == NULL && (x)->value == NULL) | |
550 | \f | |
551 | ||
552 | /* API Implementation */ | |
b2c2bd75 VP |
553 | static int |
554 | is_root_p (struct varobj *var) | |
555 | { | |
556 | return (var->root->rootvar == var); | |
557 | } | |
8b93c638 | 558 | |
d452c4bc UW |
559 | #ifdef HAVE_PYTHON |
560 | /* Helper function to install a Python environment suitable for | |
561 | use during operations on VAR. */ | |
70221824 | 562 | static struct cleanup * |
d452c4bc UW |
563 | varobj_ensure_python_env (struct varobj *var) |
564 | { | |
565 | return ensure_python_env (var->root->exp->gdbarch, | |
566 | var->root->exp->language_defn); | |
567 | } | |
568 | #endif | |
569 | ||
581e13c1 | 570 | /* Creates a varobj (not its children). */ |
8b93c638 | 571 | |
7d8547c9 AC |
572 | /* Return the full FRAME which corresponds to the given CORE_ADDR |
573 | or NULL if no FRAME on the chain corresponds to CORE_ADDR. */ | |
574 | ||
575 | static struct frame_info * | |
576 | find_frame_addr_in_frame_chain (CORE_ADDR frame_addr) | |
577 | { | |
578 | struct frame_info *frame = NULL; | |
579 | ||
580 | if (frame_addr == (CORE_ADDR) 0) | |
581 | return NULL; | |
582 | ||
9d49bdc2 PA |
583 | for (frame = get_current_frame (); |
584 | frame != NULL; | |
585 | frame = get_prev_frame (frame)) | |
7d8547c9 | 586 | { |
1fac167a UW |
587 | /* The CORE_ADDR we get as argument was parsed from a string GDB |
588 | output as $fp. This output got truncated to gdbarch_addr_bit. | |
589 | Truncate the frame base address in the same manner before | |
590 | comparing it against our argument. */ | |
591 | CORE_ADDR frame_base = get_frame_base_address (frame); | |
592 | int addr_bit = gdbarch_addr_bit (get_frame_arch (frame)); | |
a109c7c1 | 593 | |
1fac167a UW |
594 | if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT)) |
595 | frame_base &= ((CORE_ADDR) 1 << addr_bit) - 1; | |
596 | ||
597 | if (frame_base == frame_addr) | |
7d8547c9 AC |
598 | return frame; |
599 | } | |
9d49bdc2 PA |
600 | |
601 | return NULL; | |
7d8547c9 AC |
602 | } |
603 | ||
8b93c638 JM |
604 | struct varobj * |
605 | varobj_create (char *objname, | |
72330bd6 | 606 | char *expression, CORE_ADDR frame, enum varobj_type type) |
8b93c638 JM |
607 | { |
608 | struct varobj *var; | |
8b93c638 JM |
609 | struct cleanup *old_chain; |
610 | ||
581e13c1 | 611 | /* Fill out a varobj structure for the (root) variable being constructed. */ |
8b93c638 | 612 | var = new_root_variable (); |
74b7792f | 613 | old_chain = make_cleanup_free_variable (var); |
8b93c638 JM |
614 | |
615 | if (expression != NULL) | |
616 | { | |
e4195b40 | 617 | struct frame_info *fi; |
35633fef | 618 | struct frame_id old_id = null_frame_id; |
e4195b40 | 619 | struct block *block; |
8b93c638 JM |
620 | char *p; |
621 | enum varobj_languages lang; | |
e55dccf0 | 622 | struct value *value = NULL; |
8e7b59a5 | 623 | volatile struct gdb_exception except; |
8b93c638 | 624 | |
9d49bdc2 PA |
625 | /* Parse and evaluate the expression, filling in as much of the |
626 | variable's data as possible. */ | |
627 | ||
628 | if (has_stack_frames ()) | |
629 | { | |
581e13c1 | 630 | /* Allow creator to specify context of variable. */ |
9d49bdc2 PA |
631 | if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME)) |
632 | fi = get_selected_frame (NULL); | |
633 | else | |
634 | /* FIXME: cagney/2002-11-23: This code should be doing a | |
635 | lookup using the frame ID and not just the frame's | |
636 | ``address''. This, of course, means an interface | |
637 | change. However, with out that interface change ISAs, | |
638 | such as the ia64 with its two stacks, won't work. | |
639 | Similar goes for the case where there is a frameless | |
640 | function. */ | |
641 | fi = find_frame_addr_in_frame_chain (frame); | |
642 | } | |
8b93c638 | 643 | else |
9d49bdc2 | 644 | fi = NULL; |
8b93c638 | 645 | |
581e13c1 | 646 | /* frame = -2 means always use selected frame. */ |
73a93a32 | 647 | if (type == USE_SELECTED_FRAME) |
a5defcdc | 648 | var->root->floating = 1; |
73a93a32 | 649 | |
8b93c638 JM |
650 | block = NULL; |
651 | if (fi != NULL) | |
ae767bfb | 652 | block = get_frame_block (fi, 0); |
8b93c638 JM |
653 | |
654 | p = expression; | |
655 | innermost_block = NULL; | |
73a93a32 | 656 | /* Wrap the call to parse expression, so we can |
581e13c1 | 657 | return a sensible error. */ |
8e7b59a5 KS |
658 | TRY_CATCH (except, RETURN_MASK_ERROR) |
659 | { | |
660 | var->root->exp = parse_exp_1 (&p, block, 0); | |
661 | } | |
662 | ||
663 | if (except.reason < 0) | |
73a93a32 | 664 | { |
f748fb40 | 665 | do_cleanups (old_chain); |
73a93a32 JI |
666 | return NULL; |
667 | } | |
8b93c638 | 668 | |
581e13c1 | 669 | /* Don't allow variables to be created for types. */ |
8b93c638 JM |
670 | if (var->root->exp->elts[0].opcode == OP_TYPE) |
671 | { | |
672 | do_cleanups (old_chain); | |
bc8332bb AC |
673 | fprintf_unfiltered (gdb_stderr, "Attempt to use a type name" |
674 | " as an expression.\n"); | |
8b93c638 JM |
675 | return NULL; |
676 | } | |
677 | ||
678 | var->format = variable_default_display (var); | |
679 | var->root->valid_block = innermost_block; | |
1b36a34b | 680 | var->name = xstrdup (expression); |
02142340 | 681 | /* For a root var, the name and the expr are the same. */ |
1b36a34b | 682 | var->path_expr = xstrdup (expression); |
8b93c638 JM |
683 | |
684 | /* When the frame is different from the current frame, | |
685 | we must select the appropriate frame before parsing | |
686 | the expression, otherwise the value will not be current. | |
581e13c1 | 687 | Since select_frame is so benign, just call it for all cases. */ |
4e22772d | 688 | if (innermost_block) |
8b93c638 | 689 | { |
4e22772d JK |
690 | /* User could specify explicit FRAME-ADDR which was not found but |
691 | EXPRESSION is frame specific and we would not be able to evaluate | |
692 | it correctly next time. With VALID_BLOCK set we must also set | |
693 | FRAME and THREAD_ID. */ | |
694 | if (fi == NULL) | |
695 | error (_("Failed to find the specified frame")); | |
696 | ||
7a424e99 | 697 | var->root->frame = get_frame_id (fi); |
c5b48eac | 698 | var->root->thread_id = pid_to_thread_id (inferior_ptid); |
35633fef | 699 | old_id = get_frame_id (get_selected_frame (NULL)); |
c5b48eac | 700 | select_frame (fi); |
8b93c638 JM |
701 | } |
702 | ||
340a7723 | 703 | /* We definitely need to catch errors here. |
8b93c638 | 704 | If evaluate_expression succeeds we got the value we wanted. |
581e13c1 | 705 | But if it fails, we still go on with a call to evaluate_type(). */ |
8e7b59a5 KS |
706 | TRY_CATCH (except, RETURN_MASK_ERROR) |
707 | { | |
708 | value = evaluate_expression (var->root->exp); | |
709 | } | |
710 | ||
711 | if (except.reason < 0) | |
e55dccf0 VP |
712 | { |
713 | /* Error getting the value. Try to at least get the | |
714 | right type. */ | |
715 | struct value *type_only_value = evaluate_type (var->root->exp); | |
a109c7c1 | 716 | |
e55dccf0 VP |
717 | var->type = value_type (type_only_value); |
718 | } | |
719 | else | |
720 | var->type = value_type (value); | |
acd65feb | 721 | |
8b93c638 JM |
722 | /* Set language info */ |
723 | lang = variable_language (var); | |
d5d6fca5 | 724 | var->root->lang = &languages[lang]; |
8b93c638 | 725 | |
d32cafc7 JB |
726 | install_new_value (var, value, 1 /* Initial assignment */); |
727 | ||
581e13c1 | 728 | /* Set ourselves as our root. */ |
8b93c638 JM |
729 | var->root->rootvar = var; |
730 | ||
581e13c1 | 731 | /* Reset the selected frame. */ |
35633fef JK |
732 | if (frame_id_p (old_id)) |
733 | select_frame (frame_find_by_id (old_id)); | |
8b93c638 JM |
734 | } |
735 | ||
73a93a32 | 736 | /* If the variable object name is null, that means this |
581e13c1 | 737 | is a temporary variable, so don't install it. */ |
73a93a32 JI |
738 | |
739 | if ((var != NULL) && (objname != NULL)) | |
8b93c638 | 740 | { |
1b36a34b | 741 | var->obj_name = xstrdup (objname); |
8b93c638 JM |
742 | |
743 | /* If a varobj name is duplicated, the install will fail so | |
581e13c1 | 744 | we must cleanup. */ |
8b93c638 JM |
745 | if (!install_variable (var)) |
746 | { | |
747 | do_cleanups (old_chain); | |
748 | return NULL; | |
749 | } | |
750 | } | |
751 | ||
752 | discard_cleanups (old_chain); | |
753 | return var; | |
754 | } | |
755 | ||
581e13c1 | 756 | /* Generates an unique name that can be used for a varobj. */ |
8b93c638 JM |
757 | |
758 | char * | |
759 | varobj_gen_name (void) | |
760 | { | |
761 | static int id = 0; | |
e64d9b3d | 762 | char *obj_name; |
8b93c638 | 763 | |
581e13c1 | 764 | /* Generate a name for this object. */ |
8b93c638 | 765 | id++; |
b435e160 | 766 | obj_name = xstrprintf ("var%d", id); |
8b93c638 | 767 | |
e64d9b3d | 768 | return obj_name; |
8b93c638 JM |
769 | } |
770 | ||
61d8f275 JK |
771 | /* Given an OBJNAME, returns the pointer to the corresponding varobj. Call |
772 | error if OBJNAME cannot be found. */ | |
8b93c638 JM |
773 | |
774 | struct varobj * | |
775 | varobj_get_handle (char *objname) | |
776 | { | |
777 | struct vlist *cv; | |
778 | const char *chp; | |
779 | unsigned int index = 0; | |
780 | unsigned int i = 1; | |
781 | ||
782 | for (chp = objname; *chp; chp++) | |
783 | { | |
784 | index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE; | |
785 | } | |
786 | ||
787 | cv = *(varobj_table + index); | |
788 | while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0)) | |
789 | cv = cv->next; | |
790 | ||
791 | if (cv == NULL) | |
8a3fe4f8 | 792 | error (_("Variable object not found")); |
8b93c638 JM |
793 | |
794 | return cv->var; | |
795 | } | |
796 | ||
581e13c1 | 797 | /* Given the handle, return the name of the object. */ |
8b93c638 JM |
798 | |
799 | char * | |
800 | varobj_get_objname (struct varobj *var) | |
801 | { | |
802 | return var->obj_name; | |
803 | } | |
804 | ||
581e13c1 | 805 | /* Given the handle, return the expression represented by the object. */ |
8b93c638 JM |
806 | |
807 | char * | |
808 | varobj_get_expression (struct varobj *var) | |
809 | { | |
810 | return name_of_variable (var); | |
811 | } | |
812 | ||
813 | /* Deletes a varobj and all its children if only_children == 0, | |
3e43a32a MS |
814 | otherwise deletes only the children; returns a malloc'ed list of |
815 | all the (malloc'ed) names of the variables that have been deleted | |
581e13c1 | 816 | (NULL terminated). */ |
8b93c638 JM |
817 | |
818 | int | |
819 | varobj_delete (struct varobj *var, char ***dellist, int only_children) | |
820 | { | |
821 | int delcount; | |
822 | int mycount; | |
823 | struct cpstack *result = NULL; | |
824 | char **cp; | |
825 | ||
581e13c1 | 826 | /* Initialize a stack for temporary results. */ |
8b93c638 JM |
827 | cppush (&result, NULL); |
828 | ||
829 | if (only_children) | |
581e13c1 | 830 | /* Delete only the variable children. */ |
8b93c638 JM |
831 | delcount = delete_variable (&result, var, 1 /* only the children */ ); |
832 | else | |
581e13c1 | 833 | /* Delete the variable and all its children. */ |
8b93c638 JM |
834 | delcount = delete_variable (&result, var, 0 /* parent+children */ ); |
835 | ||
581e13c1 | 836 | /* We may have been asked to return a list of what has been deleted. */ |
8b93c638 JM |
837 | if (dellist != NULL) |
838 | { | |
839 | *dellist = xmalloc ((delcount + 1) * sizeof (char *)); | |
840 | ||
841 | cp = *dellist; | |
842 | mycount = delcount; | |
843 | *cp = cppop (&result); | |
844 | while ((*cp != NULL) && (mycount > 0)) | |
845 | { | |
846 | mycount--; | |
847 | cp++; | |
848 | *cp = cppop (&result); | |
849 | } | |
850 | ||
851 | if (mycount || (*cp != NULL)) | |
8a3fe4f8 | 852 | warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"), |
72330bd6 | 853 | mycount); |
8b93c638 JM |
854 | } |
855 | ||
856 | return delcount; | |
857 | } | |
858 | ||
d8b65138 JK |
859 | #if HAVE_PYTHON |
860 | ||
b6313243 TT |
861 | /* Convenience function for varobj_set_visualizer. Instantiate a |
862 | pretty-printer for a given value. */ | |
863 | static PyObject * | |
864 | instantiate_pretty_printer (PyObject *constructor, struct value *value) | |
865 | { | |
b6313243 TT |
866 | PyObject *val_obj = NULL; |
867 | PyObject *printer; | |
b6313243 | 868 | |
b6313243 | 869 | val_obj = value_to_value_object (value); |
b6313243 TT |
870 | if (! val_obj) |
871 | return NULL; | |
872 | ||
873 | printer = PyObject_CallFunctionObjArgs (constructor, val_obj, NULL); | |
874 | Py_DECREF (val_obj); | |
875 | return printer; | |
b6313243 TT |
876 | } |
877 | ||
d8b65138 JK |
878 | #endif |
879 | ||
581e13c1 | 880 | /* Set/Get variable object display format. */ |
8b93c638 JM |
881 | |
882 | enum varobj_display_formats | |
883 | varobj_set_display_format (struct varobj *var, | |
884 | enum varobj_display_formats format) | |
885 | { | |
886 | switch (format) | |
887 | { | |
888 | case FORMAT_NATURAL: | |
889 | case FORMAT_BINARY: | |
890 | case FORMAT_DECIMAL: | |
891 | case FORMAT_HEXADECIMAL: | |
892 | case FORMAT_OCTAL: | |
893 | var->format = format; | |
894 | break; | |
895 | ||
896 | default: | |
897 | var->format = variable_default_display (var); | |
898 | } | |
899 | ||
ae7d22a6 VP |
900 | if (varobj_value_is_changeable_p (var) |
901 | && var->value && !value_lazy (var->value)) | |
902 | { | |
6c761d9c | 903 | xfree (var->print_value); |
d452c4bc | 904 | var->print_value = value_get_print_value (var->value, var->format, var); |
ae7d22a6 VP |
905 | } |
906 | ||
8b93c638 JM |
907 | return var->format; |
908 | } | |
909 | ||
910 | enum varobj_display_formats | |
911 | varobj_get_display_format (struct varobj *var) | |
912 | { | |
913 | return var->format; | |
914 | } | |
915 | ||
b6313243 TT |
916 | char * |
917 | varobj_get_display_hint (struct varobj *var) | |
918 | { | |
919 | char *result = NULL; | |
920 | ||
921 | #if HAVE_PYTHON | |
d452c4bc UW |
922 | struct cleanup *back_to = varobj_ensure_python_env (var); |
923 | ||
b6313243 TT |
924 | if (var->pretty_printer) |
925 | result = gdbpy_get_display_hint (var->pretty_printer); | |
d452c4bc UW |
926 | |
927 | do_cleanups (back_to); | |
b6313243 TT |
928 | #endif |
929 | ||
930 | return result; | |
931 | } | |
932 | ||
0cc7d26f TT |
933 | /* Return true if the varobj has items after TO, false otherwise. */ |
934 | ||
935 | int | |
936 | varobj_has_more (struct varobj *var, int to) | |
937 | { | |
938 | if (VEC_length (varobj_p, var->children) > to) | |
939 | return 1; | |
940 | return ((to == -1 || VEC_length (varobj_p, var->children) == to) | |
941 | && var->saved_item != NULL); | |
942 | } | |
943 | ||
c5b48eac VP |
944 | /* If the variable object is bound to a specific thread, that |
945 | is its evaluation can always be done in context of a frame | |
946 | inside that thread, returns GDB id of the thread -- which | |
581e13c1 | 947 | is always positive. Otherwise, returns -1. */ |
c5b48eac VP |
948 | int |
949 | varobj_get_thread_id (struct varobj *var) | |
950 | { | |
951 | if (var->root->valid_block && var->root->thread_id > 0) | |
952 | return var->root->thread_id; | |
953 | else | |
954 | return -1; | |
955 | } | |
956 | ||
25d5ea92 VP |
957 | void |
958 | varobj_set_frozen (struct varobj *var, int frozen) | |
959 | { | |
960 | /* When a variable is unfrozen, we don't fetch its value. | |
961 | The 'not_fetched' flag remains set, so next -var-update | |
962 | won't complain. | |
963 | ||
964 | We don't fetch the value, because for structures the client | |
965 | should do -var-update anyway. It would be bad to have different | |
966 | client-size logic for structure and other types. */ | |
967 | var->frozen = frozen; | |
968 | } | |
969 | ||
970 | int | |
971 | varobj_get_frozen (struct varobj *var) | |
972 | { | |
973 | return var->frozen; | |
974 | } | |
975 | ||
0cc7d26f TT |
976 | /* A helper function that restricts a range to what is actually |
977 | available in a VEC. This follows the usual rules for the meaning | |
978 | of FROM and TO -- if either is negative, the entire range is | |
979 | used. */ | |
980 | ||
981 | static void | |
982 | restrict_range (VEC (varobj_p) *children, int *from, int *to) | |
983 | { | |
984 | if (*from < 0 || *to < 0) | |
985 | { | |
986 | *from = 0; | |
987 | *to = VEC_length (varobj_p, children); | |
988 | } | |
989 | else | |
990 | { | |
991 | if (*from > VEC_length (varobj_p, children)) | |
992 | *from = VEC_length (varobj_p, children); | |
993 | if (*to > VEC_length (varobj_p, children)) | |
994 | *to = VEC_length (varobj_p, children); | |
995 | if (*from > *to) | |
996 | *from = *to; | |
997 | } | |
998 | } | |
999 | ||
d8b65138 JK |
1000 | #if HAVE_PYTHON |
1001 | ||
0cc7d26f TT |
1002 | /* A helper for update_dynamic_varobj_children that installs a new |
1003 | child when needed. */ | |
1004 | ||
1005 | static void | |
1006 | install_dynamic_child (struct varobj *var, | |
1007 | VEC (varobj_p) **changed, | |
1008 | VEC (varobj_p) **new, | |
1009 | VEC (varobj_p) **unchanged, | |
1010 | int *cchanged, | |
1011 | int index, | |
1012 | const char *name, | |
1013 | struct value *value) | |
1014 | { | |
1015 | if (VEC_length (varobj_p, var->children) < index + 1) | |
1016 | { | |
1017 | /* There's no child yet. */ | |
1018 | struct varobj *child = varobj_add_child (var, name, value); | |
a109c7c1 | 1019 | |
0cc7d26f TT |
1020 | if (new) |
1021 | { | |
1022 | VEC_safe_push (varobj_p, *new, child); | |
1023 | *cchanged = 1; | |
1024 | } | |
1025 | } | |
1026 | else | |
1027 | { | |
1028 | varobj_p existing = VEC_index (varobj_p, var->children, index); | |
a109c7c1 | 1029 | |
0cc7d26f TT |
1030 | if (install_new_value (existing, value, 0)) |
1031 | { | |
1032 | if (changed) | |
1033 | VEC_safe_push (varobj_p, *changed, existing); | |
1034 | } | |
1035 | else if (unchanged) | |
1036 | VEC_safe_push (varobj_p, *unchanged, existing); | |
1037 | } | |
1038 | } | |
1039 | ||
0cc7d26f TT |
1040 | static int |
1041 | dynamic_varobj_has_child_method (struct varobj *var) | |
1042 | { | |
1043 | struct cleanup *back_to; | |
1044 | PyObject *printer = var->pretty_printer; | |
1045 | int result; | |
1046 | ||
1047 | back_to = varobj_ensure_python_env (var); | |
1048 | result = PyObject_HasAttr (printer, gdbpy_children_cst); | |
1049 | do_cleanups (back_to); | |
1050 | return result; | |
1051 | } | |
1052 | ||
1053 | #endif | |
1054 | ||
b6313243 TT |
1055 | static int |
1056 | update_dynamic_varobj_children (struct varobj *var, | |
1057 | VEC (varobj_p) **changed, | |
0cc7d26f TT |
1058 | VEC (varobj_p) **new, |
1059 | VEC (varobj_p) **unchanged, | |
1060 | int *cchanged, | |
1061 | int update_children, | |
1062 | int from, | |
1063 | int to) | |
b6313243 TT |
1064 | { |
1065 | #if HAVE_PYTHON | |
b6313243 TT |
1066 | struct cleanup *back_to; |
1067 | PyObject *children; | |
b6313243 | 1068 | int i; |
b6313243 | 1069 | PyObject *printer = var->pretty_printer; |
b6313243 | 1070 | |
d452c4bc | 1071 | back_to = varobj_ensure_python_env (var); |
b6313243 TT |
1072 | |
1073 | *cchanged = 0; | |
1074 | if (!PyObject_HasAttr (printer, gdbpy_children_cst)) | |
1075 | { | |
1076 | do_cleanups (back_to); | |
1077 | return 0; | |
1078 | } | |
1079 | ||
0cc7d26f | 1080 | if (update_children || !var->child_iter) |
b6313243 | 1081 | { |
0cc7d26f TT |
1082 | children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst, |
1083 | NULL); | |
b6313243 | 1084 | |
0cc7d26f TT |
1085 | if (!children) |
1086 | { | |
1087 | gdbpy_print_stack (); | |
1088 | error (_("Null value returned for children")); | |
1089 | } | |
b6313243 | 1090 | |
0cc7d26f | 1091 | make_cleanup_py_decref (children); |
b6313243 | 1092 | |
0cc7d26f TT |
1093 | if (!PyIter_Check (children)) |
1094 | error (_("Returned value is not iterable")); | |
1095 | ||
1096 | Py_XDECREF (var->child_iter); | |
1097 | var->child_iter = PyObject_GetIter (children); | |
1098 | if (!var->child_iter) | |
1099 | { | |
1100 | gdbpy_print_stack (); | |
1101 | error (_("Could not get children iterator")); | |
1102 | } | |
1103 | ||
1104 | Py_XDECREF (var->saved_item); | |
1105 | var->saved_item = NULL; | |
1106 | ||
1107 | i = 0; | |
b6313243 | 1108 | } |
0cc7d26f TT |
1109 | else |
1110 | i = VEC_length (varobj_p, var->children); | |
b6313243 | 1111 | |
0cc7d26f TT |
1112 | /* We ask for one extra child, so that MI can report whether there |
1113 | are more children. */ | |
1114 | for (; to < 0 || i < to + 1; ++i) | |
b6313243 | 1115 | { |
0cc7d26f | 1116 | PyObject *item; |
a4c8e806 | 1117 | int force_done = 0; |
b6313243 | 1118 | |
0cc7d26f TT |
1119 | /* See if there was a leftover from last time. */ |
1120 | if (var->saved_item) | |
1121 | { | |
1122 | item = var->saved_item; | |
1123 | var->saved_item = NULL; | |
1124 | } | |
1125 | else | |
1126 | item = PyIter_Next (var->child_iter); | |
b6313243 | 1127 | |
0cc7d26f | 1128 | if (!item) |
a4c8e806 TT |
1129 | { |
1130 | /* Normal end of iteration. */ | |
1131 | if (!PyErr_Occurred ()) | |
1132 | break; | |
1133 | ||
1134 | /* If we got a memory error, just use the text as the | |
1135 | item. */ | |
1136 | if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error)) | |
1137 | { | |
1138 | PyObject *type, *value, *trace; | |
1139 | char *name_str, *value_str; | |
1140 | ||
1141 | PyErr_Fetch (&type, &value, &trace); | |
1142 | value_str = gdbpy_exception_to_string (type, value); | |
1143 | Py_XDECREF (type); | |
1144 | Py_XDECREF (value); | |
1145 | Py_XDECREF (trace); | |
1146 | if (!value_str) | |
1147 | { | |
1148 | gdbpy_print_stack (); | |
1149 | break; | |
1150 | } | |
1151 | ||
1152 | name_str = xstrprintf ("<error at %d>", i); | |
1153 | item = Py_BuildValue ("(ss)", name_str, value_str); | |
1154 | xfree (name_str); | |
1155 | xfree (value_str); | |
1156 | if (!item) | |
1157 | { | |
1158 | gdbpy_print_stack (); | |
1159 | break; | |
1160 | } | |
1161 | ||
1162 | force_done = 1; | |
1163 | } | |
1164 | else | |
1165 | { | |
1166 | /* Any other kind of error. */ | |
1167 | gdbpy_print_stack (); | |
1168 | break; | |
1169 | } | |
1170 | } | |
b6313243 | 1171 | |
0cc7d26f TT |
1172 | /* We don't want to push the extra child on any report list. */ |
1173 | if (to < 0 || i < to) | |
b6313243 | 1174 | { |
0cc7d26f | 1175 | PyObject *py_v; |
ddd49eee | 1176 | const char *name; |
0cc7d26f TT |
1177 | struct value *v; |
1178 | struct cleanup *inner; | |
1179 | int can_mention = from < 0 || i >= from; | |
1180 | ||
1181 | inner = make_cleanup_py_decref (item); | |
1182 | ||
1183 | if (!PyArg_ParseTuple (item, "sO", &name, &py_v)) | |
a4c8e806 TT |
1184 | { |
1185 | gdbpy_print_stack (); | |
1186 | error (_("Invalid item from the child list")); | |
1187 | } | |
0cc7d26f TT |
1188 | |
1189 | v = convert_value_from_python (py_v); | |
8dc78533 JK |
1190 | if (v == NULL) |
1191 | gdbpy_print_stack (); | |
0cc7d26f TT |
1192 | install_dynamic_child (var, can_mention ? changed : NULL, |
1193 | can_mention ? new : NULL, | |
1194 | can_mention ? unchanged : NULL, | |
1195 | can_mention ? cchanged : NULL, i, name, v); | |
1196 | do_cleanups (inner); | |
b6313243 | 1197 | } |
0cc7d26f | 1198 | else |
b6313243 | 1199 | { |
0cc7d26f TT |
1200 | Py_XDECREF (var->saved_item); |
1201 | var->saved_item = item; | |
b6313243 | 1202 | |
0cc7d26f TT |
1203 | /* We want to truncate the child list just before this |
1204 | element. */ | |
1205 | break; | |
1206 | } | |
a4c8e806 TT |
1207 | |
1208 | if (force_done) | |
1209 | break; | |
b6313243 TT |
1210 | } |
1211 | ||
1212 | if (i < VEC_length (varobj_p, var->children)) | |
1213 | { | |
0cc7d26f | 1214 | int j; |
a109c7c1 | 1215 | |
0cc7d26f TT |
1216 | *cchanged = 1; |
1217 | for (j = i; j < VEC_length (varobj_p, var->children); ++j) | |
1218 | varobj_delete (VEC_index (varobj_p, var->children, j), NULL, 0); | |
1219 | VEC_truncate (varobj_p, var->children, i); | |
b6313243 | 1220 | } |
0cc7d26f TT |
1221 | |
1222 | /* If there are fewer children than requested, note that the list of | |
1223 | children changed. */ | |
1224 | if (to >= 0 && VEC_length (varobj_p, var->children) < to) | |
1225 | *cchanged = 1; | |
1226 | ||
b6313243 TT |
1227 | var->num_children = VEC_length (varobj_p, var->children); |
1228 | ||
1229 | do_cleanups (back_to); | |
1230 | ||
b6313243 TT |
1231 | return 1; |
1232 | #else | |
1233 | gdb_assert (0 && "should never be called if Python is not enabled"); | |
1234 | #endif | |
1235 | } | |
25d5ea92 | 1236 | |
8b93c638 JM |
1237 | int |
1238 | varobj_get_num_children (struct varobj *var) | |
1239 | { | |
1240 | if (var->num_children == -1) | |
b6313243 | 1241 | { |
0cc7d26f TT |
1242 | if (var->pretty_printer) |
1243 | { | |
1244 | int dummy; | |
1245 | ||
1246 | /* If we have a dynamic varobj, don't report -1 children. | |
1247 | So, try to fetch some children first. */ | |
1248 | update_dynamic_varobj_children (var, NULL, NULL, NULL, &dummy, | |
1249 | 0, 0, 0); | |
1250 | } | |
1251 | else | |
b6313243 TT |
1252 | var->num_children = number_of_children (var); |
1253 | } | |
8b93c638 | 1254 | |
0cc7d26f | 1255 | return var->num_children >= 0 ? var->num_children : 0; |
8b93c638 JM |
1256 | } |
1257 | ||
1258 | /* Creates a list of the immediate children of a variable object; | |
581e13c1 | 1259 | the return code is the number of such children or -1 on error. */ |
8b93c638 | 1260 | |
d56d46f5 | 1261 | VEC (varobj_p)* |
0cc7d26f | 1262 | varobj_list_children (struct varobj *var, int *from, int *to) |
8b93c638 | 1263 | { |
8b93c638 | 1264 | char *name; |
b6313243 TT |
1265 | int i, children_changed; |
1266 | ||
1267 | var->children_requested = 1; | |
1268 | ||
0cc7d26f TT |
1269 | if (var->pretty_printer) |
1270 | { | |
b6313243 TT |
1271 | /* This, in theory, can result in the number of children changing without |
1272 | frontend noticing. But well, calling -var-list-children on the same | |
1273 | varobj twice is not something a sane frontend would do. */ | |
0cc7d26f TT |
1274 | update_dynamic_varobj_children (var, NULL, NULL, NULL, &children_changed, |
1275 | 0, 0, *to); | |
1276 | restrict_range (var->children, from, to); | |
1277 | return var->children; | |
1278 | } | |
8b93c638 | 1279 | |
8b93c638 JM |
1280 | if (var->num_children == -1) |
1281 | var->num_children = number_of_children (var); | |
1282 | ||
74a44383 DJ |
1283 | /* If that failed, give up. */ |
1284 | if (var->num_children == -1) | |
d56d46f5 | 1285 | return var->children; |
74a44383 | 1286 | |
28335dcc VP |
1287 | /* If we're called when the list of children is not yet initialized, |
1288 | allocate enough elements in it. */ | |
1289 | while (VEC_length (varobj_p, var->children) < var->num_children) | |
1290 | VEC_safe_push (varobj_p, var->children, NULL); | |
1291 | ||
8b93c638 JM |
1292 | for (i = 0; i < var->num_children; i++) |
1293 | { | |
d56d46f5 | 1294 | varobj_p existing = VEC_index (varobj_p, var->children, i); |
28335dcc VP |
1295 | |
1296 | if (existing == NULL) | |
1297 | { | |
1298 | /* Either it's the first call to varobj_list_children for | |
1299 | this variable object, and the child was never created, | |
1300 | or it was explicitly deleted by the client. */ | |
1301 | name = name_of_child (var, i); | |
1302 | existing = create_child (var, i, name); | |
1303 | VEC_replace (varobj_p, var->children, i, existing); | |
1304 | } | |
8b93c638 JM |
1305 | } |
1306 | ||
0cc7d26f | 1307 | restrict_range (var->children, from, to); |
d56d46f5 | 1308 | return var->children; |
8b93c638 JM |
1309 | } |
1310 | ||
d8b65138 JK |
1311 | #if HAVE_PYTHON |
1312 | ||
b6313243 TT |
1313 | static struct varobj * |
1314 | varobj_add_child (struct varobj *var, const char *name, struct value *value) | |
1315 | { | |
1316 | varobj_p v = create_child_with_value (var, | |
1317 | VEC_length (varobj_p, var->children), | |
1318 | name, value); | |
a109c7c1 | 1319 | |
b6313243 | 1320 | VEC_safe_push (varobj_p, var->children, v); |
b6313243 TT |
1321 | return v; |
1322 | } | |
1323 | ||
d8b65138 JK |
1324 | #endif /* HAVE_PYTHON */ |
1325 | ||
8b93c638 | 1326 | /* Obtain the type of an object Variable as a string similar to the one gdb |
581e13c1 | 1327 | prints on the console. */ |
8b93c638 JM |
1328 | |
1329 | char * | |
1330 | varobj_get_type (struct varobj *var) | |
1331 | { | |
581e13c1 | 1332 | /* For the "fake" variables, do not return a type. (It's type is |
8756216b DP |
1333 | NULL, too.) |
1334 | Do not return a type for invalid variables as well. */ | |
1335 | if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid) | |
8b93c638 JM |
1336 | return NULL; |
1337 | ||
1a4300e9 | 1338 | return type_to_string (var->type); |
8b93c638 JM |
1339 | } |
1340 | ||
1ecb4ee0 DJ |
1341 | /* Obtain the type of an object variable. */ |
1342 | ||
1343 | struct type * | |
1344 | varobj_get_gdb_type (struct varobj *var) | |
1345 | { | |
1346 | return var->type; | |
1347 | } | |
1348 | ||
85254831 KS |
1349 | /* Is VAR a path expression parent, i.e., can it be used to construct |
1350 | a valid path expression? */ | |
1351 | ||
1352 | static int | |
1353 | is_path_expr_parent (struct varobj *var) | |
1354 | { | |
1355 | struct type *type; | |
1356 | ||
1357 | /* "Fake" children are not path_expr parents. */ | |
1358 | if (CPLUS_FAKE_CHILD (var)) | |
1359 | return 0; | |
1360 | ||
1361 | type = get_value_type (var); | |
1362 | ||
1363 | /* Anonymous unions and structs are also not path_expr parents. */ | |
1364 | return !((TYPE_CODE (type) == TYPE_CODE_STRUCT | |
1365 | || TYPE_CODE (type) == TYPE_CODE_UNION) | |
1366 | && TYPE_NAME (type) == NULL); | |
1367 | } | |
1368 | ||
1369 | /* Return the path expression parent for VAR. */ | |
1370 | ||
1371 | static struct varobj * | |
1372 | get_path_expr_parent (struct varobj *var) | |
1373 | { | |
1374 | struct varobj *parent = var; | |
1375 | ||
1376 | while (!is_root_p (parent) && !is_path_expr_parent (parent)) | |
1377 | parent = parent->parent; | |
1378 | ||
1379 | return parent; | |
1380 | } | |
1381 | ||
02142340 VP |
1382 | /* Return a pointer to the full rooted expression of varobj VAR. |
1383 | If it has not been computed yet, compute it. */ | |
1384 | char * | |
1385 | varobj_get_path_expr (struct varobj *var) | |
1386 | { | |
1387 | if (var->path_expr != NULL) | |
1388 | return var->path_expr; | |
1389 | else | |
1390 | { | |
1391 | /* For root varobjs, we initialize path_expr | |
1392 | when creating varobj, so here it should be | |
1393 | child varobj. */ | |
1394 | gdb_assert (!is_root_p (var)); | |
1395 | return (*var->root->lang->path_expr_of_child) (var); | |
1396 | } | |
1397 | } | |
1398 | ||
8b93c638 JM |
1399 | enum varobj_languages |
1400 | varobj_get_language (struct varobj *var) | |
1401 | { | |
1402 | return variable_language (var); | |
1403 | } | |
1404 | ||
1405 | int | |
1406 | varobj_get_attributes (struct varobj *var) | |
1407 | { | |
1408 | int attributes = 0; | |
1409 | ||
340a7723 | 1410 | if (varobj_editable_p (var)) |
581e13c1 | 1411 | /* FIXME: define masks for attributes. */ |
8b93c638 JM |
1412 | attributes |= 0x00000001; /* Editable */ |
1413 | ||
1414 | return attributes; | |
1415 | } | |
1416 | ||
0cc7d26f TT |
1417 | int |
1418 | varobj_pretty_printed_p (struct varobj *var) | |
1419 | { | |
1420 | return var->pretty_printer != NULL; | |
1421 | } | |
1422 | ||
de051565 MK |
1423 | char * |
1424 | varobj_get_formatted_value (struct varobj *var, | |
1425 | enum varobj_display_formats format) | |
1426 | { | |
1427 | return my_value_of_variable (var, format); | |
1428 | } | |
1429 | ||
8b93c638 JM |
1430 | char * |
1431 | varobj_get_value (struct varobj *var) | |
1432 | { | |
de051565 | 1433 | return my_value_of_variable (var, var->format); |
8b93c638 JM |
1434 | } |
1435 | ||
1436 | /* Set the value of an object variable (if it is editable) to the | |
581e13c1 MS |
1437 | value of the given expression. */ |
1438 | /* Note: Invokes functions that can call error(). */ | |
8b93c638 JM |
1439 | |
1440 | int | |
1441 | varobj_set_value (struct varobj *var, char *expression) | |
1442 | { | |
34365054 | 1443 | struct value *val = NULL; /* Initialize to keep gcc happy. */ |
8b93c638 | 1444 | /* The argument "expression" contains the variable's new value. |
581e13c1 MS |
1445 | We need to first construct a legal expression for this -- ugh! */ |
1446 | /* Does this cover all the bases? */ | |
8b93c638 | 1447 | struct expression *exp; |
34365054 | 1448 | struct value *value = NULL; /* Initialize to keep gcc happy. */ |
8b93c638 | 1449 | int saved_input_radix = input_radix; |
340a7723 | 1450 | char *s = expression; |
8e7b59a5 | 1451 | volatile struct gdb_exception except; |
8b93c638 | 1452 | |
340a7723 | 1453 | gdb_assert (varobj_editable_p (var)); |
8b93c638 | 1454 | |
581e13c1 | 1455 | input_radix = 10; /* ALWAYS reset to decimal temporarily. */ |
340a7723 | 1456 | exp = parse_exp_1 (&s, 0, 0); |
8e7b59a5 KS |
1457 | TRY_CATCH (except, RETURN_MASK_ERROR) |
1458 | { | |
1459 | value = evaluate_expression (exp); | |
1460 | } | |
1461 | ||
1462 | if (except.reason < 0) | |
340a7723 | 1463 | { |
581e13c1 | 1464 | /* We cannot proceed without a valid expression. */ |
340a7723 NR |
1465 | xfree (exp); |
1466 | return 0; | |
8b93c638 JM |
1467 | } |
1468 | ||
340a7723 NR |
1469 | /* All types that are editable must also be changeable. */ |
1470 | gdb_assert (varobj_value_is_changeable_p (var)); | |
1471 | ||
1472 | /* The value of a changeable variable object must not be lazy. */ | |
1473 | gdb_assert (!value_lazy (var->value)); | |
1474 | ||
1475 | /* Need to coerce the input. We want to check if the | |
1476 | value of the variable object will be different | |
1477 | after assignment, and the first thing value_assign | |
1478 | does is coerce the input. | |
1479 | For example, if we are assigning an array to a pointer variable we | |
b021a221 | 1480 | should compare the pointer with the array's address, not with the |
340a7723 NR |
1481 | array's content. */ |
1482 | value = coerce_array (value); | |
1483 | ||
8e7b59a5 KS |
1484 | /* The new value may be lazy. value_assign, or |
1485 | rather value_contents, will take care of this. */ | |
1486 | TRY_CATCH (except, RETURN_MASK_ERROR) | |
1487 | { | |
1488 | val = value_assign (var->value, value); | |
1489 | } | |
1490 | ||
1491 | if (except.reason < 0) | |
340a7723 | 1492 | return 0; |
8e7b59a5 | 1493 | |
340a7723 NR |
1494 | /* If the value has changed, record it, so that next -var-update can |
1495 | report this change. If a variable had a value of '1', we've set it | |
1496 | to '333' and then set again to '1', when -var-update will report this | |
1497 | variable as changed -- because the first assignment has set the | |
1498 | 'updated' flag. There's no need to optimize that, because return value | |
1499 | of -var-update should be considered an approximation. */ | |
581e13c1 | 1500 | var->updated = install_new_value (var, val, 0 /* Compare values. */); |
340a7723 NR |
1501 | input_radix = saved_input_radix; |
1502 | return 1; | |
8b93c638 JM |
1503 | } |
1504 | ||
0cc7d26f TT |
1505 | #if HAVE_PYTHON |
1506 | ||
1507 | /* A helper function to install a constructor function and visualizer | |
1508 | in a varobj. */ | |
1509 | ||
1510 | static void | |
1511 | install_visualizer (struct varobj *var, PyObject *constructor, | |
1512 | PyObject *visualizer) | |
1513 | { | |
1514 | Py_XDECREF (var->constructor); | |
1515 | var->constructor = constructor; | |
1516 | ||
1517 | Py_XDECREF (var->pretty_printer); | |
1518 | var->pretty_printer = visualizer; | |
1519 | ||
1520 | Py_XDECREF (var->child_iter); | |
1521 | var->child_iter = NULL; | |
1522 | } | |
1523 | ||
1524 | /* Install the default visualizer for VAR. */ | |
1525 | ||
1526 | static void | |
1527 | install_default_visualizer (struct varobj *var) | |
1528 | { | |
d65aec65 PM |
1529 | /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */ |
1530 | if (CPLUS_FAKE_CHILD (var)) | |
1531 | return; | |
1532 | ||
0cc7d26f TT |
1533 | if (pretty_printing) |
1534 | { | |
1535 | PyObject *pretty_printer = NULL; | |
1536 | ||
1537 | if (var->value) | |
1538 | { | |
1539 | pretty_printer = gdbpy_get_varobj_pretty_printer (var->value); | |
1540 | if (! pretty_printer) | |
1541 | { | |
1542 | gdbpy_print_stack (); | |
1543 | error (_("Cannot instantiate printer for default visualizer")); | |
1544 | } | |
1545 | } | |
1546 | ||
1547 | if (pretty_printer == Py_None) | |
1548 | { | |
1549 | Py_DECREF (pretty_printer); | |
1550 | pretty_printer = NULL; | |
1551 | } | |
1552 | ||
1553 | install_visualizer (var, NULL, pretty_printer); | |
1554 | } | |
1555 | } | |
1556 | ||
1557 | /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to | |
1558 | make a new object. */ | |
1559 | ||
1560 | static void | |
1561 | construct_visualizer (struct varobj *var, PyObject *constructor) | |
1562 | { | |
1563 | PyObject *pretty_printer; | |
1564 | ||
d65aec65 PM |
1565 | /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */ |
1566 | if (CPLUS_FAKE_CHILD (var)) | |
1567 | return; | |
1568 | ||
0cc7d26f TT |
1569 | Py_INCREF (constructor); |
1570 | if (constructor == Py_None) | |
1571 | pretty_printer = NULL; | |
1572 | else | |
1573 | { | |
1574 | pretty_printer = instantiate_pretty_printer (constructor, var->value); | |
1575 | if (! pretty_printer) | |
1576 | { | |
1577 | gdbpy_print_stack (); | |
1578 | Py_DECREF (constructor); | |
1579 | constructor = Py_None; | |
1580 | Py_INCREF (constructor); | |
1581 | } | |
1582 | ||
1583 | if (pretty_printer == Py_None) | |
1584 | { | |
1585 | Py_DECREF (pretty_printer); | |
1586 | pretty_printer = NULL; | |
1587 | } | |
1588 | } | |
1589 | ||
1590 | install_visualizer (var, constructor, pretty_printer); | |
1591 | } | |
1592 | ||
1593 | #endif /* HAVE_PYTHON */ | |
1594 | ||
1595 | /* A helper function for install_new_value. This creates and installs | |
1596 | a visualizer for VAR, if appropriate. */ | |
1597 | ||
1598 | static void | |
1599 | install_new_value_visualizer (struct varobj *var) | |
1600 | { | |
1601 | #if HAVE_PYTHON | |
1602 | /* If the constructor is None, then we want the raw value. If VAR | |
1603 | does not have a value, just skip this. */ | |
1604 | if (var->constructor != Py_None && var->value) | |
1605 | { | |
1606 | struct cleanup *cleanup; | |
0cc7d26f TT |
1607 | |
1608 | cleanup = varobj_ensure_python_env (var); | |
1609 | ||
1610 | if (!var->constructor) | |
1611 | install_default_visualizer (var); | |
1612 | else | |
1613 | construct_visualizer (var, var->constructor); | |
1614 | ||
1615 | do_cleanups (cleanup); | |
1616 | } | |
1617 | #else | |
1618 | /* Do nothing. */ | |
1619 | #endif | |
1620 | } | |
1621 | ||
acd65feb VP |
1622 | /* Assign a new value to a variable object. If INITIAL is non-zero, |
1623 | this is the first assignement after the variable object was just | |
1624 | created, or changed type. In that case, just assign the value | |
1625 | and return 0. | |
581e13c1 MS |
1626 | Otherwise, assign the new value, and return 1 if the value is |
1627 | different from the current one, 0 otherwise. The comparison is | |
1628 | done on textual representation of value. Therefore, some types | |
1629 | need not be compared. E.g. for structures the reported value is | |
1630 | always "{...}", so no comparison is necessary here. If the old | |
1631 | value was NULL and new one is not, or vice versa, we always return 1. | |
b26ed50d VP |
1632 | |
1633 | The VALUE parameter should not be released -- the function will | |
1634 | take care of releasing it when needed. */ | |
acd65feb VP |
1635 | static int |
1636 | install_new_value (struct varobj *var, struct value *value, int initial) | |
1637 | { | |
1638 | int changeable; | |
1639 | int need_to_fetch; | |
1640 | int changed = 0; | |
25d5ea92 | 1641 | int intentionally_not_fetched = 0; |
7a4d50bf | 1642 | char *print_value = NULL; |
acd65feb | 1643 | |
acd65feb | 1644 | /* We need to know the varobj's type to decide if the value should |
3e43a32a | 1645 | be fetched or not. C++ fake children (public/protected/private) |
581e13c1 | 1646 | don't have a type. */ |
acd65feb | 1647 | gdb_assert (var->type || CPLUS_FAKE_CHILD (var)); |
b2c2bd75 | 1648 | changeable = varobj_value_is_changeable_p (var); |
b6313243 TT |
1649 | |
1650 | /* If the type has custom visualizer, we consider it to be always | |
581e13c1 | 1651 | changeable. FIXME: need to make sure this behaviour will not |
b6313243 TT |
1652 | mess up read-sensitive values. */ |
1653 | if (var->pretty_printer) | |
1654 | changeable = 1; | |
1655 | ||
acd65feb VP |
1656 | need_to_fetch = changeable; |
1657 | ||
b26ed50d VP |
1658 | /* We are not interested in the address of references, and given |
1659 | that in C++ a reference is not rebindable, it cannot | |
1660 | meaningfully change. So, get hold of the real value. */ | |
1661 | if (value) | |
0cc7d26f | 1662 | value = coerce_ref (value); |
b26ed50d | 1663 | |
acd65feb VP |
1664 | if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION) |
1665 | /* For unions, we need to fetch the value implicitly because | |
1666 | of implementation of union member fetch. When gdb | |
1667 | creates a value for a field and the value of the enclosing | |
1668 | structure is not lazy, it immediately copies the necessary | |
1669 | bytes from the enclosing values. If the enclosing value is | |
1670 | lazy, the call to value_fetch_lazy on the field will read | |
1671 | the data from memory. For unions, that means we'll read the | |
1672 | same memory more than once, which is not desirable. So | |
1673 | fetch now. */ | |
1674 | need_to_fetch = 1; | |
1675 | ||
1676 | /* The new value might be lazy. If the type is changeable, | |
1677 | that is we'll be comparing values of this type, fetch the | |
1678 | value now. Otherwise, on the next update the old value | |
1679 | will be lazy, which means we've lost that old value. */ | |
1680 | if (need_to_fetch && value && value_lazy (value)) | |
1681 | { | |
25d5ea92 VP |
1682 | struct varobj *parent = var->parent; |
1683 | int frozen = var->frozen; | |
a109c7c1 | 1684 | |
25d5ea92 VP |
1685 | for (; !frozen && parent; parent = parent->parent) |
1686 | frozen |= parent->frozen; | |
1687 | ||
1688 | if (frozen && initial) | |
1689 | { | |
1690 | /* For variables that are frozen, or are children of frozen | |
1691 | variables, we don't do fetch on initial assignment. | |
1692 | For non-initial assignemnt we do the fetch, since it means we're | |
1693 | explicitly asked to compare the new value with the old one. */ | |
1694 | intentionally_not_fetched = 1; | |
1695 | } | |
8e7b59a5 | 1696 | else |
acd65feb | 1697 | { |
8e7b59a5 KS |
1698 | volatile struct gdb_exception except; |
1699 | ||
1700 | TRY_CATCH (except, RETURN_MASK_ERROR) | |
1701 | { | |
1702 | value_fetch_lazy (value); | |
1703 | } | |
1704 | ||
1705 | if (except.reason < 0) | |
1706 | { | |
1707 | /* Set the value to NULL, so that for the next -var-update, | |
1708 | we don't try to compare the new value with this value, | |
1709 | that we couldn't even read. */ | |
1710 | value = NULL; | |
1711 | } | |
acd65feb | 1712 | } |
acd65feb VP |
1713 | } |
1714 | ||
e848a8a5 TT |
1715 | /* Get a reference now, before possibly passing it to any Python |
1716 | code that might release it. */ | |
1717 | if (value != NULL) | |
1718 | value_incref (value); | |
b6313243 | 1719 | |
7a4d50bf VP |
1720 | /* Below, we'll be comparing string rendering of old and new |
1721 | values. Don't get string rendering if the value is | |
1722 | lazy -- if it is, the code above has decided that the value | |
1723 | should not be fetched. */ | |
0cc7d26f | 1724 | if (value && !value_lazy (value) && !var->pretty_printer) |
d452c4bc | 1725 | print_value = value_get_print_value (value, var->format, var); |
7a4d50bf | 1726 | |
acd65feb VP |
1727 | /* If the type is changeable, compare the old and the new values. |
1728 | If this is the initial assignment, we don't have any old value | |
1729 | to compare with. */ | |
7a4d50bf | 1730 | if (!initial && changeable) |
acd65feb | 1731 | { |
3e43a32a MS |
1732 | /* If the value of the varobj was changed by -var-set-value, |
1733 | then the value in the varobj and in the target is the same. | |
1734 | However, that value is different from the value that the | |
581e13c1 | 1735 | varobj had after the previous -var-update. So need to the |
3e43a32a | 1736 | varobj as changed. */ |
acd65feb | 1737 | if (var->updated) |
57e66780 | 1738 | { |
57e66780 DJ |
1739 | changed = 1; |
1740 | } | |
0cc7d26f | 1741 | else if (! var->pretty_printer) |
acd65feb VP |
1742 | { |
1743 | /* Try to compare the values. That requires that both | |
1744 | values are non-lazy. */ | |
25d5ea92 VP |
1745 | if (var->not_fetched && value_lazy (var->value)) |
1746 | { | |
1747 | /* This is a frozen varobj and the value was never read. | |
1748 | Presumably, UI shows some "never read" indicator. | |
1749 | Now that we've fetched the real value, we need to report | |
1750 | this varobj as changed so that UI can show the real | |
1751 | value. */ | |
1752 | changed = 1; | |
1753 | } | |
1754 | else if (var->value == NULL && value == NULL) | |
581e13c1 | 1755 | /* Equal. */ |
acd65feb VP |
1756 | ; |
1757 | else if (var->value == NULL || value == NULL) | |
57e66780 | 1758 | { |
57e66780 DJ |
1759 | changed = 1; |
1760 | } | |
acd65feb VP |
1761 | else |
1762 | { | |
1763 | gdb_assert (!value_lazy (var->value)); | |
1764 | gdb_assert (!value_lazy (value)); | |
85265413 | 1765 | |
57e66780 | 1766 | gdb_assert (var->print_value != NULL && print_value != NULL); |
85265413 | 1767 | if (strcmp (var->print_value, print_value) != 0) |
7a4d50bf | 1768 | changed = 1; |
acd65feb VP |
1769 | } |
1770 | } | |
1771 | } | |
85265413 | 1772 | |
ee342b23 VP |
1773 | if (!initial && !changeable) |
1774 | { | |
1775 | /* For values that are not changeable, we don't compare the values. | |
1776 | However, we want to notice if a value was not NULL and now is NULL, | |
1777 | or vise versa, so that we report when top-level varobjs come in scope | |
1778 | and leave the scope. */ | |
1779 | changed = (var->value != NULL) != (value != NULL); | |
1780 | } | |
1781 | ||
acd65feb | 1782 | /* We must always keep the new value, since children depend on it. */ |
25d5ea92 | 1783 | if (var->value != NULL && var->value != value) |
acd65feb VP |
1784 | value_free (var->value); |
1785 | var->value = value; | |
25d5ea92 VP |
1786 | if (value && value_lazy (value) && intentionally_not_fetched) |
1787 | var->not_fetched = 1; | |
1788 | else | |
1789 | var->not_fetched = 0; | |
acd65feb | 1790 | var->updated = 0; |
85265413 | 1791 | |
0cc7d26f TT |
1792 | install_new_value_visualizer (var); |
1793 | ||
1794 | /* If we installed a pretty-printer, re-compare the printed version | |
1795 | to see if the variable changed. */ | |
1796 | if (var->pretty_printer) | |
1797 | { | |
1798 | xfree (print_value); | |
1799 | print_value = value_get_print_value (var->value, var->format, var); | |
e8f781e2 TT |
1800 | if ((var->print_value == NULL && print_value != NULL) |
1801 | || (var->print_value != NULL && print_value == NULL) | |
1802 | || (var->print_value != NULL && print_value != NULL | |
1803 | && strcmp (var->print_value, print_value) != 0)) | |
0cc7d26f TT |
1804 | changed = 1; |
1805 | } | |
1806 | if (var->print_value) | |
1807 | xfree (var->print_value); | |
1808 | var->print_value = print_value; | |
1809 | ||
b26ed50d | 1810 | gdb_assert (!var->value || value_type (var->value)); |
acd65feb VP |
1811 | |
1812 | return changed; | |
1813 | } | |
acd65feb | 1814 | |
0cc7d26f TT |
1815 | /* Return the requested range for a varobj. VAR is the varobj. FROM |
1816 | and TO are out parameters; *FROM and *TO will be set to the | |
1817 | selected sub-range of VAR. If no range was selected using | |
1818 | -var-set-update-range, then both will be -1. */ | |
1819 | void | |
1820 | varobj_get_child_range (struct varobj *var, int *from, int *to) | |
b6313243 | 1821 | { |
0cc7d26f TT |
1822 | *from = var->from; |
1823 | *to = var->to; | |
b6313243 TT |
1824 | } |
1825 | ||
0cc7d26f TT |
1826 | /* Set the selected sub-range of children of VAR to start at index |
1827 | FROM and end at index TO. If either FROM or TO is less than zero, | |
1828 | this is interpreted as a request for all children. */ | |
1829 | void | |
1830 | varobj_set_child_range (struct varobj *var, int from, int to) | |
b6313243 | 1831 | { |
0cc7d26f TT |
1832 | var->from = from; |
1833 | var->to = to; | |
b6313243 TT |
1834 | } |
1835 | ||
1836 | void | |
1837 | varobj_set_visualizer (struct varobj *var, const char *visualizer) | |
1838 | { | |
1839 | #if HAVE_PYTHON | |
34fa1d9d MS |
1840 | PyObject *mainmod, *globals, *constructor; |
1841 | struct cleanup *back_to; | |
b6313243 | 1842 | |
d452c4bc | 1843 | back_to = varobj_ensure_python_env (var); |
b6313243 TT |
1844 | |
1845 | mainmod = PyImport_AddModule ("__main__"); | |
1846 | globals = PyModule_GetDict (mainmod); | |
1847 | Py_INCREF (globals); | |
1848 | make_cleanup_py_decref (globals); | |
1849 | ||
1850 | constructor = PyRun_String (visualizer, Py_eval_input, globals, globals); | |
b6313243 | 1851 | |
0cc7d26f | 1852 | if (! constructor) |
b6313243 TT |
1853 | { |
1854 | gdbpy_print_stack (); | |
da1f2771 | 1855 | error (_("Could not evaluate visualizer expression: %s"), visualizer); |
b6313243 TT |
1856 | } |
1857 | ||
0cc7d26f TT |
1858 | construct_visualizer (var, constructor); |
1859 | Py_XDECREF (constructor); | |
b6313243 | 1860 | |
0cc7d26f TT |
1861 | /* If there are any children now, wipe them. */ |
1862 | varobj_delete (var, NULL, 1 /* children only */); | |
1863 | var->num_children = -1; | |
b6313243 TT |
1864 | |
1865 | do_cleanups (back_to); | |
1866 | #else | |
da1f2771 | 1867 | error (_("Python support required")); |
b6313243 TT |
1868 | #endif |
1869 | } | |
1870 | ||
7a290c40 JB |
1871 | /* If NEW_VALUE is the new value of the given varobj (var), return |
1872 | non-zero if var has mutated. In other words, if the type of | |
1873 | the new value is different from the type of the varobj's old | |
1874 | value. | |
1875 | ||
1876 | NEW_VALUE may be NULL, if the varobj is now out of scope. */ | |
1877 | ||
1878 | static int | |
1879 | varobj_value_has_mutated (struct varobj *var, struct value *new_value, | |
1880 | struct type *new_type) | |
1881 | { | |
1882 | /* If we haven't previously computed the number of children in var, | |
1883 | it does not matter from the front-end's perspective whether | |
1884 | the type has mutated or not. For all intents and purposes, | |
1885 | it has not mutated. */ | |
1886 | if (var->num_children < 0) | |
1887 | return 0; | |
1888 | ||
1889 | if (var->root->lang->value_has_mutated) | |
1890 | return var->root->lang->value_has_mutated (var, new_value, new_type); | |
1891 | else | |
1892 | return 0; | |
1893 | } | |
1894 | ||
8b93c638 JM |
1895 | /* Update the values for a variable and its children. This is a |
1896 | two-pronged attack. First, re-parse the value for the root's | |
1897 | expression to see if it's changed. Then go all the way | |
1898 | through its children, reconstructing them and noting if they've | |
1899 | changed. | |
1900 | ||
25d5ea92 VP |
1901 | The EXPLICIT parameter specifies if this call is result |
1902 | of MI request to update this specific variable, or | |
581e13c1 | 1903 | result of implicit -var-update *. For implicit request, we don't |
25d5ea92 | 1904 | update frozen variables. |
705da579 | 1905 | |
581e13c1 | 1906 | NOTE: This function may delete the caller's varobj. If it |
8756216b DP |
1907 | returns TYPE_CHANGED, then it has done this and VARP will be modified |
1908 | to point to the new varobj. */ | |
8b93c638 | 1909 | |
1417b39d JB |
1910 | VEC(varobj_update_result) * |
1911 | varobj_update (struct varobj **varp, int explicit) | |
8b93c638 JM |
1912 | { |
1913 | int changed = 0; | |
25d5ea92 | 1914 | int type_changed = 0; |
8b93c638 | 1915 | int i; |
30b28db1 | 1916 | struct value *new; |
b6313243 | 1917 | VEC (varobj_update_result) *stack = NULL; |
f7f9ae2c | 1918 | VEC (varobj_update_result) *result = NULL; |
8b93c638 | 1919 | |
25d5ea92 VP |
1920 | /* Frozen means frozen -- we don't check for any change in |
1921 | this varobj, including its going out of scope, or | |
1922 | changing type. One use case for frozen varobjs is | |
1923 | retaining previously evaluated expressions, and we don't | |
1924 | want them to be reevaluated at all. */ | |
1925 | if (!explicit && (*varp)->frozen) | |
f7f9ae2c | 1926 | return result; |
8756216b DP |
1927 | |
1928 | if (!(*varp)->root->is_valid) | |
f7f9ae2c | 1929 | { |
cfce2ea2 | 1930 | varobj_update_result r = {0}; |
a109c7c1 | 1931 | |
cfce2ea2 | 1932 | r.varobj = *varp; |
f7f9ae2c VP |
1933 | r.status = VAROBJ_INVALID; |
1934 | VEC_safe_push (varobj_update_result, result, &r); | |
1935 | return result; | |
1936 | } | |
8b93c638 | 1937 | |
25d5ea92 | 1938 | if ((*varp)->root->rootvar == *varp) |
ae093f96 | 1939 | { |
cfce2ea2 | 1940 | varobj_update_result r = {0}; |
a109c7c1 | 1941 | |
cfce2ea2 | 1942 | r.varobj = *varp; |
f7f9ae2c VP |
1943 | r.status = VAROBJ_IN_SCOPE; |
1944 | ||
581e13c1 | 1945 | /* Update the root variable. value_of_root can return NULL |
25d5ea92 | 1946 | if the variable is no longer around, i.e. we stepped out of |
581e13c1 | 1947 | the frame in which a local existed. We are letting the |
25d5ea92 VP |
1948 | value_of_root variable dispose of the varobj if the type |
1949 | has changed. */ | |
25d5ea92 | 1950 | new = value_of_root (varp, &type_changed); |
f7f9ae2c VP |
1951 | r.varobj = *varp; |
1952 | ||
1953 | r.type_changed = type_changed; | |
ea56f9c2 | 1954 | if (install_new_value ((*varp), new, type_changed)) |
f7f9ae2c | 1955 | r.changed = 1; |
ea56f9c2 | 1956 | |
25d5ea92 | 1957 | if (new == NULL) |
f7f9ae2c | 1958 | r.status = VAROBJ_NOT_IN_SCOPE; |
b6313243 | 1959 | r.value_installed = 1; |
f7f9ae2c VP |
1960 | |
1961 | if (r.status == VAROBJ_NOT_IN_SCOPE) | |
b6313243 | 1962 | { |
0b4bc29a JK |
1963 | if (r.type_changed || r.changed) |
1964 | VEC_safe_push (varobj_update_result, result, &r); | |
b6313243 TT |
1965 | return result; |
1966 | } | |
1967 | ||
1968 | VEC_safe_push (varobj_update_result, stack, &r); | |
1969 | } | |
1970 | else | |
1971 | { | |
cfce2ea2 | 1972 | varobj_update_result r = {0}; |
a109c7c1 | 1973 | |
cfce2ea2 | 1974 | r.varobj = *varp; |
b6313243 | 1975 | VEC_safe_push (varobj_update_result, stack, &r); |
b20d8971 | 1976 | } |
8b93c638 | 1977 | |
8756216b | 1978 | /* Walk through the children, reconstructing them all. */ |
b6313243 | 1979 | while (!VEC_empty (varobj_update_result, stack)) |
8b93c638 | 1980 | { |
b6313243 TT |
1981 | varobj_update_result r = *(VEC_last (varobj_update_result, stack)); |
1982 | struct varobj *v = r.varobj; | |
1983 | ||
1984 | VEC_pop (varobj_update_result, stack); | |
1985 | ||
1986 | /* Update this variable, unless it's a root, which is already | |
1987 | updated. */ | |
1988 | if (!r.value_installed) | |
7a290c40 JB |
1989 | { |
1990 | struct type *new_type; | |
1991 | ||
b6313243 | 1992 | new = value_of_child (v->parent, v->index); |
7a290c40 JB |
1993 | if (new) |
1994 | new_type = value_type (new); | |
1995 | else | |
1996 | new_type = v->root->lang->type_of_child (v->parent, v->index); | |
1997 | ||
1998 | if (varobj_value_has_mutated (v, new, new_type)) | |
1999 | { | |
2000 | /* The children are no longer valid; delete them now. | |
2001 | Report the fact that its type changed as well. */ | |
2002 | varobj_delete (v, NULL, 1 /* only_children */); | |
2003 | v->num_children = -1; | |
2004 | v->to = -1; | |
2005 | v->from = -1; | |
2006 | v->type = new_type; | |
2007 | r.type_changed = 1; | |
2008 | } | |
2009 | ||
2010 | if (install_new_value (v, new, r.type_changed)) | |
b6313243 TT |
2011 | { |
2012 | r.changed = 1; | |
2013 | v->updated = 0; | |
2014 | } | |
2015 | } | |
2016 | ||
2017 | /* We probably should not get children of a varobj that has a | |
2018 | pretty-printer, but for which -var-list-children was never | |
581e13c1 | 2019 | invoked. */ |
b6313243 TT |
2020 | if (v->pretty_printer) |
2021 | { | |
0cc7d26f | 2022 | VEC (varobj_p) *changed = 0, *new = 0, *unchanged = 0; |
26f9bcee | 2023 | int i, children_changed = 0; |
b6313243 TT |
2024 | |
2025 | if (v->frozen) | |
2026 | continue; | |
2027 | ||
0cc7d26f TT |
2028 | if (!v->children_requested) |
2029 | { | |
2030 | int dummy; | |
2031 | ||
2032 | /* If we initially did not have potential children, but | |
2033 | now we do, consider the varobj as changed. | |
2034 | Otherwise, if children were never requested, consider | |
2035 | it as unchanged -- presumably, such varobj is not yet | |
2036 | expanded in the UI, so we need not bother getting | |
2037 | it. */ | |
2038 | if (!varobj_has_more (v, 0)) | |
2039 | { | |
2040 | update_dynamic_varobj_children (v, NULL, NULL, NULL, | |
2041 | &dummy, 0, 0, 0); | |
2042 | if (varobj_has_more (v, 0)) | |
2043 | r.changed = 1; | |
2044 | } | |
2045 | ||
2046 | if (r.changed) | |
2047 | VEC_safe_push (varobj_update_result, result, &r); | |
2048 | ||
2049 | continue; | |
2050 | } | |
2051 | ||
b6313243 TT |
2052 | /* If update_dynamic_varobj_children returns 0, then we have |
2053 | a non-conforming pretty-printer, so we skip it. */ | |
0cc7d26f TT |
2054 | if (update_dynamic_varobj_children (v, &changed, &new, &unchanged, |
2055 | &children_changed, 1, | |
2056 | v->from, v->to)) | |
b6313243 | 2057 | { |
0cc7d26f | 2058 | if (children_changed || new) |
b6313243 | 2059 | { |
0cc7d26f TT |
2060 | r.children_changed = 1; |
2061 | r.new = new; | |
b6313243 | 2062 | } |
0cc7d26f TT |
2063 | /* Push in reverse order so that the first child is |
2064 | popped from the work stack first, and so will be | |
2065 | added to result first. This does not affect | |
2066 | correctness, just "nicer". */ | |
2067 | for (i = VEC_length (varobj_p, changed) - 1; i >= 0; --i) | |
b6313243 | 2068 | { |
0cc7d26f | 2069 | varobj_p tmp = VEC_index (varobj_p, changed, i); |
cfce2ea2 | 2070 | varobj_update_result r = {0}; |
a109c7c1 | 2071 | |
cfce2ea2 | 2072 | r.varobj = tmp; |
0cc7d26f | 2073 | r.changed = 1; |
b6313243 TT |
2074 | r.value_installed = 1; |
2075 | VEC_safe_push (varobj_update_result, stack, &r); | |
2076 | } | |
0cc7d26f TT |
2077 | for (i = VEC_length (varobj_p, unchanged) - 1; i >= 0; --i) |
2078 | { | |
2079 | varobj_p tmp = VEC_index (varobj_p, unchanged, i); | |
a109c7c1 | 2080 | |
0cc7d26f TT |
2081 | if (!tmp->frozen) |
2082 | { | |
cfce2ea2 | 2083 | varobj_update_result r = {0}; |
a109c7c1 | 2084 | |
cfce2ea2 | 2085 | r.varobj = tmp; |
0cc7d26f TT |
2086 | r.value_installed = 1; |
2087 | VEC_safe_push (varobj_update_result, stack, &r); | |
2088 | } | |
2089 | } | |
b6313243 TT |
2090 | if (r.changed || r.children_changed) |
2091 | VEC_safe_push (varobj_update_result, result, &r); | |
0cc7d26f TT |
2092 | |
2093 | /* Free CHANGED and UNCHANGED, but not NEW, because NEW | |
2094 | has been put into the result vector. */ | |
2095 | VEC_free (varobj_p, changed); | |
2096 | VEC_free (varobj_p, unchanged); | |
2097 | ||
b6313243 TT |
2098 | continue; |
2099 | } | |
2100 | } | |
28335dcc VP |
2101 | |
2102 | /* Push any children. Use reverse order so that the first | |
2103 | child is popped from the work stack first, and so | |
2104 | will be added to result first. This does not | |
2105 | affect correctness, just "nicer". */ | |
2106 | for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i) | |
8b93c638 | 2107 | { |
28335dcc | 2108 | varobj_p c = VEC_index (varobj_p, v->children, i); |
a109c7c1 | 2109 | |
28335dcc | 2110 | /* Child may be NULL if explicitly deleted by -var-delete. */ |
25d5ea92 | 2111 | if (c != NULL && !c->frozen) |
28335dcc | 2112 | { |
cfce2ea2 | 2113 | varobj_update_result r = {0}; |
a109c7c1 | 2114 | |
cfce2ea2 | 2115 | r.varobj = c; |
b6313243 | 2116 | VEC_safe_push (varobj_update_result, stack, &r); |
28335dcc | 2117 | } |
8b93c638 | 2118 | } |
b6313243 TT |
2119 | |
2120 | if (r.changed || r.type_changed) | |
2121 | VEC_safe_push (varobj_update_result, result, &r); | |
8b93c638 JM |
2122 | } |
2123 | ||
b6313243 TT |
2124 | VEC_free (varobj_update_result, stack); |
2125 | ||
f7f9ae2c | 2126 | return result; |
8b93c638 JM |
2127 | } |
2128 | \f | |
2129 | ||
2130 | /* Helper functions */ | |
2131 | ||
2132 | /* | |
2133 | * Variable object construction/destruction | |
2134 | */ | |
2135 | ||
2136 | static int | |
fba45db2 KB |
2137 | delete_variable (struct cpstack **resultp, struct varobj *var, |
2138 | int only_children_p) | |
8b93c638 JM |
2139 | { |
2140 | int delcount = 0; | |
2141 | ||
2142 | delete_variable_1 (resultp, &delcount, var, | |
2143 | only_children_p, 1 /* remove_from_parent_p */ ); | |
2144 | ||
2145 | return delcount; | |
2146 | } | |
2147 | ||
581e13c1 | 2148 | /* Delete the variable object VAR and its children. */ |
8b93c638 JM |
2149 | /* IMPORTANT NOTE: If we delete a variable which is a child |
2150 | and the parent is not removed we dump core. It must be always | |
581e13c1 | 2151 | initially called with remove_from_parent_p set. */ |
8b93c638 | 2152 | static void |
72330bd6 AC |
2153 | delete_variable_1 (struct cpstack **resultp, int *delcountp, |
2154 | struct varobj *var, int only_children_p, | |
2155 | int remove_from_parent_p) | |
8b93c638 | 2156 | { |
28335dcc | 2157 | int i; |
8b93c638 | 2158 | |
581e13c1 | 2159 | /* Delete any children of this variable, too. */ |
28335dcc VP |
2160 | for (i = 0; i < VEC_length (varobj_p, var->children); ++i) |
2161 | { | |
2162 | varobj_p child = VEC_index (varobj_p, var->children, i); | |
a109c7c1 | 2163 | |
214270ab VP |
2164 | if (!child) |
2165 | continue; | |
8b93c638 | 2166 | if (!remove_from_parent_p) |
28335dcc VP |
2167 | child->parent = NULL; |
2168 | delete_variable_1 (resultp, delcountp, child, 0, only_children_p); | |
8b93c638 | 2169 | } |
28335dcc | 2170 | VEC_free (varobj_p, var->children); |
8b93c638 | 2171 | |
581e13c1 | 2172 | /* if we were called to delete only the children we are done here. */ |
8b93c638 JM |
2173 | if (only_children_p) |
2174 | return; | |
2175 | ||
581e13c1 | 2176 | /* Otherwise, add it to the list of deleted ones and proceed to do so. */ |
73a93a32 | 2177 | /* If the name is null, this is a temporary variable, that has not |
581e13c1 | 2178 | yet been installed, don't report it, it belongs to the caller... */ |
73a93a32 | 2179 | if (var->obj_name != NULL) |
8b93c638 | 2180 | { |
5b616ba1 | 2181 | cppush (resultp, xstrdup (var->obj_name)); |
8b93c638 JM |
2182 | *delcountp = *delcountp + 1; |
2183 | } | |
2184 | ||
581e13c1 | 2185 | /* If this variable has a parent, remove it from its parent's list. */ |
8b93c638 JM |
2186 | /* OPTIMIZATION: if the parent of this variable is also being deleted, |
2187 | (as indicated by remove_from_parent_p) we don't bother doing an | |
2188 | expensive list search to find the element to remove when we are | |
581e13c1 | 2189 | discarding the list afterwards. */ |
72330bd6 | 2190 | if ((remove_from_parent_p) && (var->parent != NULL)) |
8b93c638 | 2191 | { |
28335dcc | 2192 | VEC_replace (varobj_p, var->parent->children, var->index, NULL); |
8b93c638 | 2193 | } |
72330bd6 | 2194 | |
73a93a32 JI |
2195 | if (var->obj_name != NULL) |
2196 | uninstall_variable (var); | |
8b93c638 | 2197 | |
581e13c1 | 2198 | /* Free memory associated with this variable. */ |
8b93c638 JM |
2199 | free_variable (var); |
2200 | } | |
2201 | ||
581e13c1 | 2202 | /* Install the given variable VAR with the object name VAR->OBJ_NAME. */ |
8b93c638 | 2203 | static int |
fba45db2 | 2204 | install_variable (struct varobj *var) |
8b93c638 JM |
2205 | { |
2206 | struct vlist *cv; | |
2207 | struct vlist *newvl; | |
2208 | const char *chp; | |
2209 | unsigned int index = 0; | |
2210 | unsigned int i = 1; | |
2211 | ||
2212 | for (chp = var->obj_name; *chp; chp++) | |
2213 | { | |
2214 | index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE; | |
2215 | } | |
2216 | ||
2217 | cv = *(varobj_table + index); | |
2218 | while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0)) | |
2219 | cv = cv->next; | |
2220 | ||
2221 | if (cv != NULL) | |
8a3fe4f8 | 2222 | error (_("Duplicate variable object name")); |
8b93c638 | 2223 | |
581e13c1 | 2224 | /* Add varobj to hash table. */ |
8b93c638 JM |
2225 | newvl = xmalloc (sizeof (struct vlist)); |
2226 | newvl->next = *(varobj_table + index); | |
2227 | newvl->var = var; | |
2228 | *(varobj_table + index) = newvl; | |
2229 | ||
581e13c1 | 2230 | /* If root, add varobj to root list. */ |
b2c2bd75 | 2231 | if (is_root_p (var)) |
8b93c638 | 2232 | { |
581e13c1 | 2233 | /* Add to list of root variables. */ |
8b93c638 JM |
2234 | if (rootlist == NULL) |
2235 | var->root->next = NULL; | |
2236 | else | |
2237 | var->root->next = rootlist; | |
2238 | rootlist = var->root; | |
8b93c638 JM |
2239 | } |
2240 | ||
2241 | return 1; /* OK */ | |
2242 | } | |
2243 | ||
581e13c1 | 2244 | /* Unistall the object VAR. */ |
8b93c638 | 2245 | static void |
fba45db2 | 2246 | uninstall_variable (struct varobj *var) |
8b93c638 JM |
2247 | { |
2248 | struct vlist *cv; | |
2249 | struct vlist *prev; | |
2250 | struct varobj_root *cr; | |
2251 | struct varobj_root *prer; | |
2252 | const char *chp; | |
2253 | unsigned int index = 0; | |
2254 | unsigned int i = 1; | |
2255 | ||
581e13c1 | 2256 | /* Remove varobj from hash table. */ |
8b93c638 JM |
2257 | for (chp = var->obj_name; *chp; chp++) |
2258 | { | |
2259 | index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE; | |
2260 | } | |
2261 | ||
2262 | cv = *(varobj_table + index); | |
2263 | prev = NULL; | |
2264 | while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0)) | |
2265 | { | |
2266 | prev = cv; | |
2267 | cv = cv->next; | |
2268 | } | |
2269 | ||
2270 | if (varobjdebug) | |
2271 | fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name); | |
2272 | ||
2273 | if (cv == NULL) | |
2274 | { | |
72330bd6 AC |
2275 | warning |
2276 | ("Assertion failed: Could not find variable object \"%s\" to delete", | |
2277 | var->obj_name); | |
8b93c638 JM |
2278 | return; |
2279 | } | |
2280 | ||
2281 | if (prev == NULL) | |
2282 | *(varobj_table + index) = cv->next; | |
2283 | else | |
2284 | prev->next = cv->next; | |
2285 | ||
b8c9b27d | 2286 | xfree (cv); |
8b93c638 | 2287 | |
581e13c1 | 2288 | /* If root, remove varobj from root list. */ |
b2c2bd75 | 2289 | if (is_root_p (var)) |
8b93c638 | 2290 | { |
581e13c1 | 2291 | /* Remove from list of root variables. */ |
8b93c638 JM |
2292 | if (rootlist == var->root) |
2293 | rootlist = var->root->next; | |
2294 | else | |
2295 | { | |
2296 | prer = NULL; | |
2297 | cr = rootlist; | |
2298 | while ((cr != NULL) && (cr->rootvar != var)) | |
2299 | { | |
2300 | prer = cr; | |
2301 | cr = cr->next; | |
2302 | } | |
2303 | if (cr == NULL) | |
2304 | { | |
8f7e195f JB |
2305 | warning (_("Assertion failed: Could not find " |
2306 | "varobj \"%s\" in root list"), | |
3e43a32a | 2307 | var->obj_name); |
8b93c638 JM |
2308 | return; |
2309 | } | |
2310 | if (prer == NULL) | |
2311 | rootlist = NULL; | |
2312 | else | |
2313 | prer->next = cr->next; | |
2314 | } | |
8b93c638 JM |
2315 | } |
2316 | ||
2317 | } | |
2318 | ||
581e13c1 | 2319 | /* Create and install a child of the parent of the given name. */ |
8b93c638 | 2320 | static struct varobj * |
fba45db2 | 2321 | create_child (struct varobj *parent, int index, char *name) |
b6313243 TT |
2322 | { |
2323 | return create_child_with_value (parent, index, name, | |
2324 | value_of_child (parent, index)); | |
2325 | } | |
2326 | ||
85254831 KS |
2327 | /* Does CHILD represent a child with no name? This happens when |
2328 | the child is an anonmous struct or union and it has no field name | |
2329 | in its parent variable. | |
2330 | ||
2331 | This has already been determined by *_describe_child. The easiest | |
2332 | thing to do is to compare the child's name with ANONYMOUS_*_NAME. */ | |
2333 | ||
2334 | static int | |
2335 | is_anonymous_child (struct varobj *child) | |
2336 | { | |
2337 | return (strcmp (child->name, ANONYMOUS_STRUCT_NAME) == 0 | |
2338 | || strcmp (child->name, ANONYMOUS_UNION_NAME) == 0); | |
2339 | } | |
2340 | ||
b6313243 TT |
2341 | static struct varobj * |
2342 | create_child_with_value (struct varobj *parent, int index, const char *name, | |
2343 | struct value *value) | |
8b93c638 JM |
2344 | { |
2345 | struct varobj *child; | |
2346 | char *childs_name; | |
2347 | ||
2348 | child = new_variable (); | |
2349 | ||
581e13c1 | 2350 | /* Name is allocated by name_of_child. */ |
b6313243 TT |
2351 | /* FIXME: xstrdup should not be here. */ |
2352 | child->name = xstrdup (name); | |
8b93c638 | 2353 | child->index = index; |
8b93c638 JM |
2354 | child->parent = parent; |
2355 | child->root = parent->root; | |
85254831 KS |
2356 | |
2357 | if (is_anonymous_child (child)) | |
2358 | childs_name = xstrprintf ("%s.%d_anonymous", parent->obj_name, index); | |
2359 | else | |
2360 | childs_name = xstrprintf ("%s.%s", parent->obj_name, name); | |
8b93c638 | 2361 | child->obj_name = childs_name; |
85254831 | 2362 | |
8b93c638 JM |
2363 | install_variable (child); |
2364 | ||
acd65feb VP |
2365 | /* Compute the type of the child. Must do this before |
2366 | calling install_new_value. */ | |
2367 | if (value != NULL) | |
2368 | /* If the child had no evaluation errors, var->value | |
581e13c1 | 2369 | will be non-NULL and contain a valid type. */ |
acd65feb VP |
2370 | child->type = value_type (value); |
2371 | else | |
581e13c1 | 2372 | /* Otherwise, we must compute the type. */ |
acd65feb VP |
2373 | child->type = (*child->root->lang->type_of_child) (child->parent, |
2374 | child->index); | |
2375 | install_new_value (child, value, 1); | |
2376 | ||
8b93c638 JM |
2377 | return child; |
2378 | } | |
8b93c638 JM |
2379 | \f |
2380 | ||
2381 | /* | |
2382 | * Miscellaneous utility functions. | |
2383 | */ | |
2384 | ||
581e13c1 | 2385 | /* Allocate memory and initialize a new variable. */ |
8b93c638 JM |
2386 | static struct varobj * |
2387 | new_variable (void) | |
2388 | { | |
2389 | struct varobj *var; | |
2390 | ||
2391 | var = (struct varobj *) xmalloc (sizeof (struct varobj)); | |
2392 | var->name = NULL; | |
02142340 | 2393 | var->path_expr = NULL; |
8b93c638 JM |
2394 | var->obj_name = NULL; |
2395 | var->index = -1; | |
2396 | var->type = NULL; | |
2397 | var->value = NULL; | |
8b93c638 JM |
2398 | var->num_children = -1; |
2399 | var->parent = NULL; | |
2400 | var->children = NULL; | |
2401 | var->format = 0; | |
2402 | var->root = NULL; | |
fb9b6b35 | 2403 | var->updated = 0; |
85265413 | 2404 | var->print_value = NULL; |
25d5ea92 VP |
2405 | var->frozen = 0; |
2406 | var->not_fetched = 0; | |
b6313243 | 2407 | var->children_requested = 0; |
0cc7d26f TT |
2408 | var->from = -1; |
2409 | var->to = -1; | |
2410 | var->constructor = 0; | |
b6313243 | 2411 | var->pretty_printer = 0; |
0cc7d26f TT |
2412 | var->child_iter = 0; |
2413 | var->saved_item = 0; | |
8b93c638 JM |
2414 | |
2415 | return var; | |
2416 | } | |
2417 | ||
581e13c1 | 2418 | /* Allocate memory and initialize a new root variable. */ |
8b93c638 JM |
2419 | static struct varobj * |
2420 | new_root_variable (void) | |
2421 | { | |
2422 | struct varobj *var = new_variable (); | |
a109c7c1 | 2423 | |
3e43a32a | 2424 | var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root)); |
8b93c638 JM |
2425 | var->root->lang = NULL; |
2426 | var->root->exp = NULL; | |
2427 | var->root->valid_block = NULL; | |
7a424e99 | 2428 | var->root->frame = null_frame_id; |
a5defcdc | 2429 | var->root->floating = 0; |
8b93c638 | 2430 | var->root->rootvar = NULL; |
8756216b | 2431 | var->root->is_valid = 1; |
8b93c638 JM |
2432 | |
2433 | return var; | |
2434 | } | |
2435 | ||
581e13c1 | 2436 | /* Free any allocated memory associated with VAR. */ |
8b93c638 | 2437 | static void |
fba45db2 | 2438 | free_variable (struct varobj *var) |
8b93c638 | 2439 | { |
d452c4bc UW |
2440 | #if HAVE_PYTHON |
2441 | if (var->pretty_printer) | |
2442 | { | |
2443 | struct cleanup *cleanup = varobj_ensure_python_env (var); | |
0cc7d26f TT |
2444 | Py_XDECREF (var->constructor); |
2445 | Py_XDECREF (var->pretty_printer); | |
2446 | Py_XDECREF (var->child_iter); | |
2447 | Py_XDECREF (var->saved_item); | |
d452c4bc UW |
2448 | do_cleanups (cleanup); |
2449 | } | |
2450 | #endif | |
2451 | ||
36746093 JK |
2452 | value_free (var->value); |
2453 | ||
581e13c1 | 2454 | /* Free the expression if this is a root variable. */ |
b2c2bd75 | 2455 | if (is_root_p (var)) |
8b93c638 | 2456 | { |
3038237c | 2457 | xfree (var->root->exp); |
8038e1e2 | 2458 | xfree (var->root); |
8b93c638 JM |
2459 | } |
2460 | ||
8038e1e2 AC |
2461 | xfree (var->name); |
2462 | xfree (var->obj_name); | |
85265413 | 2463 | xfree (var->print_value); |
02142340 | 2464 | xfree (var->path_expr); |
8038e1e2 | 2465 | xfree (var); |
8b93c638 JM |
2466 | } |
2467 | ||
74b7792f AC |
2468 | static void |
2469 | do_free_variable_cleanup (void *var) | |
2470 | { | |
2471 | free_variable (var); | |
2472 | } | |
2473 | ||
2474 | static struct cleanup * | |
2475 | make_cleanup_free_variable (struct varobj *var) | |
2476 | { | |
2477 | return make_cleanup (do_free_variable_cleanup, var); | |
2478 | } | |
2479 | ||
581e13c1 | 2480 | /* This returns the type of the variable. It also skips past typedefs |
6766a268 | 2481 | to return the real type of the variable. |
94b66fa7 KS |
2482 | |
2483 | NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file | |
581e13c1 | 2484 | except within get_target_type and get_type. */ |
8b93c638 | 2485 | static struct type * |
fba45db2 | 2486 | get_type (struct varobj *var) |
8b93c638 JM |
2487 | { |
2488 | struct type *type; | |
8b93c638 | 2489 | |
a109c7c1 | 2490 | type = var->type; |
6766a268 DJ |
2491 | if (type != NULL) |
2492 | type = check_typedef (type); | |
8b93c638 JM |
2493 | |
2494 | return type; | |
2495 | } | |
2496 | ||
6e2a9270 VP |
2497 | /* Return the type of the value that's stored in VAR, |
2498 | or that would have being stored there if the | |
581e13c1 | 2499 | value were accessible. |
6e2a9270 VP |
2500 | |
2501 | This differs from VAR->type in that VAR->type is always | |
2502 | the true type of the expession in the source language. | |
2503 | The return value of this function is the type we're | |
2504 | actually storing in varobj, and using for displaying | |
2505 | the values and for comparing previous and new values. | |
2506 | ||
2507 | For example, top-level references are always stripped. */ | |
2508 | static struct type * | |
2509 | get_value_type (struct varobj *var) | |
2510 | { | |
2511 | struct type *type; | |
2512 | ||
2513 | if (var->value) | |
2514 | type = value_type (var->value); | |
2515 | else | |
2516 | type = var->type; | |
2517 | ||
2518 | type = check_typedef (type); | |
2519 | ||
2520 | if (TYPE_CODE (type) == TYPE_CODE_REF) | |
2521 | type = get_target_type (type); | |
2522 | ||
2523 | type = check_typedef (type); | |
2524 | ||
2525 | return type; | |
2526 | } | |
2527 | ||
8b93c638 | 2528 | /* This returns the target type (or NULL) of TYPE, also skipping |
94b66fa7 KS |
2529 | past typedefs, just like get_type (). |
2530 | ||
2531 | NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file | |
581e13c1 | 2532 | except within get_target_type and get_type. */ |
8b93c638 | 2533 | static struct type * |
fba45db2 | 2534 | get_target_type (struct type *type) |
8b93c638 JM |
2535 | { |
2536 | if (type != NULL) | |
2537 | { | |
2538 | type = TYPE_TARGET_TYPE (type); | |
6766a268 DJ |
2539 | if (type != NULL) |
2540 | type = check_typedef (type); | |
8b93c638 JM |
2541 | } |
2542 | ||
2543 | return type; | |
2544 | } | |
2545 | ||
2546 | /* What is the default display for this variable? We assume that | |
581e13c1 | 2547 | everything is "natural". Any exceptions? */ |
8b93c638 | 2548 | static enum varobj_display_formats |
fba45db2 | 2549 | variable_default_display (struct varobj *var) |
8b93c638 JM |
2550 | { |
2551 | return FORMAT_NATURAL; | |
2552 | } | |
2553 | ||
581e13c1 | 2554 | /* FIXME: The following should be generic for any pointer. */ |
8b93c638 | 2555 | static void |
fba45db2 | 2556 | cppush (struct cpstack **pstack, char *name) |
8b93c638 JM |
2557 | { |
2558 | struct cpstack *s; | |
2559 | ||
2560 | s = (struct cpstack *) xmalloc (sizeof (struct cpstack)); | |
2561 | s->name = name; | |
2562 | s->next = *pstack; | |
2563 | *pstack = s; | |
2564 | } | |
2565 | ||
581e13c1 | 2566 | /* FIXME: The following should be generic for any pointer. */ |
8b93c638 | 2567 | static char * |
fba45db2 | 2568 | cppop (struct cpstack **pstack) |
8b93c638 JM |
2569 | { |
2570 | struct cpstack *s; | |
2571 | char *v; | |
2572 | ||
2573 | if ((*pstack)->name == NULL && (*pstack)->next == NULL) | |
2574 | return NULL; | |
2575 | ||
2576 | s = *pstack; | |
2577 | v = s->name; | |
2578 | *pstack = (*pstack)->next; | |
b8c9b27d | 2579 | xfree (s); |
8b93c638 JM |
2580 | |
2581 | return v; | |
2582 | } | |
2583 | \f | |
2584 | /* | |
2585 | * Language-dependencies | |
2586 | */ | |
2587 | ||
2588 | /* Common entry points */ | |
2589 | ||
581e13c1 | 2590 | /* Get the language of variable VAR. */ |
8b93c638 | 2591 | static enum varobj_languages |
fba45db2 | 2592 | variable_language (struct varobj *var) |
8b93c638 JM |
2593 | { |
2594 | enum varobj_languages lang; | |
2595 | ||
2596 | switch (var->root->exp->language_defn->la_language) | |
2597 | { | |
2598 | default: | |
2599 | case language_c: | |
2600 | lang = vlang_c; | |
2601 | break; | |
2602 | case language_cplus: | |
2603 | lang = vlang_cplus; | |
2604 | break; | |
2605 | case language_java: | |
2606 | lang = vlang_java; | |
2607 | break; | |
40591b7d JCD |
2608 | case language_ada: |
2609 | lang = vlang_ada; | |
2610 | break; | |
8b93c638 JM |
2611 | } |
2612 | ||
2613 | return lang; | |
2614 | } | |
2615 | ||
2616 | /* Return the number of children for a given variable. | |
2617 | The result of this function is defined by the language | |
581e13c1 | 2618 | implementation. The number of children returned by this function |
8b93c638 | 2619 | is the number of children that the user will see in the variable |
581e13c1 | 2620 | display. */ |
8b93c638 | 2621 | static int |
fba45db2 | 2622 | number_of_children (struct varobj *var) |
8b93c638 | 2623 | { |
82ae4854 | 2624 | return (*var->root->lang->number_of_children) (var); |
8b93c638 JM |
2625 | } |
2626 | ||
3e43a32a | 2627 | /* What is the expression for the root varobj VAR? Returns a malloc'd |
581e13c1 | 2628 | string. */ |
8b93c638 | 2629 | static char * |
fba45db2 | 2630 | name_of_variable (struct varobj *var) |
8b93c638 JM |
2631 | { |
2632 | return (*var->root->lang->name_of_variable) (var); | |
2633 | } | |
2634 | ||
3e43a32a | 2635 | /* What is the name of the INDEX'th child of VAR? Returns a malloc'd |
581e13c1 | 2636 | string. */ |
8b93c638 | 2637 | static char * |
fba45db2 | 2638 | name_of_child (struct varobj *var, int index) |
8b93c638 JM |
2639 | { |
2640 | return (*var->root->lang->name_of_child) (var, index); | |
2641 | } | |
2642 | ||
a5defcdc VP |
2643 | /* What is the ``struct value *'' of the root variable VAR? |
2644 | For floating variable object, evaluation can get us a value | |
2645 | of different type from what is stored in varobj already. In | |
2646 | that case: | |
2647 | - *type_changed will be set to 1 | |
2648 | - old varobj will be freed, and new one will be | |
2649 | created, with the same name. | |
2650 | - *var_handle will be set to the new varobj | |
2651 | Otherwise, *type_changed will be set to 0. */ | |
30b28db1 | 2652 | static struct value * |
fba45db2 | 2653 | value_of_root (struct varobj **var_handle, int *type_changed) |
8b93c638 | 2654 | { |
73a93a32 JI |
2655 | struct varobj *var; |
2656 | ||
2657 | if (var_handle == NULL) | |
2658 | return NULL; | |
2659 | ||
2660 | var = *var_handle; | |
2661 | ||
2662 | /* This should really be an exception, since this should | |
581e13c1 | 2663 | only get called with a root variable. */ |
73a93a32 | 2664 | |
b2c2bd75 | 2665 | if (!is_root_p (var)) |
73a93a32 JI |
2666 | return NULL; |
2667 | ||
a5defcdc | 2668 | if (var->root->floating) |
73a93a32 JI |
2669 | { |
2670 | struct varobj *tmp_var; | |
2671 | char *old_type, *new_type; | |
6225abfa | 2672 | |
73a93a32 JI |
2673 | tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0, |
2674 | USE_SELECTED_FRAME); | |
2675 | if (tmp_var == NULL) | |
2676 | { | |
2677 | return NULL; | |
2678 | } | |
6225abfa | 2679 | old_type = varobj_get_type (var); |
73a93a32 | 2680 | new_type = varobj_get_type (tmp_var); |
72330bd6 | 2681 | if (strcmp (old_type, new_type) == 0) |
73a93a32 | 2682 | { |
fcacd99f VP |
2683 | /* The expression presently stored inside var->root->exp |
2684 | remembers the locations of local variables relatively to | |
2685 | the frame where the expression was created (in DWARF location | |
2686 | button, for example). Naturally, those locations are not | |
2687 | correct in other frames, so update the expression. */ | |
2688 | ||
2689 | struct expression *tmp_exp = var->root->exp; | |
a109c7c1 | 2690 | |
fcacd99f VP |
2691 | var->root->exp = tmp_var->root->exp; |
2692 | tmp_var->root->exp = tmp_exp; | |
2693 | ||
73a93a32 JI |
2694 | varobj_delete (tmp_var, NULL, 0); |
2695 | *type_changed = 0; | |
2696 | } | |
2697 | else | |
2698 | { | |
1b36a34b | 2699 | tmp_var->obj_name = xstrdup (var->obj_name); |
0cc7d26f TT |
2700 | tmp_var->from = var->from; |
2701 | tmp_var->to = var->to; | |
a5defcdc VP |
2702 | varobj_delete (var, NULL, 0); |
2703 | ||
73a93a32 JI |
2704 | install_variable (tmp_var); |
2705 | *var_handle = tmp_var; | |
705da579 | 2706 | var = *var_handle; |
73a93a32 JI |
2707 | *type_changed = 1; |
2708 | } | |
74dddad3 MS |
2709 | xfree (old_type); |
2710 | xfree (new_type); | |
73a93a32 JI |
2711 | } |
2712 | else | |
2713 | { | |
2714 | *type_changed = 0; | |
2715 | } | |
2716 | ||
7a290c40 JB |
2717 | { |
2718 | struct value *value; | |
2719 | ||
2720 | value = (*var->root->lang->value_of_root) (var_handle); | |
2721 | if (var->value == NULL || value == NULL) | |
2722 | { | |
2723 | /* For root varobj-s, a NULL value indicates a scoping issue. | |
2724 | So, nothing to do in terms of checking for mutations. */ | |
2725 | } | |
2726 | else if (varobj_value_has_mutated (var, value, value_type (value))) | |
2727 | { | |
2728 | /* The type has mutated, so the children are no longer valid. | |
2729 | Just delete them, and tell our caller that the type has | |
2730 | changed. */ | |
2731 | varobj_delete (var, NULL, 1 /* only_children */); | |
2732 | var->num_children = -1; | |
2733 | var->to = -1; | |
2734 | var->from = -1; | |
2735 | *type_changed = 1; | |
2736 | } | |
2737 | return value; | |
2738 | } | |
8b93c638 JM |
2739 | } |
2740 | ||
581e13c1 | 2741 | /* What is the ``struct value *'' for the INDEX'th child of PARENT? */ |
30b28db1 | 2742 | static struct value * |
fba45db2 | 2743 | value_of_child (struct varobj *parent, int index) |
8b93c638 | 2744 | { |
30b28db1 | 2745 | struct value *value; |
8b93c638 JM |
2746 | |
2747 | value = (*parent->root->lang->value_of_child) (parent, index); | |
2748 | ||
8b93c638 JM |
2749 | return value; |
2750 | } | |
2751 | ||
581e13c1 | 2752 | /* GDB already has a command called "value_of_variable". Sigh. */ |
8b93c638 | 2753 | static char * |
de051565 | 2754 | my_value_of_variable (struct varobj *var, enum varobj_display_formats format) |
8b93c638 | 2755 | { |
8756216b | 2756 | if (var->root->is_valid) |
0cc7d26f TT |
2757 | { |
2758 | if (var->pretty_printer) | |
2759 | return value_get_print_value (var->value, var->format, var); | |
2760 | return (*var->root->lang->value_of_variable) (var, format); | |
2761 | } | |
8756216b DP |
2762 | else |
2763 | return NULL; | |
8b93c638 JM |
2764 | } |
2765 | ||
85265413 | 2766 | static char * |
b6313243 | 2767 | value_get_print_value (struct value *value, enum varobj_display_formats format, |
d452c4bc | 2768 | struct varobj *var) |
85265413 | 2769 | { |
57e66780 | 2770 | struct ui_file *stb; |
621c8364 | 2771 | struct cleanup *old_chain; |
fbb8f299 | 2772 | gdb_byte *thevalue = NULL; |
79a45b7d | 2773 | struct value_print_options opts; |
be759fcf PM |
2774 | struct type *type = NULL; |
2775 | long len = 0; | |
2776 | char *encoding = NULL; | |
2777 | struct gdbarch *gdbarch = NULL; | |
3a182a69 JK |
2778 | /* Initialize it just to avoid a GCC false warning. */ |
2779 | CORE_ADDR str_addr = 0; | |
09ca9e2e | 2780 | int string_print = 0; |
57e66780 DJ |
2781 | |
2782 | if (value == NULL) | |
2783 | return NULL; | |
2784 | ||
621c8364 TT |
2785 | stb = mem_fileopen (); |
2786 | old_chain = make_cleanup_ui_file_delete (stb); | |
2787 | ||
be759fcf | 2788 | gdbarch = get_type_arch (value_type (value)); |
b6313243 TT |
2789 | #if HAVE_PYTHON |
2790 | { | |
d452c4bc UW |
2791 | PyObject *value_formatter = var->pretty_printer; |
2792 | ||
09ca9e2e TT |
2793 | varobj_ensure_python_env (var); |
2794 | ||
0cc7d26f | 2795 | if (value_formatter) |
b6313243 | 2796 | { |
0cc7d26f TT |
2797 | /* First check to see if we have any children at all. If so, |
2798 | we simply return {...}. */ | |
2799 | if (dynamic_varobj_has_child_method (var)) | |
621c8364 TT |
2800 | { |
2801 | do_cleanups (old_chain); | |
2802 | return xstrdup ("{...}"); | |
2803 | } | |
b6313243 | 2804 | |
0cc7d26f | 2805 | if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst)) |
b6313243 | 2806 | { |
0cc7d26f | 2807 | struct value *replacement; |
0cc7d26f TT |
2808 | PyObject *output = NULL; |
2809 | ||
0cc7d26f | 2810 | output = apply_varobj_pretty_printer (value_formatter, |
621c8364 TT |
2811 | &replacement, |
2812 | stb); | |
00bd41d6 PM |
2813 | |
2814 | /* If we have string like output ... */ | |
0cc7d26f TT |
2815 | if (output) |
2816 | { | |
09ca9e2e TT |
2817 | make_cleanup_py_decref (output); |
2818 | ||
00bd41d6 PM |
2819 | /* If this is a lazy string, extract it. For lazy |
2820 | strings we always print as a string, so set | |
2821 | string_print. */ | |
be759fcf | 2822 | if (gdbpy_is_lazy_string (output)) |
0cc7d26f | 2823 | { |
09ca9e2e TT |
2824 | gdbpy_extract_lazy_string (output, &str_addr, &type, |
2825 | &len, &encoding); | |
2826 | make_cleanup (free_current_contents, &encoding); | |
be759fcf PM |
2827 | string_print = 1; |
2828 | } | |
2829 | else | |
2830 | { | |
00bd41d6 PM |
2831 | /* If it is a regular (non-lazy) string, extract |
2832 | it and copy the contents into THEVALUE. If the | |
2833 | hint says to print it as a string, set | |
2834 | string_print. Otherwise just return the extracted | |
2835 | string as a value. */ | |
2836 | ||
be759fcf PM |
2837 | PyObject *py_str |
2838 | = python_string_to_target_python_string (output); | |
a109c7c1 | 2839 | |
be759fcf PM |
2840 | if (py_str) |
2841 | { | |
2842 | char *s = PyString_AsString (py_str); | |
00bd41d6 PM |
2843 | char *hint; |
2844 | ||
2845 | hint = gdbpy_get_display_hint (value_formatter); | |
2846 | if (hint) | |
2847 | { | |
2848 | if (!strcmp (hint, "string")) | |
2849 | string_print = 1; | |
2850 | xfree (hint); | |
2851 | } | |
a109c7c1 | 2852 | |
be759fcf PM |
2853 | len = PyString_Size (py_str); |
2854 | thevalue = xmemdup (s, len + 1, len + 1); | |
2855 | type = builtin_type (gdbarch)->builtin_char; | |
2856 | Py_DECREF (py_str); | |
09ca9e2e TT |
2857 | |
2858 | if (!string_print) | |
2859 | { | |
2860 | do_cleanups (old_chain); | |
2861 | return thevalue; | |
2862 | } | |
2863 | ||
2864 | make_cleanup (xfree, thevalue); | |
be759fcf | 2865 | } |
8dc78533 JK |
2866 | else |
2867 | gdbpy_print_stack (); | |
0cc7d26f | 2868 | } |
0cc7d26f | 2869 | } |
00bd41d6 PM |
2870 | /* If the printer returned a replacement value, set VALUE |
2871 | to REPLACEMENT. If there is not a replacement value, | |
2872 | just use the value passed to this function. */ | |
0cc7d26f TT |
2873 | if (replacement) |
2874 | value = replacement; | |
b6313243 | 2875 | } |
b6313243 | 2876 | } |
b6313243 TT |
2877 | } |
2878 | #endif | |
2879 | ||
79a45b7d TT |
2880 | get_formatted_print_options (&opts, format_code[(int) format]); |
2881 | opts.deref_ref = 0; | |
b6313243 | 2882 | opts.raw = 1; |
00bd41d6 PM |
2883 | |
2884 | /* If the THEVALUE has contents, it is a regular string. */ | |
b6313243 | 2885 | if (thevalue) |
09ca9e2e TT |
2886 | LA_PRINT_STRING (stb, type, thevalue, len, encoding, 0, &opts); |
2887 | else if (string_print) | |
00bd41d6 PM |
2888 | /* Otherwise, if string_print is set, and it is not a regular |
2889 | string, it is a lazy string. */ | |
09ca9e2e | 2890 | val_print_string (type, encoding, str_addr, len, stb, &opts); |
b6313243 | 2891 | else |
00bd41d6 | 2892 | /* All other cases. */ |
b6313243 | 2893 | common_val_print (value, stb, 0, &opts, current_language); |
00bd41d6 | 2894 | |
759ef836 | 2895 | thevalue = ui_file_xstrdup (stb, NULL); |
57e66780 | 2896 | |
85265413 NR |
2897 | do_cleanups (old_chain); |
2898 | return thevalue; | |
2899 | } | |
2900 | ||
340a7723 NR |
2901 | int |
2902 | varobj_editable_p (struct varobj *var) | |
2903 | { | |
2904 | struct type *type; | |
340a7723 NR |
2905 | |
2906 | if (!(var->root->is_valid && var->value && VALUE_LVAL (var->value))) | |
2907 | return 0; | |
2908 | ||
2909 | type = get_value_type (var); | |
2910 | ||
2911 | switch (TYPE_CODE (type)) | |
2912 | { | |
2913 | case TYPE_CODE_STRUCT: | |
2914 | case TYPE_CODE_UNION: | |
2915 | case TYPE_CODE_ARRAY: | |
2916 | case TYPE_CODE_FUNC: | |
2917 | case TYPE_CODE_METHOD: | |
2918 | return 0; | |
2919 | break; | |
2920 | ||
2921 | default: | |
2922 | return 1; | |
2923 | break; | |
2924 | } | |
2925 | } | |
2926 | ||
d32cafc7 | 2927 | /* Call VAR's value_is_changeable_p language-specific callback. */ |
acd65feb | 2928 | |
8b93c638 | 2929 | static int |
b2c2bd75 | 2930 | varobj_value_is_changeable_p (struct varobj *var) |
8b93c638 | 2931 | { |
d32cafc7 | 2932 | return var->root->lang->value_is_changeable_p (var); |
8b93c638 JM |
2933 | } |
2934 | ||
5a413362 VP |
2935 | /* Return 1 if that varobj is floating, that is is always evaluated in the |
2936 | selected frame, and not bound to thread/frame. Such variable objects | |
2937 | are created using '@' as frame specifier to -var-create. */ | |
2938 | int | |
2939 | varobj_floating_p (struct varobj *var) | |
2940 | { | |
2941 | return var->root->floating; | |
2942 | } | |
2943 | ||
2024f65a VP |
2944 | /* Given the value and the type of a variable object, |
2945 | adjust the value and type to those necessary | |
2946 | for getting children of the variable object. | |
2947 | This includes dereferencing top-level references | |
2948 | to all types and dereferencing pointers to | |
581e13c1 | 2949 | structures. |
2024f65a | 2950 | |
581e13c1 | 2951 | Both TYPE and *TYPE should be non-null. VALUE |
2024f65a VP |
2952 | can be null if we want to only translate type. |
2953 | *VALUE can be null as well -- if the parent | |
581e13c1 | 2954 | value is not known. |
02142340 VP |
2955 | |
2956 | If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1 | |
b6313243 | 2957 | depending on whether pointer was dereferenced |
02142340 | 2958 | in this function. */ |
2024f65a VP |
2959 | static void |
2960 | adjust_value_for_child_access (struct value **value, | |
02142340 VP |
2961 | struct type **type, |
2962 | int *was_ptr) | |
2024f65a VP |
2963 | { |
2964 | gdb_assert (type && *type); | |
2965 | ||
02142340 VP |
2966 | if (was_ptr) |
2967 | *was_ptr = 0; | |
2968 | ||
2024f65a VP |
2969 | *type = check_typedef (*type); |
2970 | ||
2971 | /* The type of value stored in varobj, that is passed | |
2972 | to us, is already supposed to be | |
2973 | reference-stripped. */ | |
2974 | ||
2975 | gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF); | |
2976 | ||
2977 | /* Pointers to structures are treated just like | |
2978 | structures when accessing children. Don't | |
2979 | dererences pointers to other types. */ | |
2980 | if (TYPE_CODE (*type) == TYPE_CODE_PTR) | |
2981 | { | |
2982 | struct type *target_type = get_target_type (*type); | |
2983 | if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT | |
2984 | || TYPE_CODE (target_type) == TYPE_CODE_UNION) | |
2985 | { | |
2986 | if (value && *value) | |
3f4178d6 | 2987 | { |
8e7b59a5 | 2988 | volatile struct gdb_exception except; |
a109c7c1 | 2989 | |
8e7b59a5 KS |
2990 | TRY_CATCH (except, RETURN_MASK_ERROR) |
2991 | { | |
2992 | *value = value_ind (*value); | |
2993 | } | |
2994 | ||
2995 | if (except.reason < 0) | |
3f4178d6 DJ |
2996 | *value = NULL; |
2997 | } | |
2024f65a | 2998 | *type = target_type; |
02142340 VP |
2999 | if (was_ptr) |
3000 | *was_ptr = 1; | |
2024f65a VP |
3001 | } |
3002 | } | |
3003 | ||
3004 | /* The 'get_target_type' function calls check_typedef on | |
3005 | result, so we can immediately check type code. No | |
3006 | need to call check_typedef here. */ | |
3007 | } | |
3008 | ||
d32cafc7 JB |
3009 | /* Implement the "value_is_changeable_p" varobj callback for most |
3010 | languages. */ | |
3011 | ||
3012 | static int | |
3013 | default_value_is_changeable_p (struct varobj *var) | |
3014 | { | |
3015 | int r; | |
3016 | struct type *type; | |
3017 | ||
3018 | if (CPLUS_FAKE_CHILD (var)) | |
3019 | return 0; | |
3020 | ||
3021 | type = get_value_type (var); | |
3022 | ||
3023 | switch (TYPE_CODE (type)) | |
3024 | { | |
3025 | case TYPE_CODE_STRUCT: | |
3026 | case TYPE_CODE_UNION: | |
3027 | case TYPE_CODE_ARRAY: | |
3028 | r = 0; | |
3029 | break; | |
3030 | ||
3031 | default: | |
3032 | r = 1; | |
3033 | } | |
3034 | ||
3035 | return r; | |
3036 | } | |
3037 | ||
8b93c638 | 3038 | /* C */ |
d32cafc7 | 3039 | |
8b93c638 | 3040 | static int |
fba45db2 | 3041 | c_number_of_children (struct varobj *var) |
8b93c638 | 3042 | { |
2024f65a VP |
3043 | struct type *type = get_value_type (var); |
3044 | int children = 0; | |
8b93c638 | 3045 | struct type *target; |
8b93c638 | 3046 | |
02142340 | 3047 | adjust_value_for_child_access (NULL, &type, NULL); |
8b93c638 | 3048 | target = get_target_type (type); |
8b93c638 JM |
3049 | |
3050 | switch (TYPE_CODE (type)) | |
3051 | { | |
3052 | case TYPE_CODE_ARRAY: | |
3053 | if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0 | |
d78df370 | 3054 | && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type)) |
8b93c638 JM |
3055 | children = TYPE_LENGTH (type) / TYPE_LENGTH (target); |
3056 | else | |
74a44383 DJ |
3057 | /* If we don't know how many elements there are, don't display |
3058 | any. */ | |
3059 | children = 0; | |
8b93c638 JM |
3060 | break; |
3061 | ||
3062 | case TYPE_CODE_STRUCT: | |
3063 | case TYPE_CODE_UNION: | |
3064 | children = TYPE_NFIELDS (type); | |
3065 | break; | |
3066 | ||
3067 | case TYPE_CODE_PTR: | |
581e13c1 | 3068 | /* The type here is a pointer to non-struct. Typically, pointers |
2024f65a VP |
3069 | have one child, except for function ptrs, which have no children, |
3070 | and except for void*, as we don't know what to show. | |
3071 | ||
0755e6c1 FN |
3072 | We can show char* so we allow it to be dereferenced. If you decide |
3073 | to test for it, please mind that a little magic is necessary to | |
3074 | properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and | |
581e13c1 | 3075 | TYPE_NAME == "char". */ |
2024f65a VP |
3076 | if (TYPE_CODE (target) == TYPE_CODE_FUNC |
3077 | || TYPE_CODE (target) == TYPE_CODE_VOID) | |
3078 | children = 0; | |
3079 | else | |
3080 | children = 1; | |
8b93c638 JM |
3081 | break; |
3082 | ||
3083 | default: | |
581e13c1 | 3084 | /* Other types have no children. */ |
8b93c638 JM |
3085 | break; |
3086 | } | |
3087 | ||
3088 | return children; | |
3089 | } | |
3090 | ||
3091 | static char * | |
fba45db2 | 3092 | c_name_of_variable (struct varobj *parent) |
8b93c638 | 3093 | { |
1b36a34b | 3094 | return xstrdup (parent->name); |
8b93c638 JM |
3095 | } |
3096 | ||
bbec2603 VP |
3097 | /* Return the value of element TYPE_INDEX of a structure |
3098 | value VALUE. VALUE's type should be a structure, | |
581e13c1 | 3099 | or union, or a typedef to struct/union. |
bbec2603 VP |
3100 | |
3101 | Returns NULL if getting the value fails. Never throws. */ | |
3102 | static struct value * | |
3103 | value_struct_element_index (struct value *value, int type_index) | |
8b93c638 | 3104 | { |
bbec2603 VP |
3105 | struct value *result = NULL; |
3106 | volatile struct gdb_exception e; | |
bbec2603 | 3107 | struct type *type = value_type (value); |
a109c7c1 | 3108 | |
bbec2603 VP |
3109 | type = check_typedef (type); |
3110 | ||
3111 | gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT | |
3112 | || TYPE_CODE (type) == TYPE_CODE_UNION); | |
8b93c638 | 3113 | |
bbec2603 VP |
3114 | TRY_CATCH (e, RETURN_MASK_ERROR) |
3115 | { | |
d6a843b5 | 3116 | if (field_is_static (&TYPE_FIELD (type, type_index))) |
bbec2603 VP |
3117 | result = value_static_field (type, type_index); |
3118 | else | |
3119 | result = value_primitive_field (value, 0, type_index, type); | |
3120 | } | |
3121 | if (e.reason < 0) | |
3122 | { | |
3123 | return NULL; | |
3124 | } | |
3125 | else | |
3126 | { | |
3127 | return result; | |
3128 | } | |
3129 | } | |
3130 | ||
3131 | /* Obtain the information about child INDEX of the variable | |
581e13c1 | 3132 | object PARENT. |
bbec2603 VP |
3133 | If CNAME is not null, sets *CNAME to the name of the child relative |
3134 | to the parent. | |
3135 | If CVALUE is not null, sets *CVALUE to the value of the child. | |
3136 | If CTYPE is not null, sets *CTYPE to the type of the child. | |
3137 | ||
3138 | If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding | |
3139 | information cannot be determined, set *CNAME, *CVALUE, or *CTYPE | |
3140 | to NULL. */ | |
3141 | static void | |
3142 | c_describe_child (struct varobj *parent, int index, | |
02142340 VP |
3143 | char **cname, struct value **cvalue, struct type **ctype, |
3144 | char **cfull_expression) | |
bbec2603 VP |
3145 | { |
3146 | struct value *value = parent->value; | |
2024f65a | 3147 | struct type *type = get_value_type (parent); |
02142340 VP |
3148 | char *parent_expression = NULL; |
3149 | int was_ptr; | |
8e7b59a5 | 3150 | volatile struct gdb_exception except; |
bbec2603 VP |
3151 | |
3152 | if (cname) | |
3153 | *cname = NULL; | |
3154 | if (cvalue) | |
3155 | *cvalue = NULL; | |
3156 | if (ctype) | |
3157 | *ctype = NULL; | |
02142340 VP |
3158 | if (cfull_expression) |
3159 | { | |
3160 | *cfull_expression = NULL; | |
85254831 | 3161 | parent_expression = varobj_get_path_expr (get_path_expr_parent (parent)); |
02142340 VP |
3162 | } |
3163 | adjust_value_for_child_access (&value, &type, &was_ptr); | |
bbec2603 | 3164 | |
8b93c638 JM |
3165 | switch (TYPE_CODE (type)) |
3166 | { | |
3167 | case TYPE_CODE_ARRAY: | |
bbec2603 | 3168 | if (cname) |
3e43a32a MS |
3169 | *cname |
3170 | = xstrdup (int_string (index | |
3171 | + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)), | |
3172 | 10, 1, 0, 0)); | |
bbec2603 VP |
3173 | |
3174 | if (cvalue && value) | |
3175 | { | |
3176 | int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)); | |
a109c7c1 | 3177 | |
8e7b59a5 KS |
3178 | TRY_CATCH (except, RETURN_MASK_ERROR) |
3179 | { | |
3180 | *cvalue = value_subscript (value, real_index); | |
3181 | } | |
bbec2603 VP |
3182 | } |
3183 | ||
3184 | if (ctype) | |
3185 | *ctype = get_target_type (type); | |
3186 | ||
02142340 | 3187 | if (cfull_expression) |
43bbcdc2 PH |
3188 | *cfull_expression = |
3189 | xstrprintf ("(%s)[%s]", parent_expression, | |
3190 | int_string (index | |
3191 | + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)), | |
3192 | 10, 1, 0, 0)); | |
02142340 VP |
3193 | |
3194 | ||
8b93c638 JM |
3195 | break; |
3196 | ||
3197 | case TYPE_CODE_STRUCT: | |
3198 | case TYPE_CODE_UNION: | |
85254831 | 3199 | { |
0d5cff50 | 3200 | const char *field_name; |
bbec2603 | 3201 | |
85254831 KS |
3202 | /* If the type is anonymous and the field has no name, |
3203 | set an appropriate name. */ | |
3204 | field_name = TYPE_FIELD_NAME (type, index); | |
3205 | if (field_name == NULL || *field_name == '\0') | |
3206 | { | |
3207 | if (cname) | |
3208 | { | |
3209 | if (TYPE_CODE (TYPE_FIELD_TYPE (type, index)) | |
3210 | == TYPE_CODE_STRUCT) | |
3211 | *cname = xstrdup (ANONYMOUS_STRUCT_NAME); | |
3212 | else | |
3213 | *cname = xstrdup (ANONYMOUS_UNION_NAME); | |
3214 | } | |
bbec2603 | 3215 | |
85254831 KS |
3216 | if (cfull_expression) |
3217 | *cfull_expression = xstrdup (""); | |
3218 | } | |
3219 | else | |
3220 | { | |
3221 | if (cname) | |
3222 | *cname = xstrdup (field_name); | |
bbec2603 | 3223 | |
85254831 KS |
3224 | if (cfull_expression) |
3225 | { | |
3226 | char *join = was_ptr ? "->" : "."; | |
a109c7c1 | 3227 | |
85254831 KS |
3228 | *cfull_expression = xstrprintf ("(%s)%s%s", parent_expression, |
3229 | join, field_name); | |
3230 | } | |
3231 | } | |
02142340 | 3232 | |
85254831 KS |
3233 | if (cvalue && value) |
3234 | { | |
3235 | /* For C, varobj index is the same as type index. */ | |
3236 | *cvalue = value_struct_element_index (value, index); | |
3237 | } | |
3238 | ||
3239 | if (ctype) | |
3240 | *ctype = TYPE_FIELD_TYPE (type, index); | |
3241 | } | |
8b93c638 JM |
3242 | break; |
3243 | ||
3244 | case TYPE_CODE_PTR: | |
bbec2603 VP |
3245 | if (cname) |
3246 | *cname = xstrprintf ("*%s", parent->name); | |
8b93c638 | 3247 | |
bbec2603 | 3248 | if (cvalue && value) |
3f4178d6 | 3249 | { |
8e7b59a5 KS |
3250 | TRY_CATCH (except, RETURN_MASK_ERROR) |
3251 | { | |
3252 | *cvalue = value_ind (value); | |
3253 | } | |
a109c7c1 | 3254 | |
8e7b59a5 | 3255 | if (except.reason < 0) |
3f4178d6 DJ |
3256 | *cvalue = NULL; |
3257 | } | |
bbec2603 | 3258 | |
2024f65a VP |
3259 | /* Don't use get_target_type because it calls |
3260 | check_typedef and here, we want to show the true | |
3261 | declared type of the variable. */ | |
bbec2603 | 3262 | if (ctype) |
2024f65a | 3263 | *ctype = TYPE_TARGET_TYPE (type); |
02142340 VP |
3264 | |
3265 | if (cfull_expression) | |
3266 | *cfull_expression = xstrprintf ("*(%s)", parent_expression); | |
bbec2603 | 3267 | |
8b93c638 JM |
3268 | break; |
3269 | ||
3270 | default: | |
581e13c1 | 3271 | /* This should not happen. */ |
bbec2603 VP |
3272 | if (cname) |
3273 | *cname = xstrdup ("???"); | |
02142340 VP |
3274 | if (cfull_expression) |
3275 | *cfull_expression = xstrdup ("???"); | |
581e13c1 | 3276 | /* Don't set value and type, we don't know then. */ |
8b93c638 | 3277 | } |
bbec2603 | 3278 | } |
8b93c638 | 3279 | |
bbec2603 VP |
3280 | static char * |
3281 | c_name_of_child (struct varobj *parent, int index) | |
3282 | { | |
3283 | char *name; | |
a109c7c1 | 3284 | |
02142340 | 3285 | c_describe_child (parent, index, &name, NULL, NULL, NULL); |
8b93c638 JM |
3286 | return name; |
3287 | } | |
3288 | ||
02142340 VP |
3289 | static char * |
3290 | c_path_expr_of_child (struct varobj *child) | |
3291 | { | |
3292 | c_describe_child (child->parent, child->index, NULL, NULL, NULL, | |
3293 | &child->path_expr); | |
3294 | return child->path_expr; | |
3295 | } | |
3296 | ||
c5b48eac VP |
3297 | /* If frame associated with VAR can be found, switch |
3298 | to it and return 1. Otherwise, return 0. */ | |
3299 | static int | |
3300 | check_scope (struct varobj *var) | |
3301 | { | |
3302 | struct frame_info *fi; | |
3303 | int scope; | |
3304 | ||
3305 | fi = frame_find_by_id (var->root->frame); | |
3306 | scope = fi != NULL; | |
3307 | ||
3308 | if (fi) | |
3309 | { | |
3310 | CORE_ADDR pc = get_frame_pc (fi); | |
a109c7c1 | 3311 | |
c5b48eac VP |
3312 | if (pc < BLOCK_START (var->root->valid_block) || |
3313 | pc >= BLOCK_END (var->root->valid_block)) | |
3314 | scope = 0; | |
3315 | else | |
3316 | select_frame (fi); | |
3317 | } | |
3318 | return scope; | |
3319 | } | |
3320 | ||
30b28db1 | 3321 | static struct value * |
fba45db2 | 3322 | c_value_of_root (struct varobj **var_handle) |
8b93c638 | 3323 | { |
5e572bb4 | 3324 | struct value *new_val = NULL; |
73a93a32 | 3325 | struct varobj *var = *var_handle; |
c5b48eac | 3326 | int within_scope = 0; |
6208b47d VP |
3327 | struct cleanup *back_to; |
3328 | ||
581e13c1 | 3329 | /* Only root variables can be updated... */ |
b2c2bd75 | 3330 | if (!is_root_p (var)) |
581e13c1 | 3331 | /* Not a root var. */ |
73a93a32 JI |
3332 | return NULL; |
3333 | ||
4f8d22e3 | 3334 | back_to = make_cleanup_restore_current_thread (); |
72330bd6 | 3335 | |
581e13c1 | 3336 | /* Determine whether the variable is still around. */ |
a5defcdc | 3337 | if (var->root->valid_block == NULL || var->root->floating) |
8b93c638 | 3338 | within_scope = 1; |
c5b48eac VP |
3339 | else if (var->root->thread_id == 0) |
3340 | { | |
3341 | /* The program was single-threaded when the variable object was | |
3342 | created. Technically, it's possible that the program became | |
3343 | multi-threaded since then, but we don't support such | |
3344 | scenario yet. */ | |
3345 | within_scope = check_scope (var); | |
3346 | } | |
8b93c638 JM |
3347 | else |
3348 | { | |
c5b48eac VP |
3349 | ptid_t ptid = thread_id_to_pid (var->root->thread_id); |
3350 | if (in_thread_list (ptid)) | |
d2353924 | 3351 | { |
c5b48eac VP |
3352 | switch_to_thread (ptid); |
3353 | within_scope = check_scope (var); | |
3354 | } | |
8b93c638 | 3355 | } |
72330bd6 | 3356 | |
8b93c638 JM |
3357 | if (within_scope) |
3358 | { | |
8e7b59a5 KS |
3359 | volatile struct gdb_exception except; |
3360 | ||
73a93a32 | 3361 | /* We need to catch errors here, because if evaluate |
85d93f1d | 3362 | expression fails we want to just return NULL. */ |
8e7b59a5 KS |
3363 | TRY_CATCH (except, RETURN_MASK_ERROR) |
3364 | { | |
3365 | new_val = evaluate_expression (var->root->exp); | |
3366 | } | |
3367 | ||
8b93c638 JM |
3368 | return new_val; |
3369 | } | |
3370 | ||
6208b47d VP |
3371 | do_cleanups (back_to); |
3372 | ||
8b93c638 JM |
3373 | return NULL; |
3374 | } | |
3375 | ||
30b28db1 | 3376 | static struct value * |
fba45db2 | 3377 | c_value_of_child (struct varobj *parent, int index) |
8b93c638 | 3378 | { |
bbec2603 | 3379 | struct value *value = NULL; |
8b93c638 | 3380 | |
a109c7c1 | 3381 | c_describe_child (parent, index, NULL, &value, NULL, NULL); |
8b93c638 JM |
3382 | return value; |
3383 | } | |
3384 | ||
3385 | static struct type * | |
fba45db2 | 3386 | c_type_of_child (struct varobj *parent, int index) |
8b93c638 | 3387 | { |
bbec2603 | 3388 | struct type *type = NULL; |
a109c7c1 | 3389 | |
02142340 | 3390 | c_describe_child (parent, index, NULL, NULL, &type, NULL); |
8b93c638 JM |
3391 | return type; |
3392 | } | |
3393 | ||
8b93c638 | 3394 | static char * |
de051565 | 3395 | c_value_of_variable (struct varobj *var, enum varobj_display_formats format) |
8b93c638 | 3396 | { |
14b3d9c9 JB |
3397 | /* BOGUS: if val_print sees a struct/class, or a reference to one, |
3398 | it will print out its children instead of "{...}". So we need to | |
3399 | catch that case explicitly. */ | |
3400 | struct type *type = get_type (var); | |
e64d9b3d | 3401 | |
581e13c1 | 3402 | /* Strip top-level references. */ |
14b3d9c9 JB |
3403 | while (TYPE_CODE (type) == TYPE_CODE_REF) |
3404 | type = check_typedef (TYPE_TARGET_TYPE (type)); | |
3405 | ||
3406 | switch (TYPE_CODE (type)) | |
8b93c638 JM |
3407 | { |
3408 | case TYPE_CODE_STRUCT: | |
3409 | case TYPE_CODE_UNION: | |
3410 | return xstrdup ("{...}"); | |
3411 | /* break; */ | |
3412 | ||
3413 | case TYPE_CODE_ARRAY: | |
3414 | { | |
e64d9b3d | 3415 | char *number; |
a109c7c1 | 3416 | |
b435e160 | 3417 | number = xstrprintf ("[%d]", var->num_children); |
e64d9b3d | 3418 | return (number); |
8b93c638 JM |
3419 | } |
3420 | /* break; */ | |
3421 | ||
3422 | default: | |
3423 | { | |
575bbeb6 KS |
3424 | if (var->value == NULL) |
3425 | { | |
3426 | /* This can happen if we attempt to get the value of a struct | |
581e13c1 MS |
3427 | member when the parent is an invalid pointer. This is an |
3428 | error condition, so we should tell the caller. */ | |
575bbeb6 KS |
3429 | return NULL; |
3430 | } | |
3431 | else | |
3432 | { | |
25d5ea92 VP |
3433 | if (var->not_fetched && value_lazy (var->value)) |
3434 | /* Frozen variable and no value yet. We don't | |
3435 | implicitly fetch the value. MI response will | |
3436 | use empty string for the value, which is OK. */ | |
3437 | return NULL; | |
3438 | ||
b2c2bd75 | 3439 | gdb_assert (varobj_value_is_changeable_p (var)); |
acd65feb | 3440 | gdb_assert (!value_lazy (var->value)); |
de051565 MK |
3441 | |
3442 | /* If the specified format is the current one, | |
581e13c1 | 3443 | we can reuse print_value. */ |
de051565 MK |
3444 | if (format == var->format) |
3445 | return xstrdup (var->print_value); | |
3446 | else | |
d452c4bc | 3447 | return value_get_print_value (var->value, format, var); |
85265413 | 3448 | } |
e64d9b3d | 3449 | } |
8b93c638 JM |
3450 | } |
3451 | } | |
3452 | \f | |
3453 | ||
3454 | /* C++ */ | |
3455 | ||
3456 | static int | |
fba45db2 | 3457 | cplus_number_of_children (struct varobj *var) |
8b93c638 JM |
3458 | { |
3459 | struct type *type; | |
3460 | int children, dont_know; | |
3461 | ||
3462 | dont_know = 1; | |
3463 | children = 0; | |
3464 | ||
3465 | if (!CPLUS_FAKE_CHILD (var)) | |
3466 | { | |
2024f65a | 3467 | type = get_value_type (var); |
02142340 | 3468 | adjust_value_for_child_access (NULL, &type, NULL); |
8b93c638 JM |
3469 | |
3470 | if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) || | |
72330bd6 | 3471 | ((TYPE_CODE (type)) == TYPE_CODE_UNION)) |
8b93c638 JM |
3472 | { |
3473 | int kids[3]; | |
3474 | ||
3475 | cplus_class_num_children (type, kids); | |
3476 | if (kids[v_public] != 0) | |
3477 | children++; | |
3478 | if (kids[v_private] != 0) | |
3479 | children++; | |
3480 | if (kids[v_protected] != 0) | |
3481 | children++; | |
3482 | ||
581e13c1 | 3483 | /* Add any baseclasses. */ |
8b93c638 JM |
3484 | children += TYPE_N_BASECLASSES (type); |
3485 | dont_know = 0; | |
3486 | ||
581e13c1 | 3487 | /* FIXME: save children in var. */ |
8b93c638 JM |
3488 | } |
3489 | } | |
3490 | else | |
3491 | { | |
3492 | int kids[3]; | |
3493 | ||
2024f65a | 3494 | type = get_value_type (var->parent); |
02142340 | 3495 | adjust_value_for_child_access (NULL, &type, NULL); |
8b93c638 JM |
3496 | |
3497 | cplus_class_num_children (type, kids); | |
6e382aa3 | 3498 | if (strcmp (var->name, "public") == 0) |
8b93c638 | 3499 | children = kids[v_public]; |
6e382aa3 | 3500 | else if (strcmp (var->name, "private") == 0) |
8b93c638 JM |
3501 | children = kids[v_private]; |
3502 | else | |
3503 | children = kids[v_protected]; | |
3504 | dont_know = 0; | |
3505 | } | |
3506 | ||
3507 | if (dont_know) | |
3508 | children = c_number_of_children (var); | |
3509 | ||
3510 | return children; | |
3511 | } | |
3512 | ||
3513 | /* Compute # of public, private, and protected variables in this class. | |
3514 | That means we need to descend into all baseclasses and find out | |
581e13c1 | 3515 | how many are there, too. */ |
8b93c638 | 3516 | static void |
1669605f | 3517 | cplus_class_num_children (struct type *type, int children[3]) |
8b93c638 | 3518 | { |
d48cc9dd DJ |
3519 | int i, vptr_fieldno; |
3520 | struct type *basetype = NULL; | |
8b93c638 JM |
3521 | |
3522 | children[v_public] = 0; | |
3523 | children[v_private] = 0; | |
3524 | children[v_protected] = 0; | |
3525 | ||
d48cc9dd | 3526 | vptr_fieldno = get_vptr_fieldno (type, &basetype); |
8b93c638 JM |
3527 | for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++) |
3528 | { | |
d48cc9dd DJ |
3529 | /* If we have a virtual table pointer, omit it. Even if virtual |
3530 | table pointers are not specifically marked in the debug info, | |
3531 | they should be artificial. */ | |
3532 | if ((type == basetype && i == vptr_fieldno) | |
3533 | || TYPE_FIELD_ARTIFICIAL (type, i)) | |
8b93c638 JM |
3534 | continue; |
3535 | ||
3536 | if (TYPE_FIELD_PROTECTED (type, i)) | |
3537 | children[v_protected]++; | |
3538 | else if (TYPE_FIELD_PRIVATE (type, i)) | |
3539 | children[v_private]++; | |
3540 | else | |
3541 | children[v_public]++; | |
3542 | } | |
3543 | } | |
3544 | ||
3545 | static char * | |
fba45db2 | 3546 | cplus_name_of_variable (struct varobj *parent) |
8b93c638 JM |
3547 | { |
3548 | return c_name_of_variable (parent); | |
3549 | } | |
3550 | ||
2024f65a VP |
3551 | enum accessibility { private_field, protected_field, public_field }; |
3552 | ||
3553 | /* Check if field INDEX of TYPE has the specified accessibility. | |
3554 | Return 0 if so and 1 otherwise. */ | |
3555 | static int | |
3556 | match_accessibility (struct type *type, int index, enum accessibility acc) | |
8b93c638 | 3557 | { |
2024f65a VP |
3558 | if (acc == private_field && TYPE_FIELD_PRIVATE (type, index)) |
3559 | return 1; | |
3560 | else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index)) | |
3561 | return 1; | |
3562 | else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index) | |
3563 | && !TYPE_FIELD_PROTECTED (type, index)) | |
3564 | return 1; | |
3565 | else | |
3566 | return 0; | |
3567 | } | |
3568 | ||
3569 | static void | |
3570 | cplus_describe_child (struct varobj *parent, int index, | |
02142340 VP |
3571 | char **cname, struct value **cvalue, struct type **ctype, |
3572 | char **cfull_expression) | |
2024f65a | 3573 | { |
2024f65a | 3574 | struct value *value; |
8b93c638 | 3575 | struct type *type; |
02142340 VP |
3576 | int was_ptr; |
3577 | char *parent_expression = NULL; | |
8b93c638 | 3578 | |
2024f65a VP |
3579 | if (cname) |
3580 | *cname = NULL; | |
3581 | if (cvalue) | |
3582 | *cvalue = NULL; | |
3583 | if (ctype) | |
3584 | *ctype = NULL; | |
02142340 VP |
3585 | if (cfull_expression) |
3586 | *cfull_expression = NULL; | |
2024f65a | 3587 | |
8b93c638 JM |
3588 | if (CPLUS_FAKE_CHILD (parent)) |
3589 | { | |
2024f65a VP |
3590 | value = parent->parent->value; |
3591 | type = get_value_type (parent->parent); | |
02142340 | 3592 | if (cfull_expression) |
85254831 KS |
3593 | parent_expression |
3594 | = varobj_get_path_expr (get_path_expr_parent (parent->parent)); | |
8b93c638 JM |
3595 | } |
3596 | else | |
2024f65a VP |
3597 | { |
3598 | value = parent->value; | |
3599 | type = get_value_type (parent); | |
02142340 | 3600 | if (cfull_expression) |
85254831 KS |
3601 | parent_expression |
3602 | = varobj_get_path_expr (get_path_expr_parent (parent)); | |
2024f65a | 3603 | } |
8b93c638 | 3604 | |
02142340 | 3605 | adjust_value_for_child_access (&value, &type, &was_ptr); |
2024f65a VP |
3606 | |
3607 | if (TYPE_CODE (type) == TYPE_CODE_STRUCT | |
3f4178d6 | 3608 | || TYPE_CODE (type) == TYPE_CODE_UNION) |
8b93c638 | 3609 | { |
02142340 | 3610 | char *join = was_ptr ? "->" : "."; |
a109c7c1 | 3611 | |
8b93c638 JM |
3612 | if (CPLUS_FAKE_CHILD (parent)) |
3613 | { | |
6e382aa3 JJ |
3614 | /* The fields of the class type are ordered as they |
3615 | appear in the class. We are given an index for a | |
3616 | particular access control type ("public","protected", | |
3617 | or "private"). We must skip over fields that don't | |
3618 | have the access control we are looking for to properly | |
581e13c1 | 3619 | find the indexed field. */ |
6e382aa3 | 3620 | int type_index = TYPE_N_BASECLASSES (type); |
2024f65a | 3621 | enum accessibility acc = public_field; |
d48cc9dd DJ |
3622 | int vptr_fieldno; |
3623 | struct type *basetype = NULL; | |
0d5cff50 | 3624 | const char *field_name; |
d48cc9dd DJ |
3625 | |
3626 | vptr_fieldno = get_vptr_fieldno (type, &basetype); | |
6e382aa3 | 3627 | if (strcmp (parent->name, "private") == 0) |
2024f65a | 3628 | acc = private_field; |
6e382aa3 | 3629 | else if (strcmp (parent->name, "protected") == 0) |
2024f65a VP |
3630 | acc = protected_field; |
3631 | ||
3632 | while (index >= 0) | |
6e382aa3 | 3633 | { |
d48cc9dd DJ |
3634 | if ((type == basetype && type_index == vptr_fieldno) |
3635 | || TYPE_FIELD_ARTIFICIAL (type, type_index)) | |
2024f65a VP |
3636 | ; /* ignore vptr */ |
3637 | else if (match_accessibility (type, type_index, acc)) | |
6e382aa3 JJ |
3638 | --index; |
3639 | ++type_index; | |
6e382aa3 | 3640 | } |
2024f65a VP |
3641 | --type_index; |
3642 | ||
85254831 KS |
3643 | /* If the type is anonymous and the field has no name, |
3644 | set an appopriate name. */ | |
3645 | field_name = TYPE_FIELD_NAME (type, type_index); | |
3646 | if (field_name == NULL || *field_name == '\0') | |
3647 | { | |
3648 | if (cname) | |
3649 | { | |
3650 | if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index)) | |
3651 | == TYPE_CODE_STRUCT) | |
3652 | *cname = xstrdup (ANONYMOUS_STRUCT_NAME); | |
3653 | else if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index)) | |
3654 | == TYPE_CODE_UNION) | |
3655 | *cname = xstrdup (ANONYMOUS_UNION_NAME); | |
3656 | } | |
3657 | ||
3658 | if (cfull_expression) | |
3659 | *cfull_expression = xstrdup (""); | |
3660 | } | |
3661 | else | |
3662 | { | |
3663 | if (cname) | |
3664 | *cname = xstrdup (TYPE_FIELD_NAME (type, type_index)); | |
3665 | ||
3666 | if (cfull_expression) | |
3667 | *cfull_expression | |
3668 | = xstrprintf ("((%s)%s%s)", parent_expression, join, | |
3669 | field_name); | |
3670 | } | |
2024f65a VP |
3671 | |
3672 | if (cvalue && value) | |
3673 | *cvalue = value_struct_element_index (value, type_index); | |
3674 | ||
3675 | if (ctype) | |
3676 | *ctype = TYPE_FIELD_TYPE (type, type_index); | |
3677 | } | |
3678 | else if (index < TYPE_N_BASECLASSES (type)) | |
3679 | { | |
3680 | /* This is a baseclass. */ | |
3681 | if (cname) | |
3682 | *cname = xstrdup (TYPE_FIELD_NAME (type, index)); | |
3683 | ||
3684 | if (cvalue && value) | |
0cc7d26f | 3685 | *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value); |
6e382aa3 | 3686 | |
2024f65a VP |
3687 | if (ctype) |
3688 | { | |
3689 | *ctype = TYPE_FIELD_TYPE (type, index); | |
3690 | } | |
02142340 VP |
3691 | |
3692 | if (cfull_expression) | |
3693 | { | |
3694 | char *ptr = was_ptr ? "*" : ""; | |
a109c7c1 | 3695 | |
581e13c1 | 3696 | /* Cast the parent to the base' type. Note that in gdb, |
02142340 VP |
3697 | expression like |
3698 | (Base1)d | |
3699 | will create an lvalue, for all appearences, so we don't | |
3700 | need to use more fancy: | |
3701 | *(Base1*)(&d) | |
0d932b2f MK |
3702 | construct. |
3703 | ||
3704 | When we are in the scope of the base class or of one | |
3705 | of its children, the type field name will be interpreted | |
3706 | as a constructor, if it exists. Therefore, we must | |
3707 | indicate that the name is a class name by using the | |
3708 | 'class' keyword. See PR mi/11912 */ | |
3709 | *cfull_expression = xstrprintf ("(%s(class %s%s) %s)", | |
02142340 VP |
3710 | ptr, |
3711 | TYPE_FIELD_NAME (type, index), | |
3712 | ptr, | |
3713 | parent_expression); | |
3714 | } | |
8b93c638 | 3715 | } |
8b93c638 JM |
3716 | else |
3717 | { | |
348144ba | 3718 | char *access = NULL; |
6e382aa3 | 3719 | int children[3]; |
a109c7c1 | 3720 | |
2024f65a | 3721 | cplus_class_num_children (type, children); |
6e382aa3 | 3722 | |
8b93c638 | 3723 | /* Everything beyond the baseclasses can |
6e382aa3 JJ |
3724 | only be "public", "private", or "protected" |
3725 | ||
3726 | The special "fake" children are always output by varobj in | |
581e13c1 | 3727 | this order. So if INDEX == 2, it MUST be "protected". */ |
8b93c638 JM |
3728 | index -= TYPE_N_BASECLASSES (type); |
3729 | switch (index) | |
3730 | { | |
3731 | case 0: | |
6e382aa3 | 3732 | if (children[v_public] > 0) |
2024f65a | 3733 | access = "public"; |
6e382aa3 | 3734 | else if (children[v_private] > 0) |
2024f65a | 3735 | access = "private"; |
6e382aa3 | 3736 | else |
2024f65a | 3737 | access = "protected"; |
6e382aa3 | 3738 | break; |
8b93c638 | 3739 | case 1: |
6e382aa3 | 3740 | if (children[v_public] > 0) |
8b93c638 | 3741 | { |
6e382aa3 | 3742 | if (children[v_private] > 0) |
2024f65a | 3743 | access = "private"; |
6e382aa3 | 3744 | else |
2024f65a | 3745 | access = "protected"; |
8b93c638 | 3746 | } |
6e382aa3 | 3747 | else if (children[v_private] > 0) |
2024f65a | 3748 | access = "protected"; |
6e382aa3 | 3749 | break; |
8b93c638 | 3750 | case 2: |
581e13c1 | 3751 | /* Must be protected. */ |
2024f65a | 3752 | access = "protected"; |
6e382aa3 | 3753 | break; |
8b93c638 | 3754 | default: |
581e13c1 | 3755 | /* error! */ |
8b93c638 JM |
3756 | break; |
3757 | } | |
348144ba MS |
3758 | |
3759 | gdb_assert (access); | |
2024f65a VP |
3760 | if (cname) |
3761 | *cname = xstrdup (access); | |
8b93c638 | 3762 | |
02142340 | 3763 | /* Value and type and full expression are null here. */ |
2024f65a | 3764 | } |
8b93c638 | 3765 | } |
8b93c638 JM |
3766 | else |
3767 | { | |
02142340 | 3768 | c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression); |
2024f65a VP |
3769 | } |
3770 | } | |
8b93c638 | 3771 | |
2024f65a VP |
3772 | static char * |
3773 | cplus_name_of_child (struct varobj *parent, int index) | |
3774 | { | |
3775 | char *name = NULL; | |
a109c7c1 | 3776 | |
02142340 | 3777 | cplus_describe_child (parent, index, &name, NULL, NULL, NULL); |
8b93c638 JM |
3778 | return name; |
3779 | } | |
3780 | ||
02142340 VP |
3781 | static char * |
3782 | cplus_path_expr_of_child (struct varobj *child) | |
3783 | { | |
3784 | cplus_describe_child (child->parent, child->index, NULL, NULL, NULL, | |
3785 | &child->path_expr); | |
3786 | return child->path_expr; | |
3787 | } | |
3788 | ||
30b28db1 | 3789 | static struct value * |
fba45db2 | 3790 | cplus_value_of_root (struct varobj **var_handle) |
8b93c638 | 3791 | { |
73a93a32 | 3792 | return c_value_of_root (var_handle); |
8b93c638 JM |
3793 | } |
3794 | ||
30b28db1 | 3795 | static struct value * |
fba45db2 | 3796 | cplus_value_of_child (struct varobj *parent, int index) |
8b93c638 | 3797 | { |
2024f65a | 3798 | struct value *value = NULL; |
a109c7c1 | 3799 | |
02142340 | 3800 | cplus_describe_child (parent, index, NULL, &value, NULL, NULL); |
8b93c638 JM |
3801 | return value; |
3802 | } | |
3803 | ||
3804 | static struct type * | |
fba45db2 | 3805 | cplus_type_of_child (struct varobj *parent, int index) |
8b93c638 | 3806 | { |
2024f65a | 3807 | struct type *type = NULL; |
a109c7c1 | 3808 | |
02142340 | 3809 | cplus_describe_child (parent, index, NULL, NULL, &type, NULL); |
8b93c638 JM |
3810 | return type; |
3811 | } | |
3812 | ||
8b93c638 | 3813 | static char * |
a109c7c1 MS |
3814 | cplus_value_of_variable (struct varobj *var, |
3815 | enum varobj_display_formats format) | |
8b93c638 JM |
3816 | { |
3817 | ||
3818 | /* If we have one of our special types, don't print out | |
581e13c1 | 3819 | any value. */ |
8b93c638 JM |
3820 | if (CPLUS_FAKE_CHILD (var)) |
3821 | return xstrdup (""); | |
3822 | ||
de051565 | 3823 | return c_value_of_variable (var, format); |
8b93c638 JM |
3824 | } |
3825 | \f | |
3826 | /* Java */ | |
3827 | ||
3828 | static int | |
fba45db2 | 3829 | java_number_of_children (struct varobj *var) |
8b93c638 JM |
3830 | { |
3831 | return cplus_number_of_children (var); | |
3832 | } | |
3833 | ||
3834 | static char * | |
fba45db2 | 3835 | java_name_of_variable (struct varobj *parent) |
8b93c638 JM |
3836 | { |
3837 | char *p, *name; | |
3838 | ||
3839 | name = cplus_name_of_variable (parent); | |
3840 | /* If the name has "-" in it, it is because we | |
581e13c1 | 3841 | needed to escape periods in the name... */ |
8b93c638 JM |
3842 | p = name; |
3843 | ||
3844 | while (*p != '\000') | |
3845 | { | |
3846 | if (*p == '-') | |
3847 | *p = '.'; | |
3848 | p++; | |
3849 | } | |
3850 | ||
3851 | return name; | |
3852 | } | |
3853 | ||
3854 | static char * | |
fba45db2 | 3855 | java_name_of_child (struct varobj *parent, int index) |
8b93c638 JM |
3856 | { |
3857 | char *name, *p; | |
3858 | ||
3859 | name = cplus_name_of_child (parent, index); | |
581e13c1 | 3860 | /* Escape any periods in the name... */ |
8b93c638 JM |
3861 | p = name; |
3862 | ||
3863 | while (*p != '\000') | |
3864 | { | |
3865 | if (*p == '.') | |
3866 | *p = '-'; | |
3867 | p++; | |
3868 | } | |
3869 | ||
3870 | return name; | |
3871 | } | |
3872 | ||
02142340 VP |
3873 | static char * |
3874 | java_path_expr_of_child (struct varobj *child) | |
3875 | { | |
3876 | return NULL; | |
3877 | } | |
3878 | ||
30b28db1 | 3879 | static struct value * |
fba45db2 | 3880 | java_value_of_root (struct varobj **var_handle) |
8b93c638 | 3881 | { |
73a93a32 | 3882 | return cplus_value_of_root (var_handle); |
8b93c638 JM |
3883 | } |
3884 | ||
30b28db1 | 3885 | static struct value * |
fba45db2 | 3886 | java_value_of_child (struct varobj *parent, int index) |
8b93c638 JM |
3887 | { |
3888 | return cplus_value_of_child (parent, index); | |
3889 | } | |
3890 | ||
3891 | static struct type * | |
fba45db2 | 3892 | java_type_of_child (struct varobj *parent, int index) |
8b93c638 JM |
3893 | { |
3894 | return cplus_type_of_child (parent, index); | |
3895 | } | |
3896 | ||
8b93c638 | 3897 | static char * |
de051565 | 3898 | java_value_of_variable (struct varobj *var, enum varobj_display_formats format) |
8b93c638 | 3899 | { |
de051565 | 3900 | return cplus_value_of_variable (var, format); |
8b93c638 | 3901 | } |
54333c3b | 3902 | |
40591b7d JCD |
3903 | /* Ada specific callbacks for VAROBJs. */ |
3904 | ||
3905 | static int | |
3906 | ada_number_of_children (struct varobj *var) | |
3907 | { | |
181875a4 | 3908 | return ada_varobj_get_number_of_children (var->value, var->type); |
40591b7d JCD |
3909 | } |
3910 | ||
3911 | static char * | |
3912 | ada_name_of_variable (struct varobj *parent) | |
3913 | { | |
3914 | return c_name_of_variable (parent); | |
3915 | } | |
3916 | ||
3917 | static char * | |
3918 | ada_name_of_child (struct varobj *parent, int index) | |
3919 | { | |
181875a4 JB |
3920 | return ada_varobj_get_name_of_child (parent->value, parent->type, |
3921 | parent->name, index); | |
40591b7d JCD |
3922 | } |
3923 | ||
3924 | static char* | |
3925 | ada_path_expr_of_child (struct varobj *child) | |
3926 | { | |
181875a4 JB |
3927 | struct varobj *parent = child->parent; |
3928 | const char *parent_path_expr = varobj_get_path_expr (parent); | |
3929 | ||
3930 | return ada_varobj_get_path_expr_of_child (parent->value, | |
3931 | parent->type, | |
3932 | parent->name, | |
3933 | parent_path_expr, | |
3934 | child->index); | |
40591b7d JCD |
3935 | } |
3936 | ||
3937 | static struct value * | |
3938 | ada_value_of_root (struct varobj **var_handle) | |
3939 | { | |
3940 | return c_value_of_root (var_handle); | |
3941 | } | |
3942 | ||
3943 | static struct value * | |
3944 | ada_value_of_child (struct varobj *parent, int index) | |
3945 | { | |
181875a4 JB |
3946 | return ada_varobj_get_value_of_child (parent->value, parent->type, |
3947 | parent->name, index); | |
40591b7d JCD |
3948 | } |
3949 | ||
3950 | static struct type * | |
3951 | ada_type_of_child (struct varobj *parent, int index) | |
3952 | { | |
181875a4 JB |
3953 | return ada_varobj_get_type_of_child (parent->value, parent->type, |
3954 | index); | |
40591b7d JCD |
3955 | } |
3956 | ||
3957 | static char * | |
3958 | ada_value_of_variable (struct varobj *var, enum varobj_display_formats format) | |
3959 | { | |
181875a4 JB |
3960 | struct value_print_options opts; |
3961 | ||
3962 | get_formatted_print_options (&opts, format_code[(int) format]); | |
3963 | opts.deref_ref = 0; | |
3964 | opts.raw = 1; | |
3965 | ||
3966 | return ada_varobj_get_value_of_variable (var->value, var->type, &opts); | |
40591b7d JCD |
3967 | } |
3968 | ||
d32cafc7 JB |
3969 | /* Implement the "value_is_changeable_p" routine for Ada. */ |
3970 | ||
3971 | static int | |
3972 | ada_value_is_changeable_p (struct varobj *var) | |
3973 | { | |
3974 | struct type *type = var->value ? value_type (var->value) : var->type; | |
3975 | ||
3976 | if (ada_is_array_descriptor_type (type) | |
3977 | && TYPE_CODE (type) == TYPE_CODE_TYPEDEF) | |
3978 | { | |
3979 | /* This is in reality a pointer to an unconstrained array. | |
3980 | its value is changeable. */ | |
3981 | return 1; | |
3982 | } | |
3983 | ||
3984 | if (ada_is_string_type (type)) | |
3985 | { | |
3986 | /* We display the contents of the string in the array's | |
3987 | "value" field. The contents can change, so consider | |
3988 | that the array is changeable. */ | |
3989 | return 1; | |
3990 | } | |
3991 | ||
3992 | return default_value_is_changeable_p (var); | |
3993 | } | |
3994 | ||
7a290c40 JB |
3995 | /* Implement the "value_has_mutated" routine for Ada. */ |
3996 | ||
3997 | static int | |
3998 | ada_value_has_mutated (struct varobj *var, struct value *new_val, | |
3999 | struct type *new_type) | |
4000 | { | |
181875a4 JB |
4001 | int i; |
4002 | int from = -1; | |
4003 | int to = -1; | |
4004 | ||
4005 | /* If the number of fields have changed, then for sure the type | |
4006 | has mutated. */ | |
4007 | if (ada_varobj_get_number_of_children (new_val, new_type) | |
4008 | != var->num_children) | |
4009 | return 1; | |
4010 | ||
4011 | /* If the number of fields have remained the same, then we need | |
4012 | to check the name of each field. If they remain the same, | |
4013 | then chances are the type hasn't mutated. This is technically | |
4014 | an incomplete test, as the child's type might have changed | |
4015 | despite the fact that the name remains the same. But we'll | |
4016 | handle this situation by saying that the child has mutated, | |
4017 | not this value. | |
4018 | ||
4019 | If only part (or none!) of the children have been fetched, | |
4020 | then only check the ones we fetched. It does not matter | |
4021 | to the frontend whether a child that it has not fetched yet | |
4022 | has mutated or not. So just assume it hasn't. */ | |
4023 | ||
4024 | restrict_range (var->children, &from, &to); | |
4025 | for (i = from; i < to; i++) | |
4026 | if (strcmp (ada_varobj_get_name_of_child (new_val, new_type, | |
4027 | var->name, i), | |
4028 | VEC_index (varobj_p, var->children, i)->name) != 0) | |
4029 | return 1; | |
4030 | ||
7a290c40 JB |
4031 | return 0; |
4032 | } | |
4033 | ||
54333c3b JK |
4034 | /* Iterate all the existing _root_ VAROBJs and call the FUNC callback for them |
4035 | with an arbitrary caller supplied DATA pointer. */ | |
4036 | ||
4037 | void | |
4038 | all_root_varobjs (void (*func) (struct varobj *var, void *data), void *data) | |
4039 | { | |
4040 | struct varobj_root *var_root, *var_root_next; | |
4041 | ||
4042 | /* Iterate "safely" - handle if the callee deletes its passed VAROBJ. */ | |
4043 | ||
4044 | for (var_root = rootlist; var_root != NULL; var_root = var_root_next) | |
4045 | { | |
4046 | var_root_next = var_root->next; | |
4047 | ||
4048 | (*func) (var_root->rootvar, data); | |
4049 | } | |
4050 | } | |
8b93c638 JM |
4051 | \f |
4052 | extern void _initialize_varobj (void); | |
4053 | void | |
4054 | _initialize_varobj (void) | |
4055 | { | |
4056 | int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE; | |
4057 | ||
4058 | varobj_table = xmalloc (sizeof_table); | |
4059 | memset (varobj_table, 0, sizeof_table); | |
4060 | ||
85c07804 | 4061 | add_setshow_zinteger_cmd ("debugvarobj", class_maintenance, |
3e43a32a MS |
4062 | &varobjdebug, |
4063 | _("Set varobj debugging."), | |
4064 | _("Show varobj debugging."), | |
4065 | _("When non-zero, varobj debugging is enabled."), | |
4066 | NULL, show_varobjdebug, | |
85c07804 | 4067 | &setlist, &showlist); |
8b93c638 | 4068 | } |
8756216b | 4069 | |
54333c3b JK |
4070 | /* Invalidate varobj VAR if it is tied to locals and re-create it if it is |
4071 | defined on globals. It is a helper for varobj_invalidate. */ | |
2dbd25e5 | 4072 | |
54333c3b JK |
4073 | static void |
4074 | varobj_invalidate_iter (struct varobj *var, void *unused) | |
8756216b | 4075 | { |
54333c3b JK |
4076 | /* Floating varobjs are reparsed on each stop, so we don't care if the |
4077 | presently parsed expression refers to something that's gone. */ | |
4078 | if (var->root->floating) | |
4079 | return; | |
8756216b | 4080 | |
54333c3b JK |
4081 | /* global var must be re-evaluated. */ |
4082 | if (var->root->valid_block == NULL) | |
2dbd25e5 | 4083 | { |
54333c3b | 4084 | struct varobj *tmp_var; |
2dbd25e5 | 4085 | |
54333c3b JK |
4086 | /* Try to create a varobj with same expression. If we succeed |
4087 | replace the old varobj, otherwise invalidate it. */ | |
4088 | tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0, | |
4089 | USE_CURRENT_FRAME); | |
4090 | if (tmp_var != NULL) | |
4091 | { | |
4092 | tmp_var->obj_name = xstrdup (var->obj_name); | |
4093 | varobj_delete (var, NULL, 0); | |
4094 | install_variable (tmp_var); | |
2dbd25e5 | 4095 | } |
54333c3b JK |
4096 | else |
4097 | var->root->is_valid = 0; | |
2dbd25e5 | 4098 | } |
54333c3b JK |
4099 | else /* locals must be invalidated. */ |
4100 | var->root->is_valid = 0; | |
4101 | } | |
4102 | ||
4103 | /* Invalidate the varobjs that are tied to locals and re-create the ones that | |
4104 | are defined on globals. | |
4105 | Invalidated varobjs will be always printed in_scope="invalid". */ | |
4106 | ||
4107 | void | |
4108 | varobj_invalidate (void) | |
4109 | { | |
4110 | all_root_varobjs (varobj_invalidate_iter, NULL); | |
8756216b | 4111 | } |