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