GDB (API)
|
00001 /* Implementation of the GDB variable objects API. 00002 00003 Copyright (C) 1999-2013 Free Software Foundation, Inc. 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 3 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00017 00018 #include "defs.h" 00019 #include "exceptions.h" 00020 #include "value.h" 00021 #include "expression.h" 00022 #include "frame.h" 00023 #include "language.h" 00024 #include "gdbcmd.h" 00025 #include "block.h" 00026 #include "valprint.h" 00027 00028 #include "gdb_assert.h" 00029 #include "gdb_string.h" 00030 #include "gdb_regex.h" 00031 00032 #include "varobj.h" 00033 #include "vec.h" 00034 #include "gdbthread.h" 00035 #include "inferior.h" 00036 #include "ada-varobj.h" 00037 #include "ada-lang.h" 00038 00039 #if HAVE_PYTHON 00040 #include "python/python.h" 00041 #include "python/python-internal.h" 00042 #else 00043 typedef int PyObject; 00044 #endif 00045 00046 /* The names of varobjs representing anonymous structs or unions. */ 00047 #define ANONYMOUS_STRUCT_NAME _("<anonymous struct>") 00048 #define ANONYMOUS_UNION_NAME _("<anonymous union>") 00049 00050 /* Non-zero if we want to see trace of varobj level stuff. */ 00051 00052 unsigned int varobjdebug = 0; 00053 static void 00054 show_varobjdebug (struct ui_file *file, int from_tty, 00055 struct cmd_list_element *c, const char *value) 00056 { 00057 fprintf_filtered (file, _("Varobj debugging is %s.\n"), value); 00058 } 00059 00060 /* String representations of gdb's format codes. */ 00061 char *varobj_format_string[] = 00062 { "natural", "binary", "decimal", "hexadecimal", "octal" }; 00063 00064 /* String representations of gdb's known languages. */ 00065 char *varobj_language_string[] = { "C", "C++", "Java" }; 00066 00067 /* True if we want to allow Python-based pretty-printing. */ 00068 static int pretty_printing = 0; 00069 00070 void 00071 varobj_enable_pretty_printing (void) 00072 { 00073 pretty_printing = 1; 00074 } 00075 00076 /* Data structures */ 00077 00078 /* Every root variable has one of these structures saved in its 00079 varobj. Members which must be free'd are noted. */ 00080 struct varobj_root 00081 { 00082 00083 /* Alloc'd expression for this parent. */ 00084 struct expression *exp; 00085 00086 /* Block for which this expression is valid. */ 00087 const struct block *valid_block; 00088 00089 /* The frame for this expression. This field is set iff valid_block is 00090 not NULL. */ 00091 struct frame_id frame; 00092 00093 /* The thread ID that this varobj_root belong to. This field 00094 is only valid if valid_block is not NULL. 00095 When not 0, indicates which thread 'frame' belongs to. 00096 When 0, indicates that the thread list was empty when the varobj_root 00097 was created. */ 00098 int thread_id; 00099 00100 /* If 1, the -var-update always recomputes the value in the 00101 current thread and frame. Otherwise, variable object is 00102 always updated in the specific scope/thread/frame. */ 00103 int floating; 00104 00105 /* Flag that indicates validity: set to 0 when this varobj_root refers 00106 to symbols that do not exist anymore. */ 00107 int is_valid; 00108 00109 /* Language info for this variable and its children. */ 00110 struct language_specific *lang; 00111 00112 /* The varobj for this root node. */ 00113 struct varobj *rootvar; 00114 00115 /* Next root variable */ 00116 struct varobj_root *next; 00117 }; 00118 00119 /* Every variable in the system has a structure of this type defined 00120 for it. This structure holds all information necessary to manipulate 00121 a particular object variable. Members which must be freed are noted. */ 00122 struct varobj 00123 { 00124 00125 /* Alloc'd name of the variable for this object. If this variable is a 00126 child, then this name will be the child's source name. 00127 (bar, not foo.bar). */ 00128 /* NOTE: This is the "expression". */ 00129 char *name; 00130 00131 /* Alloc'd expression for this child. Can be used to create a 00132 root variable corresponding to this child. */ 00133 char *path_expr; 00134 00135 /* The alloc'd name for this variable's object. This is here for 00136 convenience when constructing this object's children. */ 00137 char *obj_name; 00138 00139 /* Index of this variable in its parent or -1. */ 00140 int index; 00141 00142 /* The type of this variable. This can be NULL 00143 for artifial variable objects -- currently, the "accessibility" 00144 variable objects in C++. */ 00145 struct type *type; 00146 00147 /* The value of this expression or subexpression. A NULL value 00148 indicates there was an error getting this value. 00149 Invariant: if varobj_value_is_changeable_p (this) is non-zero, 00150 the value is either NULL, or not lazy. */ 00151 struct value *value; 00152 00153 /* The number of (immediate) children this variable has. */ 00154 int num_children; 00155 00156 /* If this object is a child, this points to its immediate parent. */ 00157 struct varobj *parent; 00158 00159 /* Children of this object. */ 00160 VEC (varobj_p) *children; 00161 00162 /* Whether the children of this varobj were requested. This field is 00163 used to decide if dynamic varobj should recompute their children. 00164 In the event that the frontend never asked for the children, we 00165 can avoid that. */ 00166 int children_requested; 00167 00168 /* Description of the root variable. Points to root variable for 00169 children. */ 00170 struct varobj_root *root; 00171 00172 /* The format of the output for this object. */ 00173 enum varobj_display_formats format; 00174 00175 /* Was this variable updated via a varobj_set_value operation. */ 00176 int updated; 00177 00178 /* Last print value. */ 00179 char *print_value; 00180 00181 /* Is this variable frozen. Frozen variables are never implicitly 00182 updated by -var-update * 00183 or -var-update <direct-or-indirect-parent>. */ 00184 int frozen; 00185 00186 /* Is the value of this variable intentionally not fetched? It is 00187 not fetched if either the variable is frozen, or any parents is 00188 frozen. */ 00189 int not_fetched; 00190 00191 /* Sub-range of children which the MI consumer has requested. If 00192 FROM < 0 or TO < 0, means that all children have been 00193 requested. */ 00194 int from; 00195 int to; 00196 00197 /* The pretty-printer constructor. If NULL, then the default 00198 pretty-printer will be looked up. If None, then no 00199 pretty-printer will be installed. */ 00200 PyObject *constructor; 00201 00202 /* The pretty-printer that has been constructed. If NULL, then a 00203 new printer object is needed, and one will be constructed. */ 00204 PyObject *pretty_printer; 00205 00206 /* The iterator returned by the printer's 'children' method, or NULL 00207 if not available. */ 00208 PyObject *child_iter; 00209 00210 /* We request one extra item from the iterator, so that we can 00211 report to the caller whether there are more items than we have 00212 already reported. However, we don't want to install this value 00213 when we read it, because that will mess up future updates. So, 00214 we stash it here instead. */ 00215 PyObject *saved_item; 00216 }; 00217 00218 struct cpstack 00219 { 00220 char *name; 00221 struct cpstack *next; 00222 }; 00223 00224 /* A list of varobjs */ 00225 00226 struct vlist 00227 { 00228 struct varobj *var; 00229 struct vlist *next; 00230 }; 00231 00232 /* Private function prototypes */ 00233 00234 /* Helper functions for the above subcommands. */ 00235 00236 static int delete_variable (struct cpstack **, struct varobj *, int); 00237 00238 static void delete_variable_1 (struct cpstack **, int *, 00239 struct varobj *, int, int); 00240 00241 static int install_variable (struct varobj *); 00242 00243 static void uninstall_variable (struct varobj *); 00244 00245 static struct varobj *create_child (struct varobj *, int, char *); 00246 00247 static struct varobj * 00248 create_child_with_value (struct varobj *parent, int index, char *name, 00249 struct value *value); 00250 00251 /* Utility routines */ 00252 00253 static struct varobj *new_variable (void); 00254 00255 static struct varobj *new_root_variable (void); 00256 00257 static void free_variable (struct varobj *var); 00258 00259 static struct cleanup *make_cleanup_free_variable (struct varobj *var); 00260 00261 static struct type *get_type (struct varobj *var); 00262 00263 static struct type *get_value_type (struct varobj *var); 00264 00265 static struct type *get_target_type (struct type *); 00266 00267 static enum varobj_display_formats variable_default_display (struct varobj *); 00268 00269 static void cppush (struct cpstack **pstack, char *name); 00270 00271 static char *cppop (struct cpstack **pstack); 00272 00273 static int update_type_if_necessary (struct varobj *var, 00274 struct value *new_value); 00275 00276 static int install_new_value (struct varobj *var, struct value *value, 00277 int initial); 00278 00279 /* Language-specific routines. */ 00280 00281 static enum varobj_languages variable_language (struct varobj *var); 00282 00283 static int number_of_children (struct varobj *); 00284 00285 static char *name_of_variable (struct varobj *); 00286 00287 static char *name_of_child (struct varobj *, int); 00288 00289 static struct value *value_of_root (struct varobj **var_handle, int *); 00290 00291 static struct value *value_of_child (struct varobj *parent, int index); 00292 00293 static char *my_value_of_variable (struct varobj *var, 00294 enum varobj_display_formats format); 00295 00296 static char *value_get_print_value (struct value *value, 00297 enum varobj_display_formats format, 00298 struct varobj *var); 00299 00300 static int varobj_value_is_changeable_p (struct varobj *var); 00301 00302 static int is_root_p (struct varobj *var); 00303 00304 #if HAVE_PYTHON 00305 00306 static struct varobj *varobj_add_child (struct varobj *var, 00307 char *name, 00308 struct value *value); 00309 00310 #endif /* HAVE_PYTHON */ 00311 00312 static int default_value_is_changeable_p (struct varobj *var); 00313 00314 /* C implementation */ 00315 00316 static int c_number_of_children (struct varobj *var); 00317 00318 static char *c_name_of_variable (struct varobj *parent); 00319 00320 static char *c_name_of_child (struct varobj *parent, int index); 00321 00322 static char *c_path_expr_of_child (struct varobj *child); 00323 00324 static struct value *c_value_of_child (struct varobj *parent, int index); 00325 00326 static struct type *c_type_of_child (struct varobj *parent, int index); 00327 00328 static char *c_value_of_variable (struct varobj *var, 00329 enum varobj_display_formats format); 00330 00331 /* C++ implementation */ 00332 00333 static int cplus_number_of_children (struct varobj *var); 00334 00335 static void cplus_class_num_children (struct type *type, int children[3]); 00336 00337 static char *cplus_name_of_variable (struct varobj *parent); 00338 00339 static char *cplus_name_of_child (struct varobj *parent, int index); 00340 00341 static char *cplus_path_expr_of_child (struct varobj *child); 00342 00343 static struct value *cplus_value_of_child (struct varobj *parent, int index); 00344 00345 static struct type *cplus_type_of_child (struct varobj *parent, int index); 00346 00347 static char *cplus_value_of_variable (struct varobj *var, 00348 enum varobj_display_formats format); 00349 00350 /* Java implementation */ 00351 00352 static int java_number_of_children (struct varobj *var); 00353 00354 static char *java_name_of_variable (struct varobj *parent); 00355 00356 static char *java_name_of_child (struct varobj *parent, int index); 00357 00358 static char *java_path_expr_of_child (struct varobj *child); 00359 00360 static struct value *java_value_of_child (struct varobj *parent, int index); 00361 00362 static struct type *java_type_of_child (struct varobj *parent, int index); 00363 00364 static char *java_value_of_variable (struct varobj *var, 00365 enum varobj_display_formats format); 00366 00367 /* Ada implementation */ 00368 00369 static int ada_number_of_children (struct varobj *var); 00370 00371 static char *ada_name_of_variable (struct varobj *parent); 00372 00373 static char *ada_name_of_child (struct varobj *parent, int index); 00374 00375 static char *ada_path_expr_of_child (struct varobj *child); 00376 00377 static struct value *ada_value_of_child (struct varobj *parent, int index); 00378 00379 static struct type *ada_type_of_child (struct varobj *parent, int index); 00380 00381 static char *ada_value_of_variable (struct varobj *var, 00382 enum varobj_display_formats format); 00383 00384 static int ada_value_is_changeable_p (struct varobj *var); 00385 00386 static int ada_value_has_mutated (struct varobj *var, struct value *new_val, 00387 struct type *new_type); 00388 00389 /* The language specific vector */ 00390 00391 struct language_specific 00392 { 00393 /* The number of children of PARENT. */ 00394 int (*number_of_children) (struct varobj * parent); 00395 00396 /* The name (expression) of a root varobj. */ 00397 char *(*name_of_variable) (struct varobj * parent); 00398 00399 /* The name of the INDEX'th child of PARENT. */ 00400 char *(*name_of_child) (struct varobj * parent, int index); 00401 00402 /* Returns the rooted expression of CHILD, which is a variable 00403 obtain that has some parent. */ 00404 char *(*path_expr_of_child) (struct varobj * child); 00405 00406 /* The ``struct value *'' of the INDEX'th child of PARENT. */ 00407 struct value *(*value_of_child) (struct varobj * parent, int index); 00408 00409 /* The type of the INDEX'th child of PARENT. */ 00410 struct type *(*type_of_child) (struct varobj * parent, int index); 00411 00412 /* The current value of VAR. */ 00413 char *(*value_of_variable) (struct varobj * var, 00414 enum varobj_display_formats format); 00415 00416 /* Return non-zero if changes in value of VAR must be detected and 00417 reported by -var-update. Return zero if -var-update should never 00418 report changes of such values. This makes sense for structures 00419 (since the changes in children values will be reported separately), 00420 or for artifical objects (like 'public' pseudo-field in C++). 00421 00422 Return value of 0 means that gdb need not call value_fetch_lazy 00423 for the value of this variable object. */ 00424 int (*value_is_changeable_p) (struct varobj *var); 00425 00426 /* Return nonzero if the type of VAR has mutated. 00427 00428 VAR's value is still the varobj's previous value, while NEW_VALUE 00429 is VAR's new value and NEW_TYPE is the var's new type. NEW_VALUE 00430 may be NULL indicating that there is no value available (the varobj 00431 may be out of scope, of may be the child of a null pointer, for 00432 instance). NEW_TYPE, on the other hand, must never be NULL. 00433 00434 This function should also be able to assume that var's number of 00435 children is set (not < 0). 00436 00437 Languages where types do not mutate can set this to NULL. */ 00438 int (*value_has_mutated) (struct varobj *var, struct value *new_value, 00439 struct type *new_type); 00440 }; 00441 00442 /* Array of known source language routines. */ 00443 static struct language_specific languages[vlang_end] = { 00444 /* C */ 00445 { 00446 c_number_of_children, 00447 c_name_of_variable, 00448 c_name_of_child, 00449 c_path_expr_of_child, 00450 c_value_of_child, 00451 c_type_of_child, 00452 c_value_of_variable, 00453 default_value_is_changeable_p, 00454 NULL /* value_has_mutated */} 00455 , 00456 /* C++ */ 00457 { 00458 cplus_number_of_children, 00459 cplus_name_of_variable, 00460 cplus_name_of_child, 00461 cplus_path_expr_of_child, 00462 cplus_value_of_child, 00463 cplus_type_of_child, 00464 cplus_value_of_variable, 00465 default_value_is_changeable_p, 00466 NULL /* value_has_mutated */} 00467 , 00468 /* Java */ 00469 { 00470 java_number_of_children, 00471 java_name_of_variable, 00472 java_name_of_child, 00473 java_path_expr_of_child, 00474 java_value_of_child, 00475 java_type_of_child, 00476 java_value_of_variable, 00477 default_value_is_changeable_p, 00478 NULL /* value_has_mutated */}, 00479 /* Ada */ 00480 { 00481 ada_number_of_children, 00482 ada_name_of_variable, 00483 ada_name_of_child, 00484 ada_path_expr_of_child, 00485 ada_value_of_child, 00486 ada_type_of_child, 00487 ada_value_of_variable, 00488 ada_value_is_changeable_p, 00489 ada_value_has_mutated} 00490 }; 00491 00492 /* A little convenience enum for dealing with C++/Java. */ 00493 enum vsections 00494 { 00495 v_public = 0, v_private, v_protected 00496 }; 00497 00498 /* Private data */ 00499 00500 /* Mappings of varobj_display_formats enums to gdb's format codes. */ 00501 static int format_code[] = { 0, 't', 'd', 'x', 'o' }; 00502 00503 /* Header of the list of root variable objects. */ 00504 static struct varobj_root *rootlist; 00505 00506 /* Prime number indicating the number of buckets in the hash table. */ 00507 /* A prime large enough to avoid too many colisions. */ 00508 #define VAROBJ_TABLE_SIZE 227 00509 00510 /* Pointer to the varobj hash table (built at run time). */ 00511 static struct vlist **varobj_table; 00512 00513 /* Is the variable X one of our "fake" children? */ 00514 #define CPLUS_FAKE_CHILD(x) \ 00515 ((x) != NULL && (x)->type == NULL && (x)->value == NULL) 00516 00517 00518 /* API Implementation */ 00519 static int 00520 is_root_p (struct varobj *var) 00521 { 00522 return (var->root->rootvar == var); 00523 } 00524 00525 #ifdef HAVE_PYTHON 00526 /* Helper function to install a Python environment suitable for 00527 use during operations on VAR. */ 00528 static struct cleanup * 00529 varobj_ensure_python_env (struct varobj *var) 00530 { 00531 return ensure_python_env (var->root->exp->gdbarch, 00532 var->root->exp->language_defn); 00533 } 00534 #endif 00535 00536 /* Creates a varobj (not its children). */ 00537 00538 /* Return the full FRAME which corresponds to the given CORE_ADDR 00539 or NULL if no FRAME on the chain corresponds to CORE_ADDR. */ 00540 00541 static struct frame_info * 00542 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr) 00543 { 00544 struct frame_info *frame = NULL; 00545 00546 if (frame_addr == (CORE_ADDR) 0) 00547 return NULL; 00548 00549 for (frame = get_current_frame (); 00550 frame != NULL; 00551 frame = get_prev_frame (frame)) 00552 { 00553 /* The CORE_ADDR we get as argument was parsed from a string GDB 00554 output as $fp. This output got truncated to gdbarch_addr_bit. 00555 Truncate the frame base address in the same manner before 00556 comparing it against our argument. */ 00557 CORE_ADDR frame_base = get_frame_base_address (frame); 00558 int addr_bit = gdbarch_addr_bit (get_frame_arch (frame)); 00559 00560 if (addr_bit < (sizeof (CORE_ADDR) * HOST_CHAR_BIT)) 00561 frame_base &= ((CORE_ADDR) 1 << addr_bit) - 1; 00562 00563 if (frame_base == frame_addr) 00564 return frame; 00565 } 00566 00567 return NULL; 00568 } 00569 00570 struct varobj * 00571 varobj_create (char *objname, 00572 char *expression, CORE_ADDR frame, enum varobj_type type) 00573 { 00574 struct varobj *var; 00575 struct cleanup *old_chain; 00576 00577 /* Fill out a varobj structure for the (root) variable being constructed. */ 00578 var = new_root_variable (); 00579 old_chain = make_cleanup_free_variable (var); 00580 00581 if (expression != NULL) 00582 { 00583 struct frame_info *fi; 00584 struct frame_id old_id = null_frame_id; 00585 struct block *block; 00586 const char *p; 00587 enum varobj_languages lang; 00588 struct value *value = NULL; 00589 volatile struct gdb_exception except; 00590 CORE_ADDR pc; 00591 00592 /* Parse and evaluate the expression, filling in as much of the 00593 variable's data as possible. */ 00594 00595 if (has_stack_frames ()) 00596 { 00597 /* Allow creator to specify context of variable. */ 00598 if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME)) 00599 fi = get_selected_frame (NULL); 00600 else 00601 /* FIXME: cagney/2002-11-23: This code should be doing a 00602 lookup using the frame ID and not just the frame's 00603 ``address''. This, of course, means an interface 00604 change. However, with out that interface change ISAs, 00605 such as the ia64 with its two stacks, won't work. 00606 Similar goes for the case where there is a frameless 00607 function. */ 00608 fi = find_frame_addr_in_frame_chain (frame); 00609 } 00610 else 00611 fi = NULL; 00612 00613 /* frame = -2 means always use selected frame. */ 00614 if (type == USE_SELECTED_FRAME) 00615 var->root->floating = 1; 00616 00617 pc = 0; 00618 block = NULL; 00619 if (fi != NULL) 00620 { 00621 block = get_frame_block (fi, 0); 00622 pc = get_frame_pc (fi); 00623 } 00624 00625 p = expression; 00626 innermost_block = NULL; 00627 /* Wrap the call to parse expression, so we can 00628 return a sensible error. */ 00629 TRY_CATCH (except, RETURN_MASK_ERROR) 00630 { 00631 var->root->exp = parse_exp_1 (&p, pc, block, 0); 00632 } 00633 00634 if (except.reason < 0) 00635 { 00636 do_cleanups (old_chain); 00637 return NULL; 00638 } 00639 00640 /* Don't allow variables to be created for types. */ 00641 if (var->root->exp->elts[0].opcode == OP_TYPE 00642 || var->root->exp->elts[0].opcode == OP_TYPEOF 00643 || var->root->exp->elts[0].opcode == OP_DECLTYPE) 00644 { 00645 do_cleanups (old_chain); 00646 fprintf_unfiltered (gdb_stderr, "Attempt to use a type name" 00647 " as an expression.\n"); 00648 return NULL; 00649 } 00650 00651 var->format = variable_default_display (var); 00652 var->root->valid_block = innermost_block; 00653 var->name = xstrdup (expression); 00654 /* For a root var, the name and the expr are the same. */ 00655 var->path_expr = xstrdup (expression); 00656 00657 /* When the frame is different from the current frame, 00658 we must select the appropriate frame before parsing 00659 the expression, otherwise the value will not be current. 00660 Since select_frame is so benign, just call it for all cases. */ 00661 if (innermost_block) 00662 { 00663 /* User could specify explicit FRAME-ADDR which was not found but 00664 EXPRESSION is frame specific and we would not be able to evaluate 00665 it correctly next time. With VALID_BLOCK set we must also set 00666 FRAME and THREAD_ID. */ 00667 if (fi == NULL) 00668 error (_("Failed to find the specified frame")); 00669 00670 var->root->frame = get_frame_id (fi); 00671 var->root->thread_id = pid_to_thread_id (inferior_ptid); 00672 old_id = get_frame_id (get_selected_frame (NULL)); 00673 select_frame (fi); 00674 } 00675 00676 /* We definitely need to catch errors here. 00677 If evaluate_expression succeeds we got the value we wanted. 00678 But if it fails, we still go on with a call to evaluate_type(). */ 00679 TRY_CATCH (except, RETURN_MASK_ERROR) 00680 { 00681 value = evaluate_expression (var->root->exp); 00682 } 00683 00684 if (except.reason < 0) 00685 { 00686 /* Error getting the value. Try to at least get the 00687 right type. */ 00688 struct value *type_only_value = evaluate_type (var->root->exp); 00689 00690 var->type = value_type (type_only_value); 00691 } 00692 else 00693 { 00694 int real_type_found = 0; 00695 00696 var->type = value_actual_type (value, 0, &real_type_found); 00697 if (real_type_found) 00698 value = value_cast (var->type, value); 00699 } 00700 00701 /* Set language info */ 00702 lang = variable_language (var); 00703 var->root->lang = &languages[lang]; 00704 00705 install_new_value (var, value, 1 /* Initial assignment */); 00706 00707 /* Set ourselves as our root. */ 00708 var->root->rootvar = var; 00709 00710 /* Reset the selected frame. */ 00711 if (frame_id_p (old_id)) 00712 select_frame (frame_find_by_id (old_id)); 00713 } 00714 00715 /* If the variable object name is null, that means this 00716 is a temporary variable, so don't install it. */ 00717 00718 if ((var != NULL) && (objname != NULL)) 00719 { 00720 var->obj_name = xstrdup (objname); 00721 00722 /* If a varobj name is duplicated, the install will fail so 00723 we must cleanup. */ 00724 if (!install_variable (var)) 00725 { 00726 do_cleanups (old_chain); 00727 return NULL; 00728 } 00729 } 00730 00731 discard_cleanups (old_chain); 00732 return var; 00733 } 00734 00735 /* Generates an unique name that can be used for a varobj. */ 00736 00737 char * 00738 varobj_gen_name (void) 00739 { 00740 static int id = 0; 00741 char *obj_name; 00742 00743 /* Generate a name for this object. */ 00744 id++; 00745 obj_name = xstrprintf ("var%d", id); 00746 00747 return obj_name; 00748 } 00749 00750 /* Given an OBJNAME, returns the pointer to the corresponding varobj. Call 00751 error if OBJNAME cannot be found. */ 00752 00753 struct varobj * 00754 varobj_get_handle (char *objname) 00755 { 00756 struct vlist *cv; 00757 const char *chp; 00758 unsigned int index = 0; 00759 unsigned int i = 1; 00760 00761 for (chp = objname; *chp; chp++) 00762 { 00763 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE; 00764 } 00765 00766 cv = *(varobj_table + index); 00767 while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0)) 00768 cv = cv->next; 00769 00770 if (cv == NULL) 00771 error (_("Variable object not found")); 00772 00773 return cv->var; 00774 } 00775 00776 /* Given the handle, return the name of the object. */ 00777 00778 char * 00779 varobj_get_objname (struct varobj *var) 00780 { 00781 return var->obj_name; 00782 } 00783 00784 /* Given the handle, return the expression represented by the object. */ 00785 00786 char * 00787 varobj_get_expression (struct varobj *var) 00788 { 00789 return name_of_variable (var); 00790 } 00791 00792 /* Deletes a varobj and all its children if only_children == 0, 00793 otherwise deletes only the children; returns a malloc'ed list of 00794 all the (malloc'ed) names of the variables that have been deleted 00795 (NULL terminated). */ 00796 00797 int 00798 varobj_delete (struct varobj *var, char ***dellist, int only_children) 00799 { 00800 int delcount; 00801 int mycount; 00802 struct cpstack *result = NULL; 00803 char **cp; 00804 00805 /* Initialize a stack for temporary results. */ 00806 cppush (&result, NULL); 00807 00808 if (only_children) 00809 /* Delete only the variable children. */ 00810 delcount = delete_variable (&result, var, 1 /* only the children */ ); 00811 else 00812 /* Delete the variable and all its children. */ 00813 delcount = delete_variable (&result, var, 0 /* parent+children */ ); 00814 00815 /* We may have been asked to return a list of what has been deleted. */ 00816 if (dellist != NULL) 00817 { 00818 *dellist = xmalloc ((delcount + 1) * sizeof (char *)); 00819 00820 cp = *dellist; 00821 mycount = delcount; 00822 *cp = cppop (&result); 00823 while ((*cp != NULL) && (mycount > 0)) 00824 { 00825 mycount--; 00826 cp++; 00827 *cp = cppop (&result); 00828 } 00829 00830 if (mycount || (*cp != NULL)) 00831 warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"), 00832 mycount); 00833 } 00834 00835 return delcount; 00836 } 00837 00838 #if HAVE_PYTHON 00839 00840 /* Convenience function for varobj_set_visualizer. Instantiate a 00841 pretty-printer for a given value. */ 00842 static PyObject * 00843 instantiate_pretty_printer (PyObject *constructor, struct value *value) 00844 { 00845 PyObject *val_obj = NULL; 00846 PyObject *printer; 00847 00848 val_obj = value_to_value_object (value); 00849 if (! val_obj) 00850 return NULL; 00851 00852 printer = PyObject_CallFunctionObjArgs (constructor, val_obj, NULL); 00853 Py_DECREF (val_obj); 00854 return printer; 00855 } 00856 00857 #endif 00858 00859 /* Set/Get variable object display format. */ 00860 00861 enum varobj_display_formats 00862 varobj_set_display_format (struct varobj *var, 00863 enum varobj_display_formats format) 00864 { 00865 switch (format) 00866 { 00867 case FORMAT_NATURAL: 00868 case FORMAT_BINARY: 00869 case FORMAT_DECIMAL: 00870 case FORMAT_HEXADECIMAL: 00871 case FORMAT_OCTAL: 00872 var->format = format; 00873 break; 00874 00875 default: 00876 var->format = variable_default_display (var); 00877 } 00878 00879 if (varobj_value_is_changeable_p (var) 00880 && var->value && !value_lazy (var->value)) 00881 { 00882 xfree (var->print_value); 00883 var->print_value = value_get_print_value (var->value, var->format, var); 00884 } 00885 00886 return var->format; 00887 } 00888 00889 enum varobj_display_formats 00890 varobj_get_display_format (struct varobj *var) 00891 { 00892 return var->format; 00893 } 00894 00895 char * 00896 varobj_get_display_hint (struct varobj *var) 00897 { 00898 char *result = NULL; 00899 00900 #if HAVE_PYTHON 00901 struct cleanup *back_to; 00902 00903 if (!gdb_python_initialized) 00904 return NULL; 00905 00906 back_to = varobj_ensure_python_env (var); 00907 00908 if (var->pretty_printer) 00909 result = gdbpy_get_display_hint (var->pretty_printer); 00910 00911 do_cleanups (back_to); 00912 #endif 00913 00914 return result; 00915 } 00916 00917 /* Return true if the varobj has items after TO, false otherwise. */ 00918 00919 int 00920 varobj_has_more (struct varobj *var, int to) 00921 { 00922 if (VEC_length (varobj_p, var->children) > to) 00923 return 1; 00924 return ((to == -1 || VEC_length (varobj_p, var->children) == to) 00925 && var->saved_item != NULL); 00926 } 00927 00928 /* If the variable object is bound to a specific thread, that 00929 is its evaluation can always be done in context of a frame 00930 inside that thread, returns GDB id of the thread -- which 00931 is always positive. Otherwise, returns -1. */ 00932 int 00933 varobj_get_thread_id (struct varobj *var) 00934 { 00935 if (var->root->valid_block && var->root->thread_id > 0) 00936 return var->root->thread_id; 00937 else 00938 return -1; 00939 } 00940 00941 void 00942 varobj_set_frozen (struct varobj *var, int frozen) 00943 { 00944 /* When a variable is unfrozen, we don't fetch its value. 00945 The 'not_fetched' flag remains set, so next -var-update 00946 won't complain. 00947 00948 We don't fetch the value, because for structures the client 00949 should do -var-update anyway. It would be bad to have different 00950 client-size logic for structure and other types. */ 00951 var->frozen = frozen; 00952 } 00953 00954 int 00955 varobj_get_frozen (struct varobj *var) 00956 { 00957 return var->frozen; 00958 } 00959 00960 /* A helper function that restricts a range to what is actually 00961 available in a VEC. This follows the usual rules for the meaning 00962 of FROM and TO -- if either is negative, the entire range is 00963 used. */ 00964 00965 static void 00966 restrict_range (VEC (varobj_p) *children, int *from, int *to) 00967 { 00968 if (*from < 0 || *to < 0) 00969 { 00970 *from = 0; 00971 *to = VEC_length (varobj_p, children); 00972 } 00973 else 00974 { 00975 if (*from > VEC_length (varobj_p, children)) 00976 *from = VEC_length (varobj_p, children); 00977 if (*to > VEC_length (varobj_p, children)) 00978 *to = VEC_length (varobj_p, children); 00979 if (*from > *to) 00980 *from = *to; 00981 } 00982 } 00983 00984 #if HAVE_PYTHON 00985 00986 /* A helper for update_dynamic_varobj_children that installs a new 00987 child when needed. */ 00988 00989 static void 00990 install_dynamic_child (struct varobj *var, 00991 VEC (varobj_p) **changed, 00992 VEC (varobj_p) **type_changed, 00993 VEC (varobj_p) **new, 00994 VEC (varobj_p) **unchanged, 00995 int *cchanged, 00996 int index, 00997 char *name, 00998 struct value *value) 00999 { 01000 if (VEC_length (varobj_p, var->children) < index + 1) 01001 { 01002 /* There's no child yet. */ 01003 struct varobj *child = varobj_add_child (var, name, value); 01004 01005 if (new) 01006 { 01007 VEC_safe_push (varobj_p, *new, child); 01008 *cchanged = 1; 01009 } 01010 } 01011 else 01012 { 01013 varobj_p existing = VEC_index (varobj_p, var->children, index); 01014 int type_updated = update_type_if_necessary (existing, value); 01015 01016 if (type_updated) 01017 { 01018 if (type_changed) 01019 VEC_safe_push (varobj_p, *type_changed, existing); 01020 } 01021 if (install_new_value (existing, value, 0)) 01022 { 01023 if (!type_updated && changed) 01024 VEC_safe_push (varobj_p, *changed, existing); 01025 } 01026 else if (!type_updated && unchanged) 01027 VEC_safe_push (varobj_p, *unchanged, existing); 01028 } 01029 } 01030 01031 static int 01032 dynamic_varobj_has_child_method (struct varobj *var) 01033 { 01034 struct cleanup *back_to; 01035 PyObject *printer = var->pretty_printer; 01036 int result; 01037 01038 if (!gdb_python_initialized) 01039 return 0; 01040 01041 back_to = varobj_ensure_python_env (var); 01042 result = PyObject_HasAttr (printer, gdbpy_children_cst); 01043 do_cleanups (back_to); 01044 return result; 01045 } 01046 01047 #endif 01048 01049 static int 01050 update_dynamic_varobj_children (struct varobj *var, 01051 VEC (varobj_p) **changed, 01052 VEC (varobj_p) **type_changed, 01053 VEC (varobj_p) **new, 01054 VEC (varobj_p) **unchanged, 01055 int *cchanged, 01056 int update_children, 01057 int from, 01058 int to) 01059 { 01060 #if HAVE_PYTHON 01061 struct cleanup *back_to; 01062 PyObject *children; 01063 int i; 01064 PyObject *printer = var->pretty_printer; 01065 01066 if (!gdb_python_initialized) 01067 return 0; 01068 01069 back_to = varobj_ensure_python_env (var); 01070 01071 *cchanged = 0; 01072 if (!PyObject_HasAttr (printer, gdbpy_children_cst)) 01073 { 01074 do_cleanups (back_to); 01075 return 0; 01076 } 01077 01078 if (update_children || !var->child_iter) 01079 { 01080 children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst, 01081 NULL); 01082 01083 if (!children) 01084 { 01085 gdbpy_print_stack (); 01086 error (_("Null value returned for children")); 01087 } 01088 01089 make_cleanup_py_decref (children); 01090 01091 Py_XDECREF (var->child_iter); 01092 var->child_iter = PyObject_GetIter (children); 01093 if (!var->child_iter) 01094 { 01095 gdbpy_print_stack (); 01096 error (_("Could not get children iterator")); 01097 } 01098 01099 Py_XDECREF (var->saved_item); 01100 var->saved_item = NULL; 01101 01102 i = 0; 01103 } 01104 else 01105 i = VEC_length (varobj_p, var->children); 01106 01107 /* We ask for one extra child, so that MI can report whether there 01108 are more children. */ 01109 for (; to < 0 || i < to + 1; ++i) 01110 { 01111 PyObject *item; 01112 int force_done = 0; 01113 01114 /* See if there was a leftover from last time. */ 01115 if (var->saved_item) 01116 { 01117 item = var->saved_item; 01118 var->saved_item = NULL; 01119 } 01120 else 01121 item = PyIter_Next (var->child_iter); 01122 01123 if (!item) 01124 { 01125 /* Normal end of iteration. */ 01126 if (!PyErr_Occurred ()) 01127 break; 01128 01129 /* If we got a memory error, just use the text as the 01130 item. */ 01131 if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error)) 01132 { 01133 PyObject *type, *value, *trace; 01134 char *name_str, *value_str; 01135 01136 PyErr_Fetch (&type, &value, &trace); 01137 value_str = gdbpy_exception_to_string (type, value); 01138 Py_XDECREF (type); 01139 Py_XDECREF (value); 01140 Py_XDECREF (trace); 01141 if (!value_str) 01142 { 01143 gdbpy_print_stack (); 01144 break; 01145 } 01146 01147 name_str = xstrprintf ("<error at %d>", i); 01148 item = Py_BuildValue ("(ss)", name_str, value_str); 01149 xfree (name_str); 01150 xfree (value_str); 01151 if (!item) 01152 { 01153 gdbpy_print_stack (); 01154 break; 01155 } 01156 01157 force_done = 1; 01158 } 01159 else 01160 { 01161 /* Any other kind of error. */ 01162 gdbpy_print_stack (); 01163 break; 01164 } 01165 } 01166 01167 /* We don't want to push the extra child on any report list. */ 01168 if (to < 0 || i < to) 01169 { 01170 PyObject *py_v; 01171 const char *name; 01172 struct value *v; 01173 struct cleanup *inner; 01174 int can_mention = from < 0 || i >= from; 01175 01176 inner = make_cleanup_py_decref (item); 01177 01178 if (!PyArg_ParseTuple (item, "sO", &name, &py_v)) 01179 { 01180 gdbpy_print_stack (); 01181 error (_("Invalid item from the child list")); 01182 } 01183 01184 v = convert_value_from_python (py_v); 01185 if (v == NULL) 01186 gdbpy_print_stack (); 01187 install_dynamic_child (var, can_mention ? changed : NULL, 01188 can_mention ? type_changed : NULL, 01189 can_mention ? new : NULL, 01190 can_mention ? unchanged : NULL, 01191 can_mention ? cchanged : NULL, i, 01192 xstrdup (name), v); 01193 do_cleanups (inner); 01194 } 01195 else 01196 { 01197 Py_XDECREF (var->saved_item); 01198 var->saved_item = item; 01199 01200 /* We want to truncate the child list just before this 01201 element. */ 01202 break; 01203 } 01204 01205 if (force_done) 01206 break; 01207 } 01208 01209 if (i < VEC_length (varobj_p, var->children)) 01210 { 01211 int j; 01212 01213 *cchanged = 1; 01214 for (j = i; j < VEC_length (varobj_p, var->children); ++j) 01215 varobj_delete (VEC_index (varobj_p, var->children, j), NULL, 0); 01216 VEC_truncate (varobj_p, var->children, i); 01217 } 01218 01219 /* If there are fewer children than requested, note that the list of 01220 children changed. */ 01221 if (to >= 0 && VEC_length (varobj_p, var->children) < to) 01222 *cchanged = 1; 01223 01224 var->num_children = VEC_length (varobj_p, var->children); 01225 01226 do_cleanups (back_to); 01227 01228 return 1; 01229 #else 01230 gdb_assert_not_reached ("should never be called if Python is not enabled"); 01231 #endif 01232 } 01233 01234 int 01235 varobj_get_num_children (struct varobj *var) 01236 { 01237 if (var->num_children == -1) 01238 { 01239 if (var->pretty_printer) 01240 { 01241 int dummy; 01242 01243 /* If we have a dynamic varobj, don't report -1 children. 01244 So, try to fetch some children first. */ 01245 update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL, &dummy, 01246 0, 0, 0); 01247 } 01248 else 01249 var->num_children = number_of_children (var); 01250 } 01251 01252 return var->num_children >= 0 ? var->num_children : 0; 01253 } 01254 01255 /* Creates a list of the immediate children of a variable object; 01256 the return code is the number of such children or -1 on error. */ 01257 01258 VEC (varobj_p)* 01259 varobj_list_children (struct varobj *var, int *from, int *to) 01260 { 01261 char *name; 01262 int i, children_changed; 01263 01264 var->children_requested = 1; 01265 01266 if (var->pretty_printer) 01267 { 01268 /* This, in theory, can result in the number of children changing without 01269 frontend noticing. But well, calling -var-list-children on the same 01270 varobj twice is not something a sane frontend would do. */ 01271 update_dynamic_varobj_children (var, NULL, NULL, NULL, NULL, 01272 &children_changed, 0, 0, *to); 01273 restrict_range (var->children, from, to); 01274 return var->children; 01275 } 01276 01277 if (var->num_children == -1) 01278 var->num_children = number_of_children (var); 01279 01280 /* If that failed, give up. */ 01281 if (var->num_children == -1) 01282 return var->children; 01283 01284 /* If we're called when the list of children is not yet initialized, 01285 allocate enough elements in it. */ 01286 while (VEC_length (varobj_p, var->children) < var->num_children) 01287 VEC_safe_push (varobj_p, var->children, NULL); 01288 01289 for (i = 0; i < var->num_children; i++) 01290 { 01291 varobj_p existing = VEC_index (varobj_p, var->children, i); 01292 01293 if (existing == NULL) 01294 { 01295 /* Either it's the first call to varobj_list_children for 01296 this variable object, and the child was never created, 01297 or it was explicitly deleted by the client. */ 01298 name = name_of_child (var, i); 01299 existing = create_child (var, i, name); 01300 VEC_replace (varobj_p, var->children, i, existing); 01301 } 01302 } 01303 01304 restrict_range (var->children, from, to); 01305 return var->children; 01306 } 01307 01308 #if HAVE_PYTHON 01309 01310 static struct varobj * 01311 varobj_add_child (struct varobj *var, char *name, struct value *value) 01312 { 01313 varobj_p v = create_child_with_value (var, 01314 VEC_length (varobj_p, var->children), 01315 name, value); 01316 01317 VEC_safe_push (varobj_p, var->children, v); 01318 return v; 01319 } 01320 01321 #endif /* HAVE_PYTHON */ 01322 01323 /* Obtain the type of an object Variable as a string similar to the one gdb 01324 prints on the console. */ 01325 01326 char * 01327 varobj_get_type (struct varobj *var) 01328 { 01329 /* For the "fake" variables, do not return a type. (It's type is 01330 NULL, too.) 01331 Do not return a type for invalid variables as well. */ 01332 if (CPLUS_FAKE_CHILD (var) || !var->root->is_valid) 01333 return NULL; 01334 01335 return type_to_string (var->type); 01336 } 01337 01338 /* Obtain the type of an object variable. */ 01339 01340 struct type * 01341 varobj_get_gdb_type (struct varobj *var) 01342 { 01343 return var->type; 01344 } 01345 01346 /* Is VAR a path expression parent, i.e., can it be used to construct 01347 a valid path expression? */ 01348 01349 static int 01350 is_path_expr_parent (struct varobj *var) 01351 { 01352 struct type *type; 01353 01354 /* "Fake" children are not path_expr parents. */ 01355 if (CPLUS_FAKE_CHILD (var)) 01356 return 0; 01357 01358 type = get_value_type (var); 01359 01360 /* Anonymous unions and structs are also not path_expr parents. */ 01361 return !((TYPE_CODE (type) == TYPE_CODE_STRUCT 01362 || TYPE_CODE (type) == TYPE_CODE_UNION) 01363 && TYPE_NAME (type) == NULL); 01364 } 01365 01366 /* Return the path expression parent for VAR. */ 01367 01368 static struct varobj * 01369 get_path_expr_parent (struct varobj *var) 01370 { 01371 struct varobj *parent = var; 01372 01373 while (!is_root_p (parent) && !is_path_expr_parent (parent)) 01374 parent = parent->parent; 01375 01376 return parent; 01377 } 01378 01379 /* Return a pointer to the full rooted expression of varobj VAR. 01380 If it has not been computed yet, compute it. */ 01381 char * 01382 varobj_get_path_expr (struct varobj *var) 01383 { 01384 if (var->path_expr != NULL) 01385 return var->path_expr; 01386 else 01387 { 01388 /* For root varobjs, we initialize path_expr 01389 when creating varobj, so here it should be 01390 child varobj. */ 01391 gdb_assert (!is_root_p (var)); 01392 return (*var->root->lang->path_expr_of_child) (var); 01393 } 01394 } 01395 01396 enum varobj_languages 01397 varobj_get_language (struct varobj *var) 01398 { 01399 return variable_language (var); 01400 } 01401 01402 int 01403 varobj_get_attributes (struct varobj *var) 01404 { 01405 int attributes = 0; 01406 01407 if (varobj_editable_p (var)) 01408 /* FIXME: define masks for attributes. */ 01409 attributes |= 0x00000001; /* Editable */ 01410 01411 return attributes; 01412 } 01413 01414 int 01415 varobj_pretty_printed_p (struct varobj *var) 01416 { 01417 return var->pretty_printer != NULL; 01418 } 01419 01420 char * 01421 varobj_get_formatted_value (struct varobj *var, 01422 enum varobj_display_formats format) 01423 { 01424 return my_value_of_variable (var, format); 01425 } 01426 01427 char * 01428 varobj_get_value (struct varobj *var) 01429 { 01430 return my_value_of_variable (var, var->format); 01431 } 01432 01433 /* Set the value of an object variable (if it is editable) to the 01434 value of the given expression. */ 01435 /* Note: Invokes functions that can call error(). */ 01436 01437 int 01438 varobj_set_value (struct varobj *var, char *expression) 01439 { 01440 struct value *val = NULL; /* Initialize to keep gcc happy. */ 01441 /* The argument "expression" contains the variable's new value. 01442 We need to first construct a legal expression for this -- ugh! */ 01443 /* Does this cover all the bases? */ 01444 struct expression *exp; 01445 struct value *value = NULL; /* Initialize to keep gcc happy. */ 01446 int saved_input_radix = input_radix; 01447 const char *s = expression; 01448 volatile struct gdb_exception except; 01449 01450 gdb_assert (varobj_editable_p (var)); 01451 01452 input_radix = 10; /* ALWAYS reset to decimal temporarily. */ 01453 exp = parse_exp_1 (&s, 0, 0, 0); 01454 TRY_CATCH (except, RETURN_MASK_ERROR) 01455 { 01456 value = evaluate_expression (exp); 01457 } 01458 01459 if (except.reason < 0) 01460 { 01461 /* We cannot proceed without a valid expression. */ 01462 xfree (exp); 01463 return 0; 01464 } 01465 01466 /* All types that are editable must also be changeable. */ 01467 gdb_assert (varobj_value_is_changeable_p (var)); 01468 01469 /* The value of a changeable variable object must not be lazy. */ 01470 gdb_assert (!value_lazy (var->value)); 01471 01472 /* Need to coerce the input. We want to check if the 01473 value of the variable object will be different 01474 after assignment, and the first thing value_assign 01475 does is coerce the input. 01476 For example, if we are assigning an array to a pointer variable we 01477 should compare the pointer with the array's address, not with the 01478 array's content. */ 01479 value = coerce_array (value); 01480 01481 /* The new value may be lazy. value_assign, or 01482 rather value_contents, will take care of this. */ 01483 TRY_CATCH (except, RETURN_MASK_ERROR) 01484 { 01485 val = value_assign (var->value, value); 01486 } 01487 01488 if (except.reason < 0) 01489 return 0; 01490 01491 /* If the value has changed, record it, so that next -var-update can 01492 report this change. If a variable had a value of '1', we've set it 01493 to '333' and then set again to '1', when -var-update will report this 01494 variable as changed -- because the first assignment has set the 01495 'updated' flag. There's no need to optimize that, because return value 01496 of -var-update should be considered an approximation. */ 01497 var->updated = install_new_value (var, val, 0 /* Compare values. */); 01498 input_radix = saved_input_radix; 01499 return 1; 01500 } 01501 01502 #if HAVE_PYTHON 01503 01504 /* A helper function to install a constructor function and visualizer 01505 in a varobj. */ 01506 01507 static void 01508 install_visualizer (struct varobj *var, PyObject *constructor, 01509 PyObject *visualizer) 01510 { 01511 Py_XDECREF (var->constructor); 01512 var->constructor = constructor; 01513 01514 Py_XDECREF (var->pretty_printer); 01515 var->pretty_printer = visualizer; 01516 01517 Py_XDECREF (var->child_iter); 01518 var->child_iter = NULL; 01519 } 01520 01521 /* Install the default visualizer for VAR. */ 01522 01523 static void 01524 install_default_visualizer (struct varobj *var) 01525 { 01526 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */ 01527 if (CPLUS_FAKE_CHILD (var)) 01528 return; 01529 01530 if (pretty_printing) 01531 { 01532 PyObject *pretty_printer = NULL; 01533 01534 if (var->value) 01535 { 01536 pretty_printer = gdbpy_get_varobj_pretty_printer (var->value); 01537 if (! pretty_printer) 01538 { 01539 gdbpy_print_stack (); 01540 error (_("Cannot instantiate printer for default visualizer")); 01541 } 01542 } 01543 01544 if (pretty_printer == Py_None) 01545 { 01546 Py_DECREF (pretty_printer); 01547 pretty_printer = NULL; 01548 } 01549 01550 install_visualizer (var, NULL, pretty_printer); 01551 } 01552 } 01553 01554 /* Instantiate and install a visualizer for VAR using CONSTRUCTOR to 01555 make a new object. */ 01556 01557 static void 01558 construct_visualizer (struct varobj *var, PyObject *constructor) 01559 { 01560 PyObject *pretty_printer; 01561 01562 /* Do not install a visualizer on a CPLUS_FAKE_CHILD. */ 01563 if (CPLUS_FAKE_CHILD (var)) 01564 return; 01565 01566 Py_INCREF (constructor); 01567 if (constructor == Py_None) 01568 pretty_printer = NULL; 01569 else 01570 { 01571 pretty_printer = instantiate_pretty_printer (constructor, var->value); 01572 if (! pretty_printer) 01573 { 01574 gdbpy_print_stack (); 01575 Py_DECREF (constructor); 01576 constructor = Py_None; 01577 Py_INCREF (constructor); 01578 } 01579 01580 if (pretty_printer == Py_None) 01581 { 01582 Py_DECREF (pretty_printer); 01583 pretty_printer = NULL; 01584 } 01585 } 01586 01587 install_visualizer (var, constructor, pretty_printer); 01588 } 01589 01590 #endif /* HAVE_PYTHON */ 01591 01592 /* A helper function for install_new_value. This creates and installs 01593 a visualizer for VAR, if appropriate. */ 01594 01595 static void 01596 install_new_value_visualizer (struct varobj *var) 01597 { 01598 #if HAVE_PYTHON 01599 /* If the constructor is None, then we want the raw value. If VAR 01600 does not have a value, just skip this. */ 01601 if (!gdb_python_initialized) 01602 return; 01603 01604 if (var->constructor != Py_None && var->value) 01605 { 01606 struct cleanup *cleanup; 01607 01608 cleanup = varobj_ensure_python_env (var); 01609 01610 if (!var->constructor) 01611 install_default_visualizer (var); 01612 else 01613 construct_visualizer (var, var->constructor); 01614 01615 do_cleanups (cleanup); 01616 } 01617 #else 01618 /* Do nothing. */ 01619 #endif 01620 } 01621 01622 /* When using RTTI to determine variable type it may be changed in runtime when 01623 the variable value is changed. This function checks whether type of varobj 01624 VAR will change when a new value NEW_VALUE is assigned and if it is so 01625 updates the type of VAR. */ 01626 01627 static int 01628 update_type_if_necessary (struct varobj *var, struct value *new_value) 01629 { 01630 if (new_value) 01631 { 01632 struct value_print_options opts; 01633 01634 get_user_print_options (&opts); 01635 if (opts.objectprint) 01636 { 01637 struct type *new_type; 01638 char *curr_type_str, *new_type_str; 01639 01640 new_type = value_actual_type (new_value, 0, 0); 01641 new_type_str = type_to_string (new_type); 01642 curr_type_str = varobj_get_type (var); 01643 if (strcmp (curr_type_str, new_type_str) != 0) 01644 { 01645 var->type = new_type; 01646 01647 /* This information may be not valid for a new type. */ 01648 varobj_delete (var, NULL, 1); 01649 VEC_free (varobj_p, var->children); 01650 var->num_children = -1; 01651 return 1; 01652 } 01653 } 01654 } 01655 01656 return 0; 01657 } 01658 01659 /* Assign a new value to a variable object. If INITIAL is non-zero, 01660 this is the first assignement after the variable object was just 01661 created, or changed type. In that case, just assign the value 01662 and return 0. 01663 Otherwise, assign the new value, and return 1 if the value is 01664 different from the current one, 0 otherwise. The comparison is 01665 done on textual representation of value. Therefore, some types 01666 need not be compared. E.g. for structures the reported value is 01667 always "{...}", so no comparison is necessary here. If the old 01668 value was NULL and new one is not, or vice versa, we always return 1. 01669 01670 The VALUE parameter should not be released -- the function will 01671 take care of releasing it when needed. */ 01672 static int 01673 install_new_value (struct varobj *var, struct value *value, int initial) 01674 { 01675 int changeable; 01676 int need_to_fetch; 01677 int changed = 0; 01678 int intentionally_not_fetched = 0; 01679 char *print_value = NULL; 01680 01681 /* We need to know the varobj's type to decide if the value should 01682 be fetched or not. C++ fake children (public/protected/private) 01683 don't have a type. */ 01684 gdb_assert (var->type || CPLUS_FAKE_CHILD (var)); 01685 changeable = varobj_value_is_changeable_p (var); 01686 01687 /* If the type has custom visualizer, we consider it to be always 01688 changeable. FIXME: need to make sure this behaviour will not 01689 mess up read-sensitive values. */ 01690 if (var->pretty_printer) 01691 changeable = 1; 01692 01693 need_to_fetch = changeable; 01694 01695 /* We are not interested in the address of references, and given 01696 that in C++ a reference is not rebindable, it cannot 01697 meaningfully change. So, get hold of the real value. */ 01698 if (value) 01699 value = coerce_ref (value); 01700 01701 if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION) 01702 /* For unions, we need to fetch the value implicitly because 01703 of implementation of union member fetch. When gdb 01704 creates a value for a field and the value of the enclosing 01705 structure is not lazy, it immediately copies the necessary 01706 bytes from the enclosing values. If the enclosing value is 01707 lazy, the call to value_fetch_lazy on the field will read 01708 the data from memory. For unions, that means we'll read the 01709 same memory more than once, which is not desirable. So 01710 fetch now. */ 01711 need_to_fetch = 1; 01712 01713 /* The new value might be lazy. If the type is changeable, 01714 that is we'll be comparing values of this type, fetch the 01715 value now. Otherwise, on the next update the old value 01716 will be lazy, which means we've lost that old value. */ 01717 if (need_to_fetch && value && value_lazy (value)) 01718 { 01719 struct varobj *parent = var->parent; 01720 int frozen = var->frozen; 01721 01722 for (; !frozen && parent; parent = parent->parent) 01723 frozen |= parent->frozen; 01724 01725 if (frozen && initial) 01726 { 01727 /* For variables that are frozen, or are children of frozen 01728 variables, we don't do fetch on initial assignment. 01729 For non-initial assignemnt we do the fetch, since it means we're 01730 explicitly asked to compare the new value with the old one. */ 01731 intentionally_not_fetched = 1; 01732 } 01733 else 01734 { 01735 volatile struct gdb_exception except; 01736 01737 TRY_CATCH (except, RETURN_MASK_ERROR) 01738 { 01739 value_fetch_lazy (value); 01740 } 01741 01742 if (except.reason < 0) 01743 { 01744 /* Set the value to NULL, so that for the next -var-update, 01745 we don't try to compare the new value with this value, 01746 that we couldn't even read. */ 01747 value = NULL; 01748 } 01749 } 01750 } 01751 01752 /* Get a reference now, before possibly passing it to any Python 01753 code that might release it. */ 01754 if (value != NULL) 01755 value_incref (value); 01756 01757 /* Below, we'll be comparing string rendering of old and new 01758 values. Don't get string rendering if the value is 01759 lazy -- if it is, the code above has decided that the value 01760 should not be fetched. */ 01761 if (value && !value_lazy (value) && !var->pretty_printer) 01762 print_value = value_get_print_value (value, var->format, var); 01763 01764 /* If the type is changeable, compare the old and the new values. 01765 If this is the initial assignment, we don't have any old value 01766 to compare with. */ 01767 if (!initial && changeable) 01768 { 01769 /* If the value of the varobj was changed by -var-set-value, 01770 then the value in the varobj and in the target is the same. 01771 However, that value is different from the value that the 01772 varobj had after the previous -var-update. So need to the 01773 varobj as changed. */ 01774 if (var->updated) 01775 { 01776 changed = 1; 01777 } 01778 else if (! var->pretty_printer) 01779 { 01780 /* Try to compare the values. That requires that both 01781 values are non-lazy. */ 01782 if (var->not_fetched && value_lazy (var->value)) 01783 { 01784 /* This is a frozen varobj and the value was never read. 01785 Presumably, UI shows some "never read" indicator. 01786 Now that we've fetched the real value, we need to report 01787 this varobj as changed so that UI can show the real 01788 value. */ 01789 changed = 1; 01790 } 01791 else if (var->value == NULL && value == NULL) 01792 /* Equal. */ 01793 ; 01794 else if (var->value == NULL || value == NULL) 01795 { 01796 changed = 1; 01797 } 01798 else 01799 { 01800 gdb_assert (!value_lazy (var->value)); 01801 gdb_assert (!value_lazy (value)); 01802 01803 gdb_assert (var->print_value != NULL && print_value != NULL); 01804 if (strcmp (var->print_value, print_value) != 0) 01805 changed = 1; 01806 } 01807 } 01808 } 01809 01810 if (!initial && !changeable) 01811 { 01812 /* For values that are not changeable, we don't compare the values. 01813 However, we want to notice if a value was not NULL and now is NULL, 01814 or vise versa, so that we report when top-level varobjs come in scope 01815 and leave the scope. */ 01816 changed = (var->value != NULL) != (value != NULL); 01817 } 01818 01819 /* We must always keep the new value, since children depend on it. */ 01820 if (var->value != NULL && var->value != value) 01821 value_free (var->value); 01822 var->value = value; 01823 if (value && value_lazy (value) && intentionally_not_fetched) 01824 var->not_fetched = 1; 01825 else 01826 var->not_fetched = 0; 01827 var->updated = 0; 01828 01829 install_new_value_visualizer (var); 01830 01831 /* If we installed a pretty-printer, re-compare the printed version 01832 to see if the variable changed. */ 01833 if (var->pretty_printer) 01834 { 01835 xfree (print_value); 01836 print_value = value_get_print_value (var->value, var->format, var); 01837 if ((var->print_value == NULL && print_value != NULL) 01838 || (var->print_value != NULL && print_value == NULL) 01839 || (var->print_value != NULL && print_value != NULL 01840 && strcmp (var->print_value, print_value) != 0)) 01841 changed = 1; 01842 } 01843 if (var->print_value) 01844 xfree (var->print_value); 01845 var->print_value = print_value; 01846 01847 gdb_assert (!var->value || value_type (var->value)); 01848 01849 return changed; 01850 } 01851 01852 /* Return the requested range for a varobj. VAR is the varobj. FROM 01853 and TO are out parameters; *FROM and *TO will be set to the 01854 selected sub-range of VAR. If no range was selected using 01855 -var-set-update-range, then both will be -1. */ 01856 void 01857 varobj_get_child_range (struct varobj *var, int *from, int *to) 01858 { 01859 *from = var->from; 01860 *to = var->to; 01861 } 01862 01863 /* Set the selected sub-range of children of VAR to start at index 01864 FROM and end at index TO. If either FROM or TO is less than zero, 01865 this is interpreted as a request for all children. */ 01866 void 01867 varobj_set_child_range (struct varobj *var, int from, int to) 01868 { 01869 var->from = from; 01870 var->to = to; 01871 } 01872 01873 void 01874 varobj_set_visualizer (struct varobj *var, const char *visualizer) 01875 { 01876 #if HAVE_PYTHON 01877 PyObject *mainmod, *globals, *constructor; 01878 struct cleanup *back_to; 01879 01880 if (!gdb_python_initialized) 01881 return; 01882 01883 back_to = varobj_ensure_python_env (var); 01884 01885 mainmod = PyImport_AddModule ("__main__"); 01886 globals = PyModule_GetDict (mainmod); 01887 Py_INCREF (globals); 01888 make_cleanup_py_decref (globals); 01889 01890 constructor = PyRun_String (visualizer, Py_eval_input, globals, globals); 01891 01892 if (! constructor) 01893 { 01894 gdbpy_print_stack (); 01895 error (_("Could not evaluate visualizer expression: %s"), visualizer); 01896 } 01897 01898 construct_visualizer (var, constructor); 01899 Py_XDECREF (constructor); 01900 01901 /* If there are any children now, wipe them. */ 01902 varobj_delete (var, NULL, 1 /* children only */); 01903 var->num_children = -1; 01904 01905 do_cleanups (back_to); 01906 #else 01907 error (_("Python support required")); 01908 #endif 01909 } 01910 01911 /* If NEW_VALUE is the new value of the given varobj (var), return 01912 non-zero if var has mutated. In other words, if the type of 01913 the new value is different from the type of the varobj's old 01914 value. 01915 01916 NEW_VALUE may be NULL, if the varobj is now out of scope. */ 01917 01918 static int 01919 varobj_value_has_mutated (struct varobj *var, struct value *new_value, 01920 struct type *new_type) 01921 { 01922 /* If we haven't previously computed the number of children in var, 01923 it does not matter from the front-end's perspective whether 01924 the type has mutated or not. For all intents and purposes, 01925 it has not mutated. */ 01926 if (var->num_children < 0) 01927 return 0; 01928 01929 if (var->root->lang->value_has_mutated) 01930 return var->root->lang->value_has_mutated (var, new_value, new_type); 01931 else 01932 return 0; 01933 } 01934 01935 /* Update the values for a variable and its children. This is a 01936 two-pronged attack. First, re-parse the value for the root's 01937 expression to see if it's changed. Then go all the way 01938 through its children, reconstructing them and noting if they've 01939 changed. 01940 01941 The EXPLICIT parameter specifies if this call is result 01942 of MI request to update this specific variable, or 01943 result of implicit -var-update *. For implicit request, we don't 01944 update frozen variables. 01945 01946 NOTE: This function may delete the caller's varobj. If it 01947 returns TYPE_CHANGED, then it has done this and VARP will be modified 01948 to point to the new varobj. */ 01949 01950 VEC(varobj_update_result) * 01951 varobj_update (struct varobj **varp, int explicit) 01952 { 01953 int type_changed = 0; 01954 int i; 01955 struct value *new; 01956 VEC (varobj_update_result) *stack = NULL; 01957 VEC (varobj_update_result) *result = NULL; 01958 01959 /* Frozen means frozen -- we don't check for any change in 01960 this varobj, including its going out of scope, or 01961 changing type. One use case for frozen varobjs is 01962 retaining previously evaluated expressions, and we don't 01963 want them to be reevaluated at all. */ 01964 if (!explicit && (*varp)->frozen) 01965 return result; 01966 01967 if (!(*varp)->root->is_valid) 01968 { 01969 varobj_update_result r = {0}; 01970 01971 r.varobj = *varp; 01972 r.status = VAROBJ_INVALID; 01973 VEC_safe_push (varobj_update_result, result, &r); 01974 return result; 01975 } 01976 01977 if ((*varp)->root->rootvar == *varp) 01978 { 01979 varobj_update_result r = {0}; 01980 01981 r.varobj = *varp; 01982 r.status = VAROBJ_IN_SCOPE; 01983 01984 /* Update the root variable. value_of_root can return NULL 01985 if the variable is no longer around, i.e. we stepped out of 01986 the frame in which a local existed. We are letting the 01987 value_of_root variable dispose of the varobj if the type 01988 has changed. */ 01989 new = value_of_root (varp, &type_changed); 01990 if (update_type_if_necessary(*varp, new)) 01991 type_changed = 1; 01992 r.varobj = *varp; 01993 r.type_changed = type_changed; 01994 if (install_new_value ((*varp), new, type_changed)) 01995 r.changed = 1; 01996 01997 if (new == NULL) 01998 r.status = VAROBJ_NOT_IN_SCOPE; 01999 r.value_installed = 1; 02000 02001 if (r.status == VAROBJ_NOT_IN_SCOPE) 02002 { 02003 if (r.type_changed || r.changed) 02004 VEC_safe_push (varobj_update_result, result, &r); 02005 return result; 02006 } 02007 02008 VEC_safe_push (varobj_update_result, stack, &r); 02009 } 02010 else 02011 { 02012 varobj_update_result r = {0}; 02013 02014 r.varobj = *varp; 02015 VEC_safe_push (varobj_update_result, stack, &r); 02016 } 02017 02018 /* Walk through the children, reconstructing them all. */ 02019 while (!VEC_empty (varobj_update_result, stack)) 02020 { 02021 varobj_update_result r = *(VEC_last (varobj_update_result, stack)); 02022 struct varobj *v = r.varobj; 02023 02024 VEC_pop (varobj_update_result, stack); 02025 02026 /* Update this variable, unless it's a root, which is already 02027 updated. */ 02028 if (!r.value_installed) 02029 { 02030 struct type *new_type; 02031 02032 new = value_of_child (v->parent, v->index); 02033 if (update_type_if_necessary(v, new)) 02034 r.type_changed = 1; 02035 if (new) 02036 new_type = value_type (new); 02037 else 02038 new_type = v->root->lang->type_of_child (v->parent, v->index); 02039 02040 if (varobj_value_has_mutated (v, new, new_type)) 02041 { 02042 /* The children are no longer valid; delete them now. 02043 Report the fact that its type changed as well. */ 02044 varobj_delete (v, NULL, 1 /* only_children */); 02045 v->num_children = -1; 02046 v->to = -1; 02047 v->from = -1; 02048 v->type = new_type; 02049 r.type_changed = 1; 02050 } 02051 02052 if (install_new_value (v, new, r.type_changed)) 02053 { 02054 r.changed = 1; 02055 v->updated = 0; 02056 } 02057 } 02058 02059 /* We probably should not get children of a varobj that has a 02060 pretty-printer, but for which -var-list-children was never 02061 invoked. */ 02062 if (v->pretty_printer) 02063 { 02064 VEC (varobj_p) *changed = 0, *type_changed = 0, *unchanged = 0; 02065 VEC (varobj_p) *new = 0; 02066 int i, children_changed = 0; 02067 02068 if (v->frozen) 02069 continue; 02070 02071 if (!v->children_requested) 02072 { 02073 int dummy; 02074 02075 /* If we initially did not have potential children, but 02076 now we do, consider the varobj as changed. 02077 Otherwise, if children were never requested, consider 02078 it as unchanged -- presumably, such varobj is not yet 02079 expanded in the UI, so we need not bother getting 02080 it. */ 02081 if (!varobj_has_more (v, 0)) 02082 { 02083 update_dynamic_varobj_children (v, NULL, NULL, NULL, NULL, 02084 &dummy, 0, 0, 0); 02085 if (varobj_has_more (v, 0)) 02086 r.changed = 1; 02087 } 02088 02089 if (r.changed) 02090 VEC_safe_push (varobj_update_result, result, &r); 02091 02092 continue; 02093 } 02094 02095 /* If update_dynamic_varobj_children returns 0, then we have 02096 a non-conforming pretty-printer, so we skip it. */ 02097 if (update_dynamic_varobj_children (v, &changed, &type_changed, &new, 02098 &unchanged, &children_changed, 1, 02099 v->from, v->to)) 02100 { 02101 if (children_changed || new) 02102 { 02103 r.children_changed = 1; 02104 r.new = new; 02105 } 02106 /* Push in reverse order so that the first child is 02107 popped from the work stack first, and so will be 02108 added to result first. This does not affect 02109 correctness, just "nicer". */ 02110 for (i = VEC_length (varobj_p, type_changed) - 1; i >= 0; --i) 02111 { 02112 varobj_p tmp = VEC_index (varobj_p, type_changed, i); 02113 varobj_update_result r = {0}; 02114 02115 /* Type may change only if value was changed. */ 02116 r.varobj = tmp; 02117 r.changed = 1; 02118 r.type_changed = 1; 02119 r.value_installed = 1; 02120 VEC_safe_push (varobj_update_result, stack, &r); 02121 } 02122 for (i = VEC_length (varobj_p, changed) - 1; i >= 0; --i) 02123 { 02124 varobj_p tmp = VEC_index (varobj_p, changed, i); 02125 varobj_update_result r = {0}; 02126 02127 r.varobj = tmp; 02128 r.changed = 1; 02129 r.value_installed = 1; 02130 VEC_safe_push (varobj_update_result, stack, &r); 02131 } 02132 for (i = VEC_length (varobj_p, unchanged) - 1; i >= 0; --i) 02133 { 02134 varobj_p tmp = VEC_index (varobj_p, unchanged, i); 02135 02136 if (!tmp->frozen) 02137 { 02138 varobj_update_result r = {0}; 02139 02140 r.varobj = tmp; 02141 r.value_installed = 1; 02142 VEC_safe_push (varobj_update_result, stack, &r); 02143 } 02144 } 02145 if (r.changed || r.children_changed) 02146 VEC_safe_push (varobj_update_result, result, &r); 02147 02148 /* Free CHANGED, TYPE_CHANGED and UNCHANGED, but not NEW, 02149 because NEW has been put into the result vector. */ 02150 VEC_free (varobj_p, changed); 02151 VEC_free (varobj_p, type_changed); 02152 VEC_free (varobj_p, unchanged); 02153 02154 continue; 02155 } 02156 } 02157 02158 /* Push any children. Use reverse order so that the first 02159 child is popped from the work stack first, and so 02160 will be added to result first. This does not 02161 affect correctness, just "nicer". */ 02162 for (i = VEC_length (varobj_p, v->children)-1; i >= 0; --i) 02163 { 02164 varobj_p c = VEC_index (varobj_p, v->children, i); 02165 02166 /* Child may be NULL if explicitly deleted by -var-delete. */ 02167 if (c != NULL && !c->frozen) 02168 { 02169 varobj_update_result r = {0}; 02170 02171 r.varobj = c; 02172 VEC_safe_push (varobj_update_result, stack, &r); 02173 } 02174 } 02175 02176 if (r.changed || r.type_changed) 02177 VEC_safe_push (varobj_update_result, result, &r); 02178 } 02179 02180 VEC_free (varobj_update_result, stack); 02181 02182 return result; 02183 } 02184 02185 02186 /* Helper functions */ 02187 02188 /* 02189 * Variable object construction/destruction 02190 */ 02191 02192 static int 02193 delete_variable (struct cpstack **resultp, struct varobj *var, 02194 int only_children_p) 02195 { 02196 int delcount = 0; 02197 02198 delete_variable_1 (resultp, &delcount, var, 02199 only_children_p, 1 /* remove_from_parent_p */ ); 02200 02201 return delcount; 02202 } 02203 02204 /* Delete the variable object VAR and its children. */ 02205 /* IMPORTANT NOTE: If we delete a variable which is a child 02206 and the parent is not removed we dump core. It must be always 02207 initially called with remove_from_parent_p set. */ 02208 static void 02209 delete_variable_1 (struct cpstack **resultp, int *delcountp, 02210 struct varobj *var, int only_children_p, 02211 int remove_from_parent_p) 02212 { 02213 int i; 02214 02215 /* Delete any children of this variable, too. */ 02216 for (i = 0; i < VEC_length (varobj_p, var->children); ++i) 02217 { 02218 varobj_p child = VEC_index (varobj_p, var->children, i); 02219 02220 if (!child) 02221 continue; 02222 if (!remove_from_parent_p) 02223 child->parent = NULL; 02224 delete_variable_1 (resultp, delcountp, child, 0, only_children_p); 02225 } 02226 VEC_free (varobj_p, var->children); 02227 02228 /* if we were called to delete only the children we are done here. */ 02229 if (only_children_p) 02230 return; 02231 02232 /* Otherwise, add it to the list of deleted ones and proceed to do so. */ 02233 /* If the name is null, this is a temporary variable, that has not 02234 yet been installed, don't report it, it belongs to the caller... */ 02235 if (var->obj_name != NULL) 02236 { 02237 cppush (resultp, xstrdup (var->obj_name)); 02238 *delcountp = *delcountp + 1; 02239 } 02240 02241 /* If this variable has a parent, remove it from its parent's list. */ 02242 /* OPTIMIZATION: if the parent of this variable is also being deleted, 02243 (as indicated by remove_from_parent_p) we don't bother doing an 02244 expensive list search to find the element to remove when we are 02245 discarding the list afterwards. */ 02246 if ((remove_from_parent_p) && (var->parent != NULL)) 02247 { 02248 VEC_replace (varobj_p, var->parent->children, var->index, NULL); 02249 } 02250 02251 if (var->obj_name != NULL) 02252 uninstall_variable (var); 02253 02254 /* Free memory associated with this variable. */ 02255 free_variable (var); 02256 } 02257 02258 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */ 02259 static int 02260 install_variable (struct varobj *var) 02261 { 02262 struct vlist *cv; 02263 struct vlist *newvl; 02264 const char *chp; 02265 unsigned int index = 0; 02266 unsigned int i = 1; 02267 02268 for (chp = var->obj_name; *chp; chp++) 02269 { 02270 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE; 02271 } 02272 02273 cv = *(varobj_table + index); 02274 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0)) 02275 cv = cv->next; 02276 02277 if (cv != NULL) 02278 error (_("Duplicate variable object name")); 02279 02280 /* Add varobj to hash table. */ 02281 newvl = xmalloc (sizeof (struct vlist)); 02282 newvl->next = *(varobj_table + index); 02283 newvl->var = var; 02284 *(varobj_table + index) = newvl; 02285 02286 /* If root, add varobj to root list. */ 02287 if (is_root_p (var)) 02288 { 02289 /* Add to list of root variables. */ 02290 if (rootlist == NULL) 02291 var->root->next = NULL; 02292 else 02293 var->root->next = rootlist; 02294 rootlist = var->root; 02295 } 02296 02297 return 1; /* OK */ 02298 } 02299 02300 /* Unistall the object VAR. */ 02301 static void 02302 uninstall_variable (struct varobj *var) 02303 { 02304 struct vlist *cv; 02305 struct vlist *prev; 02306 struct varobj_root *cr; 02307 struct varobj_root *prer; 02308 const char *chp; 02309 unsigned int index = 0; 02310 unsigned int i = 1; 02311 02312 /* Remove varobj from hash table. */ 02313 for (chp = var->obj_name; *chp; chp++) 02314 { 02315 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE; 02316 } 02317 02318 cv = *(varobj_table + index); 02319 prev = NULL; 02320 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0)) 02321 { 02322 prev = cv; 02323 cv = cv->next; 02324 } 02325 02326 if (varobjdebug) 02327 fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name); 02328 02329 if (cv == NULL) 02330 { 02331 warning 02332 ("Assertion failed: Could not find variable object \"%s\" to delete", 02333 var->obj_name); 02334 return; 02335 } 02336 02337 if (prev == NULL) 02338 *(varobj_table + index) = cv->next; 02339 else 02340 prev->next = cv->next; 02341 02342 xfree (cv); 02343 02344 /* If root, remove varobj from root list. */ 02345 if (is_root_p (var)) 02346 { 02347 /* Remove from list of root variables. */ 02348 if (rootlist == var->root) 02349 rootlist = var->root->next; 02350 else 02351 { 02352 prer = NULL; 02353 cr = rootlist; 02354 while ((cr != NULL) && (cr->rootvar != var)) 02355 { 02356 prer = cr; 02357 cr = cr->next; 02358 } 02359 if (cr == NULL) 02360 { 02361 warning (_("Assertion failed: Could not find " 02362 "varobj \"%s\" in root list"), 02363 var->obj_name); 02364 return; 02365 } 02366 if (prer == NULL) 02367 rootlist = NULL; 02368 else 02369 prer->next = cr->next; 02370 } 02371 } 02372 02373 } 02374 02375 /* Create and install a child of the parent of the given name. */ 02376 static struct varobj * 02377 create_child (struct varobj *parent, int index, char *name) 02378 { 02379 return create_child_with_value (parent, index, name, 02380 value_of_child (parent, index)); 02381 } 02382 02383 /* Does CHILD represent a child with no name? This happens when 02384 the child is an anonmous struct or union and it has no field name 02385 in its parent variable. 02386 02387 This has already been determined by *_describe_child. The easiest 02388 thing to do is to compare the child's name with ANONYMOUS_*_NAME. */ 02389 02390 static int 02391 is_anonymous_child (struct varobj *child) 02392 { 02393 return (strcmp (child->name, ANONYMOUS_STRUCT_NAME) == 0 02394 || strcmp (child->name, ANONYMOUS_UNION_NAME) == 0); 02395 } 02396 02397 static struct varobj * 02398 create_child_with_value (struct varobj *parent, int index, char *name, 02399 struct value *value) 02400 { 02401 struct varobj *child; 02402 char *childs_name; 02403 02404 child = new_variable (); 02405 02406 /* NAME is allocated by caller. */ 02407 child->name = name; 02408 child->index = index; 02409 child->parent = parent; 02410 child->root = parent->root; 02411 02412 if (is_anonymous_child (child)) 02413 childs_name = xstrprintf ("%s.%d_anonymous", parent->obj_name, index); 02414 else 02415 childs_name = xstrprintf ("%s.%s", parent->obj_name, name); 02416 child->obj_name = childs_name; 02417 02418 install_variable (child); 02419 02420 /* Compute the type of the child. Must do this before 02421 calling install_new_value. */ 02422 if (value != NULL) 02423 /* If the child had no evaluation errors, var->value 02424 will be non-NULL and contain a valid type. */ 02425 child->type = value_actual_type (value, 0, NULL); 02426 else 02427 /* Otherwise, we must compute the type. */ 02428 child->type = (*child->root->lang->type_of_child) (child->parent, 02429 child->index); 02430 install_new_value (child, value, 1); 02431 02432 return child; 02433 } 02434 02435 02436 /* 02437 * Miscellaneous utility functions. 02438 */ 02439 02440 /* Allocate memory and initialize a new variable. */ 02441 static struct varobj * 02442 new_variable (void) 02443 { 02444 struct varobj *var; 02445 02446 var = (struct varobj *) xmalloc (sizeof (struct varobj)); 02447 var->name = NULL; 02448 var->path_expr = NULL; 02449 var->obj_name = NULL; 02450 var->index = -1; 02451 var->type = NULL; 02452 var->value = NULL; 02453 var->num_children = -1; 02454 var->parent = NULL; 02455 var->children = NULL; 02456 var->format = 0; 02457 var->root = NULL; 02458 var->updated = 0; 02459 var->print_value = NULL; 02460 var->frozen = 0; 02461 var->not_fetched = 0; 02462 var->children_requested = 0; 02463 var->from = -1; 02464 var->to = -1; 02465 var->constructor = 0; 02466 var->pretty_printer = 0; 02467 var->child_iter = 0; 02468 var->saved_item = 0; 02469 02470 return var; 02471 } 02472 02473 /* Allocate memory and initialize a new root variable. */ 02474 static struct varobj * 02475 new_root_variable (void) 02476 { 02477 struct varobj *var = new_variable (); 02478 02479 var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root)); 02480 var->root->lang = NULL; 02481 var->root->exp = NULL; 02482 var->root->valid_block = NULL; 02483 var->root->frame = null_frame_id; 02484 var->root->floating = 0; 02485 var->root->rootvar = NULL; 02486 var->root->is_valid = 1; 02487 02488 return var; 02489 } 02490 02491 /* Free any allocated memory associated with VAR. */ 02492 static void 02493 free_variable (struct varobj *var) 02494 { 02495 #if HAVE_PYTHON 02496 if (var->pretty_printer) 02497 { 02498 struct cleanup *cleanup = varobj_ensure_python_env (var); 02499 Py_XDECREF (var->constructor); 02500 Py_XDECREF (var->pretty_printer); 02501 Py_XDECREF (var->child_iter); 02502 Py_XDECREF (var->saved_item); 02503 do_cleanups (cleanup); 02504 } 02505 #endif 02506 02507 value_free (var->value); 02508 02509 /* Free the expression if this is a root variable. */ 02510 if (is_root_p (var)) 02511 { 02512 xfree (var->root->exp); 02513 xfree (var->root); 02514 } 02515 02516 xfree (var->name); 02517 xfree (var->obj_name); 02518 xfree (var->print_value); 02519 xfree (var->path_expr); 02520 xfree (var); 02521 } 02522 02523 static void 02524 do_free_variable_cleanup (void *var) 02525 { 02526 free_variable (var); 02527 } 02528 02529 static struct cleanup * 02530 make_cleanup_free_variable (struct varobj *var) 02531 { 02532 return make_cleanup (do_free_variable_cleanup, var); 02533 } 02534 02535 /* This returns the type of the variable. It also skips past typedefs 02536 to return the real type of the variable. 02537 02538 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file 02539 except within get_target_type and get_type. */ 02540 static struct type * 02541 get_type (struct varobj *var) 02542 { 02543 struct type *type; 02544 02545 type = var->type; 02546 if (type != NULL) 02547 type = check_typedef (type); 02548 02549 return type; 02550 } 02551 02552 /* Return the type of the value that's stored in VAR, 02553 or that would have being stored there if the 02554 value were accessible. 02555 02556 This differs from VAR->type in that VAR->type is always 02557 the true type of the expession in the source language. 02558 The return value of this function is the type we're 02559 actually storing in varobj, and using for displaying 02560 the values and for comparing previous and new values. 02561 02562 For example, top-level references are always stripped. */ 02563 static struct type * 02564 get_value_type (struct varobj *var) 02565 { 02566 struct type *type; 02567 02568 if (var->value) 02569 type = value_type (var->value); 02570 else 02571 type = var->type; 02572 02573 type = check_typedef (type); 02574 02575 if (TYPE_CODE (type) == TYPE_CODE_REF) 02576 type = get_target_type (type); 02577 02578 type = check_typedef (type); 02579 02580 return type; 02581 } 02582 02583 /* This returns the target type (or NULL) of TYPE, also skipping 02584 past typedefs, just like get_type (). 02585 02586 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file 02587 except within get_target_type and get_type. */ 02588 static struct type * 02589 get_target_type (struct type *type) 02590 { 02591 if (type != NULL) 02592 { 02593 type = TYPE_TARGET_TYPE (type); 02594 if (type != NULL) 02595 type = check_typedef (type); 02596 } 02597 02598 return type; 02599 } 02600 02601 /* What is the default display for this variable? We assume that 02602 everything is "natural". Any exceptions? */ 02603 static enum varobj_display_formats 02604 variable_default_display (struct varobj *var) 02605 { 02606 return FORMAT_NATURAL; 02607 } 02608 02609 /* FIXME: The following should be generic for any pointer. */ 02610 static void 02611 cppush (struct cpstack **pstack, char *name) 02612 { 02613 struct cpstack *s; 02614 02615 s = (struct cpstack *) xmalloc (sizeof (struct cpstack)); 02616 s->name = name; 02617 s->next = *pstack; 02618 *pstack = s; 02619 } 02620 02621 /* FIXME: The following should be generic for any pointer. */ 02622 static char * 02623 cppop (struct cpstack **pstack) 02624 { 02625 struct cpstack *s; 02626 char *v; 02627 02628 if ((*pstack)->name == NULL && (*pstack)->next == NULL) 02629 return NULL; 02630 02631 s = *pstack; 02632 v = s->name; 02633 *pstack = (*pstack)->next; 02634 xfree (s); 02635 02636 return v; 02637 } 02638 02639 /* 02640 * Language-dependencies 02641 */ 02642 02643 /* Common entry points */ 02644 02645 /* Get the language of variable VAR. */ 02646 static enum varobj_languages 02647 variable_language (struct varobj *var) 02648 { 02649 enum varobj_languages lang; 02650 02651 switch (var->root->exp->language_defn->la_language) 02652 { 02653 default: 02654 case language_c: 02655 lang = vlang_c; 02656 break; 02657 case language_cplus: 02658 lang = vlang_cplus; 02659 break; 02660 case language_java: 02661 lang = vlang_java; 02662 break; 02663 case language_ada: 02664 lang = vlang_ada; 02665 break; 02666 } 02667 02668 return lang; 02669 } 02670 02671 /* Return the number of children for a given variable. 02672 The result of this function is defined by the language 02673 implementation. The number of children returned by this function 02674 is the number of children that the user will see in the variable 02675 display. */ 02676 static int 02677 number_of_children (struct varobj *var) 02678 { 02679 return (*var->root->lang->number_of_children) (var); 02680 } 02681 02682 /* What is the expression for the root varobj VAR? Returns a malloc'd 02683 string. */ 02684 static char * 02685 name_of_variable (struct varobj *var) 02686 { 02687 return (*var->root->lang->name_of_variable) (var); 02688 } 02689 02690 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd 02691 string. */ 02692 static char * 02693 name_of_child (struct varobj *var, int index) 02694 { 02695 return (*var->root->lang->name_of_child) (var, index); 02696 } 02697 02698 /* If frame associated with VAR can be found, switch 02699 to it and return 1. Otherwise, return 0. */ 02700 02701 static int 02702 check_scope (struct varobj *var) 02703 { 02704 struct frame_info *fi; 02705 int scope; 02706 02707 fi = frame_find_by_id (var->root->frame); 02708 scope = fi != NULL; 02709 02710 if (fi) 02711 { 02712 CORE_ADDR pc = get_frame_pc (fi); 02713 02714 if (pc < BLOCK_START (var->root->valid_block) || 02715 pc >= BLOCK_END (var->root->valid_block)) 02716 scope = 0; 02717 else 02718 select_frame (fi); 02719 } 02720 return scope; 02721 } 02722 02723 /* Helper function to value_of_root. */ 02724 02725 static struct value * 02726 value_of_root_1 (struct varobj **var_handle) 02727 { 02728 struct value *new_val = NULL; 02729 struct varobj *var = *var_handle; 02730 int within_scope = 0; 02731 struct cleanup *back_to; 02732 02733 /* Only root variables can be updated... */ 02734 if (!is_root_p (var)) 02735 /* Not a root var. */ 02736 return NULL; 02737 02738 back_to = make_cleanup_restore_current_thread (); 02739 02740 /* Determine whether the variable is still around. */ 02741 if (var->root->valid_block == NULL || var->root->floating) 02742 within_scope = 1; 02743 else if (var->root->thread_id == 0) 02744 { 02745 /* The program was single-threaded when the variable object was 02746 created. Technically, it's possible that the program became 02747 multi-threaded since then, but we don't support such 02748 scenario yet. */ 02749 within_scope = check_scope (var); 02750 } 02751 else 02752 { 02753 ptid_t ptid = thread_id_to_pid (var->root->thread_id); 02754 if (in_thread_list (ptid)) 02755 { 02756 switch_to_thread (ptid); 02757 within_scope = check_scope (var); 02758 } 02759 } 02760 02761 if (within_scope) 02762 { 02763 volatile struct gdb_exception except; 02764 02765 /* We need to catch errors here, because if evaluate 02766 expression fails we want to just return NULL. */ 02767 TRY_CATCH (except, RETURN_MASK_ERROR) 02768 { 02769 new_val = evaluate_expression (var->root->exp); 02770 } 02771 } 02772 02773 do_cleanups (back_to); 02774 02775 return new_val; 02776 } 02777 02778 /* What is the ``struct value *'' of the root variable VAR? 02779 For floating variable object, evaluation can get us a value 02780 of different type from what is stored in varobj already. In 02781 that case: 02782 - *type_changed will be set to 1 02783 - old varobj will be freed, and new one will be 02784 created, with the same name. 02785 - *var_handle will be set to the new varobj 02786 Otherwise, *type_changed will be set to 0. */ 02787 static struct value * 02788 value_of_root (struct varobj **var_handle, int *type_changed) 02789 { 02790 struct varobj *var; 02791 02792 if (var_handle == NULL) 02793 return NULL; 02794 02795 var = *var_handle; 02796 02797 /* This should really be an exception, since this should 02798 only get called with a root variable. */ 02799 02800 if (!is_root_p (var)) 02801 return NULL; 02802 02803 if (var->root->floating) 02804 { 02805 struct varobj *tmp_var; 02806 char *old_type, *new_type; 02807 02808 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0, 02809 USE_SELECTED_FRAME); 02810 if (tmp_var == NULL) 02811 { 02812 return NULL; 02813 } 02814 old_type = varobj_get_type (var); 02815 new_type = varobj_get_type (tmp_var); 02816 if (strcmp (old_type, new_type) == 0) 02817 { 02818 /* The expression presently stored inside var->root->exp 02819 remembers the locations of local variables relatively to 02820 the frame where the expression was created (in DWARF location 02821 button, for example). Naturally, those locations are not 02822 correct in other frames, so update the expression. */ 02823 02824 struct expression *tmp_exp = var->root->exp; 02825 02826 var->root->exp = tmp_var->root->exp; 02827 tmp_var->root->exp = tmp_exp; 02828 02829 varobj_delete (tmp_var, NULL, 0); 02830 *type_changed = 0; 02831 } 02832 else 02833 { 02834 tmp_var->obj_name = xstrdup (var->obj_name); 02835 tmp_var->from = var->from; 02836 tmp_var->to = var->to; 02837 varobj_delete (var, NULL, 0); 02838 02839 install_variable (tmp_var); 02840 *var_handle = tmp_var; 02841 var = *var_handle; 02842 *type_changed = 1; 02843 } 02844 xfree (old_type); 02845 xfree (new_type); 02846 } 02847 else 02848 { 02849 *type_changed = 0; 02850 } 02851 02852 { 02853 struct value *value; 02854 02855 value = value_of_root_1 (var_handle); 02856 if (var->value == NULL || value == NULL) 02857 { 02858 /* For root varobj-s, a NULL value indicates a scoping issue. 02859 So, nothing to do in terms of checking for mutations. */ 02860 } 02861 else if (varobj_value_has_mutated (var, value, value_type (value))) 02862 { 02863 /* The type has mutated, so the children are no longer valid. 02864 Just delete them, and tell our caller that the type has 02865 changed. */ 02866 varobj_delete (var, NULL, 1 /* only_children */); 02867 var->num_children = -1; 02868 var->to = -1; 02869 var->from = -1; 02870 *type_changed = 1; 02871 } 02872 return value; 02873 } 02874 } 02875 02876 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */ 02877 static struct value * 02878 value_of_child (struct varobj *parent, int index) 02879 { 02880 struct value *value; 02881 02882 value = (*parent->root->lang->value_of_child) (parent, index); 02883 02884 return value; 02885 } 02886 02887 /* GDB already has a command called "value_of_variable". Sigh. */ 02888 static char * 02889 my_value_of_variable (struct varobj *var, enum varobj_display_formats format) 02890 { 02891 if (var->root->is_valid) 02892 { 02893 if (var->pretty_printer) 02894 return value_get_print_value (var->value, var->format, var); 02895 return (*var->root->lang->value_of_variable) (var, format); 02896 } 02897 else 02898 return NULL; 02899 } 02900 02901 static char * 02902 value_get_print_value (struct value *value, enum varobj_display_formats format, 02903 struct varobj *var) 02904 { 02905 struct ui_file *stb; 02906 struct cleanup *old_chain; 02907 char *thevalue = NULL; 02908 struct value_print_options opts; 02909 struct type *type = NULL; 02910 long len = 0; 02911 char *encoding = NULL; 02912 struct gdbarch *gdbarch = NULL; 02913 /* Initialize it just to avoid a GCC false warning. */ 02914 CORE_ADDR str_addr = 0; 02915 int string_print = 0; 02916 02917 if (value == NULL) 02918 return NULL; 02919 02920 stb = mem_fileopen (); 02921 old_chain = make_cleanup_ui_file_delete (stb); 02922 02923 gdbarch = get_type_arch (value_type (value)); 02924 #if HAVE_PYTHON 02925 if (gdb_python_initialized) 02926 { 02927 PyObject *value_formatter = var->pretty_printer; 02928 02929 varobj_ensure_python_env (var); 02930 02931 if (value_formatter) 02932 { 02933 /* First check to see if we have any children at all. If so, 02934 we simply return {...}. */ 02935 if (dynamic_varobj_has_child_method (var)) 02936 { 02937 do_cleanups (old_chain); 02938 return xstrdup ("{...}"); 02939 } 02940 02941 if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst)) 02942 { 02943 struct value *replacement; 02944 PyObject *output = NULL; 02945 02946 output = apply_varobj_pretty_printer (value_formatter, 02947 &replacement, 02948 stb); 02949 02950 /* If we have string like output ... */ 02951 if (output) 02952 { 02953 make_cleanup_py_decref (output); 02954 02955 /* If this is a lazy string, extract it. For lazy 02956 strings we always print as a string, so set 02957 string_print. */ 02958 if (gdbpy_is_lazy_string (output)) 02959 { 02960 gdbpy_extract_lazy_string (output, &str_addr, &type, 02961 &len, &encoding); 02962 make_cleanup (free_current_contents, &encoding); 02963 string_print = 1; 02964 } 02965 else 02966 { 02967 /* If it is a regular (non-lazy) string, extract 02968 it and copy the contents into THEVALUE. If the 02969 hint says to print it as a string, set 02970 string_print. Otherwise just return the extracted 02971 string as a value. */ 02972 02973 char *s = python_string_to_target_string (output); 02974 02975 if (s) 02976 { 02977 char *hint; 02978 02979 hint = gdbpy_get_display_hint (value_formatter); 02980 if (hint) 02981 { 02982 if (!strcmp (hint, "string")) 02983 string_print = 1; 02984 xfree (hint); 02985 } 02986 02987 len = strlen (s); 02988 thevalue = xmemdup (s, len + 1, len + 1); 02989 type = builtin_type (gdbarch)->builtin_char; 02990 xfree (s); 02991 02992 if (!string_print) 02993 { 02994 do_cleanups (old_chain); 02995 return thevalue; 02996 } 02997 02998 make_cleanup (xfree, thevalue); 02999 } 03000 else 03001 gdbpy_print_stack (); 03002 } 03003 } 03004 /* If the printer returned a replacement value, set VALUE 03005 to REPLACEMENT. If there is not a replacement value, 03006 just use the value passed to this function. */ 03007 if (replacement) 03008 value = replacement; 03009 } 03010 } 03011 } 03012 #endif 03013 03014 get_formatted_print_options (&opts, format_code[(int) format]); 03015 opts.deref_ref = 0; 03016 opts.raw = 1; 03017 03018 /* If the THEVALUE has contents, it is a regular string. */ 03019 if (thevalue) 03020 LA_PRINT_STRING (stb, type, (gdb_byte *) thevalue, len, encoding, 0, &opts); 03021 else if (string_print) 03022 /* Otherwise, if string_print is set, and it is not a regular 03023 string, it is a lazy string. */ 03024 val_print_string (type, encoding, str_addr, len, stb, &opts); 03025 else 03026 /* All other cases. */ 03027 common_val_print (value, stb, 0, &opts, current_language); 03028 03029 thevalue = ui_file_xstrdup (stb, NULL); 03030 03031 do_cleanups (old_chain); 03032 return thevalue; 03033 } 03034 03035 int 03036 varobj_editable_p (struct varobj *var) 03037 { 03038 struct type *type; 03039 03040 if (!(var->root->is_valid && var->value && VALUE_LVAL (var->value))) 03041 return 0; 03042 03043 type = get_value_type (var); 03044 03045 switch (TYPE_CODE (type)) 03046 { 03047 case TYPE_CODE_STRUCT: 03048 case TYPE_CODE_UNION: 03049 case TYPE_CODE_ARRAY: 03050 case TYPE_CODE_FUNC: 03051 case TYPE_CODE_METHOD: 03052 return 0; 03053 break; 03054 03055 default: 03056 return 1; 03057 break; 03058 } 03059 } 03060 03061 /* Call VAR's value_is_changeable_p language-specific callback. */ 03062 03063 static int 03064 varobj_value_is_changeable_p (struct varobj *var) 03065 { 03066 return var->root->lang->value_is_changeable_p (var); 03067 } 03068 03069 /* Return 1 if that varobj is floating, that is is always evaluated in the 03070 selected frame, and not bound to thread/frame. Such variable objects 03071 are created using '@' as frame specifier to -var-create. */ 03072 int 03073 varobj_floating_p (struct varobj *var) 03074 { 03075 return var->root->floating; 03076 } 03077 03078 /* Given the value and the type of a variable object, 03079 adjust the value and type to those necessary 03080 for getting children of the variable object. 03081 This includes dereferencing top-level references 03082 to all types and dereferencing pointers to 03083 structures. 03084 03085 If LOOKUP_ACTUAL_TYPE is set the enclosing type of the 03086 value will be fetched and if it differs from static type 03087 the value will be casted to it. 03088 03089 Both TYPE and *TYPE should be non-null. VALUE 03090 can be null if we want to only translate type. 03091 *VALUE can be null as well -- if the parent 03092 value is not known. 03093 03094 If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1 03095 depending on whether pointer was dereferenced 03096 in this function. */ 03097 static void 03098 adjust_value_for_child_access (struct value **value, 03099 struct type **type, 03100 int *was_ptr, 03101 int lookup_actual_type) 03102 { 03103 gdb_assert (type && *type); 03104 03105 if (was_ptr) 03106 *was_ptr = 0; 03107 03108 *type = check_typedef (*type); 03109 03110 /* The type of value stored in varobj, that is passed 03111 to us, is already supposed to be 03112 reference-stripped. */ 03113 03114 gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF); 03115 03116 /* Pointers to structures are treated just like 03117 structures when accessing children. Don't 03118 dererences pointers to other types. */ 03119 if (TYPE_CODE (*type) == TYPE_CODE_PTR) 03120 { 03121 struct type *target_type = get_target_type (*type); 03122 if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT 03123 || TYPE_CODE (target_type) == TYPE_CODE_UNION) 03124 { 03125 if (value && *value) 03126 { 03127 volatile struct gdb_exception except; 03128 03129 TRY_CATCH (except, RETURN_MASK_ERROR) 03130 { 03131 *value = value_ind (*value); 03132 } 03133 03134 if (except.reason < 0) 03135 *value = NULL; 03136 } 03137 *type = target_type; 03138 if (was_ptr) 03139 *was_ptr = 1; 03140 } 03141 } 03142 03143 /* The 'get_target_type' function calls check_typedef on 03144 result, so we can immediately check type code. No 03145 need to call check_typedef here. */ 03146 03147 /* Access a real type of the value (if necessary and possible). */ 03148 if (value && *value && lookup_actual_type) 03149 { 03150 struct type *enclosing_type; 03151 int real_type_found = 0; 03152 03153 enclosing_type = value_actual_type (*value, 1, &real_type_found); 03154 if (real_type_found) 03155 { 03156 *type = enclosing_type; 03157 *value = value_cast (enclosing_type, *value); 03158 } 03159 } 03160 } 03161 03162 /* Implement the "value_is_changeable_p" varobj callback for most 03163 languages. */ 03164 03165 static int 03166 default_value_is_changeable_p (struct varobj *var) 03167 { 03168 int r; 03169 struct type *type; 03170 03171 if (CPLUS_FAKE_CHILD (var)) 03172 return 0; 03173 03174 type = get_value_type (var); 03175 03176 switch (TYPE_CODE (type)) 03177 { 03178 case TYPE_CODE_STRUCT: 03179 case TYPE_CODE_UNION: 03180 case TYPE_CODE_ARRAY: 03181 r = 0; 03182 break; 03183 03184 default: 03185 r = 1; 03186 } 03187 03188 return r; 03189 } 03190 03191 /* C */ 03192 03193 static int 03194 c_number_of_children (struct varobj *var) 03195 { 03196 struct type *type = get_value_type (var); 03197 int children = 0; 03198 struct type *target; 03199 03200 adjust_value_for_child_access (NULL, &type, NULL, 0); 03201 target = get_target_type (type); 03202 03203 switch (TYPE_CODE (type)) 03204 { 03205 case TYPE_CODE_ARRAY: 03206 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0 03207 && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type)) 03208 children = TYPE_LENGTH (type) / TYPE_LENGTH (target); 03209 else 03210 /* If we don't know how many elements there are, don't display 03211 any. */ 03212 children = 0; 03213 break; 03214 03215 case TYPE_CODE_STRUCT: 03216 case TYPE_CODE_UNION: 03217 children = TYPE_NFIELDS (type); 03218 break; 03219 03220 case TYPE_CODE_PTR: 03221 /* The type here is a pointer to non-struct. Typically, pointers 03222 have one child, except for function ptrs, which have no children, 03223 and except for void*, as we don't know what to show. 03224 03225 We can show char* so we allow it to be dereferenced. If you decide 03226 to test for it, please mind that a little magic is necessary to 03227 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and 03228 TYPE_NAME == "char". */ 03229 if (TYPE_CODE (target) == TYPE_CODE_FUNC 03230 || TYPE_CODE (target) == TYPE_CODE_VOID) 03231 children = 0; 03232 else 03233 children = 1; 03234 break; 03235 03236 default: 03237 /* Other types have no children. */ 03238 break; 03239 } 03240 03241 return children; 03242 } 03243 03244 static char * 03245 c_name_of_variable (struct varobj *parent) 03246 { 03247 return xstrdup (parent->name); 03248 } 03249 03250 /* Return the value of element TYPE_INDEX of a structure 03251 value VALUE. VALUE's type should be a structure, 03252 or union, or a typedef to struct/union. 03253 03254 Returns NULL if getting the value fails. Never throws. */ 03255 static struct value * 03256 value_struct_element_index (struct value *value, int type_index) 03257 { 03258 struct value *result = NULL; 03259 volatile struct gdb_exception e; 03260 struct type *type = value_type (value); 03261 03262 type = check_typedef (type); 03263 03264 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT 03265 || TYPE_CODE (type) == TYPE_CODE_UNION); 03266 03267 TRY_CATCH (e, RETURN_MASK_ERROR) 03268 { 03269 if (field_is_static (&TYPE_FIELD (type, type_index))) 03270 result = value_static_field (type, type_index); 03271 else 03272 result = value_primitive_field (value, 0, type_index, type); 03273 } 03274 if (e.reason < 0) 03275 { 03276 return NULL; 03277 } 03278 else 03279 { 03280 return result; 03281 } 03282 } 03283 03284 /* Obtain the information about child INDEX of the variable 03285 object PARENT. 03286 If CNAME is not null, sets *CNAME to the name of the child relative 03287 to the parent. 03288 If CVALUE is not null, sets *CVALUE to the value of the child. 03289 If CTYPE is not null, sets *CTYPE to the type of the child. 03290 03291 If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding 03292 information cannot be determined, set *CNAME, *CVALUE, or *CTYPE 03293 to NULL. */ 03294 static void 03295 c_describe_child (struct varobj *parent, int index, 03296 char **cname, struct value **cvalue, struct type **ctype, 03297 char **cfull_expression) 03298 { 03299 struct value *value = parent->value; 03300 struct type *type = get_value_type (parent); 03301 char *parent_expression = NULL; 03302 int was_ptr; 03303 volatile struct gdb_exception except; 03304 03305 if (cname) 03306 *cname = NULL; 03307 if (cvalue) 03308 *cvalue = NULL; 03309 if (ctype) 03310 *ctype = NULL; 03311 if (cfull_expression) 03312 { 03313 *cfull_expression = NULL; 03314 parent_expression = varobj_get_path_expr (get_path_expr_parent (parent)); 03315 } 03316 adjust_value_for_child_access (&value, &type, &was_ptr, 0); 03317 03318 switch (TYPE_CODE (type)) 03319 { 03320 case TYPE_CODE_ARRAY: 03321 if (cname) 03322 *cname 03323 = xstrdup (int_string (index 03324 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)), 03325 10, 1, 0, 0)); 03326 03327 if (cvalue && value) 03328 { 03329 int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)); 03330 03331 TRY_CATCH (except, RETURN_MASK_ERROR) 03332 { 03333 *cvalue = value_subscript (value, real_index); 03334 } 03335 } 03336 03337 if (ctype) 03338 *ctype = get_target_type (type); 03339 03340 if (cfull_expression) 03341 *cfull_expression = 03342 xstrprintf ("(%s)[%s]", parent_expression, 03343 int_string (index 03344 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)), 03345 10, 1, 0, 0)); 03346 03347 03348 break; 03349 03350 case TYPE_CODE_STRUCT: 03351 case TYPE_CODE_UNION: 03352 { 03353 const char *field_name; 03354 03355 /* If the type is anonymous and the field has no name, 03356 set an appropriate name. */ 03357 field_name = TYPE_FIELD_NAME (type, index); 03358 if (field_name == NULL || *field_name == '\0') 03359 { 03360 if (cname) 03361 { 03362 if (TYPE_CODE (TYPE_FIELD_TYPE (type, index)) 03363 == TYPE_CODE_STRUCT) 03364 *cname = xstrdup (ANONYMOUS_STRUCT_NAME); 03365 else 03366 *cname = xstrdup (ANONYMOUS_UNION_NAME); 03367 } 03368 03369 if (cfull_expression) 03370 *cfull_expression = xstrdup (""); 03371 } 03372 else 03373 { 03374 if (cname) 03375 *cname = xstrdup (field_name); 03376 03377 if (cfull_expression) 03378 { 03379 char *join = was_ptr ? "->" : "."; 03380 03381 *cfull_expression = xstrprintf ("(%s)%s%s", parent_expression, 03382 join, field_name); 03383 } 03384 } 03385 03386 if (cvalue && value) 03387 { 03388 /* For C, varobj index is the same as type index. */ 03389 *cvalue = value_struct_element_index (value, index); 03390 } 03391 03392 if (ctype) 03393 *ctype = TYPE_FIELD_TYPE (type, index); 03394 } 03395 break; 03396 03397 case TYPE_CODE_PTR: 03398 if (cname) 03399 *cname = xstrprintf ("*%s", parent->name); 03400 03401 if (cvalue && value) 03402 { 03403 TRY_CATCH (except, RETURN_MASK_ERROR) 03404 { 03405 *cvalue = value_ind (value); 03406 } 03407 03408 if (except.reason < 0) 03409 *cvalue = NULL; 03410 } 03411 03412 /* Don't use get_target_type because it calls 03413 check_typedef and here, we want to show the true 03414 declared type of the variable. */ 03415 if (ctype) 03416 *ctype = TYPE_TARGET_TYPE (type); 03417 03418 if (cfull_expression) 03419 *cfull_expression = xstrprintf ("*(%s)", parent_expression); 03420 03421 break; 03422 03423 default: 03424 /* This should not happen. */ 03425 if (cname) 03426 *cname = xstrdup ("???"); 03427 if (cfull_expression) 03428 *cfull_expression = xstrdup ("???"); 03429 /* Don't set value and type, we don't know then. */ 03430 } 03431 } 03432 03433 static char * 03434 c_name_of_child (struct varobj *parent, int index) 03435 { 03436 char *name; 03437 03438 c_describe_child (parent, index, &name, NULL, NULL, NULL); 03439 return name; 03440 } 03441 03442 static char * 03443 c_path_expr_of_child (struct varobj *child) 03444 { 03445 c_describe_child (child->parent, child->index, NULL, NULL, NULL, 03446 &child->path_expr); 03447 return child->path_expr; 03448 } 03449 03450 static struct value * 03451 c_value_of_child (struct varobj *parent, int index) 03452 { 03453 struct value *value = NULL; 03454 03455 c_describe_child (parent, index, NULL, &value, NULL, NULL); 03456 return value; 03457 } 03458 03459 static struct type * 03460 c_type_of_child (struct varobj *parent, int index) 03461 { 03462 struct type *type = NULL; 03463 03464 c_describe_child (parent, index, NULL, NULL, &type, NULL); 03465 return type; 03466 } 03467 03468 static char * 03469 c_value_of_variable (struct varobj *var, enum varobj_display_formats format) 03470 { 03471 /* BOGUS: if val_print sees a struct/class, or a reference to one, 03472 it will print out its children instead of "{...}". So we need to 03473 catch that case explicitly. */ 03474 struct type *type = get_type (var); 03475 03476 /* Strip top-level references. */ 03477 while (TYPE_CODE (type) == TYPE_CODE_REF) 03478 type = check_typedef (TYPE_TARGET_TYPE (type)); 03479 03480 switch (TYPE_CODE (type)) 03481 { 03482 case TYPE_CODE_STRUCT: 03483 case TYPE_CODE_UNION: 03484 return xstrdup ("{...}"); 03485 /* break; */ 03486 03487 case TYPE_CODE_ARRAY: 03488 { 03489 char *number; 03490 03491 number = xstrprintf ("[%d]", var->num_children); 03492 return (number); 03493 } 03494 /* break; */ 03495 03496 default: 03497 { 03498 if (var->value == NULL) 03499 { 03500 /* This can happen if we attempt to get the value of a struct 03501 member when the parent is an invalid pointer. This is an 03502 error condition, so we should tell the caller. */ 03503 return NULL; 03504 } 03505 else 03506 { 03507 if (var->not_fetched && value_lazy (var->value)) 03508 /* Frozen variable and no value yet. We don't 03509 implicitly fetch the value. MI response will 03510 use empty string for the value, which is OK. */ 03511 return NULL; 03512 03513 gdb_assert (varobj_value_is_changeable_p (var)); 03514 gdb_assert (!value_lazy (var->value)); 03515 03516 /* If the specified format is the current one, 03517 we can reuse print_value. */ 03518 if (format == var->format) 03519 return xstrdup (var->print_value); 03520 else 03521 return value_get_print_value (var->value, format, var); 03522 } 03523 } 03524 } 03525 } 03526 03527 03528 /* C++ */ 03529 03530 static int 03531 cplus_number_of_children (struct varobj *var) 03532 { 03533 struct value *value = NULL; 03534 struct type *type; 03535 int children, dont_know; 03536 int lookup_actual_type = 0; 03537 struct value_print_options opts; 03538 03539 dont_know = 1; 03540 children = 0; 03541 03542 get_user_print_options (&opts); 03543 03544 if (!CPLUS_FAKE_CHILD (var)) 03545 { 03546 type = get_value_type (var); 03547 03548 /* It is necessary to access a real type (via RTTI). */ 03549 if (opts.objectprint) 03550 { 03551 value = var->value; 03552 lookup_actual_type = (TYPE_CODE (var->type) == TYPE_CODE_REF 03553 || TYPE_CODE (var->type) == TYPE_CODE_PTR); 03554 } 03555 adjust_value_for_child_access (&value, &type, NULL, lookup_actual_type); 03556 03557 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) || 03558 ((TYPE_CODE (type)) == TYPE_CODE_UNION)) 03559 { 03560 int kids[3]; 03561 03562 cplus_class_num_children (type, kids); 03563 if (kids[v_public] != 0) 03564 children++; 03565 if (kids[v_private] != 0) 03566 children++; 03567 if (kids[v_protected] != 0) 03568 children++; 03569 03570 /* Add any baseclasses. */ 03571 children += TYPE_N_BASECLASSES (type); 03572 dont_know = 0; 03573 03574 /* FIXME: save children in var. */ 03575 } 03576 } 03577 else 03578 { 03579 int kids[3]; 03580 03581 type = get_value_type (var->parent); 03582 03583 /* It is necessary to access a real type (via RTTI). */ 03584 if (opts.objectprint) 03585 { 03586 struct varobj *parent = var->parent; 03587 03588 value = parent->value; 03589 lookup_actual_type = (TYPE_CODE (parent->type) == TYPE_CODE_REF 03590 || TYPE_CODE (parent->type) == TYPE_CODE_PTR); 03591 } 03592 adjust_value_for_child_access (&value, &type, NULL, lookup_actual_type); 03593 03594 cplus_class_num_children (type, kids); 03595 if (strcmp (var->name, "public") == 0) 03596 children = kids[v_public]; 03597 else if (strcmp (var->name, "private") == 0) 03598 children = kids[v_private]; 03599 else 03600 children = kids[v_protected]; 03601 dont_know = 0; 03602 } 03603 03604 if (dont_know) 03605 children = c_number_of_children (var); 03606 03607 return children; 03608 } 03609 03610 /* Compute # of public, private, and protected variables in this class. 03611 That means we need to descend into all baseclasses and find out 03612 how many are there, too. */ 03613 static void 03614 cplus_class_num_children (struct type *type, int children[3]) 03615 { 03616 int i, vptr_fieldno; 03617 struct type *basetype = NULL; 03618 03619 children[v_public] = 0; 03620 children[v_private] = 0; 03621 children[v_protected] = 0; 03622 03623 vptr_fieldno = get_vptr_fieldno (type, &basetype); 03624 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++) 03625 { 03626 /* If we have a virtual table pointer, omit it. Even if virtual 03627 table pointers are not specifically marked in the debug info, 03628 they should be artificial. */ 03629 if ((type == basetype && i == vptr_fieldno) 03630 || TYPE_FIELD_ARTIFICIAL (type, i)) 03631 continue; 03632 03633 if (TYPE_FIELD_PROTECTED (type, i)) 03634 children[v_protected]++; 03635 else if (TYPE_FIELD_PRIVATE (type, i)) 03636 children[v_private]++; 03637 else 03638 children[v_public]++; 03639 } 03640 } 03641 03642 static char * 03643 cplus_name_of_variable (struct varobj *parent) 03644 { 03645 return c_name_of_variable (parent); 03646 } 03647 03648 enum accessibility { private_field, protected_field, public_field }; 03649 03650 /* Check if field INDEX of TYPE has the specified accessibility. 03651 Return 0 if so and 1 otherwise. */ 03652 static int 03653 match_accessibility (struct type *type, int index, enum accessibility acc) 03654 { 03655 if (acc == private_field && TYPE_FIELD_PRIVATE (type, index)) 03656 return 1; 03657 else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index)) 03658 return 1; 03659 else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index) 03660 && !TYPE_FIELD_PROTECTED (type, index)) 03661 return 1; 03662 else 03663 return 0; 03664 } 03665 03666 static void 03667 cplus_describe_child (struct varobj *parent, int index, 03668 char **cname, struct value **cvalue, struct type **ctype, 03669 char **cfull_expression) 03670 { 03671 struct value *value; 03672 struct type *type; 03673 int was_ptr; 03674 int lookup_actual_type = 0; 03675 char *parent_expression = NULL; 03676 struct varobj *var; 03677 struct value_print_options opts; 03678 03679 if (cname) 03680 *cname = NULL; 03681 if (cvalue) 03682 *cvalue = NULL; 03683 if (ctype) 03684 *ctype = NULL; 03685 if (cfull_expression) 03686 *cfull_expression = NULL; 03687 03688 get_user_print_options (&opts); 03689 03690 var = (CPLUS_FAKE_CHILD (parent)) ? parent->parent : parent; 03691 if (opts.objectprint) 03692 lookup_actual_type = (TYPE_CODE (var->type) == TYPE_CODE_REF 03693 || TYPE_CODE (var->type) == TYPE_CODE_PTR); 03694 value = var->value; 03695 type = get_value_type (var); 03696 if (cfull_expression) 03697 parent_expression = varobj_get_path_expr (get_path_expr_parent (var)); 03698 03699 adjust_value_for_child_access (&value, &type, &was_ptr, lookup_actual_type); 03700 03701 if (TYPE_CODE (type) == TYPE_CODE_STRUCT 03702 || TYPE_CODE (type) == TYPE_CODE_UNION) 03703 { 03704 char *join = was_ptr ? "->" : "."; 03705 03706 if (CPLUS_FAKE_CHILD (parent)) 03707 { 03708 /* The fields of the class type are ordered as they 03709 appear in the class. We are given an index for a 03710 particular access control type ("public","protected", 03711 or "private"). We must skip over fields that don't 03712 have the access control we are looking for to properly 03713 find the indexed field. */ 03714 int type_index = TYPE_N_BASECLASSES (type); 03715 enum accessibility acc = public_field; 03716 int vptr_fieldno; 03717 struct type *basetype = NULL; 03718 const char *field_name; 03719 03720 vptr_fieldno = get_vptr_fieldno (type, &basetype); 03721 if (strcmp (parent->name, "private") == 0) 03722 acc = private_field; 03723 else if (strcmp (parent->name, "protected") == 0) 03724 acc = protected_field; 03725 03726 while (index >= 0) 03727 { 03728 if ((type == basetype && type_index == vptr_fieldno) 03729 || TYPE_FIELD_ARTIFICIAL (type, type_index)) 03730 ; /* ignore vptr */ 03731 else if (match_accessibility (type, type_index, acc)) 03732 --index; 03733 ++type_index; 03734 } 03735 --type_index; 03736 03737 /* If the type is anonymous and the field has no name, 03738 set an appopriate name. */ 03739 field_name = TYPE_FIELD_NAME (type, type_index); 03740 if (field_name == NULL || *field_name == '\0') 03741 { 03742 if (cname) 03743 { 03744 if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index)) 03745 == TYPE_CODE_STRUCT) 03746 *cname = xstrdup (ANONYMOUS_STRUCT_NAME); 03747 else if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index)) 03748 == TYPE_CODE_UNION) 03749 *cname = xstrdup (ANONYMOUS_UNION_NAME); 03750 } 03751 03752 if (cfull_expression) 03753 *cfull_expression = xstrdup (""); 03754 } 03755 else 03756 { 03757 if (cname) 03758 *cname = xstrdup (TYPE_FIELD_NAME (type, type_index)); 03759 03760 if (cfull_expression) 03761 *cfull_expression 03762 = xstrprintf ("((%s)%s%s)", parent_expression, join, 03763 field_name); 03764 } 03765 03766 if (cvalue && value) 03767 *cvalue = value_struct_element_index (value, type_index); 03768 03769 if (ctype) 03770 *ctype = TYPE_FIELD_TYPE (type, type_index); 03771 } 03772 else if (index < TYPE_N_BASECLASSES (type)) 03773 { 03774 /* This is a baseclass. */ 03775 if (cname) 03776 *cname = xstrdup (TYPE_FIELD_NAME (type, index)); 03777 03778 if (cvalue && value) 03779 *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value); 03780 03781 if (ctype) 03782 { 03783 *ctype = TYPE_FIELD_TYPE (type, index); 03784 } 03785 03786 if (cfull_expression) 03787 { 03788 char *ptr = was_ptr ? "*" : ""; 03789 03790 /* Cast the parent to the base' type. Note that in gdb, 03791 expression like 03792 (Base1)d 03793 will create an lvalue, for all appearences, so we don't 03794 need to use more fancy: 03795 *(Base1*)(&d) 03796 construct. 03797 03798 When we are in the scope of the base class or of one 03799 of its children, the type field name will be interpreted 03800 as a constructor, if it exists. Therefore, we must 03801 indicate that the name is a class name by using the 03802 'class' keyword. See PR mi/11912 */ 03803 *cfull_expression = xstrprintf ("(%s(class %s%s) %s)", 03804 ptr, 03805 TYPE_FIELD_NAME (type, index), 03806 ptr, 03807 parent_expression); 03808 } 03809 } 03810 else 03811 { 03812 char *access = NULL; 03813 int children[3]; 03814 03815 cplus_class_num_children (type, children); 03816 03817 /* Everything beyond the baseclasses can 03818 only be "public", "private", or "protected" 03819 03820 The special "fake" children are always output by varobj in 03821 this order. So if INDEX == 2, it MUST be "protected". */ 03822 index -= TYPE_N_BASECLASSES (type); 03823 switch (index) 03824 { 03825 case 0: 03826 if (children[v_public] > 0) 03827 access = "public"; 03828 else if (children[v_private] > 0) 03829 access = "private"; 03830 else 03831 access = "protected"; 03832 break; 03833 case 1: 03834 if (children[v_public] > 0) 03835 { 03836 if (children[v_private] > 0) 03837 access = "private"; 03838 else 03839 access = "protected"; 03840 } 03841 else if (children[v_private] > 0) 03842 access = "protected"; 03843 break; 03844 case 2: 03845 /* Must be protected. */ 03846 access = "protected"; 03847 break; 03848 default: 03849 /* error! */ 03850 break; 03851 } 03852 03853 gdb_assert (access); 03854 if (cname) 03855 *cname = xstrdup (access); 03856 03857 /* Value and type and full expression are null here. */ 03858 } 03859 } 03860 else 03861 { 03862 c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression); 03863 } 03864 } 03865 03866 static char * 03867 cplus_name_of_child (struct varobj *parent, int index) 03868 { 03869 char *name = NULL; 03870 03871 cplus_describe_child (parent, index, &name, NULL, NULL, NULL); 03872 return name; 03873 } 03874 03875 static char * 03876 cplus_path_expr_of_child (struct varobj *child) 03877 { 03878 cplus_describe_child (child->parent, child->index, NULL, NULL, NULL, 03879 &child->path_expr); 03880 return child->path_expr; 03881 } 03882 03883 static struct value * 03884 cplus_value_of_child (struct varobj *parent, int index) 03885 { 03886 struct value *value = NULL; 03887 03888 cplus_describe_child (parent, index, NULL, &value, NULL, NULL); 03889 return value; 03890 } 03891 03892 static struct type * 03893 cplus_type_of_child (struct varobj *parent, int index) 03894 { 03895 struct type *type = NULL; 03896 03897 cplus_describe_child (parent, index, NULL, NULL, &type, NULL); 03898 return type; 03899 } 03900 03901 static char * 03902 cplus_value_of_variable (struct varobj *var, 03903 enum varobj_display_formats format) 03904 { 03905 03906 /* If we have one of our special types, don't print out 03907 any value. */ 03908 if (CPLUS_FAKE_CHILD (var)) 03909 return xstrdup (""); 03910 03911 return c_value_of_variable (var, format); 03912 } 03913 03914 /* Java */ 03915 03916 static int 03917 java_number_of_children (struct varobj *var) 03918 { 03919 return cplus_number_of_children (var); 03920 } 03921 03922 static char * 03923 java_name_of_variable (struct varobj *parent) 03924 { 03925 char *p, *name; 03926 03927 name = cplus_name_of_variable (parent); 03928 /* If the name has "-" in it, it is because we 03929 needed to escape periods in the name... */ 03930 p = name; 03931 03932 while (*p != '\000') 03933 { 03934 if (*p == '-') 03935 *p = '.'; 03936 p++; 03937 } 03938 03939 return name; 03940 } 03941 03942 static char * 03943 java_name_of_child (struct varobj *parent, int index) 03944 { 03945 char *name, *p; 03946 03947 name = cplus_name_of_child (parent, index); 03948 /* Escape any periods in the name... */ 03949 p = name; 03950 03951 while (*p != '\000') 03952 { 03953 if (*p == '.') 03954 *p = '-'; 03955 p++; 03956 } 03957 03958 return name; 03959 } 03960 03961 static char * 03962 java_path_expr_of_child (struct varobj *child) 03963 { 03964 return NULL; 03965 } 03966 03967 static struct value * 03968 java_value_of_child (struct varobj *parent, int index) 03969 { 03970 return cplus_value_of_child (parent, index); 03971 } 03972 03973 static struct type * 03974 java_type_of_child (struct varobj *parent, int index) 03975 { 03976 return cplus_type_of_child (parent, index); 03977 } 03978 03979 static char * 03980 java_value_of_variable (struct varobj *var, enum varobj_display_formats format) 03981 { 03982 return cplus_value_of_variable (var, format); 03983 } 03984 03985 /* Ada specific callbacks for VAROBJs. */ 03986 03987 static int 03988 ada_number_of_children (struct varobj *var) 03989 { 03990 return ada_varobj_get_number_of_children (var->value, var->type); 03991 } 03992 03993 static char * 03994 ada_name_of_variable (struct varobj *parent) 03995 { 03996 return c_name_of_variable (parent); 03997 } 03998 03999 static char * 04000 ada_name_of_child (struct varobj *parent, int index) 04001 { 04002 return ada_varobj_get_name_of_child (parent->value, parent->type, 04003 parent->name, index); 04004 } 04005 04006 static char* 04007 ada_path_expr_of_child (struct varobj *child) 04008 { 04009 struct varobj *parent = child->parent; 04010 const char *parent_path_expr = varobj_get_path_expr (parent); 04011 04012 return ada_varobj_get_path_expr_of_child (parent->value, 04013 parent->type, 04014 parent->name, 04015 parent_path_expr, 04016 child->index); 04017 } 04018 04019 static struct value * 04020 ada_value_of_child (struct varobj *parent, int index) 04021 { 04022 return ada_varobj_get_value_of_child (parent->value, parent->type, 04023 parent->name, index); 04024 } 04025 04026 static struct type * 04027 ada_type_of_child (struct varobj *parent, int index) 04028 { 04029 return ada_varobj_get_type_of_child (parent->value, parent->type, 04030 index); 04031 } 04032 04033 static char * 04034 ada_value_of_variable (struct varobj *var, enum varobj_display_formats format) 04035 { 04036 struct value_print_options opts; 04037 04038 get_formatted_print_options (&opts, format_code[(int) format]); 04039 opts.deref_ref = 0; 04040 opts.raw = 1; 04041 04042 return ada_varobj_get_value_of_variable (var->value, var->type, &opts); 04043 } 04044 04045 /* Implement the "value_is_changeable_p" routine for Ada. */ 04046 04047 static int 04048 ada_value_is_changeable_p (struct varobj *var) 04049 { 04050 struct type *type = var->value ? value_type (var->value) : var->type; 04051 04052 if (ada_is_array_descriptor_type (type) 04053 && TYPE_CODE (type) == TYPE_CODE_TYPEDEF) 04054 { 04055 /* This is in reality a pointer to an unconstrained array. 04056 its value is changeable. */ 04057 return 1; 04058 } 04059 04060 if (ada_is_string_type (type)) 04061 { 04062 /* We display the contents of the string in the array's 04063 "value" field. The contents can change, so consider 04064 that the array is changeable. */ 04065 return 1; 04066 } 04067 04068 return default_value_is_changeable_p (var); 04069 } 04070 04071 /* Implement the "value_has_mutated" routine for Ada. */ 04072 04073 static int 04074 ada_value_has_mutated (struct varobj *var, struct value *new_val, 04075 struct type *new_type) 04076 { 04077 int i; 04078 int from = -1; 04079 int to = -1; 04080 04081 /* If the number of fields have changed, then for sure the type 04082 has mutated. */ 04083 if (ada_varobj_get_number_of_children (new_val, new_type) 04084 != var->num_children) 04085 return 1; 04086 04087 /* If the number of fields have remained the same, then we need 04088 to check the name of each field. If they remain the same, 04089 then chances are the type hasn't mutated. This is technically 04090 an incomplete test, as the child's type might have changed 04091 despite the fact that the name remains the same. But we'll 04092 handle this situation by saying that the child has mutated, 04093 not this value. 04094 04095 If only part (or none!) of the children have been fetched, 04096 then only check the ones we fetched. It does not matter 04097 to the frontend whether a child that it has not fetched yet 04098 has mutated or not. So just assume it hasn't. */ 04099 04100 restrict_range (var->children, &from, &to); 04101 for (i = from; i < to; i++) 04102 if (strcmp (ada_varobj_get_name_of_child (new_val, new_type, 04103 var->name, i), 04104 VEC_index (varobj_p, var->children, i)->name) != 0) 04105 return 1; 04106 04107 return 0; 04108 } 04109 04110 /* Iterate all the existing _root_ VAROBJs and call the FUNC callback for them 04111 with an arbitrary caller supplied DATA pointer. */ 04112 04113 void 04114 all_root_varobjs (void (*func) (struct varobj *var, void *data), void *data) 04115 { 04116 struct varobj_root *var_root, *var_root_next; 04117 04118 /* Iterate "safely" - handle if the callee deletes its passed VAROBJ. */ 04119 04120 for (var_root = rootlist; var_root != NULL; var_root = var_root_next) 04121 { 04122 var_root_next = var_root->next; 04123 04124 (*func) (var_root->rootvar, data); 04125 } 04126 } 04127 04128 extern void _initialize_varobj (void); 04129 void 04130 _initialize_varobj (void) 04131 { 04132 int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE; 04133 04134 varobj_table = xmalloc (sizeof_table); 04135 memset (varobj_table, 0, sizeof_table); 04136 04137 add_setshow_zuinteger_cmd ("debugvarobj", class_maintenance, 04138 &varobjdebug, 04139 _("Set varobj debugging."), 04140 _("Show varobj debugging."), 04141 _("When non-zero, varobj debugging is enabled."), 04142 NULL, show_varobjdebug, 04143 &setlist, &showlist); 04144 } 04145 04146 /* Invalidate varobj VAR if it is tied to locals and re-create it if it is 04147 defined on globals. It is a helper for varobj_invalidate. 04148 04149 This function is called after changing the symbol file, in this case the 04150 pointers to "struct type" stored by the varobj are no longer valid. All 04151 varobj must be either re-evaluated, or marked as invalid here. */ 04152 04153 static void 04154 varobj_invalidate_iter (struct varobj *var, void *unused) 04155 { 04156 /* global and floating var must be re-evaluated. */ 04157 if (var->root->floating || var->root->valid_block == NULL) 04158 { 04159 struct varobj *tmp_var; 04160 04161 /* Try to create a varobj with same expression. If we succeed 04162 replace the old varobj, otherwise invalidate it. */ 04163 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0, 04164 USE_CURRENT_FRAME); 04165 if (tmp_var != NULL) 04166 { 04167 tmp_var->obj_name = xstrdup (var->obj_name); 04168 varobj_delete (var, NULL, 0); 04169 install_variable (tmp_var); 04170 } 04171 else 04172 var->root->is_valid = 0; 04173 } 04174 else /* locals must be invalidated. */ 04175 var->root->is_valid = 0; 04176 } 04177 04178 /* Invalidate the varobjs that are tied to locals and re-create the ones that 04179 are defined on globals. 04180 Invalidated varobjs will be always printed in_scope="invalid". */ 04181 04182 void 04183 varobj_invalidate (void) 04184 { 04185 all_root_varobjs (varobj_invalidate_iter, NULL); 04186 }