Commit | Line | Data |
---|---|---|
8b93c638 JM |
1 | /* Implementation of the GDB variable objects API. |
2 | Copyright 1999, 2000 Free Software Foundation, Inc. | |
3 | ||
4 | This program is free software; you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation; either version 2 of the License, or | |
7 | (at your option) any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program; if not, write to the Free Software | |
16 | Foundation, Inc., 59 Temple Place - Suite 330, | |
17 | Boston, MA 02111-1307, USA. */ | |
18 | ||
19 | #include "defs.h" | |
20 | #include "value.h" | |
21 | #include "expression.h" | |
22 | #include "frame.h" | |
23 | #include "valprint.h" | |
24 | #include "language.h" | |
25 | #include "wrapper.h" | |
26 | #include "gdbcmd.h" | |
27 | #include <math.h> | |
28 | ||
29 | #include "varobj.h" | |
30 | ||
31 | /* Non-zero if we want to see trace of varobj level stuff. */ | |
32 | ||
33 | int varobjdebug = 0; | |
34 | ||
35 | /* String representations of gdb's format codes */ | |
36 | char *varobj_format_string[] = | |
37 | {"natural", "binary", "decimal", "hexadecimal", "octal"}; | |
38 | ||
39 | /* String representations of gdb's known languages */ | |
40 | char *varobj_language_string[] = | |
41 | {"unknown", "C", "C++", "Java"}; | |
42 | ||
43 | /* Data structures */ | |
44 | ||
45 | /* Every root variable has one of these structures saved in its | |
46 | varobj. Members which must be free'd are noted. */ | |
47 | struct varobj_root | |
48 | { | |
49 | ||
50 | /* Alloc'd expression for this parent. */ | |
51 | struct expression *exp; | |
52 | ||
53 | /* Block for which this expression is valid */ | |
54 | struct block *valid_block; | |
55 | ||
56 | /* The frame for this expression */ | |
57 | CORE_ADDR frame; | |
58 | ||
73a93a32 JI |
59 | /* If 1, "update" always recomputes the frame & valid block |
60 | using the currently selected frame. */ | |
61 | int use_selected_frame; | |
62 | ||
8b93c638 JM |
63 | /* Language info for this variable and its children */ |
64 | struct language_specific *lang; | |
65 | ||
66 | /* The varobj for this root node. */ | |
67 | struct varobj *rootvar; | |
68 | ||
69 | /* Next root variable */ | |
70 | struct varobj_root *next; | |
71 | }; | |
72 | ||
73 | /* Every variable in the system has a structure of this type defined | |
74 | for it. This structure holds all information necessary to manipulate | |
75 | a particular object variable. Members which must be freed are noted. */ | |
76 | struct varobj | |
77 | { | |
78 | ||
79 | /* Alloc'd name of the variable for this object.. If this variable is a | |
80 | child, then this name will be the child's source name. | |
81 | (bar, not foo.bar) */ | |
82 | /* NOTE: This is the "expression" */ | |
83 | char *name; | |
84 | ||
85 | /* The alloc'd name for this variable's object. This is here for | |
86 | convenience when constructing this object's children. */ | |
87 | char *obj_name; | |
88 | ||
89 | /* Index of this variable in its parent or -1 */ | |
90 | int index; | |
91 | ||
92 | /* The type of this variable. This may NEVER be NULL. */ | |
93 | struct type *type; | |
94 | ||
95 | /* The value of this expression or subexpression. This may be NULL. */ | |
96 | value_ptr value; | |
97 | ||
98 | /* Did an error occur evaluating the expression or getting its value? */ | |
99 | int error; | |
100 | ||
101 | /* The number of (immediate) children this variable has */ | |
102 | int num_children; | |
103 | ||
104 | /* If this object is a child, this points to its immediate parent. */ | |
105 | struct varobj *parent; | |
106 | ||
107 | /* A list of this object's children */ | |
108 | struct varobj_child *children; | |
109 | ||
110 | /* Description of the root variable. Points to root variable for children. */ | |
111 | struct varobj_root *root; | |
112 | ||
113 | /* The format of the output for this object */ | |
114 | enum varobj_display_formats format; | |
115 | }; | |
116 | ||
117 | /* Every variable keeps a linked list of its children, described | |
118 | by the following structure. */ | |
119 | /* FIXME: Deprecated. All should use vlist instead */ | |
120 | ||
121 | struct varobj_child | |
122 | { | |
123 | ||
124 | /* Pointer to the child's data */ | |
125 | struct varobj *child; | |
126 | ||
127 | /* Pointer to the next child */ | |
128 | struct varobj_child *next; | |
129 | }; | |
130 | ||
131 | /* A stack of varobjs */ | |
132 | /* FIXME: Deprecated. All should use vlist instead */ | |
133 | ||
134 | struct vstack | |
135 | { | |
136 | struct varobj *var; | |
137 | struct vstack *next; | |
138 | }; | |
139 | ||
140 | struct cpstack | |
141 | { | |
142 | char *name; | |
143 | struct cpstack *next; | |
144 | }; | |
145 | ||
146 | /* A list of varobjs */ | |
147 | ||
148 | struct vlist | |
149 | { | |
150 | struct varobj *var; | |
151 | struct vlist *next; | |
152 | }; | |
153 | ||
154 | /* Private function prototypes */ | |
155 | ||
156 | /* Helper functions for the above subcommands. */ | |
157 | ||
a14ed312 | 158 | static int delete_variable (struct cpstack **, struct varobj *, int); |
8b93c638 | 159 | |
a14ed312 KB |
160 | static void delete_variable_1 (struct cpstack **, int *, |
161 | struct varobj *, int, int); | |
8b93c638 | 162 | |
a14ed312 | 163 | static int install_variable (struct varobj *); |
8b93c638 | 164 | |
a14ed312 | 165 | static void uninstall_variable (struct varobj *); |
8b93c638 | 166 | |
a14ed312 | 167 | static struct varobj *child_exists (struct varobj *, char *); |
8b93c638 | 168 | |
a14ed312 | 169 | static struct varobj *create_child (struct varobj *, int, char *); |
8b93c638 | 170 | |
a14ed312 | 171 | static void save_child_in_parent (struct varobj *, struct varobj *); |
8b93c638 | 172 | |
a14ed312 | 173 | static void remove_child_from_parent (struct varobj *, struct varobj *); |
8b93c638 JM |
174 | |
175 | /* Utility routines */ | |
176 | ||
a14ed312 | 177 | static struct varobj *new_variable (void); |
8b93c638 | 178 | |
a14ed312 | 179 | static struct varobj *new_root_variable (void); |
8b93c638 | 180 | |
a14ed312 | 181 | static void free_variable (struct varobj *var); |
8b93c638 | 182 | |
74b7792f AC |
183 | static struct cleanup *make_cleanup_free_variable (struct varobj *var); |
184 | ||
a14ed312 | 185 | static struct type *get_type (struct varobj *var); |
8b93c638 | 186 | |
a14ed312 | 187 | static struct type *get_type_deref (struct varobj *var); |
8b93c638 | 188 | |
a14ed312 | 189 | static struct type *get_target_type (struct type *); |
8b93c638 | 190 | |
a14ed312 | 191 | static enum varobj_display_formats variable_default_display (struct varobj *); |
8b93c638 | 192 | |
a14ed312 | 193 | static int my_value_equal (value_ptr, value_ptr, int *); |
8b93c638 | 194 | |
a14ed312 | 195 | static void vpush (struct vstack **pstack, struct varobj *var); |
8b93c638 | 196 | |
a14ed312 | 197 | static struct varobj *vpop (struct vstack **pstack); |
8b93c638 | 198 | |
a14ed312 | 199 | static void cppush (struct cpstack **pstack, char *name); |
8b93c638 | 200 | |
a14ed312 | 201 | static char *cppop (struct cpstack **pstack); |
8b93c638 JM |
202 | |
203 | /* Language-specific routines. */ | |
204 | ||
a14ed312 | 205 | static enum varobj_languages variable_language (struct varobj *var); |
8b93c638 | 206 | |
a14ed312 | 207 | static int number_of_children (struct varobj *); |
8b93c638 | 208 | |
a14ed312 | 209 | static char *name_of_variable (struct varobj *); |
8b93c638 | 210 | |
a14ed312 | 211 | static char *name_of_child (struct varobj *, int); |
8b93c638 | 212 | |
a14ed312 | 213 | static value_ptr value_of_root (struct varobj **var_handle, int *); |
8b93c638 | 214 | |
a14ed312 | 215 | static value_ptr value_of_child (struct varobj *parent, int index); |
8b93c638 | 216 | |
a14ed312 | 217 | static struct type *type_of_child (struct varobj *var); |
8b93c638 | 218 | |
a14ed312 | 219 | static int variable_editable (struct varobj *var); |
8b93c638 | 220 | |
a14ed312 | 221 | static char *my_value_of_variable (struct varobj *var); |
8b93c638 | 222 | |
a14ed312 | 223 | static int type_changeable (struct varobj *var); |
8b93c638 JM |
224 | |
225 | /* C implementation */ | |
226 | ||
a14ed312 | 227 | static int c_number_of_children (struct varobj *var); |
8b93c638 | 228 | |
a14ed312 | 229 | static char *c_name_of_variable (struct varobj *parent); |
8b93c638 | 230 | |
a14ed312 | 231 | static char *c_name_of_child (struct varobj *parent, int index); |
8b93c638 | 232 | |
a14ed312 | 233 | static value_ptr c_value_of_root (struct varobj **var_handle); |
8b93c638 | 234 | |
a14ed312 | 235 | static value_ptr c_value_of_child (struct varobj *parent, int index); |
8b93c638 | 236 | |
a14ed312 | 237 | static struct type *c_type_of_child (struct varobj *parent, int index); |
8b93c638 | 238 | |
a14ed312 | 239 | static int c_variable_editable (struct varobj *var); |
8b93c638 | 240 | |
a14ed312 | 241 | static char *c_value_of_variable (struct varobj *var); |
8b93c638 JM |
242 | |
243 | /* C++ implementation */ | |
244 | ||
a14ed312 | 245 | static int cplus_number_of_children (struct varobj *var); |
8b93c638 | 246 | |
a14ed312 | 247 | static void cplus_class_num_children (struct type *type, int children[3]); |
8b93c638 | 248 | |
a14ed312 | 249 | static char *cplus_name_of_variable (struct varobj *parent); |
8b93c638 | 250 | |
a14ed312 | 251 | static char *cplus_name_of_child (struct varobj *parent, int index); |
8b93c638 | 252 | |
a14ed312 | 253 | static value_ptr cplus_value_of_root (struct varobj **var_handle); |
8b93c638 | 254 | |
a14ed312 | 255 | static value_ptr cplus_value_of_child (struct varobj *parent, int index); |
8b93c638 | 256 | |
a14ed312 | 257 | static struct type *cplus_type_of_child (struct varobj *parent, int index); |
8b93c638 | 258 | |
a14ed312 | 259 | static int cplus_variable_editable (struct varobj *var); |
8b93c638 | 260 | |
a14ed312 | 261 | static char *cplus_value_of_variable (struct varobj *var); |
8b93c638 JM |
262 | |
263 | /* Java implementation */ | |
264 | ||
a14ed312 | 265 | static int java_number_of_children (struct varobj *var); |
8b93c638 | 266 | |
a14ed312 | 267 | static char *java_name_of_variable (struct varobj *parent); |
8b93c638 | 268 | |
a14ed312 | 269 | static char *java_name_of_child (struct varobj *parent, int index); |
8b93c638 | 270 | |
a14ed312 | 271 | static value_ptr java_value_of_root (struct varobj **var_handle); |
8b93c638 | 272 | |
a14ed312 | 273 | static value_ptr java_value_of_child (struct varobj *parent, int index); |
8b93c638 | 274 | |
a14ed312 | 275 | static struct type *java_type_of_child (struct varobj *parent, int index); |
8b93c638 | 276 | |
a14ed312 | 277 | static int java_variable_editable (struct varobj *var); |
8b93c638 | 278 | |
a14ed312 | 279 | static char *java_value_of_variable (struct varobj *var); |
8b93c638 JM |
280 | |
281 | /* The language specific vector */ | |
282 | ||
283 | struct language_specific | |
284 | { | |
285 | ||
286 | /* The language of this variable */ | |
287 | enum varobj_languages language; | |
288 | ||
289 | /* The number of children of PARENT. */ | |
507f3c78 | 290 | int (*number_of_children) (struct varobj * parent); |
8b93c638 JM |
291 | |
292 | /* The name (expression) of a root varobj. */ | |
507f3c78 | 293 | char *(*name_of_variable) (struct varobj * parent); |
8b93c638 JM |
294 | |
295 | /* The name of the INDEX'th child of PARENT. */ | |
507f3c78 | 296 | char *(*name_of_child) (struct varobj * parent, int index); |
8b93c638 JM |
297 | |
298 | /* The value_ptr of the root variable ROOT. */ | |
507f3c78 | 299 | value_ptr (*value_of_root) (struct varobj ** root_handle); |
8b93c638 JM |
300 | |
301 | /* The value_ptr of the INDEX'th child of PARENT. */ | |
507f3c78 | 302 | value_ptr (*value_of_child) (struct varobj * parent, int index); |
8b93c638 JM |
303 | |
304 | /* The type of the INDEX'th child of PARENT. */ | |
507f3c78 | 305 | struct type *(*type_of_child) (struct varobj * parent, int index); |
8b93c638 JM |
306 | |
307 | /* Is VAR editable? */ | |
507f3c78 | 308 | int (*variable_editable) (struct varobj * var); |
8b93c638 JM |
309 | |
310 | /* The current value of VAR. */ | |
507f3c78 | 311 | char *(*value_of_variable) (struct varobj * var); |
8b93c638 JM |
312 | }; |
313 | ||
314 | /* Array of known source language routines. */ | |
315 | static struct language_specific | |
316 | languages[vlang_end][sizeof (struct language_specific)] = | |
317 | { | |
318 | /* Unknown (try treating as C */ | |
319 | { | |
320 | vlang_unknown, | |
321 | c_number_of_children, | |
322 | c_name_of_variable, | |
323 | c_name_of_child, | |
324 | c_value_of_root, | |
325 | c_value_of_child, | |
326 | c_type_of_child, | |
327 | c_variable_editable, | |
328 | c_value_of_variable | |
329 | } | |
330 | , | |
331 | /* C */ | |
332 | { | |
333 | vlang_c, | |
334 | c_number_of_children, | |
335 | c_name_of_variable, | |
336 | c_name_of_child, | |
337 | c_value_of_root, | |
338 | c_value_of_child, | |
339 | c_type_of_child, | |
340 | c_variable_editable, | |
341 | c_value_of_variable | |
342 | } | |
343 | , | |
344 | /* C++ */ | |
345 | { | |
346 | vlang_cplus, | |
347 | cplus_number_of_children, | |
348 | cplus_name_of_variable, | |
349 | cplus_name_of_child, | |
350 | cplus_value_of_root, | |
351 | cplus_value_of_child, | |
352 | cplus_type_of_child, | |
353 | cplus_variable_editable, | |
354 | cplus_value_of_variable | |
355 | } | |
356 | , | |
357 | /* Java */ | |
358 | { | |
359 | vlang_java, | |
360 | java_number_of_children, | |
361 | java_name_of_variable, | |
362 | java_name_of_child, | |
363 | java_value_of_root, | |
364 | java_value_of_child, | |
365 | java_type_of_child, | |
366 | java_variable_editable, | |
367 | java_value_of_variable | |
368 | } | |
369 | }; | |
370 | ||
371 | /* A little convenience enum for dealing with C++/Java */ | |
372 | enum vsections | |
373 | { | |
374 | v_public = 0, v_private, v_protected | |
375 | }; | |
376 | ||
377 | /* Private data */ | |
378 | ||
379 | /* Mappings of varobj_display_formats enums to gdb's format codes */ | |
380 | static int format_code[] = | |
381 | {0, 't', 'd', 'x', 'o'}; | |
382 | ||
383 | /* Header of the list of root variable objects */ | |
384 | static struct varobj_root *rootlist; | |
385 | static int rootcount = 0; /* number of root varobjs in the list */ | |
386 | ||
387 | /* Prime number indicating the number of buckets in the hash table */ | |
388 | /* A prime large enough to avoid too many colisions */ | |
389 | #define VAROBJ_TABLE_SIZE 227 | |
390 | ||
391 | /* Pointer to the varobj hash table (built at run time) */ | |
392 | static struct vlist **varobj_table; | |
393 | ||
394 | #if defined(FREEIF) | |
395 | #undef FREEIF | |
396 | #endif | |
397 | #define FREEIF(x) if (x != NULL) free((char *) (x)) | |
398 | ||
399 | /* Is the variable X one of our "fake" children? */ | |
400 | #define CPLUS_FAKE_CHILD(x) \ | |
401 | ((x) != NULL && (x)->type == NULL && (x)->value == NULL) | |
402 | \f | |
403 | ||
404 | /* API Implementation */ | |
405 | ||
406 | /* Creates a varobj (not its children) */ | |
407 | ||
408 | struct varobj * | |
409 | varobj_create (char *objname, | |
73a93a32 JI |
410 | char *expression, CORE_ADDR frame, |
411 | enum varobj_type type) | |
8b93c638 JM |
412 | { |
413 | struct varobj *var; | |
414 | struct frame_info *fi, *old_fi; | |
415 | struct block *block; | |
416 | struct cleanup *old_chain; | |
417 | ||
418 | /* Fill out a varobj structure for the (root) variable being constructed. */ | |
419 | var = new_root_variable (); | |
74b7792f | 420 | old_chain = make_cleanup_free_variable (var); |
8b93c638 JM |
421 | |
422 | if (expression != NULL) | |
423 | { | |
424 | char *p; | |
425 | enum varobj_languages lang; | |
426 | ||
427 | /* Parse and evaluate the expression, filling in as much | |
428 | of the variable's data as possible */ | |
429 | ||
430 | /* Allow creator to specify context of variable */ | |
73a93a32 JI |
431 | if ((type == USE_CURRENT_FRAME) |
432 | || (type == USE_SELECTED_FRAME)) | |
8b93c638 JM |
433 | fi = selected_frame; |
434 | else | |
435 | fi = find_frame_addr_in_frame_chain (frame); | |
436 | ||
73a93a32 JI |
437 | /* frame = -2 means always use selected frame */ |
438 | if (type == USE_SELECTED_FRAME) | |
439 | var->root->use_selected_frame = 1; | |
440 | ||
8b93c638 JM |
441 | block = NULL; |
442 | if (fi != NULL) | |
443 | block = get_frame_block (fi); | |
444 | ||
445 | p = expression; | |
446 | innermost_block = NULL; | |
73a93a32 JI |
447 | /* Wrap the call to parse expression, so we can |
448 | return a sensible error. */ | |
449 | if (!gdb_parse_exp_1 (&p, block, 0, &var->root->exp)) | |
450 | { | |
451 | return NULL; | |
452 | } | |
8b93c638 JM |
453 | |
454 | /* Don't allow variables to be created for types. */ | |
455 | if (var->root->exp->elts[0].opcode == OP_TYPE) | |
456 | { | |
457 | do_cleanups (old_chain); | |
458 | fprintf_unfiltered (gdb_stderr, | |
459 | "Attempt to use a type name as an expression."); | |
460 | return NULL; | |
461 | } | |
462 | ||
463 | var->format = variable_default_display (var); | |
464 | var->root->valid_block = innermost_block; | |
465 | var->name = savestring (expression, strlen (expression)); | |
466 | ||
467 | /* When the frame is different from the current frame, | |
468 | we must select the appropriate frame before parsing | |
469 | the expression, otherwise the value will not be current. | |
470 | Since select_frame is so benign, just call it for all cases. */ | |
471 | if (fi != NULL) | |
472 | { | |
473 | var->root->frame = FRAME_FP (fi); | |
474 | old_fi = selected_frame; | |
475 | select_frame (fi, -1); | |
476 | } | |
477 | ||
478 | /* We definitively need to catch errors here. | |
479 | If evaluate_expression succeeds we got the value we wanted. | |
480 | But if it fails, we still go on with a call to evaluate_type() */ | |
481 | if (gdb_evaluate_expression (var->root->exp, &var->value)) | |
482 | { | |
483 | /* no error */ | |
484 | release_value (var->value); | |
485 | if (VALUE_LAZY (var->value)) | |
486 | gdb_value_fetch_lazy (var->value); | |
487 | } | |
488 | else | |
489 | var->value = evaluate_type (var->root->exp); | |
490 | ||
491 | var->type = VALUE_TYPE (var->value); | |
492 | ||
493 | /* Set language info */ | |
494 | lang = variable_language (var); | |
495 | var->root->lang = languages[lang]; | |
496 | ||
497 | /* Set ourselves as our root */ | |
498 | var->root->rootvar = var; | |
499 | ||
500 | /* Reset the selected frame */ | |
501 | if (fi != NULL) | |
502 | select_frame (old_fi, -1); | |
503 | } | |
504 | ||
73a93a32 JI |
505 | /* If the variable object name is null, that means this |
506 | is a temporary variable, so don't install it. */ | |
507 | ||
508 | if ((var != NULL) && (objname != NULL)) | |
8b93c638 JM |
509 | { |
510 | var->obj_name = savestring (objname, strlen (objname)); | |
511 | ||
512 | /* If a varobj name is duplicated, the install will fail so | |
513 | we must clenup */ | |
514 | if (!install_variable (var)) | |
515 | { | |
516 | do_cleanups (old_chain); | |
517 | return NULL; | |
518 | } | |
519 | } | |
520 | ||
521 | discard_cleanups (old_chain); | |
522 | return var; | |
523 | } | |
524 | ||
525 | /* Generates an unique name that can be used for a varobj */ | |
526 | ||
527 | char * | |
528 | varobj_gen_name (void) | |
529 | { | |
530 | static int id = 0; | |
531 | char obj_name[31]; | |
532 | ||
533 | /* generate a name for this object */ | |
534 | id++; | |
535 | sprintf (obj_name, "var%d", id); | |
536 | ||
537 | return xstrdup (obj_name); | |
538 | } | |
539 | ||
540 | /* Given an "objname", returns the pointer to the corresponding varobj | |
541 | or NULL if not found */ | |
542 | ||
543 | struct varobj * | |
544 | varobj_get_handle (char *objname) | |
545 | { | |
546 | struct vlist *cv; | |
547 | const char *chp; | |
548 | unsigned int index = 0; | |
549 | unsigned int i = 1; | |
550 | ||
551 | for (chp = objname; *chp; chp++) | |
552 | { | |
553 | index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE; | |
554 | } | |
555 | ||
556 | cv = *(varobj_table + index); | |
557 | while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0)) | |
558 | cv = cv->next; | |
559 | ||
560 | if (cv == NULL) | |
561 | error ("Variable object not found"); | |
562 | ||
563 | return cv->var; | |
564 | } | |
565 | ||
566 | /* Given the handle, return the name of the object */ | |
567 | ||
568 | char * | |
569 | varobj_get_objname (struct varobj *var) | |
570 | { | |
571 | return var->obj_name; | |
572 | } | |
573 | ||
574 | /* Given the handle, return the expression represented by the object */ | |
575 | ||
576 | char * | |
577 | varobj_get_expression (struct varobj *var) | |
578 | { | |
579 | return name_of_variable (var); | |
580 | } | |
581 | ||
582 | /* Deletes a varobj and all its children if only_children == 0, | |
583 | otherwise deletes only the children; returns a malloc'ed list of all the | |
584 | (malloc'ed) names of the variables that have been deleted (NULL terminated) */ | |
585 | ||
586 | int | |
587 | varobj_delete (struct varobj *var, char ***dellist, int only_children) | |
588 | { | |
589 | int delcount; | |
590 | int mycount; | |
591 | struct cpstack *result = NULL; | |
592 | char **cp; | |
593 | ||
594 | /* Initialize a stack for temporary results */ | |
595 | cppush (&result, NULL); | |
596 | ||
597 | if (only_children) | |
598 | /* Delete only the variable children */ | |
599 | delcount = delete_variable (&result, var, 1 /* only the children */ ); | |
600 | else | |
601 | /* Delete the variable and all its children */ | |
602 | delcount = delete_variable (&result, var, 0 /* parent+children */ ); | |
603 | ||
604 | /* We may have been asked to return a list of what has been deleted */ | |
605 | if (dellist != NULL) | |
606 | { | |
607 | *dellist = xmalloc ((delcount + 1) * sizeof (char *)); | |
608 | ||
609 | cp = *dellist; | |
610 | mycount = delcount; | |
611 | *cp = cppop (&result); | |
612 | while ((*cp != NULL) && (mycount > 0)) | |
613 | { | |
614 | mycount--; | |
615 | cp++; | |
616 | *cp = cppop (&result); | |
617 | } | |
618 | ||
619 | if (mycount || (*cp != NULL)) | |
620 | warning ("varobj_delete: assertion failed - mycount(=%d) <> 0", mycount); | |
621 | } | |
622 | ||
623 | return delcount; | |
624 | } | |
625 | ||
626 | /* Set/Get variable object display format */ | |
627 | ||
628 | enum varobj_display_formats | |
629 | varobj_set_display_format (struct varobj *var, | |
630 | enum varobj_display_formats format) | |
631 | { | |
632 | switch (format) | |
633 | { | |
634 | case FORMAT_NATURAL: | |
635 | case FORMAT_BINARY: | |
636 | case FORMAT_DECIMAL: | |
637 | case FORMAT_HEXADECIMAL: | |
638 | case FORMAT_OCTAL: | |
639 | var->format = format; | |
640 | break; | |
641 | ||
642 | default: | |
643 | var->format = variable_default_display (var); | |
644 | } | |
645 | ||
646 | return var->format; | |
647 | } | |
648 | ||
649 | enum varobj_display_formats | |
650 | varobj_get_display_format (struct varobj *var) | |
651 | { | |
652 | return var->format; | |
653 | } | |
654 | ||
655 | int | |
656 | varobj_get_num_children (struct varobj *var) | |
657 | { | |
658 | if (var->num_children == -1) | |
659 | var->num_children = number_of_children (var); | |
660 | ||
661 | return var->num_children; | |
662 | } | |
663 | ||
664 | /* Creates a list of the immediate children of a variable object; | |
665 | the return code is the number of such children or -1 on error */ | |
666 | ||
667 | int | |
668 | varobj_list_children (struct varobj *var, struct varobj ***childlist) | |
669 | { | |
670 | struct varobj *child; | |
671 | char *name; | |
672 | int i; | |
673 | ||
674 | /* sanity check: have we been passed a pointer? */ | |
675 | if (childlist == NULL) | |
676 | return -1; | |
677 | ||
678 | *childlist = NULL; | |
679 | ||
680 | if (var->num_children == -1) | |
681 | var->num_children = number_of_children (var); | |
682 | ||
683 | /* List of children */ | |
684 | *childlist = xmalloc ((var->num_children + 1) * sizeof (struct varobj *)); | |
685 | ||
686 | for (i = 0; i < var->num_children; i++) | |
687 | { | |
688 | /* Mark as the end in case we bail out */ | |
689 | *((*childlist) + i) = NULL; | |
690 | ||
691 | /* check if child exists, if not create */ | |
692 | name = name_of_child (var, i); | |
693 | child = child_exists (var, name); | |
694 | if (child == NULL) | |
695 | child = create_child (var, i, name); | |
696 | ||
697 | *((*childlist) + i) = child; | |
698 | } | |
699 | ||
700 | /* End of list is marked by a NULL pointer */ | |
701 | *((*childlist) + i) = NULL; | |
702 | ||
703 | return var->num_children; | |
704 | } | |
705 | ||
706 | /* Obtain the type of an object Variable as a string similar to the one gdb | |
707 | prints on the console */ | |
708 | ||
709 | char * | |
710 | varobj_get_type (struct varobj *var) | |
711 | { | |
712 | value_ptr val; | |
713 | struct cleanup *old_chain; | |
714 | struct ui_file *stb; | |
715 | char *thetype; | |
716 | long length; | |
717 | ||
718 | /* For the "fake" variables, do not return a type. (It's type is | |
719 | NULL, too.) */ | |
720 | if (CPLUS_FAKE_CHILD (var)) | |
721 | return NULL; | |
722 | ||
723 | stb = mem_fileopen (); | |
724 | old_chain = make_cleanup_ui_file_delete (stb); | |
725 | ||
726 | /* To print the type, we simply create a zero value_ptr and | |
727 | cast it to our type. We then typeprint this variable. */ | |
728 | val = value_zero (var->type, not_lval); | |
729 | type_print (VALUE_TYPE (val), "", stb, -1); | |
730 | ||
731 | thetype = ui_file_xstrdup (stb, &length); | |
732 | do_cleanups (old_chain); | |
733 | return thetype; | |
734 | } | |
735 | ||
736 | enum varobj_languages | |
737 | varobj_get_language (struct varobj *var) | |
738 | { | |
739 | return variable_language (var); | |
740 | } | |
741 | ||
742 | int | |
743 | varobj_get_attributes (struct varobj *var) | |
744 | { | |
745 | int attributes = 0; | |
746 | ||
747 | if (variable_editable (var)) | |
748 | /* FIXME: define masks for attributes */ | |
749 | attributes |= 0x00000001; /* Editable */ | |
750 | ||
751 | return attributes; | |
752 | } | |
753 | ||
754 | char * | |
755 | varobj_get_value (struct varobj *var) | |
756 | { | |
757 | return my_value_of_variable (var); | |
758 | } | |
759 | ||
760 | /* Set the value of an object variable (if it is editable) to the | |
761 | value of the given expression */ | |
762 | /* Note: Invokes functions that can call error() */ | |
763 | ||
764 | int | |
765 | varobj_set_value (struct varobj *var, char *expression) | |
766 | { | |
767 | value_ptr val; | |
768 | int offset = 0; | |
769 | ||
770 | /* The argument "expression" contains the variable's new value. | |
771 | We need to first construct a legal expression for this -- ugh! */ | |
772 | /* Does this cover all the bases? */ | |
773 | struct expression *exp; | |
774 | value_ptr value; | |
775 | int saved_input_radix = input_radix; | |
776 | ||
777 | if (variable_editable (var) && !var->error) | |
778 | { | |
779 | char *s = expression; | |
780 | int i; | |
781 | value_ptr temp; | |
782 | ||
783 | input_radix = 10; /* ALWAYS reset to decimal temporarily */ | |
586e589c FN |
784 | if (!gdb_parse_exp_1 (&s, 0, 0, &exp)) |
785 | /* We cannot proceed without a well-formed expression. */ | |
786 | return 0; | |
8b93c638 JM |
787 | if (!gdb_evaluate_expression (exp, &value)) |
788 | { | |
789 | /* We cannot proceed without a valid expression. */ | |
790 | FREEIF (exp); | |
791 | return 0; | |
792 | } | |
793 | ||
794 | /* If our parent is "public", "private", or "protected", we could | |
795 | be asking to modify the value of a baseclass. If so, we need to | |
796 | adjust our address by the offset of our baseclass in the subclass, | |
797 | since VALUE_ADDRESS (var->value) points at the start of the subclass. | |
798 | For some reason, value_cast doesn't take care of this properly. */ | |
799 | temp = var->value; | |
800 | if (var->parent != NULL && CPLUS_FAKE_CHILD (var->parent)) | |
801 | { | |
802 | struct varobj *super, *sub; | |
803 | struct type *type; | |
804 | super = var->parent->parent; | |
805 | sub = super->parent; | |
806 | if (sub != NULL) | |
807 | { | |
808 | /* Yes, it is a baseclass */ | |
809 | type = get_type_deref (sub); | |
810 | ||
811 | if (super->index < TYPE_N_BASECLASSES (type)) | |
812 | { | |
813 | temp = value_copy (var->value); | |
814 | for (i = 0; i < super->index; i++) | |
815 | offset += TYPE_LENGTH (TYPE_FIELD_TYPE (type, i)); | |
816 | } | |
817 | } | |
818 | } | |
819 | ||
820 | VALUE_ADDRESS (temp) += offset; | |
821 | val = value_assign (temp, value); | |
822 | VALUE_ADDRESS (val) -= offset; | |
823 | value_free (var->value); | |
824 | release_value (val); | |
825 | var->value = val; | |
826 | input_radix = saved_input_radix; | |
827 | return 1; | |
828 | } | |
829 | ||
830 | return 0; | |
831 | } | |
832 | ||
833 | /* Returns a malloc'ed list with all root variable objects */ | |
834 | int | |
835 | varobj_list (struct varobj ***varlist) | |
836 | { | |
837 | struct varobj **cv; | |
838 | struct varobj_root *croot; | |
839 | int mycount = rootcount; | |
840 | ||
841 | /* Alloc (rootcount + 1) entries for the result */ | |
842 | *varlist = xmalloc ((rootcount + 1) * sizeof (struct varobj *)); | |
843 | ||
844 | cv = *varlist; | |
845 | croot = rootlist; | |
846 | while ((croot != NULL) && (mycount > 0)) | |
847 | { | |
848 | *cv = croot->rootvar; | |
849 | mycount--; | |
850 | cv++; | |
851 | croot = croot->next; | |
852 | } | |
853 | /* Mark the end of the list */ | |
854 | *cv = NULL; | |
855 | ||
856 | if (mycount || (croot != NULL)) | |
857 | warning ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)", | |
858 | rootcount, mycount); | |
859 | ||
860 | return rootcount; | |
861 | } | |
862 | ||
863 | /* Update the values for a variable and its children. This is a | |
864 | two-pronged attack. First, re-parse the value for the root's | |
865 | expression to see if it's changed. Then go all the way | |
866 | through its children, reconstructing them and noting if they've | |
867 | changed. | |
73a93a32 JI |
868 | Return value: |
869 | -1 if there was an error updating the varobj | |
870 | -2 if the type changed | |
871 | Otherwise it is the number of children + parent changed | |
8b93c638 JM |
872 | |
873 | Only root variables can be updated... */ | |
874 | ||
875 | int | |
876 | varobj_update (struct varobj *var, struct varobj ***changelist) | |
877 | { | |
878 | int changed = 0; | |
73a93a32 | 879 | int type_changed; |
8b93c638 JM |
880 | int i; |
881 | int vleft; | |
882 | int error2; | |
883 | struct varobj *v; | |
884 | struct varobj **cv; | |
885 | struct varobj **templist; | |
886 | value_ptr new; | |
887 | struct vstack *stack = NULL; | |
888 | struct vstack *result = NULL; | |
889 | struct frame_info *old_fi; | |
890 | ||
891 | /* sanity check: have we been passed a pointer? */ | |
892 | if (changelist == NULL) | |
893 | return -1; | |
894 | ||
895 | /* Only root variables can be updated... */ | |
896 | if (var->root->rootvar != var) | |
897 | /* Not a root var */ | |
898 | return -1; | |
899 | ||
900 | /* Save the selected stack frame, since we will need to change it | |
901 | in order to evaluate expressions. */ | |
902 | old_fi = selected_frame; | |
903 | ||
904 | /* Update the root variable. value_of_root can return NULL | |
905 | if the variable is no longer around, i.e. we stepped out of | |
73a93a32 JI |
906 | the frame in which a local existed. We are letting the |
907 | value_of_root variable dispose of the varobj if the type | |
908 | has changed. */ | |
909 | type_changed = 1; | |
910 | new = value_of_root (&var, &type_changed); | |
8b93c638 | 911 | if (new == NULL) |
73a93a32 JI |
912 | { |
913 | var->error = 1; | |
914 | return -1; | |
915 | } | |
8b93c638 JM |
916 | |
917 | /* Initialize a stack for temporary results */ | |
918 | vpush (&result, NULL); | |
919 | ||
73a93a32 | 920 | if (type_changed || !my_value_equal (var->value, new, &error2)) |
8b93c638 JM |
921 | { |
922 | /* Note that it's changed There a couple of exceptions here, | |
73a93a32 JI |
923 | though. We don't want some types to be reported as |
924 | "changed". The exception to this is if this is a | |
925 | "use_selected_frame" varobj, and its type has changed. */ | |
926 | if (type_changed || type_changeable (var)) | |
8b93c638 JM |
927 | { |
928 | vpush (&result, var); | |
929 | changed++; | |
930 | } | |
931 | } | |
932 | /* error2 replaces var->error since this new value | |
933 | WILL replace the old one. */ | |
934 | var->error = error2; | |
935 | ||
936 | /* We must always keep around the new value for this root | |
937 | variable expression, or we lose the updated children! */ | |
938 | value_free (var->value); | |
939 | var->value = new; | |
940 | ||
941 | /* Initialize a stack */ | |
942 | vpush (&stack, NULL); | |
943 | ||
944 | /* Push the root's children */ | |
945 | if (var->children != NULL) | |
946 | { | |
947 | struct varobj_child *c; | |
948 | for (c = var->children; c != NULL; c = c->next) | |
949 | vpush (&stack, c->child); | |
950 | } | |
951 | ||
952 | /* Walk through the children, reconstructing them all. */ | |
953 | v = vpop (&stack); | |
954 | while (v != NULL) | |
955 | { | |
956 | /* Push any children */ | |
957 | if (v->children != NULL) | |
958 | { | |
959 | struct varobj_child *c; | |
960 | for (c = v->children; c != NULL; c = c->next) | |
961 | vpush (&stack, c->child); | |
962 | } | |
963 | ||
964 | /* Update this variable */ | |
965 | new = value_of_child (v->parent, v->index); | |
966 | if (type_changeable (v) && !my_value_equal (v->value, new, &error2)) | |
967 | { | |
968 | /* Note that it's changed */ | |
969 | vpush (&result, v); | |
970 | changed++; | |
971 | } | |
972 | /* error2 replaces v->error since this new value | |
973 | WILL replace the old one. */ | |
974 | v->error = error2; | |
975 | ||
976 | /* We must always keep new values, since children depend on it. */ | |
977 | if (v->value != NULL) | |
978 | value_free (v->value); | |
979 | v->value = new; | |
980 | ||
981 | /* Get next child */ | |
982 | v = vpop (&stack); | |
983 | } | |
984 | ||
985 | /* Alloc (changed + 1) list entries */ | |
986 | /* FIXME: add a cleanup for the allocated list(s) | |
987 | because one day the select_frame called below can longjump */ | |
988 | *changelist = xmalloc ((changed + 1) * sizeof (struct varobj *)); | |
989 | if (changed > 1) | |
990 | { | |
991 | templist = xmalloc ((changed + 1) * sizeof (struct varobj *)); | |
992 | cv = templist; | |
993 | } | |
994 | else | |
995 | cv = *changelist; | |
996 | ||
997 | /* Copy from result stack to list */ | |
998 | vleft = changed; | |
999 | *cv = vpop (&result); | |
1000 | while ((*cv != NULL) && (vleft > 0)) | |
1001 | { | |
1002 | vleft--; | |
1003 | cv++; | |
1004 | *cv = vpop (&result); | |
1005 | } | |
1006 | if (vleft) | |
1007 | warning ("varobj_update: assertion failed - vleft <> 0"); | |
1008 | ||
1009 | if (changed > 1) | |
1010 | { | |
1011 | /* Now we revert the order. */ | |
1012 | for (i=0; i < changed; i++) | |
1013 | *(*changelist + i) = *(templist + changed -1 - i); | |
1014 | *(*changelist + changed) = NULL; | |
1015 | } | |
1016 | ||
1017 | /* Restore selected frame */ | |
1018 | select_frame (old_fi, -1); | |
1019 | ||
73a93a32 JI |
1020 | if (type_changed) |
1021 | return -2; | |
1022 | else | |
1023 | return changed; | |
8b93c638 JM |
1024 | } |
1025 | \f | |
1026 | ||
1027 | /* Helper functions */ | |
1028 | ||
1029 | /* | |
1030 | * Variable object construction/destruction | |
1031 | */ | |
1032 | ||
1033 | static int | |
fba45db2 KB |
1034 | delete_variable (struct cpstack **resultp, struct varobj *var, |
1035 | int only_children_p) | |
8b93c638 JM |
1036 | { |
1037 | int delcount = 0; | |
1038 | ||
1039 | delete_variable_1 (resultp, &delcount, var, | |
1040 | only_children_p, 1 /* remove_from_parent_p */ ); | |
1041 | ||
1042 | return delcount; | |
1043 | } | |
1044 | ||
1045 | /* Delete the variable object VAR and its children */ | |
1046 | /* IMPORTANT NOTE: If we delete a variable which is a child | |
1047 | and the parent is not removed we dump core. It must be always | |
1048 | initially called with remove_from_parent_p set */ | |
1049 | static void | |
fba45db2 KB |
1050 | delete_variable_1 (struct cpstack **resultp, int *delcountp, struct varobj *var, |
1051 | int only_children_p, int remove_from_parent_p) | |
8b93c638 JM |
1052 | { |
1053 | struct varobj_child *vc; | |
1054 | struct varobj_child *next; | |
1055 | ||
1056 | /* Delete any children of this variable, too. */ | |
1057 | for (vc = var->children; vc != NULL; vc = next) | |
1058 | { | |
1059 | if (!remove_from_parent_p) | |
1060 | vc->child->parent = NULL; | |
1061 | delete_variable_1 (resultp, delcountp, vc->child, 0, only_children_p); | |
1062 | next = vc->next; | |
1063 | free (vc); | |
1064 | } | |
1065 | ||
1066 | /* if we were called to delete only the children we are done here */ | |
1067 | if (only_children_p) | |
1068 | return; | |
1069 | ||
1070 | /* Otherwise, add it to the list of deleted ones and proceed to do so */ | |
73a93a32 JI |
1071 | /* If the name is null, this is a temporary variable, that has not |
1072 | yet been installed, don't report it, it belongs to the caller... */ | |
1073 | if (var->obj_name != NULL) | |
8b93c638 JM |
1074 | { |
1075 | cppush (resultp, strdup (var->obj_name)); | |
1076 | *delcountp = *delcountp + 1; | |
1077 | } | |
1078 | ||
1079 | /* If this variable has a parent, remove it from its parent's list */ | |
1080 | /* OPTIMIZATION: if the parent of this variable is also being deleted, | |
1081 | (as indicated by remove_from_parent_p) we don't bother doing an | |
1082 | expensive list search to find the element to remove when we are | |
1083 | discarding the list afterwards */ | |
1084 | if ((remove_from_parent_p) && | |
1085 | (var->parent != NULL)) | |
1086 | { | |
1087 | remove_child_from_parent (var->parent, var); | |
1088 | } | |
73a93a32 JI |
1089 | |
1090 | if (var->obj_name != NULL) | |
1091 | uninstall_variable (var); | |
8b93c638 JM |
1092 | |
1093 | /* Free memory associated with this variable */ | |
1094 | free_variable (var); | |
1095 | } | |
1096 | ||
1097 | /* Install the given variable VAR with the object name VAR->OBJ_NAME. */ | |
1098 | static int | |
fba45db2 | 1099 | install_variable (struct varobj *var) |
8b93c638 JM |
1100 | { |
1101 | struct vlist *cv; | |
1102 | struct vlist *newvl; | |
1103 | const char *chp; | |
1104 | unsigned int index = 0; | |
1105 | unsigned int i = 1; | |
1106 | ||
1107 | for (chp = var->obj_name; *chp; chp++) | |
1108 | { | |
1109 | index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE; | |
1110 | } | |
1111 | ||
1112 | cv = *(varobj_table + index); | |
1113 | while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0)) | |
1114 | cv = cv->next; | |
1115 | ||
1116 | if (cv != NULL) | |
1117 | error ("Duplicate variable object name"); | |
1118 | ||
1119 | /* Add varobj to hash table */ | |
1120 | newvl = xmalloc (sizeof (struct vlist)); | |
1121 | newvl->next = *(varobj_table + index); | |
1122 | newvl->var = var; | |
1123 | *(varobj_table + index) = newvl; | |
1124 | ||
1125 | /* If root, add varobj to root list */ | |
1126 | if (var->root->rootvar == var) | |
1127 | { | |
1128 | /* Add to list of root variables */ | |
1129 | if (rootlist == NULL) | |
1130 | var->root->next = NULL; | |
1131 | else | |
1132 | var->root->next = rootlist; | |
1133 | rootlist = var->root; | |
1134 | rootcount++; | |
1135 | } | |
1136 | ||
1137 | return 1; /* OK */ | |
1138 | } | |
1139 | ||
1140 | /* Unistall the object VAR. */ | |
1141 | static void | |
fba45db2 | 1142 | uninstall_variable (struct varobj *var) |
8b93c638 JM |
1143 | { |
1144 | struct vlist *cv; | |
1145 | struct vlist *prev; | |
1146 | struct varobj_root *cr; | |
1147 | struct varobj_root *prer; | |
1148 | const char *chp; | |
1149 | unsigned int index = 0; | |
1150 | unsigned int i = 1; | |
1151 | ||
1152 | /* Remove varobj from hash table */ | |
1153 | for (chp = var->obj_name; *chp; chp++) | |
1154 | { | |
1155 | index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE; | |
1156 | } | |
1157 | ||
1158 | cv = *(varobj_table + index); | |
1159 | prev = NULL; | |
1160 | while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0)) | |
1161 | { | |
1162 | prev = cv; | |
1163 | cv = cv->next; | |
1164 | } | |
1165 | ||
1166 | if (varobjdebug) | |
1167 | fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name); | |
1168 | ||
1169 | if (cv == NULL) | |
1170 | { | |
1171 | warning ("Assertion failed: Could not find variable object \"%s\" to delete", var->obj_name); | |
1172 | return; | |
1173 | } | |
1174 | ||
1175 | if (prev == NULL) | |
1176 | *(varobj_table + index) = cv->next; | |
1177 | else | |
1178 | prev->next = cv->next; | |
1179 | ||
1180 | free (cv); | |
1181 | ||
1182 | /* If root, remove varobj from root list */ | |
1183 | if (var->root->rootvar == var) | |
1184 | { | |
1185 | /* Remove from list of root variables */ | |
1186 | if (rootlist == var->root) | |
1187 | rootlist = var->root->next; | |
1188 | else | |
1189 | { | |
1190 | prer = NULL; | |
1191 | cr = rootlist; | |
1192 | while ((cr != NULL) && (cr->rootvar != var)) | |
1193 | { | |
1194 | prer = cr; | |
1195 | cr = cr->next; | |
1196 | } | |
1197 | if (cr == NULL) | |
1198 | { | |
1199 | warning ("Assertion failed: Could not find varobj \"%s\" in root list", var->obj_name); | |
1200 | return; | |
1201 | } | |
1202 | if (prer == NULL) | |
1203 | rootlist = NULL; | |
1204 | else | |
1205 | prer->next = cr->next; | |
1206 | } | |
1207 | rootcount--; | |
1208 | } | |
1209 | ||
1210 | } | |
1211 | ||
1212 | /* Does a child with the name NAME exist in VAR? If so, return its data. | |
1213 | If not, return NULL. */ | |
1214 | static struct varobj * | |
1215 | child_exists (var, name) | |
1216 | struct varobj *var; /* Parent */ | |
1217 | char *name; /* name of child */ | |
1218 | { | |
1219 | struct varobj_child *vc; | |
1220 | ||
1221 | for (vc = var->children; vc != NULL; vc = vc->next) | |
1222 | { | |
1223 | if (STREQ (vc->child->name, name)) | |
1224 | return vc->child; | |
1225 | } | |
1226 | ||
1227 | return NULL; | |
1228 | } | |
1229 | ||
1230 | /* Create and install a child of the parent of the given name */ | |
1231 | static struct varobj * | |
fba45db2 | 1232 | create_child (struct varobj *parent, int index, char *name) |
8b93c638 JM |
1233 | { |
1234 | struct varobj *child; | |
1235 | char *childs_name; | |
1236 | ||
1237 | child = new_variable (); | |
1238 | ||
1239 | /* name is allocated by name_of_child */ | |
1240 | child->name = name; | |
1241 | child->index = index; | |
1242 | child->value = value_of_child (parent, index); | |
1243 | if (child->value == NULL || parent->error) | |
1244 | child->error = 1; | |
1245 | child->parent = parent; | |
1246 | child->root = parent->root; | |
1247 | childs_name = (char *) xmalloc ((strlen (parent->obj_name) + strlen (name) + 2) | |
1248 | * sizeof (char)); | |
1249 | sprintf (childs_name, "%s.%s", parent->obj_name, name); | |
1250 | child->obj_name = childs_name; | |
1251 | install_variable (child); | |
1252 | ||
1253 | /* Save a pointer to this child in the parent */ | |
1254 | save_child_in_parent (parent, child); | |
1255 | ||
1256 | /* Note the type of this child */ | |
1257 | child->type = type_of_child (child); | |
1258 | ||
1259 | return child; | |
1260 | } | |
1261 | ||
1262 | /* FIXME: This should be a generic add to list */ | |
1263 | /* Save CHILD in the PARENT's data. */ | |
1264 | static void | |
fba45db2 | 1265 | save_child_in_parent (struct varobj *parent, struct varobj *child) |
8b93c638 JM |
1266 | { |
1267 | struct varobj_child *vc; | |
1268 | ||
1269 | /* Insert the child at the top */ | |
1270 | vc = parent->children; | |
1271 | parent->children = | |
1272 | (struct varobj_child *) xmalloc (sizeof (struct varobj_child)); | |
1273 | ||
1274 | parent->children->next = vc; | |
1275 | parent->children->child = child; | |
1276 | } | |
1277 | ||
1278 | /* FIXME: This should be a generic remove from list */ | |
1279 | /* Remove the CHILD from the PARENT's list of children. */ | |
1280 | static void | |
fba45db2 | 1281 | remove_child_from_parent (struct varobj *parent, struct varobj *child) |
8b93c638 JM |
1282 | { |
1283 | struct varobj_child *vc, *prev; | |
1284 | ||
1285 | /* Find the child in the parent's list */ | |
1286 | prev = NULL; | |
1287 | for (vc = parent->children; vc != NULL;) | |
1288 | { | |
1289 | if (vc->child == child) | |
1290 | break; | |
1291 | prev = vc; | |
1292 | vc = vc->next; | |
1293 | } | |
1294 | ||
1295 | if (prev == NULL) | |
1296 | parent->children = vc->next; | |
1297 | else | |
1298 | prev->next = vc->next; | |
1299 | ||
1300 | } | |
1301 | \f | |
1302 | ||
1303 | /* | |
1304 | * Miscellaneous utility functions. | |
1305 | */ | |
1306 | ||
1307 | /* Allocate memory and initialize a new variable */ | |
1308 | static struct varobj * | |
1309 | new_variable (void) | |
1310 | { | |
1311 | struct varobj *var; | |
1312 | ||
1313 | var = (struct varobj *) xmalloc (sizeof (struct varobj)); | |
1314 | var->name = NULL; | |
1315 | var->obj_name = NULL; | |
1316 | var->index = -1; | |
1317 | var->type = NULL; | |
1318 | var->value = NULL; | |
1319 | var->error = 0; | |
1320 | var->num_children = -1; | |
1321 | var->parent = NULL; | |
1322 | var->children = NULL; | |
1323 | var->format = 0; | |
1324 | var->root = NULL; | |
1325 | ||
1326 | return var; | |
1327 | } | |
1328 | ||
1329 | /* Allocate memory and initialize a new root variable */ | |
1330 | static struct varobj * | |
1331 | new_root_variable (void) | |
1332 | { | |
1333 | struct varobj *var = new_variable (); | |
1334 | var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));; | |
1335 | var->root->lang = NULL; | |
1336 | var->root->exp = NULL; | |
1337 | var->root->valid_block = NULL; | |
1338 | var->root->frame = (CORE_ADDR) -1; | |
73a93a32 | 1339 | var->root->use_selected_frame = 0; |
8b93c638 JM |
1340 | var->root->rootvar = NULL; |
1341 | ||
1342 | return var; | |
1343 | } | |
1344 | ||
1345 | /* Free any allocated memory associated with VAR. */ | |
1346 | static void | |
fba45db2 | 1347 | free_variable (struct varobj *var) |
8b93c638 JM |
1348 | { |
1349 | /* Free the expression if this is a root variable. */ | |
1350 | if (var->root->rootvar == var) | |
1351 | { | |
1352 | free_current_contents ((char **) &var->root->exp); | |
1353 | FREEIF (var->root); | |
1354 | } | |
1355 | ||
1356 | FREEIF (var->name); | |
1357 | FREEIF (var->obj_name); | |
1358 | FREEIF (var); | |
1359 | } | |
1360 | ||
74b7792f AC |
1361 | static void |
1362 | do_free_variable_cleanup (void *var) | |
1363 | { | |
1364 | free_variable (var); | |
1365 | } | |
1366 | ||
1367 | static struct cleanup * | |
1368 | make_cleanup_free_variable (struct varobj *var) | |
1369 | { | |
1370 | return make_cleanup (do_free_variable_cleanup, var); | |
1371 | } | |
1372 | ||
8b93c638 JM |
1373 | /* This returns the type of the variable. This skips past typedefs |
1374 | and returns the real type of the variable. It also dereferences | |
1375 | pointers and references. */ | |
1376 | static struct type * | |
fba45db2 | 1377 | get_type (struct varobj *var) |
8b93c638 JM |
1378 | { |
1379 | struct type *type; | |
1380 | type = var->type; | |
1381 | ||
1382 | while (type != NULL && TYPE_CODE (type) == TYPE_CODE_TYPEDEF) | |
1383 | type = TYPE_TARGET_TYPE (type); | |
1384 | ||
1385 | return type; | |
1386 | } | |
1387 | ||
1388 | /* This returns the type of the variable, dereferencing pointers, too. */ | |
1389 | static struct type * | |
fba45db2 | 1390 | get_type_deref (struct varobj *var) |
8b93c638 JM |
1391 | { |
1392 | struct type *type; | |
1393 | ||
1394 | type = get_type (var); | |
1395 | ||
1396 | if (type != NULL && (TYPE_CODE (type) == TYPE_CODE_PTR | |
1397 | || TYPE_CODE (type) == TYPE_CODE_REF)) | |
1398 | type = get_target_type (type); | |
1399 | ||
1400 | return type; | |
1401 | } | |
1402 | ||
1403 | /* This returns the target type (or NULL) of TYPE, also skipping | |
1404 | past typedefs, just like get_type (). */ | |
1405 | static struct type * | |
fba45db2 | 1406 | get_target_type (struct type *type) |
8b93c638 JM |
1407 | { |
1408 | if (type != NULL) | |
1409 | { | |
1410 | type = TYPE_TARGET_TYPE (type); | |
1411 | while (type != NULL && TYPE_CODE (type) == TYPE_CODE_TYPEDEF) | |
1412 | type = TYPE_TARGET_TYPE (type); | |
1413 | } | |
1414 | ||
1415 | return type; | |
1416 | } | |
1417 | ||
1418 | /* What is the default display for this variable? We assume that | |
1419 | everything is "natural". Any exceptions? */ | |
1420 | static enum varobj_display_formats | |
fba45db2 | 1421 | variable_default_display (struct varobj *var) |
8b93c638 JM |
1422 | { |
1423 | return FORMAT_NATURAL; | |
1424 | } | |
1425 | ||
1426 | /* This function is similar to gdb's value_equal, except that this | |
1427 | one is "safe" -- it NEVER longjmps. It determines if the VAR's | |
1428 | value is the same as VAL2. */ | |
1429 | static int | |
fba45db2 | 1430 | my_value_equal (value_ptr val1, value_ptr val2, int *error2) |
8b93c638 JM |
1431 | { |
1432 | int r, err1, err2; | |
1433 | ||
1434 | *error2 = 0; | |
1435 | /* Special case: NULL values. If both are null, say | |
1436 | they're equal. */ | |
1437 | if (val1 == NULL && val2 == NULL) | |
1438 | return 1; | |
1439 | else if (val1 == NULL || val2 == NULL) | |
1440 | return 0; | |
1441 | ||
1442 | /* This is bogus, but unfortunately necessary. We must know | |
1443 | exactly what caused an error -- reading val1 or val2 -- so | |
1444 | that we can really determine if we think that something has changed. */ | |
1445 | err1 = 0; | |
1446 | err2 = 0; | |
1447 | /* We do need to catch errors here because the whole purpose | |
1448 | is to test if value_equal() has errored */ | |
1449 | if (!gdb_value_equal (val1, val1, &r)) | |
1450 | err1 = 1; | |
1451 | ||
1452 | if (!gdb_value_equal (val2, val2, &r)) | |
1453 | *error2 = err2 = 1; | |
1454 | ||
1455 | if (err1 != err2) | |
1456 | return 0; | |
1457 | ||
1458 | if (!gdb_value_equal (val1, val2, &r)) | |
1459 | { | |
1460 | /* An error occurred, this could have happened if | |
1461 | either val1 or val2 errored. ERR1 and ERR2 tell | |
1462 | us which of these it is. If both errored, then | |
1463 | we assume nothing has changed. If one of them is | |
1464 | valid, though, then something has changed. */ | |
1465 | if (err1 == err2) | |
1466 | { | |
1467 | /* both the old and new values caused errors, so | |
1468 | we say the value did not change */ | |
1469 | /* This is indeterminate, though. Perhaps we should | |
1470 | be safe and say, yes, it changed anyway?? */ | |
1471 | return 1; | |
1472 | } | |
1473 | else | |
1474 | { | |
1475 | return 0; | |
1476 | } | |
1477 | } | |
1478 | ||
1479 | return r; | |
1480 | } | |
1481 | ||
1482 | /* FIXME: The following should be generic for any pointer */ | |
1483 | static void | |
fba45db2 | 1484 | vpush (struct vstack **pstack, struct varobj *var) |
8b93c638 JM |
1485 | { |
1486 | struct vstack *s; | |
1487 | ||
1488 | s = (struct vstack *) xmalloc (sizeof (struct vstack)); | |
1489 | s->var = var; | |
1490 | s->next = *pstack; | |
1491 | *pstack = s; | |
1492 | } | |
1493 | ||
1494 | /* FIXME: The following should be generic for any pointer */ | |
1495 | static struct varobj * | |
fba45db2 | 1496 | vpop (struct vstack **pstack) |
8b93c638 JM |
1497 | { |
1498 | struct vstack *s; | |
1499 | struct varobj *v; | |
1500 | ||
1501 | if ((*pstack)->var == NULL && (*pstack)->next == NULL) | |
1502 | return NULL; | |
1503 | ||
1504 | s = *pstack; | |
1505 | v = s->var; | |
1506 | *pstack = (*pstack)->next; | |
1507 | free (s); | |
1508 | ||
1509 | return v; | |
1510 | } | |
1511 | ||
1512 | /* FIXME: The following should be generic for any pointer */ | |
1513 | static void | |
fba45db2 | 1514 | cppush (struct cpstack **pstack, char *name) |
8b93c638 JM |
1515 | { |
1516 | struct cpstack *s; | |
1517 | ||
1518 | s = (struct cpstack *) xmalloc (sizeof (struct cpstack)); | |
1519 | s->name = name; | |
1520 | s->next = *pstack; | |
1521 | *pstack = s; | |
1522 | } | |
1523 | ||
1524 | /* FIXME: The following should be generic for any pointer */ | |
1525 | static char * | |
fba45db2 | 1526 | cppop (struct cpstack **pstack) |
8b93c638 JM |
1527 | { |
1528 | struct cpstack *s; | |
1529 | char *v; | |
1530 | ||
1531 | if ((*pstack)->name == NULL && (*pstack)->next == NULL) | |
1532 | return NULL; | |
1533 | ||
1534 | s = *pstack; | |
1535 | v = s->name; | |
1536 | *pstack = (*pstack)->next; | |
1537 | free (s); | |
1538 | ||
1539 | return v; | |
1540 | } | |
1541 | \f | |
1542 | /* | |
1543 | * Language-dependencies | |
1544 | */ | |
1545 | ||
1546 | /* Common entry points */ | |
1547 | ||
1548 | /* Get the language of variable VAR. */ | |
1549 | static enum varobj_languages | |
fba45db2 | 1550 | variable_language (struct varobj *var) |
8b93c638 JM |
1551 | { |
1552 | enum varobj_languages lang; | |
1553 | ||
1554 | switch (var->root->exp->language_defn->la_language) | |
1555 | { | |
1556 | default: | |
1557 | case language_c: | |
1558 | lang = vlang_c; | |
1559 | break; | |
1560 | case language_cplus: | |
1561 | lang = vlang_cplus; | |
1562 | break; | |
1563 | case language_java: | |
1564 | lang = vlang_java; | |
1565 | break; | |
1566 | } | |
1567 | ||
1568 | return lang; | |
1569 | } | |
1570 | ||
1571 | /* Return the number of children for a given variable. | |
1572 | The result of this function is defined by the language | |
1573 | implementation. The number of children returned by this function | |
1574 | is the number of children that the user will see in the variable | |
1575 | display. */ | |
1576 | static int | |
fba45db2 | 1577 | number_of_children (struct varobj *var) |
8b93c638 JM |
1578 | { |
1579 | return (*var->root->lang->number_of_children) (var);; | |
1580 | } | |
1581 | ||
1582 | /* What is the expression for the root varobj VAR? Returns a malloc'd string. */ | |
1583 | static char * | |
fba45db2 | 1584 | name_of_variable (struct varobj *var) |
8b93c638 JM |
1585 | { |
1586 | return (*var->root->lang->name_of_variable) (var); | |
1587 | } | |
1588 | ||
1589 | /* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */ | |
1590 | static char * | |
fba45db2 | 1591 | name_of_child (struct varobj *var, int index) |
8b93c638 JM |
1592 | { |
1593 | return (*var->root->lang->name_of_child) (var, index); | |
1594 | } | |
1595 | ||
73a93a32 JI |
1596 | /* What is the value_ptr of the root variable VAR? |
1597 | TYPE_CHANGED controls what to do if the type of a | |
1598 | use_selected_frame = 1 variable changes. On input, | |
1599 | TYPE_CHANGED = 1 means discard the old varobj, and replace | |
1600 | it with this one. TYPE_CHANGED = 0 means leave it around. | |
1601 | NB: In both cases, var_handle will point to the new varobj, | |
1602 | so if you use TYPE_CHANGED = 0, you will have to stash the | |
1603 | old varobj pointer away somewhere before calling this. | |
1604 | On return, TYPE_CHANGED will be 1 if the type has changed, and | |
1605 | 0 otherwise. */ | |
8b93c638 | 1606 | static value_ptr |
fba45db2 | 1607 | value_of_root (struct varobj **var_handle, int *type_changed) |
8b93c638 | 1608 | { |
73a93a32 JI |
1609 | struct varobj *var; |
1610 | ||
1611 | if (var_handle == NULL) | |
1612 | return NULL; | |
1613 | ||
1614 | var = *var_handle; | |
1615 | ||
1616 | /* This should really be an exception, since this should | |
1617 | only get called with a root variable. */ | |
1618 | ||
1619 | if (var->root->rootvar != var) | |
1620 | return NULL; | |
1621 | ||
1622 | if (var->root->use_selected_frame) | |
1623 | { | |
1624 | struct varobj *tmp_var; | |
1625 | char *old_type, *new_type; | |
1626 | old_type = varobj_get_type (var); | |
1627 | tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0, | |
1628 | USE_SELECTED_FRAME); | |
1629 | if (tmp_var == NULL) | |
1630 | { | |
1631 | return NULL; | |
1632 | } | |
1633 | new_type = varobj_get_type (tmp_var); | |
1634 | if (strcmp(old_type, new_type) == 0) | |
1635 | { | |
1636 | varobj_delete (tmp_var, NULL, 0); | |
1637 | *type_changed = 0; | |
1638 | } | |
1639 | else | |
1640 | { | |
1641 | if (*type_changed) | |
1642 | { | |
1643 | tmp_var->obj_name = | |
1644 | savestring (var->obj_name, strlen (var->obj_name)); | |
1645 | uninstall_variable (var); | |
1646 | } | |
1647 | else | |
1648 | { | |
1649 | tmp_var->obj_name = varobj_gen_name (); | |
1650 | } | |
1651 | install_variable (tmp_var); | |
1652 | *var_handle = tmp_var; | |
1653 | *type_changed = 1; | |
1654 | } | |
1655 | } | |
1656 | else | |
1657 | { | |
1658 | *type_changed = 0; | |
1659 | } | |
1660 | ||
1661 | return (*var->root->lang->value_of_root) (var_handle); | |
8b93c638 JM |
1662 | } |
1663 | ||
1664 | /* What is the value_ptr for the INDEX'th child of PARENT? */ | |
1665 | static value_ptr | |
fba45db2 | 1666 | value_of_child (struct varobj *parent, int index) |
8b93c638 JM |
1667 | { |
1668 | value_ptr value; | |
1669 | ||
1670 | value = (*parent->root->lang->value_of_child) (parent, index); | |
1671 | ||
1672 | /* If we're being lazy, fetch the real value of the variable. */ | |
1673 | if (value != NULL && VALUE_LAZY (value)) | |
1674 | gdb_value_fetch_lazy (value); | |
1675 | ||
1676 | return value; | |
1677 | } | |
1678 | ||
1679 | /* What is the type of VAR? */ | |
1680 | static struct type * | |
fba45db2 | 1681 | type_of_child (struct varobj *var) |
8b93c638 JM |
1682 | { |
1683 | ||
1684 | /* If the child had no evaluation errors, var->value | |
1685 | will be non-NULL and contain a valid type. */ | |
1686 | if (var->value != NULL) | |
1687 | return VALUE_TYPE (var->value); | |
1688 | ||
1689 | /* Otherwise, we must compute the type. */ | |
1690 | return (*var->root->lang->type_of_child) (var->parent, var->index); | |
1691 | } | |
1692 | ||
1693 | /* Is this variable editable? Use the variable's type to make | |
1694 | this determination. */ | |
1695 | static int | |
fba45db2 | 1696 | variable_editable (struct varobj *var) |
8b93c638 JM |
1697 | { |
1698 | return (*var->root->lang->variable_editable) (var); | |
1699 | } | |
1700 | ||
1701 | /* GDB already has a command called "value_of_variable". Sigh. */ | |
1702 | static char * | |
fba45db2 | 1703 | my_value_of_variable (struct varobj *var) |
8b93c638 JM |
1704 | { |
1705 | return (*var->root->lang->value_of_variable) (var); | |
1706 | } | |
1707 | ||
1708 | /* Is VAR something that can change? Depending on language, | |
1709 | some variable's values never change. For example, | |
1710 | struct and unions never change values. */ | |
1711 | static int | |
fba45db2 | 1712 | type_changeable (struct varobj *var) |
8b93c638 JM |
1713 | { |
1714 | int r; | |
1715 | struct type *type; | |
1716 | ||
1717 | if (CPLUS_FAKE_CHILD (var)) | |
1718 | return 0; | |
1719 | ||
1720 | type = get_type (var); | |
1721 | ||
1722 | switch (TYPE_CODE (type)) | |
1723 | { | |
1724 | case TYPE_CODE_STRUCT: | |
1725 | case TYPE_CODE_UNION: | |
1726 | r = 0; | |
1727 | break; | |
1728 | ||
1729 | default: | |
1730 | r = 1; | |
1731 | } | |
1732 | ||
1733 | return r; | |
1734 | } | |
1735 | ||
1736 | /* C */ | |
1737 | static int | |
fba45db2 | 1738 | c_number_of_children (struct varobj *var) |
8b93c638 JM |
1739 | { |
1740 | struct type *type; | |
1741 | struct type *target; | |
1742 | int children; | |
1743 | ||
1744 | type = get_type (var); | |
1745 | target = get_target_type (type); | |
1746 | children = 0; | |
1747 | ||
1748 | switch (TYPE_CODE (type)) | |
1749 | { | |
1750 | case TYPE_CODE_ARRAY: | |
1751 | if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0 | |
1752 | && TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED) | |
1753 | children = TYPE_LENGTH (type) / TYPE_LENGTH (target); | |
1754 | else | |
1755 | children = -1; | |
1756 | break; | |
1757 | ||
1758 | case TYPE_CODE_STRUCT: | |
1759 | case TYPE_CODE_UNION: | |
1760 | children = TYPE_NFIELDS (type); | |
1761 | break; | |
1762 | ||
1763 | case TYPE_CODE_PTR: | |
1764 | /* This is where things get compilcated. All pointers have one child. | |
1765 | Except, of course, for struct and union ptr, which we automagically | |
1766 | dereference for the user and function ptrs, which have no children. */ | |
1767 | switch (TYPE_CODE (target)) | |
1768 | { | |
1769 | case TYPE_CODE_STRUCT: | |
1770 | case TYPE_CODE_UNION: | |
1771 | children = TYPE_NFIELDS (target); | |
1772 | break; | |
1773 | ||
1774 | case TYPE_CODE_FUNC: | |
1775 | children = 0; | |
1776 | break; | |
1777 | ||
1778 | default: | |
1779 | /* Don't dereference char* or void*. */ | |
1780 | if (TYPE_NAME (target) != NULL | |
1781 | && (STREQ (TYPE_NAME (target), "char") | |
1782 | || STREQ (TYPE_NAME (target), "void"))) | |
1783 | children = 0; | |
1784 | else | |
1785 | children = 1; | |
1786 | } | |
1787 | break; | |
1788 | ||
1789 | default: | |
1790 | /* Other types have no children */ | |
1791 | break; | |
1792 | } | |
1793 | ||
1794 | return children; | |
1795 | } | |
1796 | ||
1797 | static char * | |
fba45db2 | 1798 | c_name_of_variable (struct varobj *parent) |
8b93c638 JM |
1799 | { |
1800 | return savestring (parent->name, strlen (parent->name)); | |
1801 | } | |
1802 | ||
1803 | static char * | |
fba45db2 | 1804 | c_name_of_child (struct varobj *parent, int index) |
8b93c638 JM |
1805 | { |
1806 | struct type *type; | |
1807 | struct type *target; | |
1808 | char *name; | |
1809 | char *string; | |
1810 | ||
1811 | type = get_type (parent); | |
1812 | target = get_target_type (type); | |
1813 | ||
1814 | switch (TYPE_CODE (type)) | |
1815 | { | |
1816 | case TYPE_CODE_ARRAY: | |
1817 | { | |
1818 | /* We never get here unless parent->num_children is greater than 0... */ | |
1819 | int len = 1; | |
1820 | while ((int) pow ((double) 10, (double) len) < index) | |
1821 | len++; | |
1822 | name = (char *) xmalloc (1 + len * sizeof (char)); | |
1823 | sprintf (name, "%d", index); | |
1824 | } | |
1825 | break; | |
1826 | ||
1827 | case TYPE_CODE_STRUCT: | |
1828 | case TYPE_CODE_UNION: | |
1829 | string = TYPE_FIELD_NAME (type, index); | |
1830 | name = savestring (string, strlen (string)); | |
1831 | break; | |
1832 | ||
1833 | case TYPE_CODE_PTR: | |
1834 | switch (TYPE_CODE (target)) | |
1835 | { | |
1836 | case TYPE_CODE_STRUCT: | |
1837 | case TYPE_CODE_UNION: | |
1838 | string = TYPE_FIELD_NAME (target, index); | |
1839 | name = savestring (string, strlen (string)); | |
1840 | break; | |
1841 | ||
1842 | default: | |
1843 | name = (char *) xmalloc ((strlen (parent->name) + 2) * sizeof (char)); | |
1844 | sprintf (name, "*%s", parent->name); | |
1845 | break; | |
1846 | } | |
1847 | break; | |
1848 | ||
1849 | default: | |
1850 | /* This should not happen */ | |
1851 | name = xstrdup ("???"); | |
1852 | } | |
1853 | ||
1854 | return name; | |
1855 | } | |
1856 | ||
1857 | static value_ptr | |
fba45db2 | 1858 | c_value_of_root (struct varobj **var_handle) |
8b93c638 JM |
1859 | { |
1860 | value_ptr new_val; | |
73a93a32 | 1861 | struct varobj *var = *var_handle; |
8b93c638 JM |
1862 | struct frame_info *fi; |
1863 | int within_scope; | |
1864 | ||
73a93a32 JI |
1865 | /* Only root variables can be updated... */ |
1866 | if (var->root->rootvar != var) | |
1867 | /* Not a root var */ | |
1868 | return NULL; | |
1869 | ||
1870 | ||
8b93c638 JM |
1871 | /* Determine whether the variable is still around. */ |
1872 | if (var->root->valid_block == NULL) | |
1873 | within_scope = 1; | |
1874 | else | |
1875 | { | |
1876 | reinit_frame_cache (); | |
73a93a32 JI |
1877 | |
1878 | ||
8b93c638 | 1879 | fi = find_frame_addr_in_frame_chain (var->root->frame); |
73a93a32 | 1880 | |
8b93c638 JM |
1881 | within_scope = fi != NULL; |
1882 | /* FIXME: select_frame could fail */ | |
1883 | if (within_scope) | |
1884 | select_frame (fi, -1); | |
1885 | } | |
73a93a32 | 1886 | |
8b93c638 JM |
1887 | if (within_scope) |
1888 | { | |
73a93a32 JI |
1889 | /* We need to catch errors here, because if evaluate |
1890 | expression fails we just want to make val->error = 1 and | |
1891 | go on */ | |
8b93c638 JM |
1892 | if (gdb_evaluate_expression (var->root->exp, &new_val)) |
1893 | { | |
1894 | if (VALUE_LAZY (new_val)) | |
1895 | { | |
73a93a32 JI |
1896 | /* We need to catch errors because if |
1897 | value_fetch_lazy fails we still want to continue | |
1898 | (after making val->error = 1) */ | |
1899 | /* FIXME: Shouldn't be using VALUE_CONTENTS? The | |
1900 | comment on value_fetch_lazy() says it is only | |
1901 | called from the macro... */ | |
8b93c638 JM |
1902 | if (!gdb_value_fetch_lazy (new_val)) |
1903 | var->error = 1; | |
1904 | else | |
1905 | var->error = 0; | |
1906 | } | |
1907 | } | |
1908 | else | |
1909 | var->error = 1; | |
73a93a32 | 1910 | |
8b93c638 JM |
1911 | release_value (new_val); |
1912 | return new_val; | |
1913 | } | |
1914 | ||
1915 | return NULL; | |
1916 | } | |
1917 | ||
1918 | static value_ptr | |
fba45db2 | 1919 | c_value_of_child (struct varobj *parent, int index) |
8b93c638 | 1920 | { |
8310b29b | 1921 | value_ptr value, temp, indval; |
8b93c638 JM |
1922 | struct type *type, *target; |
1923 | char *name; | |
1924 | ||
1925 | type = get_type (parent); | |
1926 | target = get_target_type (type); | |
1927 | name = name_of_child (parent, index); | |
1928 | temp = parent->value; | |
1929 | value = NULL; | |
1930 | ||
1931 | if (temp != NULL) | |
1932 | { | |
1933 | switch (TYPE_CODE (type)) | |
1934 | { | |
1935 | case TYPE_CODE_ARRAY: | |
8310b29b FN |
1936 | #if 0 |
1937 | /* This breaks if the array lives in a (vector) register. */ | |
8b93c638 JM |
1938 | value = value_slice (temp, index, 1); |
1939 | temp = value_coerce_array (value); | |
1940 | gdb_value_ind (temp, &value); | |
8310b29b FN |
1941 | #else |
1942 | indval = value_from_longest (builtin_type_int, (LONGEST) index); | |
1943 | gdb_value_subscript (temp, indval, &value); | |
1944 | #endif | |
8b93c638 JM |
1945 | break; |
1946 | ||
1947 | case TYPE_CODE_STRUCT: | |
1948 | case TYPE_CODE_UNION: | |
1949 | value = value_struct_elt (&temp, NULL, name, NULL, "vstructure"); | |
1950 | break; | |
1951 | ||
1952 | case TYPE_CODE_PTR: | |
1953 | switch (TYPE_CODE (target)) | |
1954 | { | |
1955 | case TYPE_CODE_STRUCT: | |
1956 | case TYPE_CODE_UNION: | |
1957 | value = value_struct_elt (&temp, NULL, name, NULL, "vstructure"); | |
1958 | break; | |
1959 | ||
1960 | default: | |
1961 | gdb_value_ind (temp, &value); | |
1962 | break; | |
1963 | } | |
1964 | break; | |
1965 | ||
1966 | default: | |
1967 | break; | |
1968 | } | |
1969 | } | |
1970 | ||
1971 | if (value != NULL) | |
1972 | release_value (value); | |
1973 | ||
1974 | return value; | |
1975 | } | |
1976 | ||
1977 | static struct type * | |
fba45db2 | 1978 | c_type_of_child (struct varobj *parent, int index) |
8b93c638 JM |
1979 | { |
1980 | struct type *type; | |
1981 | char *name = name_of_child (parent, index); | |
1982 | ||
1983 | switch (TYPE_CODE (parent->type)) | |
1984 | { | |
1985 | case TYPE_CODE_ARRAY: | |
1986 | type = TYPE_TARGET_TYPE (parent->type); | |
1987 | break; | |
1988 | ||
1989 | case TYPE_CODE_STRUCT: | |
1990 | case TYPE_CODE_UNION: | |
1991 | type = lookup_struct_elt_type (parent->type, name, 0); | |
1992 | break; | |
1993 | ||
1994 | case TYPE_CODE_PTR: | |
1995 | switch (TYPE_CODE (TYPE_TARGET_TYPE (parent->type))) | |
1996 | { | |
1997 | case TYPE_CODE_STRUCT: | |
1998 | case TYPE_CODE_UNION: | |
1999 | type = lookup_struct_elt_type (parent->type, name, 0); | |
2000 | break; | |
2001 | ||
2002 | default: | |
2003 | type = TYPE_TARGET_TYPE (parent->type); | |
2004 | break; | |
2005 | } | |
2006 | break; | |
2007 | ||
2008 | default: | |
2009 | /* This should not happen as only the above types have children */ | |
2010 | warning ("Child of parent whose type does not allow children"); | |
2011 | /* FIXME: Can we still go on? */ | |
2012 | type = NULL; | |
2013 | break; | |
2014 | } | |
2015 | ||
2016 | return type; | |
2017 | } | |
2018 | ||
2019 | static int | |
fba45db2 | 2020 | c_variable_editable (struct varobj *var) |
8b93c638 JM |
2021 | { |
2022 | switch (TYPE_CODE (get_type (var))) | |
2023 | { | |
2024 | case TYPE_CODE_STRUCT: | |
2025 | case TYPE_CODE_UNION: | |
2026 | case TYPE_CODE_ARRAY: | |
2027 | case TYPE_CODE_FUNC: | |
2028 | case TYPE_CODE_MEMBER: | |
2029 | case TYPE_CODE_METHOD: | |
2030 | return 0; | |
2031 | break; | |
2032 | ||
2033 | default: | |
2034 | return 1; | |
2035 | break; | |
2036 | } | |
2037 | } | |
2038 | ||
2039 | static char * | |
fba45db2 | 2040 | c_value_of_variable (struct varobj *var) |
8b93c638 JM |
2041 | { |
2042 | struct type *type; | |
2043 | value_ptr val; | |
2044 | ||
2045 | if (var->value != NULL) | |
2046 | val = var->value; | |
2047 | else | |
2048 | { | |
2049 | /* This can happen if we attempt to get the value of a struct | |
2050 | member when the parent is an invalid pointer. */ | |
2051 | return xstrdup ("???"); | |
2052 | } | |
2053 | ||
2054 | /* BOGUS: if val_print sees a struct/class, it will print out its | |
2055 | children instead of "{...}" */ | |
2056 | type = get_type (var); | |
2057 | switch (TYPE_CODE (type)) | |
2058 | { | |
2059 | case TYPE_CODE_STRUCT: | |
2060 | case TYPE_CODE_UNION: | |
2061 | return xstrdup ("{...}"); | |
2062 | /* break; */ | |
2063 | ||
2064 | case TYPE_CODE_ARRAY: | |
2065 | { | |
2066 | char number[18]; | |
2067 | sprintf (number, "[%d]", var->num_children); | |
2068 | return xstrdup (number); | |
2069 | } | |
2070 | /* break; */ | |
2071 | ||
2072 | default: | |
2073 | { | |
2074 | long dummy; | |
2075 | struct ui_file *stb = mem_fileopen (); | |
2076 | struct cleanup *old_chain = make_cleanup_ui_file_delete (stb); | |
2077 | char *thevalue; | |
2078 | ||
2079 | if (VALUE_LAZY (val)) | |
2080 | gdb_value_fetch_lazy (val); | |
2081 | val_print (VALUE_TYPE (val), VALUE_CONTENTS_RAW (val), 0, | |
2082 | VALUE_ADDRESS (val), | |
2083 | stb, format_code[(int) var->format], 1, 0, 0); | |
2084 | thevalue = ui_file_xstrdup (stb, &dummy); | |
2085 | do_cleanups (old_chain); | |
2086 | return thevalue; | |
2087 | } | |
2088 | /* break; */ | |
2089 | } | |
2090 | } | |
2091 | \f | |
2092 | ||
2093 | /* C++ */ | |
2094 | ||
2095 | static int | |
fba45db2 | 2096 | cplus_number_of_children (struct varobj *var) |
8b93c638 JM |
2097 | { |
2098 | struct type *type; | |
2099 | int children, dont_know; | |
2100 | ||
2101 | dont_know = 1; | |
2102 | children = 0; | |
2103 | ||
2104 | if (!CPLUS_FAKE_CHILD (var)) | |
2105 | { | |
2106 | type = get_type_deref (var); | |
2107 | ||
2108 | if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) || | |
2109 | ((TYPE_CODE (type)) == TYPE_CODE_UNION)) | |
2110 | { | |
2111 | int kids[3]; | |
2112 | ||
2113 | cplus_class_num_children (type, kids); | |
2114 | if (kids[v_public] != 0) | |
2115 | children++; | |
2116 | if (kids[v_private] != 0) | |
2117 | children++; | |
2118 | if (kids[v_protected] != 0) | |
2119 | children++; | |
2120 | ||
2121 | /* Add any baseclasses */ | |
2122 | children += TYPE_N_BASECLASSES (type); | |
2123 | dont_know = 0; | |
2124 | ||
2125 | /* FIXME: save children in var */ | |
2126 | } | |
2127 | } | |
2128 | else | |
2129 | { | |
2130 | int kids[3]; | |
2131 | ||
2132 | type = get_type_deref (var->parent); | |
2133 | ||
2134 | cplus_class_num_children (type, kids); | |
2135 | if (STREQ (var->name, "public")) | |
2136 | children = kids[v_public]; | |
2137 | else if (STREQ (var->name, "private")) | |
2138 | children = kids[v_private]; | |
2139 | else | |
2140 | children = kids[v_protected]; | |
2141 | dont_know = 0; | |
2142 | } | |
2143 | ||
2144 | if (dont_know) | |
2145 | children = c_number_of_children (var); | |
2146 | ||
2147 | return children; | |
2148 | } | |
2149 | ||
2150 | /* Compute # of public, private, and protected variables in this class. | |
2151 | That means we need to descend into all baseclasses and find out | |
2152 | how many are there, too. */ | |
2153 | static void | |
2154 | cplus_class_num_children (type, children) | |
2155 | struct type *type; | |
2156 | int children[3]; | |
2157 | { | |
2158 | int i; | |
2159 | ||
2160 | children[v_public] = 0; | |
2161 | children[v_private] = 0; | |
2162 | children[v_protected] = 0; | |
2163 | ||
2164 | for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++) | |
2165 | { | |
2166 | /* If we have a virtual table pointer, omit it. */ | |
2167 | if (TYPE_VPTR_BASETYPE (type) == type | |
2168 | && TYPE_VPTR_FIELDNO (type) == i) | |
2169 | continue; | |
2170 | ||
2171 | if (TYPE_FIELD_PROTECTED (type, i)) | |
2172 | children[v_protected]++; | |
2173 | else if (TYPE_FIELD_PRIVATE (type, i)) | |
2174 | children[v_private]++; | |
2175 | else | |
2176 | children[v_public]++; | |
2177 | } | |
2178 | } | |
2179 | ||
2180 | static char * | |
fba45db2 | 2181 | cplus_name_of_variable (struct varobj *parent) |
8b93c638 JM |
2182 | { |
2183 | return c_name_of_variable (parent); | |
2184 | } | |
2185 | ||
2186 | static char * | |
fba45db2 | 2187 | cplus_name_of_child (struct varobj *parent, int index) |
8b93c638 JM |
2188 | { |
2189 | char *name; | |
2190 | struct type *type; | |
2191 | int children[3]; | |
2192 | ||
2193 | if (CPLUS_FAKE_CHILD (parent)) | |
2194 | { | |
2195 | /* Looking for children of public, private, or protected. */ | |
2196 | type = get_type_deref (parent->parent); | |
2197 | } | |
2198 | else | |
2199 | type = get_type_deref (parent); | |
2200 | ||
2201 | name = NULL; | |
2202 | switch (TYPE_CODE (type)) | |
2203 | { | |
2204 | case TYPE_CODE_STRUCT: | |
2205 | case TYPE_CODE_UNION: | |
2206 | cplus_class_num_children (type, children); | |
2207 | ||
2208 | if (CPLUS_FAKE_CHILD (parent)) | |
2209 | { | |
2210 | /* FIXME: This assumes that type orders | |
2211 | inherited, public, private, protected */ | |
2212 | int i = index + TYPE_N_BASECLASSES (type); | |
2213 | if (STREQ (parent->name, "private") || STREQ (parent->name, "protected")) | |
2214 | i += children[v_public]; | |
2215 | if (STREQ (parent->name, "protected")) | |
2216 | i += children[v_private]; | |
2217 | ||
2218 | name = TYPE_FIELD_NAME (type, i); | |
2219 | } | |
2220 | else if (index < TYPE_N_BASECLASSES (type)) | |
2221 | name = TYPE_FIELD_NAME (type, index); | |
2222 | else | |
2223 | { | |
2224 | /* Everything beyond the baseclasses can | |
2225 | only be "public", "private", or "protected" */ | |
2226 | index -= TYPE_N_BASECLASSES (type); | |
2227 | switch (index) | |
2228 | { | |
2229 | case 0: | |
2230 | if (children[v_public] != 0) | |
2231 | { | |
2232 | name = "public"; | |
2233 | break; | |
2234 | } | |
2235 | case 1: | |
2236 | if (children[v_private] != 0) | |
2237 | { | |
2238 | name = "private"; | |
2239 | break; | |
2240 | } | |
2241 | case 2: | |
2242 | if (children[v_protected] != 0) | |
2243 | { | |
2244 | name = "protected"; | |
2245 | break; | |
2246 | } | |
2247 | default: | |
2248 | /* error! */ | |
2249 | break; | |
2250 | } | |
2251 | } | |
2252 | break; | |
2253 | ||
2254 | default: | |
2255 | break; | |
2256 | } | |
2257 | ||
2258 | if (name == NULL) | |
2259 | return c_name_of_child (parent, index); | |
2260 | else | |
2261 | { | |
2262 | if (name != NULL) | |
2263 | name = savestring (name, strlen (name)); | |
2264 | } | |
2265 | ||
2266 | return name; | |
2267 | } | |
2268 | ||
2269 | static value_ptr | |
fba45db2 | 2270 | cplus_value_of_root (struct varobj **var_handle) |
8b93c638 | 2271 | { |
73a93a32 | 2272 | return c_value_of_root (var_handle); |
8b93c638 JM |
2273 | } |
2274 | ||
2275 | static value_ptr | |
fba45db2 | 2276 | cplus_value_of_child (struct varobj *parent, int index) |
8b93c638 JM |
2277 | { |
2278 | struct type *type; | |
2279 | value_ptr value; | |
2280 | char *name; | |
2281 | ||
2282 | if (CPLUS_FAKE_CHILD (parent)) | |
2283 | type = get_type_deref (parent->parent); | |
2284 | else | |
2285 | type = get_type_deref (parent); | |
2286 | ||
2287 | value = NULL; | |
2288 | name = name_of_child (parent, index); | |
2289 | ||
2290 | if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) || | |
2291 | ((TYPE_CODE (type)) == TYPE_CODE_UNION)) | |
2292 | { | |
2293 | if (CPLUS_FAKE_CHILD (parent)) | |
2294 | { | |
2295 | value_ptr temp = parent->parent->value; | |
2296 | value = value_struct_elt (&temp, NULL, name, | |
2297 | NULL, "cplus_structure"); | |
2298 | release_value (value); | |
2299 | } | |
2300 | else if (index >= TYPE_N_BASECLASSES (type)) | |
2301 | { | |
2302 | /* public, private, or protected */ | |
2303 | return NULL; | |
2304 | } | |
2305 | else | |
2306 | { | |
2307 | /* Baseclass */ | |
2308 | if (parent->value != NULL) | |
2309 | { | |
2310 | value_ptr temp; | |
2311 | ||
2312 | if (TYPE_CODE (VALUE_TYPE (parent->value)) == TYPE_CODE_PTR | |
2313 | || TYPE_CODE (VALUE_TYPE (parent->value)) == TYPE_CODE_REF) | |
2314 | gdb_value_ind (parent->value, &temp); | |
2315 | else | |
2316 | temp = parent->value; | |
2317 | ||
2318 | value = value_cast (TYPE_FIELD_TYPE (type, index), temp); | |
2319 | release_value (value); | |
2320 | } | |
2321 | } | |
2322 | } | |
2323 | ||
2324 | if (value == NULL) | |
2325 | return c_value_of_child (parent, index); | |
2326 | ||
2327 | return value; | |
2328 | } | |
2329 | ||
2330 | static struct type * | |
fba45db2 | 2331 | cplus_type_of_child (struct varobj *parent, int index) |
8b93c638 JM |
2332 | { |
2333 | struct type *type, *t; | |
2334 | ||
2335 | t = get_type_deref (parent); | |
2336 | type = NULL; | |
2337 | switch (TYPE_CODE (t)) | |
2338 | { | |
2339 | case TYPE_CODE_STRUCT: | |
2340 | case TYPE_CODE_UNION: | |
2341 | if (index >= TYPE_N_BASECLASSES (t)) | |
2342 | { | |
2343 | /* special */ | |
2344 | return NULL; | |
2345 | } | |
2346 | else | |
2347 | { | |
2348 | /* Baseclass */ | |
2349 | type = TYPE_FIELD_TYPE (t, index); | |
2350 | } | |
2351 | break; | |
2352 | ||
2353 | default: | |
2354 | break; | |
2355 | } | |
2356 | ||
2357 | if (type == NULL) | |
2358 | return c_type_of_child (parent, index); | |
2359 | ||
2360 | return type; | |
2361 | } | |
2362 | ||
2363 | static int | |
fba45db2 | 2364 | cplus_variable_editable (struct varobj *var) |
8b93c638 JM |
2365 | { |
2366 | if (CPLUS_FAKE_CHILD (var)) | |
2367 | return 0; | |
2368 | ||
2369 | return c_variable_editable (var); | |
2370 | } | |
2371 | ||
2372 | static char * | |
fba45db2 | 2373 | cplus_value_of_variable (struct varobj *var) |
8b93c638 JM |
2374 | { |
2375 | ||
2376 | /* If we have one of our special types, don't print out | |
2377 | any value. */ | |
2378 | if (CPLUS_FAKE_CHILD (var)) | |
2379 | return xstrdup (""); | |
2380 | ||
2381 | return c_value_of_variable (var); | |
2382 | } | |
2383 | \f | |
2384 | /* Java */ | |
2385 | ||
2386 | static int | |
fba45db2 | 2387 | java_number_of_children (struct varobj *var) |
8b93c638 JM |
2388 | { |
2389 | return cplus_number_of_children (var); | |
2390 | } | |
2391 | ||
2392 | static char * | |
fba45db2 | 2393 | java_name_of_variable (struct varobj *parent) |
8b93c638 JM |
2394 | { |
2395 | char *p, *name; | |
2396 | ||
2397 | name = cplus_name_of_variable (parent); | |
2398 | /* If the name has "-" in it, it is because we | |
2399 | needed to escape periods in the name... */ | |
2400 | p = name; | |
2401 | ||
2402 | while (*p != '\000') | |
2403 | { | |
2404 | if (*p == '-') | |
2405 | *p = '.'; | |
2406 | p++; | |
2407 | } | |
2408 | ||
2409 | return name; | |
2410 | } | |
2411 | ||
2412 | static char * | |
fba45db2 | 2413 | java_name_of_child (struct varobj *parent, int index) |
8b93c638 JM |
2414 | { |
2415 | char *name, *p; | |
2416 | ||
2417 | name = cplus_name_of_child (parent, index); | |
2418 | /* Escape any periods in the name... */ | |
2419 | p = name; | |
2420 | ||
2421 | while (*p != '\000') | |
2422 | { | |
2423 | if (*p == '.') | |
2424 | *p = '-'; | |
2425 | p++; | |
2426 | } | |
2427 | ||
2428 | return name; | |
2429 | } | |
2430 | ||
2431 | static value_ptr | |
fba45db2 | 2432 | java_value_of_root (struct varobj **var_handle) |
8b93c638 | 2433 | { |
73a93a32 | 2434 | return cplus_value_of_root (var_handle); |
8b93c638 JM |
2435 | } |
2436 | ||
2437 | static value_ptr | |
fba45db2 | 2438 | java_value_of_child (struct varobj *parent, int index) |
8b93c638 JM |
2439 | { |
2440 | return cplus_value_of_child (parent, index); | |
2441 | } | |
2442 | ||
2443 | static struct type * | |
fba45db2 | 2444 | java_type_of_child (struct varobj *parent, int index) |
8b93c638 JM |
2445 | { |
2446 | return cplus_type_of_child (parent, index); | |
2447 | } | |
2448 | ||
2449 | static int | |
fba45db2 | 2450 | java_variable_editable (struct varobj *var) |
8b93c638 JM |
2451 | { |
2452 | return cplus_variable_editable (var); | |
2453 | } | |
2454 | ||
2455 | static char * | |
fba45db2 | 2456 | java_value_of_variable (struct varobj *var) |
8b93c638 JM |
2457 | { |
2458 | return cplus_value_of_variable (var); | |
2459 | } | |
2460 | \f | |
2461 | extern void _initialize_varobj (void); | |
2462 | void | |
2463 | _initialize_varobj (void) | |
2464 | { | |
2465 | int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE; | |
2466 | ||
2467 | varobj_table = xmalloc (sizeof_table); | |
2468 | memset (varobj_table, 0, sizeof_table); | |
2469 | ||
2470 | add_show_from_set ( | |
2471 | add_set_cmd ("debugvarobj", class_maintenance, var_zinteger, | |
2472 | (char *) &varobjdebug, | |
2473 | "Set varobj debugging.\n\ | |
2474 | When non-zero, varobj debugging is enabled.", &setlist), | |
2475 | &showlist); | |
2476 | } |