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