1 /* MI Command Set - varobj commands.
3 Copyright (C) 2000, 2002, 2004, 2005, 2007 Free Software Foundation, Inc.
5 Contributed by Cygnus Solutions (a Red Hat company).
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
31 #include "gdb_string.h"
33 const char mi_no_values
[] = "--no-values";
34 const char mi_simple_values
[] = "--simple-values";
35 const char mi_all_values
[] = "--all-values";
37 extern int varobjdebug
; /* defined in varobj.c. */
39 static void varobj_update_one (struct varobj
*var
,
40 enum print_values print_values
,
43 static int mi_print_value_p (struct type
*type
, enum print_values print_values
);
45 /* Print variable object VAR. The PRINT_VALUES parameter controls
46 if the value should be printed. The PRINT_EXPRESSION parameter
47 controls if the expression should be printed. */
49 print_varobj (struct varobj
*var
, enum print_values print_values
,
54 ui_out_field_string (uiout
, "name", varobj_get_objname (var
));
56 ui_out_field_string (uiout
, "exp", varobj_get_expression (var
));
57 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
59 if (mi_print_value_p (varobj_get_gdb_type (var
), print_values
))
60 ui_out_field_string (uiout
, "value", varobj_get_value (var
));
62 type
= varobj_get_type (var
);
65 ui_out_field_string (uiout
, "type", type
);
69 if (varobj_get_frozen (var
))
70 ui_out_field_int (uiout
, "frozen", 1);
73 /* VAROBJ operations */
76 mi_cmd_var_create (char *command
, char **argv
, int argc
)
78 CORE_ADDR frameaddr
= 0;
83 struct cleanup
*old_cleanups
;
84 enum varobj_type var_type
;
88 /* mi_error_message = xstrprintf ("mi_cmd_var_create: Usage:
89 ...."); return MI_CMD_ERROR; */
90 error (_("mi_cmd_var_create: Usage: NAME FRAME EXPRESSION."));
93 name
= xstrdup (argv
[0]);
94 /* Add cleanup for name. Must be free_current_contents as
95 name can be reallocated */
96 old_cleanups
= make_cleanup (free_current_contents
, &name
);
98 frame
= xstrdup (argv
[1]);
99 make_cleanup (xfree
, frame
);
101 expr
= xstrdup (argv
[2]);
102 make_cleanup (xfree
, expr
);
104 if (strcmp (name
, "-") == 0)
107 name
= varobj_gen_name ();
109 else if (!isalpha (*name
))
110 error (_("mi_cmd_var_create: name of object must begin with a letter"));
112 if (strcmp (frame
, "*") == 0)
113 var_type
= USE_CURRENT_FRAME
;
114 else if (strcmp (frame
, "@") == 0)
115 var_type
= USE_SELECTED_FRAME
;
118 var_type
= USE_SPECIFIED_FRAME
;
119 frameaddr
= string_to_core_addr (frame
);
123 fprintf_unfiltered (gdb_stdlog
,
124 "Name=\"%s\", Frame=\"%s\" (0x%s), Expression=\"%s\"\n",
125 name
, frame
, paddr (frameaddr
), expr
);
127 var
= varobj_create (name
, expr
, frameaddr
, var_type
);
130 error (_("mi_cmd_var_create: unable to create variable object"));
132 print_varobj (var
, PRINT_ALL_VALUES
, 0 /* don't print expression */);
134 do_cleanups (old_cleanups
);
139 mi_cmd_var_delete (char *command
, char **argv
, int argc
)
145 int children_only_p
= 0;
146 struct cleanup
*old_cleanups
;
148 if (argc
< 1 || argc
> 2)
149 error (_("mi_cmd_var_delete: Usage: [-c] EXPRESSION."));
151 name
= xstrdup (argv
[0]);
152 /* Add cleanup for name. Must be free_current_contents as
153 name can be reallocated */
154 old_cleanups
= make_cleanup (free_current_contents
, &name
);
156 /* If we have one single argument it cannot be '-c' or any string
157 starting with '-'. */
160 if (strcmp (name
, "-c") == 0)
161 error (_("mi_cmd_var_delete: Missing required argument after '-c': variable object name"));
163 error (_("mi_cmd_var_delete: Illegal variable object name"));
166 /* If we have 2 arguments they must be '-c' followed by a string
167 which would be the variable name. */
170 expr
= xstrdup (argv
[1]);
171 if (strcmp (name
, "-c") != 0)
172 error (_("mi_cmd_var_delete: Invalid option."));
175 name
= xstrdup (expr
);
179 /* If we didn't error out, now NAME contains the name of the
182 var
= varobj_get_handle (name
);
185 error (_("mi_cmd_var_delete: Variable object not found."));
187 numdel
= varobj_delete (var
, NULL
, children_only_p
);
189 ui_out_field_int (uiout
, "ndeleted", numdel
);
191 do_cleanups (old_cleanups
);
196 mi_cmd_var_set_format (char *command
, char **argv
, int argc
)
198 enum varobj_display_formats format
;
204 error (_("mi_cmd_var_set_format: Usage: NAME FORMAT."));
206 /* Get varobj handle, if a valid var obj name was specified */
207 var
= varobj_get_handle (argv
[0]);
210 error (_("mi_cmd_var_set_format: Variable object not found"));
212 formspec
= xstrdup (argv
[1]);
213 if (formspec
== NULL
)
214 error (_("mi_cmd_var_set_format: Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
216 len
= strlen (formspec
);
218 if (strncmp (formspec
, "natural", len
) == 0)
219 format
= FORMAT_NATURAL
;
220 else if (strncmp (formspec
, "binary", len
) == 0)
221 format
= FORMAT_BINARY
;
222 else if (strncmp (formspec
, "decimal", len
) == 0)
223 format
= FORMAT_DECIMAL
;
224 else if (strncmp (formspec
, "hexadecimal", len
) == 0)
225 format
= FORMAT_HEXADECIMAL
;
226 else if (strncmp (formspec
, "octal", len
) == 0)
227 format
= FORMAT_OCTAL
;
229 error (_("mi_cmd_var_set_format: Unknown display format: must be: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
231 /* Set the format of VAR to given format */
232 varobj_set_display_format (var
, format
);
234 /* Report the new current format */
235 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
240 mi_cmd_var_set_frozen (char *command
, char **argv
, int argc
)
246 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
248 var
= varobj_get_handle (argv
[0]);
250 error (_("Variable object not found"));
252 if (strcmp (argv
[1], "0") == 0)
254 else if (strcmp (argv
[1], "1") == 0)
257 error (_("Invalid flag value"));
259 varobj_set_frozen (var
, frozen
);
261 /* We don't automatically return the new value, or what varobjs got new
262 values during unfreezing. If this information is required, client
263 should call -var-update explicitly. */
269 mi_cmd_var_show_format (char *command
, char **argv
, int argc
)
271 enum varobj_display_formats format
;
275 error (_("mi_cmd_var_show_format: Usage: NAME."));
277 /* Get varobj handle, if a valid var obj name was specified */
278 var
= varobj_get_handle (argv
[0]);
280 error (_("mi_cmd_var_show_format: Variable object not found"));
282 format
= varobj_get_display_format (var
);
284 /* Report the current format */
285 ui_out_field_string (uiout
, "format", varobj_format_string
[(int) format
]);
290 mi_cmd_var_info_num_children (char *command
, char **argv
, int argc
)
295 error (_("mi_cmd_var_info_num_children: Usage: NAME."));
297 /* Get varobj handle, if a valid var obj name was specified */
298 var
= varobj_get_handle (argv
[0]);
300 error (_("mi_cmd_var_info_num_children: Variable object not found"));
302 ui_out_field_int (uiout
, "numchild", varobj_get_num_children (var
));
306 /* Parse a string argument into a print_values value. */
308 static enum print_values
309 mi_parse_values_option (const char *arg
)
311 if (strcmp (arg
, "0") == 0
312 || strcmp (arg
, mi_no_values
) == 0)
313 return PRINT_NO_VALUES
;
314 else if (strcmp (arg
, "1") == 0
315 || strcmp (arg
, mi_all_values
) == 0)
316 return PRINT_ALL_VALUES
;
317 else if (strcmp (arg
, "2") == 0
318 || strcmp (arg
, mi_simple_values
) == 0)
319 return PRINT_SIMPLE_VALUES
;
321 error (_("Unknown value for PRINT_VALUES\n\
322 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
323 mi_no_values
, mi_simple_values
, mi_all_values
);
326 /* Return 1 if given the argument PRINT_VALUES we should display
327 a value of type TYPE. */
330 mi_print_value_p (struct type
*type
, enum print_values print_values
)
333 type
= check_typedef (type
);
335 if (print_values
== PRINT_NO_VALUES
)
338 if (print_values
== PRINT_ALL_VALUES
)
341 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
342 and that type is not a compound type. */
344 return (TYPE_CODE (type
) != TYPE_CODE_ARRAY
345 && TYPE_CODE (type
) != TYPE_CODE_STRUCT
346 && TYPE_CODE (type
) != TYPE_CODE_UNION
);
350 mi_cmd_var_list_children (char *command
, char **argv
, int argc
)
353 struct varobj
**childlist
;
355 struct cleanup
*cleanup_children
;
357 enum print_values print_values
;
359 if (argc
!= 1 && argc
!= 2)
360 error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME"));
362 /* Get varobj handle, if a valid var obj name was specified */
364 var
= varobj_get_handle (argv
[0]);
366 var
= varobj_get_handle (argv
[1]);
368 error (_("Variable object not found"));
370 numchild
= varobj_list_children (var
, &childlist
);
371 ui_out_field_int (uiout
, "numchild", numchild
);
373 print_values
= mi_parse_values_option (argv
[0]);
375 print_values
= PRINT_NO_VALUES
;
380 if (mi_version (uiout
) == 1)
381 cleanup_children
= make_cleanup_ui_out_tuple_begin_end (uiout
, "children");
383 cleanup_children
= make_cleanup_ui_out_list_begin_end (uiout
, "children");
387 struct cleanup
*cleanup_child
;
388 cleanup_child
= make_cleanup_ui_out_tuple_begin_end (uiout
, "child");
389 print_varobj (*cc
, print_values
, 1 /* print expression */);
391 do_cleanups (cleanup_child
);
393 do_cleanups (cleanup_children
);
399 mi_cmd_var_info_type (char *command
, char **argv
, int argc
)
404 error (_("mi_cmd_var_info_type: Usage: NAME."));
406 /* Get varobj handle, if a valid var obj name was specified */
407 var
= varobj_get_handle (argv
[0]);
409 error (_("mi_cmd_var_info_type: Variable object not found"));
411 ui_out_field_string (uiout
, "type", varobj_get_type (var
));
416 mi_cmd_var_info_expression (char *command
, char **argv
, int argc
)
418 enum varobj_languages lang
;
422 error (_("mi_cmd_var_info_expression: Usage: NAME."));
424 /* Get varobj handle, if a valid var obj name was specified */
425 var
= varobj_get_handle (argv
[0]);
427 error (_("mi_cmd_var_info_expression: Variable object not found"));
429 lang
= varobj_get_language (var
);
431 ui_out_field_string (uiout
, "lang", varobj_language_string
[(int) lang
]);
432 ui_out_field_string (uiout
, "exp", varobj_get_expression (var
));
437 mi_cmd_var_show_attributes (char *command
, char **argv
, int argc
)
444 error (_("mi_cmd_var_show_attributes: Usage: NAME."));
446 /* Get varobj handle, if a valid var obj name was specified */
447 var
= varobj_get_handle (argv
[0]);
449 error (_("mi_cmd_var_show_attributes: Variable object not found"));
451 attr
= varobj_get_attributes (var
);
452 /* FIXME: define masks for attributes */
453 if (attr
& 0x00000001)
456 attstr
= "noneditable";
458 ui_out_field_string (uiout
, "attr", attstr
);
463 mi_cmd_var_evaluate_expression (char *command
, char **argv
, int argc
)
468 error (_("mi_cmd_var_evaluate_expression: Usage: NAME."));
470 /* Get varobj handle, if a valid var obj name was specified */
471 var
= varobj_get_handle (argv
[0]);
473 error (_("mi_cmd_var_evaluate_expression: Variable object not found"));
475 ui_out_field_string (uiout
, "value", varobj_get_value (var
));
480 mi_cmd_var_assign (char *command
, char **argv
, int argc
)
486 error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION."));
488 /* Get varobj handle, if a valid var obj name was specified */
489 var
= varobj_get_handle (argv
[0]);
491 error (_("mi_cmd_var_assign: Variable object not found"));
493 /* FIXME: define masks for attributes */
494 if (!(varobj_get_attributes (var
) & 0x00000001))
495 error (_("mi_cmd_var_assign: Variable object is not editable"));
497 expression
= xstrdup (argv
[1]);
499 if (!varobj_set_value (var
, expression
))
500 error (_("mi_cmd_var_assign: Could not assign expression to variable object"));
502 ui_out_field_string (uiout
, "value", varobj_get_value (var
));
507 mi_cmd_var_update (char *command
, char **argv
, int argc
)
510 struct varobj
**rootlist
;
512 struct cleanup
*cleanup
;
515 enum print_values print_values
;
517 if (argc
!= 1 && argc
!= 2)
518 error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME."));
526 print_values
= mi_parse_values_option (argv
[0]);
528 print_values
= PRINT_NO_VALUES
;
530 /* Check if the parameter is a "*" which means that we want
531 to update all variables */
533 if ((*name
== '*') && (*(name
+ 1) == '\0'))
535 nv
= varobj_list (&rootlist
);
536 cleanup
= make_cleanup (xfree
, rootlist
);
537 if (mi_version (uiout
) <= 1)
538 make_cleanup_ui_out_tuple_begin_end (uiout
, "changelist");
540 make_cleanup_ui_out_list_begin_end (uiout
, "changelist");
543 do_cleanups (cleanup
);
549 varobj_update_one (*cr
, print_values
, 0 /* implicit */);
552 do_cleanups (cleanup
);
556 /* Get varobj handle, if a valid var obj name was specified */
557 var
= varobj_get_handle (name
);
559 error (_("mi_cmd_var_update: Variable object not found"));
561 if (mi_version (uiout
) <= 1)
562 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, "changelist");
564 cleanup
= make_cleanup_ui_out_list_begin_end (uiout
, "changelist");
565 varobj_update_one (var
, print_values
, 1 /* explicit */);
566 do_cleanups (cleanup
);
571 /* Helper for mi_cmd_var_update(). */
574 varobj_update_one (struct varobj
*var
, enum print_values print_values
,
577 struct varobj
**changelist
;
579 struct cleanup
*cleanup
= NULL
;
582 nc
= varobj_update (&var
, &changelist
, explicit);
584 /* nc >= 0 represents the number of changes reported into changelist.
585 nc < 0 means that an error occured or the the variable has
586 changed type (TYPE_CHANGED). */
592 if (mi_version (uiout
) > 1)
593 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
594 ui_out_field_string (uiout
, "name", varobj_get_objname(var
));
599 ui_out_field_string (uiout
, "in_scope", "false");
602 ui_out_field_string (uiout
, "in_scope", "invalid");
605 ui_out_field_string (uiout
, "in_scope", "true");
606 ui_out_field_string (uiout
, "new_type", varobj_get_type(var
));
607 ui_out_field_int (uiout
, "new_num_children",
608 varobj_get_num_children(var
));
611 if (mi_version (uiout
) > 1)
612 do_cleanups (cleanup
);
619 if (mi_version (uiout
) > 1)
620 cleanup
= make_cleanup_ui_out_tuple_begin_end (uiout
, NULL
);
621 ui_out_field_string (uiout
, "name", varobj_get_objname (*cc
));
622 if (mi_print_value_p (varobj_get_gdb_type (*cc
), print_values
))
623 ui_out_field_string (uiout
, "value", varobj_get_value (*cc
));
624 ui_out_field_string (uiout
, "in_scope", "true");
625 ui_out_field_string (uiout
, "type_changed", "false");
626 if (mi_version (uiout
) > 1)
627 do_cleanups (cleanup
);