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