1 /* MI Command Set - varobj commands.
3 Copyright (C) 2000, 2002, 2004, 2005, 2007, 2008
4 Free Software Foundation, Inc.
6 Contributed by Cygnus Solutions (a Red Hat company).
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 #include "gdb_string.h"
32 const char mi_no_values
[] = "--no-values";
33 const char mi_simple_values
[] = "--simple-values";
34 const char mi_all_values
[] = "--all-values";
36 extern int varobjdebug
; /* defined in varobj.c. */
38 static void varobj_update_one (struct varobj
*var
,
39 enum print_values print_values
,
42 static int mi_print_value_p (struct type
*type
, enum print_values print_values
);
44 /* Print variable object VAR. The PRINT_VALUES parameter controls
45 if the value should be printed. The PRINT_EXPRESSION parameter
46 controls if the expression should be printed. */
48 print_varobj (struct varobj
*var
, enum print_values print_values
,
51 struct type
*gdb_type
;
55 ui_out_field_string (uiout
, "name", varobj_get_objname (var
));
57 ui_out_field_string (uiout
, "exp", varobj_get_expression (var
));
58 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
60 if (mi_print_value_p (varobj_get_gdb_type (var
), print_values
))
61 ui_out_field_string (uiout
, "value", varobj_get_value (var
));
63 type
= varobj_get_type (var
);
66 ui_out_field_string (uiout
, "type", type
);
70 thread_id
= varobj_get_thread_id (var
);
72 ui_out_field_int (uiout
, "thread-id", thread_id
);
74 if (varobj_get_frozen (var
))
75 ui_out_field_int (uiout
, "frozen", 1);
78 /* VAROBJ operations */
81 mi_cmd_var_create (char *command
, char **argv
, int argc
)
83 CORE_ADDR frameaddr
= 0;
88 struct cleanup
*old_cleanups
;
89 enum varobj_type var_type
;
93 /* mi_error_message = xstrprintf ("mi_cmd_var_create: Usage:
94 ...."); return MI_CMD_ERROR; */
95 error (_("mi_cmd_var_create: Usage: NAME FRAME EXPRESSION."));
98 name
= xstrdup (argv
[0]);
99 /* Add cleanup for name. Must be free_current_contents as
100 name can be reallocated */
101 old_cleanups
= make_cleanup (free_current_contents
, &name
);
103 frame
= xstrdup (argv
[1]);
104 make_cleanup (xfree
, frame
);
106 expr
= xstrdup (argv
[2]);
107 make_cleanup (xfree
, expr
);
109 if (strcmp (name
, "-") == 0)
112 name
= varobj_gen_name ();
114 else if (!isalpha (*name
))
115 error (_("mi_cmd_var_create: name of object must begin with a letter"));
117 if (strcmp (frame
, "*") == 0)
118 var_type
= USE_CURRENT_FRAME
;
119 else if (strcmp (frame
, "@") == 0)
120 var_type
= USE_SELECTED_FRAME
;
123 var_type
= USE_SPECIFIED_FRAME
;
124 frameaddr
= string_to_core_addr (frame
);
128 fprintf_unfiltered (gdb_stdlog
,
129 "Name=\"%s\", Frame=\"%s\" (0x%s), Expression=\"%s\"\n",
130 name
, frame
, paddr (frameaddr
), expr
);
132 var
= varobj_create (name
, expr
, frameaddr
, var_type
);
135 error (_("mi_cmd_var_create: unable to create variable object"));
137 print_varobj (var
, PRINT_ALL_VALUES
, 0 /* don't print expression */);
139 do_cleanups (old_cleanups
);
144 mi_cmd_var_delete (char *command
, char **argv
, int argc
)
149 int children_only_p
= 0;
150 struct cleanup
*old_cleanups
;
152 if (argc
< 1 || argc
> 2)
153 error (_("mi_cmd_var_delete: Usage: [-c] EXPRESSION."));
155 name
= xstrdup (argv
[0]);
156 /* Add cleanup for name. Must be free_current_contents as
157 name can be reallocated */
158 old_cleanups
= make_cleanup (free_current_contents
, &name
);
160 /* If we have one single argument it cannot be '-c' or any string
161 starting with '-'. */
164 if (strcmp (name
, "-c") == 0)
165 error (_("mi_cmd_var_delete: Missing required argument after '-c': variable object name"));
167 error (_("mi_cmd_var_delete: Illegal variable object name"));
170 /* If we have 2 arguments they must be '-c' followed by a string
171 which would be the variable name. */
174 if (strcmp (name
, "-c") != 0)
175 error (_("mi_cmd_var_delete: Invalid option."));
177 do_cleanups (old_cleanups
);
178 name
= xstrdup (argv
[1]);
179 make_cleanup (free_current_contents
, &name
);
182 /* If we didn't error out, now NAME contains the name of the
185 var
= varobj_get_handle (name
);
188 error (_("mi_cmd_var_delete: Variable object not found."));
190 numdel
= varobj_delete (var
, NULL
, children_only_p
);
192 ui_out_field_int (uiout
, "ndeleted", numdel
);
194 do_cleanups (old_cleanups
);
199 mi_cmd_var_set_format (char *command
, char **argv
, int argc
)
201 enum varobj_display_formats format
;
207 error (_("mi_cmd_var_set_format: Usage: NAME FORMAT."));
209 /* Get varobj handle, if a valid var obj name was specified */
210 var
= varobj_get_handle (argv
[0]);
213 error (_("mi_cmd_var_set_format: Variable object not found"));
216 if (formspec
== NULL
)
217 error (_("mi_cmd_var_set_format: Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
219 len
= strlen (formspec
);
221 if (strncmp (formspec
, "natural", len
) == 0)
222 format
= FORMAT_NATURAL
;
223 else if (strncmp (formspec
, "binary", len
) == 0)
224 format
= FORMAT_BINARY
;
225 else if (strncmp (formspec
, "decimal", len
) == 0)
226 format
= FORMAT_DECIMAL
;
227 else if (strncmp (formspec
, "hexadecimal", len
) == 0)
228 format
= FORMAT_HEXADECIMAL
;
229 else if (strncmp (formspec
, "octal", len
) == 0)
230 format
= FORMAT_OCTAL
;
232 error (_("mi_cmd_var_set_format: Unknown display format: must be: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
234 /* Set the format of VAR to given format */
235 varobj_set_display_format (var
, format
);
237 /* Report the new current format */
238 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
240 /* Report the value in the new format */
241 ui_out_field_string (uiout
, "value", varobj_get_value (var
));
246 mi_cmd_var_set_frozen (char *command
, char **argv
, int argc
)
252 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
254 var
= varobj_get_handle (argv
[0]);
256 error (_("Variable object not found"));
258 if (strcmp (argv
[1], "0") == 0)
260 else if (strcmp (argv
[1], "1") == 0)
263 error (_("Invalid flag value"));
265 varobj_set_frozen (var
, frozen
);
267 /* We don't automatically return the new value, or what varobjs got new
268 values during unfreezing. If this information is required, client
269 should call -var-update explicitly. */
275 mi_cmd_var_show_format (char *command
, char **argv
, int argc
)
277 enum varobj_display_formats format
;
281 error (_("mi_cmd_var_show_format: Usage: NAME."));
283 /* Get varobj handle, if a valid var obj name was specified */
284 var
= varobj_get_handle (argv
[0]);
286 error (_("mi_cmd_var_show_format: Variable object not found"));
288 format
= varobj_get_display_format (var
);
290 /* Report the current format */
291 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
296 mi_cmd_var_info_num_children (char *command
, char **argv
, int argc
)
301 error (_("mi_cmd_var_info_num_children: Usage: NAME."));
303 /* Get varobj handle, if a valid var obj name was specified */
304 var
= varobj_get_handle (argv
[0]);
306 error (_("mi_cmd_var_info_num_children: Variable object not found"));
308 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
312 /* Parse a string argument into a print_values value. */
314 static enum print_values
315 mi_parse_values_option (const char *arg
)
317 if (strcmp (arg
, "0") == 0
318 || strcmp (arg
, mi_no_values
) == 0)
319 return PRINT_NO_VALUES
;
320 else if (strcmp (arg
, "1") == 0
321 || strcmp (arg
, mi_all_values
) == 0)
322 return PRINT_ALL_VALUES
;
323 else if (strcmp (arg
, "2") == 0
324 || strcmp (arg
, mi_simple_values
) == 0)
325 return PRINT_SIMPLE_VALUES
;
327 error (_("Unknown value for PRINT_VALUES\n\
328 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
329 mi_no_values
, mi_simple_values
, mi_all_values
);
332 /* Return 1 if given the argument PRINT_VALUES we should display
333 a value of type TYPE. */
336 mi_print_value_p (struct type
*type
, enum print_values print_values
)
339 if (print_values
== PRINT_NO_VALUES
)
342 if (print_values
== PRINT_ALL_VALUES
)
349 type
= check_typedef (type
);
351 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
352 and that type is not a compound type. */
353 return (TYPE_CODE (type
) != TYPE_CODE_ARRAY
354 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
355 && TYPE_CODE (type
) != TYPE_CODE_UNION
);
360 mi_cmd_var_list_children (char *command
, char **argv
, int argc
)
363 VEC(varobj_p
) *children
;
364 struct varobj
*child
;
365 struct cleanup
*cleanup_children
;
367 enum print_values print_values
;
370 if (argc
!= 1 && argc
!= 2)
371 error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME"));
373 /* Get varobj handle, if a valid var obj name was specified */
375 var
= varobj_get_handle (argv
[0]);
377 var
= varobj_get_handle (argv
[1]);
379 error (_("Variable object not found"));
381 children
= varobj_list_children (var
);
382 ui_out_field_int (uiout
, "numchild", VEC_length (varobj_p
, children
));
384 print_values
= mi_parse_values_option (argv
[0]);
386 print_values
= PRINT_NO_VALUES
;
388 if (VEC_length (varobj_p
, children
) == 0)
391 if (mi_version (uiout
) == 1)
392 cleanup_children
= make_cleanup_ui_out_tuple_begin_end (uiout
, "children");
394 cleanup_children
= make_cleanup_ui_out_list_begin_end (uiout
, "children");
395 for (ix
= 0; VEC_iterate (varobj_p
, children
, ix
, child
); ++ix
)
397 struct cleanup
*cleanup_child
;
398 cleanup_child
= make_cleanup_ui_out_tuple_begin_end (uiout
, "child");
399 print_varobj (child
, print_values
, 1 /* print expression */);
400 do_cleanups (cleanup_child
);
402 do_cleanups (cleanup_children
);
407 mi_cmd_var_info_type (char *command
, char **argv
, int argc
)
412 error (_("mi_cmd_var_info_type: Usage: NAME."));
414 /* Get varobj handle, if a valid var obj name was specified */
415 var
= varobj_get_handle (argv
[0]);
417 error (_("mi_cmd_var_info_type: Variable object not found"));
419 ui_out_field_string (uiout
, "type", varobj_get_type (var
));
424 mi_cmd_var_info_path_expression (char *command
, char **argv
, int argc
)
430 error (_("Usage: NAME."));
432 /* Get varobj handle, if a valid var obj name was specified. */
433 var
= varobj_get_handle (argv
[0]);
435 error (_("Variable object not found"));
437 path_expr
= varobj_get_path_expr (var
);
439 ui_out_field_string (uiout
, "path_expr", path_expr
);
445 mi_cmd_var_info_expression (char *command
, char **argv
, int argc
)
447 enum varobj_languages lang
;
451 error (_("mi_cmd_var_info_expression: Usage: NAME."));
453 /* Get varobj handle, if a valid var obj name was specified */
454 var
= varobj_get_handle (argv
[0]);
456 error (_("mi_cmd_var_info_expression: Variable object not found"));
458 lang
= varobj_get_language (var
);
460 ui_out_field_string (uiout
, "lang", varobj_language_string
[(int) lang
]);
461 ui_out_field_string (uiout
, "exp", varobj_get_expression (var
));
466 mi_cmd_var_show_attributes (char *command
, char **argv
, int argc
)
473 error (_("mi_cmd_var_show_attributes: Usage: NAME."));
475 /* Get varobj handle, if a valid var obj name was specified */
476 var
= varobj_get_handle (argv
[0]);
478 error (_("mi_cmd_var_show_attributes: Variable object not found"));
480 attr
= varobj_get_attributes (var
);
481 /* FIXME: define masks for attributes */
482 if (attr
& 0x00000001)
485 attstr
= "noneditable";
487 ui_out_field_string (uiout
, "attr", attstr
);
492 mi_cmd_var_evaluate_expression (char *command
, char **argv
, int argc
)
497 error (_("mi_cmd_var_evaluate_expression: Usage: NAME."));
499 /* Get varobj handle, if a valid var obj name was specified */
500 var
= varobj_get_handle (argv
[0]);
502 error (_("mi_cmd_var_evaluate_expression: Variable object not found"));
504 ui_out_field_string (uiout
, "value", varobj_get_value (var
));
509 mi_cmd_var_assign (char *command
, char **argv
, int argc
)
515 error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION."));
517 /* Get varobj handle, if a valid var obj name was specified */
518 var
= varobj_get_handle (argv
[0]);
520 error (_("mi_cmd_var_assign: Variable object not found"));
522 if (!varobj_editable_p (var
))
523 error (_("mi_cmd_var_assign: Variable object is not editable"));
525 expression
= xstrdup (argv
[1]);
527 if (!varobj_set_value (var
, expression
))
528 error (_("mi_cmd_var_assign: Could not assign expression to variable object"));
530 ui_out_field_string (uiout
, "value", varobj_get_value (var
));
535 mi_cmd_var_update (char *command
, char **argv
, int argc
)
538 struct varobj
**rootlist
;
540 struct cleanup
*cleanup
;
543 enum print_values print_values
;
545 if (argc
!= 1 && argc
!= 2)
546 error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME."));
554 print_values
= mi_parse_values_option (argv
[0]);
556 print_values
= PRINT_NO_VALUES
;
558 /* Check if the parameter is a "*" which means that we want
559 to update all variables */
561 if ((*name
== '*') && (*(name
+ 1) == '\0'))
563 nv
= varobj_list (&rootlist
);
564 cleanup
= make_cleanup (xfree
, rootlist
);
565 if (mi_version (uiout
) <= 1)
566 make_cleanup_ui_out_tuple_begin_end (uiout
, "changelist");
568 make_cleanup_ui_out_list_begin_end (uiout
, "changelist");
571 do_cleanups (cleanup
);
577 varobj_update_one (*cr
, print_values
, 0 /* implicit */);
580 do_cleanups (cleanup
);
584 /* Get varobj handle, if a valid var obj name was specified */
585 var
= varobj_get_handle (name
);
587 error (_("mi_cmd_var_update: Variable object not found"));
589 if (mi_version (uiout
) <= 1)
590 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, "changelist");
592 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changelist");
593 varobj_update_one (var
, print_values
, 1 /* explicit */);
594 do_cleanups (cleanup
);
599 /* Helper for mi_cmd_var_update(). */
602 varobj_update_one (struct varobj
*var
, enum print_values print_values
,
605 struct varobj
**changelist
;
607 struct cleanup
*cleanup
= NULL
;
610 nc
= varobj_update (&var
, &changelist
, explicit);
612 /* nc >= 0 represents the number of changes reported into changelist.
613 nc < 0 means that an error occured or the the variable has
614 changed type (TYPE_CHANGED). */
620 if (mi_version (uiout
) > 1)
621 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
622 ui_out_field_string (uiout
, "name", varobj_get_objname(var
));
627 ui_out_field_string (uiout
, "in_scope", "false");
630 ui_out_field_string (uiout
, "in_scope", "invalid");
633 ui_out_field_string (uiout
, "in_scope", "true");
634 ui_out_field_string (uiout
, "new_type", varobj_get_type(var
));
635 ui_out_field_int (uiout
, "new_num_children",
636 varobj_get_num_children(var
));
639 if (mi_version (uiout
) > 1)
640 do_cleanups (cleanup
);
647 if (mi_version (uiout
) > 1)
648 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
649 ui_out_field_string (uiout
, "name", varobj_get_objname (*cc
));
650 if (mi_print_value_p (varobj_get_gdb_type (*cc
), print_values
))
651 ui_out_field_string (uiout
, "value", varobj_get_value (*cc
));
652 ui_out_field_string (uiout
, "in_scope", "true");
653 ui_out_field_string (uiout
, "type_changed", "false");
654 if (mi_version (uiout
) > 1)
655 do_cleanups (cleanup
);