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 */ | |
784 | /* FIXME: Callee may longjump */ | |
785 | exp = parse_exp_1 (&s, 0, 0); | |
786 | if (!gdb_evaluate_expression (exp, &value)) | |
787 | { | |
788 | /* We cannot proceed without a valid expression. */ | |
789 | FREEIF (exp); | |
790 | return 0; | |
791 | } | |
792 | ||
793 | /* If our parent is "public", "private", or "protected", we could | |
794 | be asking to modify the value of a baseclass. If so, we need to | |
795 | adjust our address by the offset of our baseclass in the subclass, | |
796 | since VALUE_ADDRESS (var->value) points at the start of the subclass. | |
797 | For some reason, value_cast doesn't take care of this properly. */ | |
798 | temp = var->value; | |
799 | if (var->parent != NULL && CPLUS_FAKE_CHILD (var->parent)) | |
800 | { | |
801 | struct varobj *super, *sub; | |
802 | struct type *type; | |
803 | super = var->parent->parent; | |
804 | sub = super->parent; | |
805 | if (sub != NULL) | |
806 | { | |
807 | /* Yes, it is a baseclass */ | |
808 | type = get_type_deref (sub); | |
809 | ||
810 | if (super->index < TYPE_N_BASECLASSES (type)) | |
811 | { | |
812 | temp = value_copy (var->value); | |
813 | for (i = 0; i < super->index; i++) | |
814 | offset += TYPE_LENGTH (TYPE_FIELD_TYPE (type, i)); | |
815 | } | |
816 | } | |
817 | } | |
818 | ||
819 | VALUE_ADDRESS (temp) += offset; | |
820 | val = value_assign (temp, value); | |
821 | VALUE_ADDRESS (val) -= offset; | |
822 | value_free (var->value); | |
823 | release_value (val); | |
824 | var->value = val; | |
825 | input_radix = saved_input_radix; | |
826 | return 1; | |
827 | } | |
828 | ||
829 | return 0; | |
830 | } | |
831 | ||
832 | /* Returns a malloc'ed list with all root variable objects */ | |
833 | int | |
834 | varobj_list (struct varobj ***varlist) | |
835 | { | |
836 | struct varobj **cv; | |
837 | struct varobj_root *croot; | |
838 | int mycount = rootcount; | |
839 | ||
840 | /* Alloc (rootcount + 1) entries for the result */ | |
841 | *varlist = xmalloc ((rootcount + 1) * sizeof (struct varobj *)); | |
842 | ||
843 | cv = *varlist; | |
844 | croot = rootlist; | |
845 | while ((croot != NULL) && (mycount > 0)) | |
846 | { | |
847 | *cv = croot->rootvar; | |
848 | mycount--; | |
849 | cv++; | |
850 | croot = croot->next; | |
851 | } | |
852 | /* Mark the end of the list */ | |
853 | *cv = NULL; | |
854 | ||
855 | if (mycount || (croot != NULL)) | |
856 | warning ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)", | |
857 | rootcount, mycount); | |
858 | ||
859 | return rootcount; | |
860 | } | |
861 | ||
862 | /* Update the values for a variable and its children. This is a | |
863 | two-pronged attack. First, re-parse the value for the root's | |
864 | expression to see if it's changed. Then go all the way | |
865 | through its children, reconstructing them and noting if they've | |
866 | changed. | |
73a93a32 JI |
867 | Return value: |
868 | -1 if there was an error updating the varobj | |
869 | -2 if the type changed | |
870 | Otherwise it is the number of children + parent changed | |
8b93c638 JM |
871 | |
872 | Only root variables can be updated... */ | |
873 | ||
874 | int | |
875 | varobj_update (struct varobj *var, struct varobj ***changelist) | |
876 | { | |
877 | int changed = 0; | |
73a93a32 | 878 | int type_changed; |
8b93c638 JM |
879 | int i; |
880 | int vleft; | |
881 | int error2; | |
882 | struct varobj *v; | |
883 | struct varobj **cv; | |
884 | struct varobj **templist; | |
885 | value_ptr new; | |
886 | struct vstack *stack = NULL; | |
887 | struct vstack *result = NULL; | |
888 | struct frame_info *old_fi; | |
889 | ||
890 | /* sanity check: have we been passed a pointer? */ | |
891 | if (changelist == NULL) | |
892 | return -1; | |
893 | ||
894 | /* Only root variables can be updated... */ | |
895 | if (var->root->rootvar != var) | |
896 | /* Not a root var */ | |
897 | return -1; | |
898 | ||
899 | /* Save the selected stack frame, since we will need to change it | |
900 | in order to evaluate expressions. */ | |
901 | old_fi = selected_frame; | |
902 | ||
903 | /* Update the root variable. value_of_root can return NULL | |
904 | if the variable is no longer around, i.e. we stepped out of | |
73a93a32 JI |
905 | the frame in which a local existed. We are letting the |
906 | value_of_root variable dispose of the varobj if the type | |
907 | has changed. */ | |
908 | type_changed = 1; | |
909 | new = value_of_root (&var, &type_changed); | |
8b93c638 | 910 | if (new == NULL) |
73a93a32 JI |
911 | { |
912 | var->error = 1; | |
913 | return -1; | |
914 | } | |
8b93c638 JM |
915 | |
916 | /* Initialize a stack for temporary results */ | |
917 | vpush (&result, NULL); | |
918 | ||
73a93a32 | 919 | if (type_changed || !my_value_equal (var->value, new, &error2)) |
8b93c638 JM |
920 | { |
921 | /* Note that it's changed There a couple of exceptions here, | |
73a93a32 JI |
922 | though. We don't want some types to be reported as |
923 | "changed". The exception to this is if this is a | |
924 | "use_selected_frame" varobj, and its type has changed. */ | |
925 | if (type_changed || type_changeable (var)) | |
8b93c638 JM |
926 | { |
927 | vpush (&result, var); | |
928 | changed++; | |
929 | } | |
930 | } | |
931 | /* error2 replaces var->error since this new value | |
932 | WILL replace the old one. */ | |
933 | var->error = error2; | |
934 | ||
935 | /* We must always keep around the new value for this root | |
936 | variable expression, or we lose the updated children! */ | |
937 | value_free (var->value); | |
938 | var->value = new; | |
939 | ||
940 | /* Initialize a stack */ | |
941 | vpush (&stack, NULL); | |
942 | ||
943 | /* Push the root's children */ | |
944 | if (var->children != NULL) | |
945 | { | |
946 | struct varobj_child *c; | |
947 | for (c = var->children; c != NULL; c = c->next) | |
948 | vpush (&stack, c->child); | |
949 | } | |
950 | ||
951 | /* Walk through the children, reconstructing them all. */ | |
952 | v = vpop (&stack); | |
953 | while (v != NULL) | |
954 | { | |
955 | /* Push any children */ | |
956 | if (v->children != NULL) | |
957 | { | |
958 | struct varobj_child *c; | |
959 | for (c = v->children; c != NULL; c = c->next) | |
960 | vpush (&stack, c->child); | |
961 | } | |
962 | ||
963 | /* Update this variable */ | |
964 | new = value_of_child (v->parent, v->index); | |
965 | if (type_changeable (v) && !my_value_equal (v->value, new, &error2)) | |
966 | { | |
967 | /* Note that it's changed */ | |
968 | vpush (&result, v); | |
969 | changed++; | |
970 | } | |
971 | /* error2 replaces v->error since this new value | |
972 | WILL replace the old one. */ | |
973 | v->error = error2; | |
974 | ||
975 | /* We must always keep new values, since children depend on it. */ | |
976 | if (v->value != NULL) | |
977 | value_free (v->value); | |
978 | v->value = new; | |
979 | ||
980 | /* Get next child */ | |
981 | v = vpop (&stack); | |
982 | } | |
983 | ||
984 | /* Alloc (changed + 1) list entries */ | |
985 | /* FIXME: add a cleanup for the allocated list(s) | |
986 | because one day the select_frame called below can longjump */ | |
987 | *changelist = xmalloc ((changed + 1) * sizeof (struct varobj *)); | |
988 | if (changed > 1) | |
989 | { | |
990 | templist = xmalloc ((changed + 1) * sizeof (struct varobj *)); | |
991 | cv = templist; | |
992 | } | |
993 | else | |
994 | cv = *changelist; | |
995 | ||
996 | /* Copy from result stack to list */ | |
997 | vleft = changed; | |
998 | *cv = vpop (&result); | |
999 | while ((*cv != NULL) && (vleft > 0)) | |
1000 | { | |
1001 | vleft--; | |
1002 | cv++; | |
1003 | *cv = vpop (&result); | |
1004 | } | |
1005 | if (vleft) | |
1006 | warning ("varobj_update: assertion failed - vleft <> 0"); | |
1007 | ||
1008 | if (changed > 1) | |
1009 | { | |
1010 | /* Now we revert the order. */ | |
1011 | for (i=0; i < changed; i++) | |
1012 | *(*changelist + i) = *(templist + changed -1 - i); | |
1013 | *(*changelist + changed) = NULL; | |
1014 | } | |
1015 | ||
1016 | /* Restore selected frame */ | |
1017 | select_frame (old_fi, -1); | |
1018 | ||
73a93a32 JI |
1019 | if (type_changed) |
1020 | return -2; | |
1021 | else | |
1022 | return changed; | |
8b93c638 JM |
1023 | } |
1024 | \f | |
1025 | ||
1026 | /* Helper functions */ | |
1027 | ||
1028 | /* | |
1029 | * Variable object construction/destruction | |
1030 | */ | |
1031 | ||
1032 | static int | |
fba45db2 KB |
1033 | delete_variable (struct cpstack **resultp, struct varobj *var, |
1034 | int only_children_p) | |
8b93c638 JM |
1035 | { |
1036 | int delcount = 0; | |
1037 | ||
1038 | delete_variable_1 (resultp, &delcount, var, | |
1039 | only_children_p, 1 /* remove_from_parent_p */ ); | |
1040 | ||
1041 | return delcount; | |
1042 | } | |
1043 | ||
1044 | /* Delete the variable object VAR and its children */ | |
1045 | /* IMPORTANT NOTE: If we delete a variable which is a child | |
1046 | and the parent is not removed we dump core. It must be always | |
1047 | initially called with remove_from_parent_p set */ | |
1048 | static void | |
fba45db2 KB |
1049 | delete_variable_1 (struct cpstack **resultp, int *delcountp, struct varobj *var, |
1050 | int only_children_p, int remove_from_parent_p) | |
8b93c638 JM |
1051 | { |
1052 | struct varobj_child *vc; | |
1053 | struct varobj_child *next; | |
1054 | ||
1055 | /* Delete any children of this variable, too. */ | |
1056 | for (vc = var->children; vc != NULL; vc = next) | |
1057 | { | |
1058 | if (!remove_from_parent_p) | |
1059 | vc->child->parent = NULL; | |
1060 | delete_variable_1 (resultp, delcountp, vc->child, 0, only_children_p); | |
1061 | next = vc->next; | |
1062 | free (vc); | |
1063 | } | |
1064 | ||
1065 | /* if we were called to delete only the children we are done here */ | |
1066 | if (only_children_p) | |
1067 | return; | |
1068 | ||
1069 | /* Otherwise, add it to the list of deleted ones and proceed to do so */ | |
73a93a32 JI |
1070 | /* If the name is null, this is a temporary variable, that has not |
1071 | yet been installed, don't report it, it belongs to the caller... */ | |
1072 | if (var->obj_name != NULL) | |
8b93c638 JM |
1073 | { |
1074 | cppush (resultp, strdup (var->obj_name)); | |
1075 | *delcountp = *delcountp + 1; | |
1076 | } | |
1077 | ||
1078 | /* If this variable has a parent, remove it from its parent's list */ | |
1079 | /* OPTIMIZATION: if the parent of this variable is also being deleted, | |
1080 | (as indicated by remove_from_parent_p) we don't bother doing an | |
1081 | expensive list search to find the element to remove when we are | |
1082 | discarding the list afterwards */ | |
1083 | if ((remove_from_parent_p) && | |
1084 | (var->parent != NULL)) | |
1085 | { | |
1086 | remove_child_from_parent (var->parent, var); | |
1087 | } | |
73a93a32 JI |
1088 | |
1089 | if (var->obj_name != NULL) | |
1090 | uninstall_variable (var); | |
8b93c638 JM |
1091 | |
1092 | /* Free memory associated with this variable */ | |
1093 | free_variable (var); | |
1094 | } | |
1095 | ||
1096 | /* Install the given variable VAR with the object name VAR->OBJ_NAME. */ | |
1097 | static int | |
fba45db2 | 1098 | install_variable (struct varobj *var) |
8b93c638 JM |
1099 | { |
1100 | struct vlist *cv; | |
1101 | struct vlist *newvl; | |
1102 | const char *chp; | |
1103 | unsigned int index = 0; | |
1104 | unsigned int i = 1; | |
1105 | ||
1106 | for (chp = var->obj_name; *chp; chp++) | |
1107 | { | |
1108 | index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE; | |
1109 | } | |
1110 | ||
1111 | cv = *(varobj_table + index); | |
1112 | while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0)) | |
1113 | cv = cv->next; | |
1114 | ||
1115 | if (cv != NULL) | |
1116 | error ("Duplicate variable object name"); | |
1117 | ||
1118 | /* Add varobj to hash table */ | |
1119 | newvl = xmalloc (sizeof (struct vlist)); | |
1120 | newvl->next = *(varobj_table + index); | |
1121 | newvl->var = var; | |
1122 | *(varobj_table + index) = newvl; | |
1123 | ||
1124 | /* If root, add varobj to root list */ | |
1125 | if (var->root->rootvar == var) | |
1126 | { | |
1127 | /* Add to list of root variables */ | |
1128 | if (rootlist == NULL) | |
1129 | var->root->next = NULL; | |
1130 | else | |
1131 | var->root->next = rootlist; | |
1132 | rootlist = var->root; | |
1133 | rootcount++; | |
1134 | } | |
1135 | ||
1136 | return 1; /* OK */ | |
1137 | } | |
1138 | ||
1139 | /* Unistall the object VAR. */ | |
1140 | static void | |
fba45db2 | 1141 | uninstall_variable (struct varobj *var) |
8b93c638 JM |
1142 | { |
1143 | struct vlist *cv; | |
1144 | struct vlist *prev; | |
1145 | struct varobj_root *cr; | |
1146 | struct varobj_root *prer; | |
1147 | const char *chp; | |
1148 | unsigned int index = 0; | |
1149 | unsigned int i = 1; | |
1150 | ||
1151 | /* Remove varobj from hash table */ | |
1152 | for (chp = var->obj_name; *chp; chp++) | |
1153 | { | |
1154 | index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE; | |
1155 | } | |
1156 | ||
1157 | cv = *(varobj_table + index); | |
1158 | prev = NULL; | |
1159 | while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0)) | |
1160 | { | |
1161 | prev = cv; | |
1162 | cv = cv->next; | |
1163 | } | |
1164 | ||
1165 | if (varobjdebug) | |
1166 | fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name); | |
1167 | ||
1168 | if (cv == NULL) | |
1169 | { | |
1170 | warning ("Assertion failed: Could not find variable object \"%s\" to delete", var->obj_name); | |
1171 | return; | |
1172 | } | |
1173 | ||
1174 | if (prev == NULL) | |
1175 | *(varobj_table + index) = cv->next; | |
1176 | else | |
1177 | prev->next = cv->next; | |
1178 | ||
1179 | free (cv); | |
1180 | ||
1181 | /* If root, remove varobj from root list */ | |
1182 | if (var->root->rootvar == var) | |
1183 | { | |
1184 | /* Remove from list of root variables */ | |
1185 | if (rootlist == var->root) | |
1186 | rootlist = var->root->next; | |
1187 | else | |
1188 | { | |
1189 | prer = NULL; | |
1190 | cr = rootlist; | |
1191 | while ((cr != NULL) && (cr->rootvar != var)) | |
1192 | { | |
1193 | prer = cr; | |
1194 | cr = cr->next; | |
1195 | } | |
1196 | if (cr == NULL) | |
1197 | { | |
1198 | warning ("Assertion failed: Could not find varobj \"%s\" in root list", var->obj_name); | |
1199 | return; | |
1200 | } | |
1201 | if (prer == NULL) | |
1202 | rootlist = NULL; | |
1203 | else | |
1204 | prer->next = cr->next; | |
1205 | } | |
1206 | rootcount--; | |
1207 | } | |
1208 | ||
1209 | } | |
1210 | ||
1211 | /* Does a child with the name NAME exist in VAR? If so, return its data. | |
1212 | If not, return NULL. */ | |
1213 | static struct varobj * | |
1214 | child_exists (var, name) | |
1215 | struct varobj *var; /* Parent */ | |
1216 | char *name; /* name of child */ | |
1217 | { | |
1218 | struct varobj_child *vc; | |
1219 | ||
1220 | for (vc = var->children; vc != NULL; vc = vc->next) | |
1221 | { | |
1222 | if (STREQ (vc->child->name, name)) | |
1223 | return vc->child; | |
1224 | } | |
1225 | ||
1226 | return NULL; | |
1227 | } | |
1228 | ||
1229 | /* Create and install a child of the parent of the given name */ | |
1230 | static struct varobj * | |
fba45db2 | 1231 | create_child (struct varobj *parent, int index, char *name) |
8b93c638 JM |
1232 | { |
1233 | struct varobj *child; | |
1234 | char *childs_name; | |
1235 | ||
1236 | child = new_variable (); | |
1237 | ||
1238 | /* name is allocated by name_of_child */ | |
1239 | child->name = name; | |
1240 | child->index = index; | |
1241 | child->value = value_of_child (parent, index); | |
1242 | if (child->value == NULL || parent->error) | |
1243 | child->error = 1; | |
1244 | child->parent = parent; | |
1245 | child->root = parent->root; | |
1246 | childs_name = (char *) xmalloc ((strlen (parent->obj_name) + strlen (name) + 2) | |
1247 | * sizeof (char)); | |
1248 | sprintf (childs_name, "%s.%s", parent->obj_name, name); | |
1249 | child->obj_name = childs_name; | |
1250 | install_variable (child); | |
1251 | ||
1252 | /* Save a pointer to this child in the parent */ | |
1253 | save_child_in_parent (parent, child); | |
1254 | ||
1255 | /* Note the type of this child */ | |
1256 | child->type = type_of_child (child); | |
1257 | ||
1258 | return child; | |
1259 | } | |
1260 | ||
1261 | /* FIXME: This should be a generic add to list */ | |
1262 | /* Save CHILD in the PARENT's data. */ | |
1263 | static void | |
fba45db2 | 1264 | save_child_in_parent (struct varobj *parent, struct varobj *child) |
8b93c638 JM |
1265 | { |
1266 | struct varobj_child *vc; | |
1267 | ||
1268 | /* Insert the child at the top */ | |
1269 | vc = parent->children; | |
1270 | parent->children = | |
1271 | (struct varobj_child *) xmalloc (sizeof (struct varobj_child)); | |
1272 | ||
1273 | parent->children->next = vc; | |
1274 | parent->children->child = child; | |
1275 | } | |
1276 | ||
1277 | /* FIXME: This should be a generic remove from list */ | |
1278 | /* Remove the CHILD from the PARENT's list of children. */ | |
1279 | static void | |
fba45db2 | 1280 | remove_child_from_parent (struct varobj *parent, struct varobj *child) |
8b93c638 JM |
1281 | { |
1282 | struct varobj_child *vc, *prev; | |
1283 | ||
1284 | /* Find the child in the parent's list */ | |
1285 | prev = NULL; | |
1286 | for (vc = parent->children; vc != NULL;) | |
1287 | { | |
1288 | if (vc->child == child) | |
1289 | break; | |
1290 | prev = vc; | |
1291 | vc = vc->next; | |
1292 | } | |
1293 | ||
1294 | if (prev == NULL) | |
1295 | parent->children = vc->next; | |
1296 | else | |
1297 | prev->next = vc->next; | |
1298 | ||
1299 | } | |
1300 | \f | |
1301 | ||
1302 | /* | |
1303 | * Miscellaneous utility functions. | |
1304 | */ | |
1305 | ||
1306 | /* Allocate memory and initialize a new variable */ | |
1307 | static struct varobj * | |
1308 | new_variable (void) | |
1309 | { | |
1310 | struct varobj *var; | |
1311 | ||
1312 | var = (struct varobj *) xmalloc (sizeof (struct varobj)); | |
1313 | var->name = NULL; | |
1314 | var->obj_name = NULL; | |
1315 | var->index = -1; | |
1316 | var->type = NULL; | |
1317 | var->value = NULL; | |
1318 | var->error = 0; | |
1319 | var->num_children = -1; | |
1320 | var->parent = NULL; | |
1321 | var->children = NULL; | |
1322 | var->format = 0; | |
1323 | var->root = NULL; | |
1324 | ||
1325 | return var; | |
1326 | } | |
1327 | ||
1328 | /* Allocate memory and initialize a new root variable */ | |
1329 | static struct varobj * | |
1330 | new_root_variable (void) | |
1331 | { | |
1332 | struct varobj *var = new_variable (); | |
1333 | var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));; | |
1334 | var->root->lang = NULL; | |
1335 | var->root->exp = NULL; | |
1336 | var->root->valid_block = NULL; | |
1337 | var->root->frame = (CORE_ADDR) -1; | |
73a93a32 | 1338 | var->root->use_selected_frame = 0; |
8b93c638 JM |
1339 | var->root->rootvar = NULL; |
1340 | ||
1341 | return var; | |
1342 | } | |
1343 | ||
1344 | /* Free any allocated memory associated with VAR. */ | |
1345 | static void | |
fba45db2 | 1346 | free_variable (struct varobj *var) |
8b93c638 JM |
1347 | { |
1348 | /* Free the expression if this is a root variable. */ | |
1349 | if (var->root->rootvar == var) | |
1350 | { | |
1351 | free_current_contents ((char **) &var->root->exp); | |
1352 | FREEIF (var->root); | |
1353 | } | |
1354 | ||
1355 | FREEIF (var->name); | |
1356 | FREEIF (var->obj_name); | |
1357 | FREEIF (var); | |
1358 | } | |
1359 | ||
74b7792f AC |
1360 | static void |
1361 | do_free_variable_cleanup (void *var) | |
1362 | { | |
1363 | free_variable (var); | |
1364 | } | |
1365 | ||
1366 | static struct cleanup * | |
1367 | make_cleanup_free_variable (struct varobj *var) | |
1368 | { | |
1369 | return make_cleanup (do_free_variable_cleanup, var); | |
1370 | } | |
1371 | ||
8b93c638 JM |
1372 | /* This returns the type of the variable. This skips past typedefs |
1373 | and returns the real type of the variable. It also dereferences | |
1374 | pointers and references. */ | |
1375 | static struct type * | |
fba45db2 | 1376 | get_type (struct varobj *var) |
8b93c638 JM |
1377 | { |
1378 | struct type *type; | |
1379 | type = var->type; | |
1380 | ||
1381 | while (type != NULL && TYPE_CODE (type) == TYPE_CODE_TYPEDEF) | |
1382 | type = TYPE_TARGET_TYPE (type); | |
1383 | ||
1384 | return type; | |
1385 | } | |
1386 | ||
1387 | /* This returns the type of the variable, dereferencing pointers, too. */ | |
1388 | static struct type * | |
fba45db2 | 1389 | get_type_deref (struct varobj *var) |
8b93c638 JM |
1390 | { |
1391 | struct type *type; | |
1392 | ||
1393 | type = get_type (var); | |
1394 | ||
1395 | if (type != NULL && (TYPE_CODE (type) == TYPE_CODE_PTR | |
1396 | || TYPE_CODE (type) == TYPE_CODE_REF)) | |
1397 | type = get_target_type (type); | |
1398 | ||
1399 | return type; | |
1400 | } | |
1401 | ||
1402 | /* This returns the target type (or NULL) of TYPE, also skipping | |
1403 | past typedefs, just like get_type (). */ | |
1404 | static struct type * | |
fba45db2 | 1405 | get_target_type (struct type *type) |
8b93c638 JM |
1406 | { |
1407 | if (type != NULL) | |
1408 | { | |
1409 | type = TYPE_TARGET_TYPE (type); | |
1410 | while (type != NULL && TYPE_CODE (type) == TYPE_CODE_TYPEDEF) | |
1411 | type = TYPE_TARGET_TYPE (type); | |
1412 | } | |
1413 | ||
1414 | return type; | |
1415 | } | |
1416 | ||
1417 | /* What is the default display for this variable? We assume that | |
1418 | everything is "natural". Any exceptions? */ | |
1419 | static enum varobj_display_formats | |
fba45db2 | 1420 | variable_default_display (struct varobj *var) |
8b93c638 JM |
1421 | { |
1422 | return FORMAT_NATURAL; | |
1423 | } | |
1424 | ||
1425 | /* This function is similar to gdb's value_equal, except that this | |
1426 | one is "safe" -- it NEVER longjmps. It determines if the VAR's | |
1427 | value is the same as VAL2. */ | |
1428 | static int | |
fba45db2 | 1429 | my_value_equal (value_ptr val1, value_ptr val2, int *error2) |
8b93c638 JM |
1430 | { |
1431 | int r, err1, err2; | |
1432 | ||
1433 | *error2 = 0; | |
1434 | /* Special case: NULL values. If both are null, say | |
1435 | they're equal. */ | |
1436 | if (val1 == NULL && val2 == NULL) | |
1437 | return 1; | |
1438 | else if (val1 == NULL || val2 == NULL) | |
1439 | return 0; | |
1440 | ||
1441 | /* This is bogus, but unfortunately necessary. We must know | |
1442 | exactly what caused an error -- reading val1 or val2 -- so | |
1443 | that we can really determine if we think that something has changed. */ | |
1444 | err1 = 0; | |
1445 | err2 = 0; | |
1446 | /* We do need to catch errors here because the whole purpose | |
1447 | is to test if value_equal() has errored */ | |
1448 | if (!gdb_value_equal (val1, val1, &r)) | |
1449 | err1 = 1; | |
1450 | ||
1451 | if (!gdb_value_equal (val2, val2, &r)) | |
1452 | *error2 = err2 = 1; | |
1453 | ||
1454 | if (err1 != err2) | |
1455 | return 0; | |
1456 | ||
1457 | if (!gdb_value_equal (val1, val2, &r)) | |
1458 | { | |
1459 | /* An error occurred, this could have happened if | |
1460 | either val1 or val2 errored. ERR1 and ERR2 tell | |
1461 | us which of these it is. If both errored, then | |
1462 | we assume nothing has changed. If one of them is | |
1463 | valid, though, then something has changed. */ | |
1464 | if (err1 == err2) | |
1465 | { | |
1466 | /* both the old and new values caused errors, so | |
1467 | we say the value did not change */ | |
1468 | /* This is indeterminate, though. Perhaps we should | |
1469 | be safe and say, yes, it changed anyway?? */ | |
1470 | return 1; | |
1471 | } | |
1472 | else | |
1473 | { | |
1474 | return 0; | |
1475 | } | |
1476 | } | |
1477 | ||
1478 | return r; | |
1479 | } | |
1480 | ||
1481 | /* FIXME: The following should be generic for any pointer */ | |
1482 | static void | |
fba45db2 | 1483 | vpush (struct vstack **pstack, struct varobj *var) |
8b93c638 JM |
1484 | { |
1485 | struct vstack *s; | |
1486 | ||
1487 | s = (struct vstack *) xmalloc (sizeof (struct vstack)); | |
1488 | s->var = var; | |
1489 | s->next = *pstack; | |
1490 | *pstack = s; | |
1491 | } | |
1492 | ||
1493 | /* FIXME: The following should be generic for any pointer */ | |
1494 | static struct varobj * | |
fba45db2 | 1495 | vpop (struct vstack **pstack) |
8b93c638 JM |
1496 | { |
1497 | struct vstack *s; | |
1498 | struct varobj *v; | |
1499 | ||
1500 | if ((*pstack)->var == NULL && (*pstack)->next == NULL) | |
1501 | return NULL; | |
1502 | ||
1503 | s = *pstack; | |
1504 | v = s->var; | |
1505 | *pstack = (*pstack)->next; | |
1506 | free (s); | |
1507 | ||
1508 | return v; | |
1509 | } | |
1510 | ||
1511 | /* FIXME: The following should be generic for any pointer */ | |
1512 | static void | |
fba45db2 | 1513 | cppush (struct cpstack **pstack, char *name) |
8b93c638 JM |
1514 | { |
1515 | struct cpstack *s; | |
1516 | ||
1517 | s = (struct cpstack *) xmalloc (sizeof (struct cpstack)); | |
1518 | s->name = name; | |
1519 | s->next = *pstack; | |
1520 | *pstack = s; | |
1521 | } | |
1522 | ||
1523 | /* FIXME: The following should be generic for any pointer */ | |
1524 | static char * | |
fba45db2 | 1525 | cppop (struct cpstack **pstack) |
8b93c638 JM |
1526 | { |
1527 | struct cpstack *s; | |
1528 | char *v; | |
1529 | ||
1530 | if ((*pstack)->name == NULL && (*pstack)->next == NULL) | |
1531 | return NULL; | |
1532 | ||
1533 | s = *pstack; | |
1534 | v = s->name; | |
1535 | *pstack = (*pstack)->next; | |
1536 | free (s); | |
1537 | ||
1538 | return v; | |
1539 | } | |
1540 | \f | |
1541 | /* | |
1542 | * Language-dependencies | |
1543 | */ | |
1544 | ||
1545 | /* Common entry points */ | |
1546 | ||
1547 | /* Get the language of variable VAR. */ | |
1548 | static enum varobj_languages | |
fba45db2 | 1549 | variable_language (struct varobj *var) |
8b93c638 JM |
1550 | { |
1551 | enum varobj_languages lang; | |
1552 | ||
1553 | switch (var->root->exp->language_defn->la_language) | |
1554 | { | |
1555 | default: | |
1556 | case language_c: | |
1557 | lang = vlang_c; | |
1558 | break; | |
1559 | case language_cplus: | |
1560 | lang = vlang_cplus; | |
1561 | break; | |
1562 | case language_java: | |
1563 | lang = vlang_java; | |
1564 | break; | |
1565 | } | |
1566 | ||
1567 | return lang; | |
1568 | } | |
1569 | ||
1570 | /* Return the number of children for a given variable. | |
1571 | The result of this function is defined by the language | |
1572 | implementation. The number of children returned by this function | |
1573 | is the number of children that the user will see in the variable | |
1574 | display. */ | |
1575 | static int | |
fba45db2 | 1576 | number_of_children (struct varobj *var) |
8b93c638 JM |
1577 | { |
1578 | return (*var->root->lang->number_of_children) (var);; | |
1579 | } | |
1580 | ||
1581 | /* What is the expression for the root varobj VAR? Returns a malloc'd string. */ | |
1582 | static char * | |
fba45db2 | 1583 | name_of_variable (struct varobj *var) |
8b93c638 JM |
1584 | { |
1585 | return (*var->root->lang->name_of_variable) (var); | |
1586 | } | |
1587 | ||
1588 | /* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */ | |
1589 | static char * | |
fba45db2 | 1590 | name_of_child (struct varobj *var, int index) |
8b93c638 JM |
1591 | { |
1592 | return (*var->root->lang->name_of_child) (var, index); | |
1593 | } | |
1594 | ||
73a93a32 JI |
1595 | /* What is the value_ptr of the root variable VAR? |
1596 | TYPE_CHANGED controls what to do if the type of a | |
1597 | use_selected_frame = 1 variable changes. On input, | |
1598 | TYPE_CHANGED = 1 means discard the old varobj, and replace | |
1599 | it with this one. TYPE_CHANGED = 0 means leave it around. | |
1600 | NB: In both cases, var_handle will point to the new varobj, | |
1601 | so if you use TYPE_CHANGED = 0, you will have to stash the | |
1602 | old varobj pointer away somewhere before calling this. | |
1603 | On return, TYPE_CHANGED will be 1 if the type has changed, and | |
1604 | 0 otherwise. */ | |
8b93c638 | 1605 | static value_ptr |
fba45db2 | 1606 | value_of_root (struct varobj **var_handle, int *type_changed) |
8b93c638 | 1607 | { |
73a93a32 JI |
1608 | struct varobj *var; |
1609 | ||
1610 | if (var_handle == NULL) | |
1611 | return NULL; | |
1612 | ||
1613 | var = *var_handle; | |
1614 | ||
1615 | /* This should really be an exception, since this should | |
1616 | only get called with a root variable. */ | |
1617 | ||
1618 | if (var->root->rootvar != var) | |
1619 | return NULL; | |
1620 | ||
1621 | if (var->root->use_selected_frame) | |
1622 | { | |
1623 | struct varobj *tmp_var; | |
1624 | char *old_type, *new_type; | |
1625 | old_type = varobj_get_type (var); | |
1626 | tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0, | |
1627 | USE_SELECTED_FRAME); | |
1628 | if (tmp_var == NULL) | |
1629 | { | |
1630 | return NULL; | |
1631 | } | |
1632 | new_type = varobj_get_type (tmp_var); | |
1633 | if (strcmp(old_type, new_type) == 0) | |
1634 | { | |
1635 | varobj_delete (tmp_var, NULL, 0); | |
1636 | *type_changed = 0; | |
1637 | } | |
1638 | else | |
1639 | { | |
1640 | if (*type_changed) | |
1641 | { | |
1642 | tmp_var->obj_name = | |
1643 | savestring (var->obj_name, strlen (var->obj_name)); | |
1644 | uninstall_variable (var); | |
1645 | } | |
1646 | else | |
1647 | { | |
1648 | tmp_var->obj_name = varobj_gen_name (); | |
1649 | } | |
1650 | install_variable (tmp_var); | |
1651 | *var_handle = tmp_var; | |
1652 | *type_changed = 1; | |
1653 | } | |
1654 | } | |
1655 | else | |
1656 | { | |
1657 | *type_changed = 0; | |
1658 | } | |
1659 | ||
1660 | return (*var->root->lang->value_of_root) (var_handle); | |
8b93c638 JM |
1661 | } |
1662 | ||
1663 | /* What is the value_ptr for the INDEX'th child of PARENT? */ | |
1664 | static value_ptr | |
fba45db2 | 1665 | value_of_child (struct varobj *parent, int index) |
8b93c638 JM |
1666 | { |
1667 | value_ptr value; | |
1668 | ||
1669 | value = (*parent->root->lang->value_of_child) (parent, index); | |
1670 | ||
1671 | /* If we're being lazy, fetch the real value of the variable. */ | |
1672 | if (value != NULL && VALUE_LAZY (value)) | |
1673 | gdb_value_fetch_lazy (value); | |
1674 | ||
1675 | return value; | |
1676 | } | |
1677 | ||
1678 | /* What is the type of VAR? */ | |
1679 | static struct type * | |
fba45db2 | 1680 | type_of_child (struct varobj *var) |
8b93c638 JM |
1681 | { |
1682 | ||
1683 | /* If the child had no evaluation errors, var->value | |
1684 | will be non-NULL and contain a valid type. */ | |
1685 | if (var->value != NULL) | |
1686 | return VALUE_TYPE (var->value); | |
1687 | ||
1688 | /* Otherwise, we must compute the type. */ | |
1689 | return (*var->root->lang->type_of_child) (var->parent, var->index); | |
1690 | } | |
1691 | ||
1692 | /* Is this variable editable? Use the variable's type to make | |
1693 | this determination. */ | |
1694 | static int | |
fba45db2 | 1695 | variable_editable (struct varobj *var) |
8b93c638 JM |
1696 | { |
1697 | return (*var->root->lang->variable_editable) (var); | |
1698 | } | |
1699 | ||
1700 | /* GDB already has a command called "value_of_variable". Sigh. */ | |
1701 | static char * | |
fba45db2 | 1702 | my_value_of_variable (struct varobj *var) |
8b93c638 JM |
1703 | { |
1704 | return (*var->root->lang->value_of_variable) (var); | |
1705 | } | |
1706 | ||
1707 | /* Is VAR something that can change? Depending on language, | |
1708 | some variable's values never change. For example, | |
1709 | struct and unions never change values. */ | |
1710 | static int | |
fba45db2 | 1711 | type_changeable (struct varobj *var) |
8b93c638 JM |
1712 | { |
1713 | int r; | |
1714 | struct type *type; | |
1715 | ||
1716 | if (CPLUS_FAKE_CHILD (var)) | |
1717 | return 0; | |
1718 | ||
1719 | type = get_type (var); | |
1720 | ||
1721 | switch (TYPE_CODE (type)) | |
1722 | { | |
1723 | case TYPE_CODE_STRUCT: | |
1724 | case TYPE_CODE_UNION: | |
1725 | r = 0; | |
1726 | break; | |
1727 | ||
1728 | default: | |
1729 | r = 1; | |
1730 | } | |
1731 | ||
1732 | return r; | |
1733 | } | |
1734 | ||
1735 | /* C */ | |
1736 | static int | |
fba45db2 | 1737 | c_number_of_children (struct varobj *var) |
8b93c638 JM |
1738 | { |
1739 | struct type *type; | |
1740 | struct type *target; | |
1741 | int children; | |
1742 | ||
1743 | type = get_type (var); | |
1744 | target = get_target_type (type); | |
1745 | children = 0; | |
1746 | ||
1747 | switch (TYPE_CODE (type)) | |
1748 | { | |
1749 | case TYPE_CODE_ARRAY: | |
1750 | if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0 | |
1751 | && TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED) | |
1752 | children = TYPE_LENGTH (type) / TYPE_LENGTH (target); | |
1753 | else | |
1754 | children = -1; | |
1755 | break; | |
1756 | ||
1757 | case TYPE_CODE_STRUCT: | |
1758 | case TYPE_CODE_UNION: | |
1759 | children = TYPE_NFIELDS (type); | |
1760 | break; | |
1761 | ||
1762 | case TYPE_CODE_PTR: | |
1763 | /* This is where things get compilcated. All pointers have one child. | |
1764 | Except, of course, for struct and union ptr, which we automagically | |
1765 | dereference for the user and function ptrs, which have no children. */ | |
1766 | switch (TYPE_CODE (target)) | |
1767 | { | |
1768 | case TYPE_CODE_STRUCT: | |
1769 | case TYPE_CODE_UNION: | |
1770 | children = TYPE_NFIELDS (target); | |
1771 | break; | |
1772 | ||
1773 | case TYPE_CODE_FUNC: | |
1774 | children = 0; | |
1775 | break; | |
1776 | ||
1777 | default: | |
1778 | /* Don't dereference char* or void*. */ | |
1779 | if (TYPE_NAME (target) != NULL | |
1780 | && (STREQ (TYPE_NAME (target), "char") | |
1781 | || STREQ (TYPE_NAME (target), "void"))) | |
1782 | children = 0; | |
1783 | else | |
1784 | children = 1; | |
1785 | } | |
1786 | break; | |
1787 | ||
1788 | default: | |
1789 | /* Other types have no children */ | |
1790 | break; | |
1791 | } | |
1792 | ||
1793 | return children; | |
1794 | } | |
1795 | ||
1796 | static char * | |
fba45db2 | 1797 | c_name_of_variable (struct varobj *parent) |
8b93c638 JM |
1798 | { |
1799 | return savestring (parent->name, strlen (parent->name)); | |
1800 | } | |
1801 | ||
1802 | static char * | |
fba45db2 | 1803 | c_name_of_child (struct varobj *parent, int index) |
8b93c638 JM |
1804 | { |
1805 | struct type *type; | |
1806 | struct type *target; | |
1807 | char *name; | |
1808 | char *string; | |
1809 | ||
1810 | type = get_type (parent); | |
1811 | target = get_target_type (type); | |
1812 | ||
1813 | switch (TYPE_CODE (type)) | |
1814 | { | |
1815 | case TYPE_CODE_ARRAY: | |
1816 | { | |
1817 | /* We never get here unless parent->num_children is greater than 0... */ | |
1818 | int len = 1; | |
1819 | while ((int) pow ((double) 10, (double) len) < index) | |
1820 | len++; | |
1821 | name = (char *) xmalloc (1 + len * sizeof (char)); | |
1822 | sprintf (name, "%d", index); | |
1823 | } | |
1824 | break; | |
1825 | ||
1826 | case TYPE_CODE_STRUCT: | |
1827 | case TYPE_CODE_UNION: | |
1828 | string = TYPE_FIELD_NAME (type, index); | |
1829 | name = savestring (string, strlen (string)); | |
1830 | break; | |
1831 | ||
1832 | case TYPE_CODE_PTR: | |
1833 | switch (TYPE_CODE (target)) | |
1834 | { | |
1835 | case TYPE_CODE_STRUCT: | |
1836 | case TYPE_CODE_UNION: | |
1837 | string = TYPE_FIELD_NAME (target, index); | |
1838 | name = savestring (string, strlen (string)); | |
1839 | break; | |
1840 | ||
1841 | default: | |
1842 | name = (char *) xmalloc ((strlen (parent->name) + 2) * sizeof (char)); | |
1843 | sprintf (name, "*%s", parent->name); | |
1844 | break; | |
1845 | } | |
1846 | break; | |
1847 | ||
1848 | default: | |
1849 | /* This should not happen */ | |
1850 | name = xstrdup ("???"); | |
1851 | } | |
1852 | ||
1853 | return name; | |
1854 | } | |
1855 | ||
1856 | static value_ptr | |
fba45db2 | 1857 | c_value_of_root (struct varobj **var_handle) |
8b93c638 JM |
1858 | { |
1859 | value_ptr new_val; | |
73a93a32 | 1860 | struct varobj *var = *var_handle; |
8b93c638 JM |
1861 | struct frame_info *fi; |
1862 | int within_scope; | |
1863 | ||
73a93a32 JI |
1864 | /* Only root variables can be updated... */ |
1865 | if (var->root->rootvar != var) | |
1866 | /* Not a root var */ | |
1867 | return NULL; | |
1868 | ||
1869 | ||
8b93c638 JM |
1870 | /* Determine whether the variable is still around. */ |
1871 | if (var->root->valid_block == NULL) | |
1872 | within_scope = 1; | |
1873 | else | |
1874 | { | |
1875 | reinit_frame_cache (); | |
73a93a32 JI |
1876 | |
1877 | ||
8b93c638 | 1878 | fi = find_frame_addr_in_frame_chain (var->root->frame); |
73a93a32 | 1879 | |
8b93c638 JM |
1880 | within_scope = fi != NULL; |
1881 | /* FIXME: select_frame could fail */ | |
1882 | if (within_scope) | |
1883 | select_frame (fi, -1); | |
1884 | } | |
73a93a32 | 1885 | |
8b93c638 JM |
1886 | if (within_scope) |
1887 | { | |
73a93a32 JI |
1888 | /* We need to catch errors here, because if evaluate |
1889 | expression fails we just want to make val->error = 1 and | |
1890 | go on */ | |
8b93c638 JM |
1891 | if (gdb_evaluate_expression (var->root->exp, &new_val)) |
1892 | { | |
1893 | if (VALUE_LAZY (new_val)) | |
1894 | { | |
73a93a32 JI |
1895 | /* We need to catch errors because if |
1896 | value_fetch_lazy fails we still want to continue | |
1897 | (after making val->error = 1) */ | |
1898 | /* FIXME: Shouldn't be using VALUE_CONTENTS? The | |
1899 | comment on value_fetch_lazy() says it is only | |
1900 | called from the macro... */ | |
8b93c638 JM |
1901 | if (!gdb_value_fetch_lazy (new_val)) |
1902 | var->error = 1; | |
1903 | else | |
1904 | var->error = 0; | |
1905 | } | |
1906 | } | |
1907 | else | |
1908 | var->error = 1; | |
73a93a32 | 1909 | |
8b93c638 JM |
1910 | release_value (new_val); |
1911 | return new_val; | |
1912 | } | |
1913 | ||
1914 | return NULL; | |
1915 | } | |
1916 | ||
1917 | static value_ptr | |
fba45db2 | 1918 | c_value_of_child (struct varobj *parent, int index) |
8b93c638 | 1919 | { |
8310b29b | 1920 | value_ptr value, temp, indval; |
8b93c638 JM |
1921 | struct type *type, *target; |
1922 | char *name; | |
1923 | ||
1924 | type = get_type (parent); | |
1925 | target = get_target_type (type); | |
1926 | name = name_of_child (parent, index); | |
1927 | temp = parent->value; | |
1928 | value = NULL; | |
1929 | ||
1930 | if (temp != NULL) | |
1931 | { | |
1932 | switch (TYPE_CODE (type)) | |
1933 | { | |
1934 | case TYPE_CODE_ARRAY: | |
8310b29b FN |
1935 | #if 0 |
1936 | /* This breaks if the array lives in a (vector) register. */ | |
8b93c638 JM |
1937 | value = value_slice (temp, index, 1); |
1938 | temp = value_coerce_array (value); | |
1939 | gdb_value_ind (temp, &value); | |
8310b29b FN |
1940 | #else |
1941 | indval = value_from_longest (builtin_type_int, (LONGEST) index); | |
1942 | gdb_value_subscript (temp, indval, &value); | |
1943 | #endif | |
8b93c638 JM |
1944 | break; |
1945 | ||
1946 | case TYPE_CODE_STRUCT: | |
1947 | case TYPE_CODE_UNION: | |
1948 | value = value_struct_elt (&temp, NULL, name, NULL, "vstructure"); | |
1949 | break; | |
1950 | ||
1951 | case TYPE_CODE_PTR: | |
1952 | switch (TYPE_CODE (target)) | |
1953 | { | |
1954 | case TYPE_CODE_STRUCT: | |
1955 | case TYPE_CODE_UNION: | |
1956 | value = value_struct_elt (&temp, NULL, name, NULL, "vstructure"); | |
1957 | break; | |
1958 | ||
1959 | default: | |
1960 | gdb_value_ind (temp, &value); | |
1961 | break; | |
1962 | } | |
1963 | break; | |
1964 | ||
1965 | default: | |
1966 | break; | |
1967 | } | |
1968 | } | |
1969 | ||
1970 | if (value != NULL) | |
1971 | release_value (value); | |
1972 | ||
1973 | return value; | |
1974 | } | |
1975 | ||
1976 | static struct type * | |
fba45db2 | 1977 | c_type_of_child (struct varobj *parent, int index) |
8b93c638 JM |
1978 | { |
1979 | struct type *type; | |
1980 | char *name = name_of_child (parent, index); | |
1981 | ||
1982 | switch (TYPE_CODE (parent->type)) | |
1983 | { | |
1984 | case TYPE_CODE_ARRAY: | |
1985 | type = TYPE_TARGET_TYPE (parent->type); | |
1986 | break; | |
1987 | ||
1988 | case TYPE_CODE_STRUCT: | |
1989 | case TYPE_CODE_UNION: | |
1990 | type = lookup_struct_elt_type (parent->type, name, 0); | |
1991 | break; | |
1992 | ||
1993 | case TYPE_CODE_PTR: | |
1994 | switch (TYPE_CODE (TYPE_TARGET_TYPE (parent->type))) | |
1995 | { | |
1996 | case TYPE_CODE_STRUCT: | |
1997 | case TYPE_CODE_UNION: | |
1998 | type = lookup_struct_elt_type (parent->type, name, 0); | |
1999 | break; | |
2000 | ||
2001 | default: | |
2002 | type = TYPE_TARGET_TYPE (parent->type); | |
2003 | break; | |
2004 | } | |
2005 | break; | |
2006 | ||
2007 | default: | |
2008 | /* This should not happen as only the above types have children */ | |
2009 | warning ("Child of parent whose type does not allow children"); | |
2010 | /* FIXME: Can we still go on? */ | |
2011 | type = NULL; | |
2012 | break; | |
2013 | } | |
2014 | ||
2015 | return type; | |
2016 | } | |
2017 | ||
2018 | static int | |
fba45db2 | 2019 | c_variable_editable (struct varobj *var) |
8b93c638 JM |
2020 | { |
2021 | switch (TYPE_CODE (get_type (var))) | |
2022 | { | |
2023 | case TYPE_CODE_STRUCT: | |
2024 | case TYPE_CODE_UNION: | |
2025 | case TYPE_CODE_ARRAY: | |
2026 | case TYPE_CODE_FUNC: | |
2027 | case TYPE_CODE_MEMBER: | |
2028 | case TYPE_CODE_METHOD: | |
2029 | return 0; | |
2030 | break; | |
2031 | ||
2032 | default: | |
2033 | return 1; | |
2034 | break; | |
2035 | } | |
2036 | } | |
2037 | ||
2038 | static char * | |
fba45db2 | 2039 | c_value_of_variable (struct varobj *var) |
8b93c638 JM |
2040 | { |
2041 | struct type *type; | |
2042 | value_ptr val; | |
2043 | ||
2044 | if (var->value != NULL) | |
2045 | val = var->value; | |
2046 | else | |
2047 | { | |
2048 | /* This can happen if we attempt to get the value of a struct | |
2049 | member when the parent is an invalid pointer. */ | |
2050 | return xstrdup ("???"); | |
2051 | } | |
2052 | ||
2053 | /* BOGUS: if val_print sees a struct/class, it will print out its | |
2054 | children instead of "{...}" */ | |
2055 | type = get_type (var); | |
2056 | switch (TYPE_CODE (type)) | |
2057 | { | |
2058 | case TYPE_CODE_STRUCT: | |
2059 | case TYPE_CODE_UNION: | |
2060 | return xstrdup ("{...}"); | |
2061 | /* break; */ | |
2062 | ||
2063 | case TYPE_CODE_ARRAY: | |
2064 | { | |
2065 | char number[18]; | |
2066 | sprintf (number, "[%d]", var->num_children); | |
2067 | return xstrdup (number); | |
2068 | } | |
2069 | /* break; */ | |
2070 | ||
2071 | default: | |
2072 | { | |
2073 | long dummy; | |
2074 | struct ui_file *stb = mem_fileopen (); | |
2075 | struct cleanup *old_chain = make_cleanup_ui_file_delete (stb); | |
2076 | char *thevalue; | |
2077 | ||
2078 | if (VALUE_LAZY (val)) | |
2079 | gdb_value_fetch_lazy (val); | |
2080 | val_print (VALUE_TYPE (val), VALUE_CONTENTS_RAW (val), 0, | |
2081 | VALUE_ADDRESS (val), | |
2082 | stb, format_code[(int) var->format], 1, 0, 0); | |
2083 | thevalue = ui_file_xstrdup (stb, &dummy); | |
2084 | do_cleanups (old_chain); | |
2085 | return thevalue; | |
2086 | } | |
2087 | /* break; */ | |
2088 | } | |
2089 | } | |
2090 | \f | |
2091 | ||
2092 | /* C++ */ | |
2093 | ||
2094 | static int | |
fba45db2 | 2095 | cplus_number_of_children (struct varobj *var) |
8b93c638 JM |
2096 | { |
2097 | struct type *type; | |
2098 | int children, dont_know; | |
2099 | ||
2100 | dont_know = 1; | |
2101 | children = 0; | |
2102 | ||
2103 | if (!CPLUS_FAKE_CHILD (var)) | |
2104 | { | |
2105 | type = get_type_deref (var); | |
2106 | ||
2107 | if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) || | |
2108 | ((TYPE_CODE (type)) == TYPE_CODE_UNION)) | |
2109 | { | |
2110 | int kids[3]; | |
2111 | ||
2112 | cplus_class_num_children (type, kids); | |
2113 | if (kids[v_public] != 0) | |
2114 | children++; | |
2115 | if (kids[v_private] != 0) | |
2116 | children++; | |
2117 | if (kids[v_protected] != 0) | |
2118 | children++; | |
2119 | ||
2120 | /* Add any baseclasses */ | |
2121 | children += TYPE_N_BASECLASSES (type); | |
2122 | dont_know = 0; | |
2123 | ||
2124 | /* FIXME: save children in var */ | |
2125 | } | |
2126 | } | |
2127 | else | |
2128 | { | |
2129 | int kids[3]; | |
2130 | ||
2131 | type = get_type_deref (var->parent); | |
2132 | ||
2133 | cplus_class_num_children (type, kids); | |
2134 | if (STREQ (var->name, "public")) | |
2135 | children = kids[v_public]; | |
2136 | else if (STREQ (var->name, "private")) | |
2137 | children = kids[v_private]; | |
2138 | else | |
2139 | children = kids[v_protected]; | |
2140 | dont_know = 0; | |
2141 | } | |
2142 | ||
2143 | if (dont_know) | |
2144 | children = c_number_of_children (var); | |
2145 | ||
2146 | return children; | |
2147 | } | |
2148 | ||
2149 | /* Compute # of public, private, and protected variables in this class. | |
2150 | That means we need to descend into all baseclasses and find out | |
2151 | how many are there, too. */ | |
2152 | static void | |
2153 | cplus_class_num_children (type, children) | |
2154 | struct type *type; | |
2155 | int children[3]; | |
2156 | { | |
2157 | int i; | |
2158 | ||
2159 | children[v_public] = 0; | |
2160 | children[v_private] = 0; | |
2161 | children[v_protected] = 0; | |
2162 | ||
2163 | for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++) | |
2164 | { | |
2165 | /* If we have a virtual table pointer, omit it. */ | |
2166 | if (TYPE_VPTR_BASETYPE (type) == type | |
2167 | && TYPE_VPTR_FIELDNO (type) == i) | |
2168 | continue; | |
2169 | ||
2170 | if (TYPE_FIELD_PROTECTED (type, i)) | |
2171 | children[v_protected]++; | |
2172 | else if (TYPE_FIELD_PRIVATE (type, i)) | |
2173 | children[v_private]++; | |
2174 | else | |
2175 | children[v_public]++; | |
2176 | } | |
2177 | } | |
2178 | ||
2179 | static char * | |
fba45db2 | 2180 | cplus_name_of_variable (struct varobj *parent) |
8b93c638 JM |
2181 | { |
2182 | return c_name_of_variable (parent); | |
2183 | } | |
2184 | ||
2185 | static char * | |
fba45db2 | 2186 | cplus_name_of_child (struct varobj *parent, int index) |
8b93c638 JM |
2187 | { |
2188 | char *name; | |
2189 | struct type *type; | |
2190 | int children[3]; | |
2191 | ||
2192 | if (CPLUS_FAKE_CHILD (parent)) | |
2193 | { | |
2194 | /* Looking for children of public, private, or protected. */ | |
2195 | type = get_type_deref (parent->parent); | |
2196 | } | |
2197 | else | |
2198 | type = get_type_deref (parent); | |
2199 | ||
2200 | name = NULL; | |
2201 | switch (TYPE_CODE (type)) | |
2202 | { | |
2203 | case TYPE_CODE_STRUCT: | |
2204 | case TYPE_CODE_UNION: | |
2205 | cplus_class_num_children (type, children); | |
2206 | ||
2207 | if (CPLUS_FAKE_CHILD (parent)) | |
2208 | { | |
2209 | /* FIXME: This assumes that type orders | |
2210 | inherited, public, private, protected */ | |
2211 | int i = index + TYPE_N_BASECLASSES (type); | |
2212 | if (STREQ (parent->name, "private") || STREQ (parent->name, "protected")) | |
2213 | i += children[v_public]; | |
2214 | if (STREQ (parent->name, "protected")) | |
2215 | i += children[v_private]; | |
2216 | ||
2217 | name = TYPE_FIELD_NAME (type, i); | |
2218 | } | |
2219 | else if (index < TYPE_N_BASECLASSES (type)) | |
2220 | name = TYPE_FIELD_NAME (type, index); | |
2221 | else | |
2222 | { | |
2223 | /* Everything beyond the baseclasses can | |
2224 | only be "public", "private", or "protected" */ | |
2225 | index -= TYPE_N_BASECLASSES (type); | |
2226 | switch (index) | |
2227 | { | |
2228 | case 0: | |
2229 | if (children[v_public] != 0) | |
2230 | { | |
2231 | name = "public"; | |
2232 | break; | |
2233 | } | |
2234 | case 1: | |
2235 | if (children[v_private] != 0) | |
2236 | { | |
2237 | name = "private"; | |
2238 | break; | |
2239 | } | |
2240 | case 2: | |
2241 | if (children[v_protected] != 0) | |
2242 | { | |
2243 | name = "protected"; | |
2244 | break; | |
2245 | } | |
2246 | default: | |
2247 | /* error! */ | |
2248 | break; | |
2249 | } | |
2250 | } | |
2251 | break; | |
2252 | ||
2253 | default: | |
2254 | break; | |
2255 | } | |
2256 | ||
2257 | if (name == NULL) | |
2258 | return c_name_of_child (parent, index); | |
2259 | else | |
2260 | { | |
2261 | if (name != NULL) | |
2262 | name = savestring (name, strlen (name)); | |
2263 | } | |
2264 | ||
2265 | return name; | |
2266 | } | |
2267 | ||
2268 | static value_ptr | |
fba45db2 | 2269 | cplus_value_of_root (struct varobj **var_handle) |
8b93c638 | 2270 | { |
73a93a32 | 2271 | return c_value_of_root (var_handle); |
8b93c638 JM |
2272 | } |
2273 | ||
2274 | static value_ptr | |
fba45db2 | 2275 | cplus_value_of_child (struct varobj *parent, int index) |
8b93c638 JM |
2276 | { |
2277 | struct type *type; | |
2278 | value_ptr value; | |
2279 | char *name; | |
2280 | ||
2281 | if (CPLUS_FAKE_CHILD (parent)) | |
2282 | type = get_type_deref (parent->parent); | |
2283 | else | |
2284 | type = get_type_deref (parent); | |
2285 | ||
2286 | value = NULL; | |
2287 | name = name_of_child (parent, index); | |
2288 | ||
2289 | if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) || | |
2290 | ((TYPE_CODE (type)) == TYPE_CODE_UNION)) | |
2291 | { | |
2292 | if (CPLUS_FAKE_CHILD (parent)) | |
2293 | { | |
2294 | value_ptr temp = parent->parent->value; | |
2295 | value = value_struct_elt (&temp, NULL, name, | |
2296 | NULL, "cplus_structure"); | |
2297 | release_value (value); | |
2298 | } | |
2299 | else if (index >= TYPE_N_BASECLASSES (type)) | |
2300 | { | |
2301 | /* public, private, or protected */ | |
2302 | return NULL; | |
2303 | } | |
2304 | else | |
2305 | { | |
2306 | /* Baseclass */ | |
2307 | if (parent->value != NULL) | |
2308 | { | |
2309 | value_ptr temp; | |
2310 | ||
2311 | if (TYPE_CODE (VALUE_TYPE (parent->value)) == TYPE_CODE_PTR | |
2312 | || TYPE_CODE (VALUE_TYPE (parent->value)) == TYPE_CODE_REF) | |
2313 | gdb_value_ind (parent->value, &temp); | |
2314 | else | |
2315 | temp = parent->value; | |
2316 | ||
2317 | value = value_cast (TYPE_FIELD_TYPE (type, index), temp); | |
2318 | release_value (value); | |
2319 | } | |
2320 | } | |
2321 | } | |
2322 | ||
2323 | if (value == NULL) | |
2324 | return c_value_of_child (parent, index); | |
2325 | ||
2326 | return value; | |
2327 | } | |
2328 | ||
2329 | static struct type * | |
fba45db2 | 2330 | cplus_type_of_child (struct varobj *parent, int index) |
8b93c638 JM |
2331 | { |
2332 | struct type *type, *t; | |
2333 | ||
2334 | t = get_type_deref (parent); | |
2335 | type = NULL; | |
2336 | switch (TYPE_CODE (t)) | |
2337 | { | |
2338 | case TYPE_CODE_STRUCT: | |
2339 | case TYPE_CODE_UNION: | |
2340 | if (index >= TYPE_N_BASECLASSES (t)) | |
2341 | { | |
2342 | /* special */ | |
2343 | return NULL; | |
2344 | } | |
2345 | else | |
2346 | { | |
2347 | /* Baseclass */ | |
2348 | type = TYPE_FIELD_TYPE (t, index); | |
2349 | } | |
2350 | break; | |
2351 | ||
2352 | default: | |
2353 | break; | |
2354 | } | |
2355 | ||
2356 | if (type == NULL) | |
2357 | return c_type_of_child (parent, index); | |
2358 | ||
2359 | return type; | |
2360 | } | |
2361 | ||
2362 | static int | |
fba45db2 | 2363 | cplus_variable_editable (struct varobj *var) |
8b93c638 JM |
2364 | { |
2365 | if (CPLUS_FAKE_CHILD (var)) | |
2366 | return 0; | |
2367 | ||
2368 | return c_variable_editable (var); | |
2369 | } | |
2370 | ||
2371 | static char * | |
fba45db2 | 2372 | cplus_value_of_variable (struct varobj *var) |
8b93c638 JM |
2373 | { |
2374 | ||
2375 | /* If we have one of our special types, don't print out | |
2376 | any value. */ | |
2377 | if (CPLUS_FAKE_CHILD (var)) | |
2378 | return xstrdup (""); | |
2379 | ||
2380 | return c_value_of_variable (var); | |
2381 | } | |
2382 | \f | |
2383 | /* Java */ | |
2384 | ||
2385 | static int | |
fba45db2 | 2386 | java_number_of_children (struct varobj *var) |
8b93c638 JM |
2387 | { |
2388 | return cplus_number_of_children (var); | |
2389 | } | |
2390 | ||
2391 | static char * | |
fba45db2 | 2392 | java_name_of_variable (struct varobj *parent) |
8b93c638 JM |
2393 | { |
2394 | char *p, *name; | |
2395 | ||
2396 | name = cplus_name_of_variable (parent); | |
2397 | /* If the name has "-" in it, it is because we | |
2398 | needed to escape periods in the name... */ | |
2399 | p = name; | |
2400 | ||
2401 | while (*p != '\000') | |
2402 | { | |
2403 | if (*p == '-') | |
2404 | *p = '.'; | |
2405 | p++; | |
2406 | } | |
2407 | ||
2408 | return name; | |
2409 | } | |
2410 | ||
2411 | static char * | |
fba45db2 | 2412 | java_name_of_child (struct varobj *parent, int index) |
8b93c638 JM |
2413 | { |
2414 | char *name, *p; | |
2415 | ||
2416 | name = cplus_name_of_child (parent, index); | |
2417 | /* Escape any periods in the name... */ | |
2418 | p = name; | |
2419 | ||
2420 | while (*p != '\000') | |
2421 | { | |
2422 | if (*p == '.') | |
2423 | *p = '-'; | |
2424 | p++; | |
2425 | } | |
2426 | ||
2427 | return name; | |
2428 | } | |
2429 | ||
2430 | static value_ptr | |
fba45db2 | 2431 | java_value_of_root (struct varobj **var_handle) |
8b93c638 | 2432 | { |
73a93a32 | 2433 | return cplus_value_of_root (var_handle); |
8b93c638 JM |
2434 | } |
2435 | ||
2436 | static value_ptr | |
fba45db2 | 2437 | java_value_of_child (struct varobj *parent, int index) |
8b93c638 JM |
2438 | { |
2439 | return cplus_value_of_child (parent, index); | |
2440 | } | |
2441 | ||
2442 | static struct type * | |
fba45db2 | 2443 | java_type_of_child (struct varobj *parent, int index) |
8b93c638 JM |
2444 | { |
2445 | return cplus_type_of_child (parent, index); | |
2446 | } | |
2447 | ||
2448 | static int | |
fba45db2 | 2449 | java_variable_editable (struct varobj *var) |
8b93c638 JM |
2450 | { |
2451 | return cplus_variable_editable (var); | |
2452 | } | |
2453 | ||
2454 | static char * | |
fba45db2 | 2455 | java_value_of_variable (struct varobj *var) |
8b93c638 JM |
2456 | { |
2457 | return cplus_value_of_variable (var); | |
2458 | } | |
2459 | \f | |
2460 | extern void _initialize_varobj (void); | |
2461 | void | |
2462 | _initialize_varobj (void) | |
2463 | { | |
2464 | int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE; | |
2465 | ||
2466 | varobj_table = xmalloc (sizeof_table); | |
2467 | memset (varobj_table, 0, sizeof_table); | |
2468 | ||
2469 | add_show_from_set ( | |
2470 | add_set_cmd ("debugvarobj", class_maintenance, var_zinteger, | |
2471 | (char *) &varobjdebug, | |
2472 | "Set varobj debugging.\n\ | |
2473 | When non-zero, varobj debugging is enabled.", &setlist), | |
2474 | &showlist); | |
2475 | } |