This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / gdb / mi / mi-cmd-var.c
1 /* MI Command Set - varobj commands.
2
3    Copyright (C) 2000, 2002, 2004, 2005 Free Software Foundation, Inc.
4
5    Contributed by Cygnus Solutions (a Red Hat company).
6
7    This file is part of GDB.
8
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.
13
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.
18
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.  */
23
24 #include "defs.h"
25 #include "mi-cmds.h"
26 #include "ui-out.h"
27 #include "mi-out.h"
28 #include "varobj.h"
29 #include "value.h"
30 #include <ctype.h>
31 #include "gdb_string.h"
32
33 const char mi_no_values[] = "--no-values";
34 const char mi_simple_values[] = "--simple-values";
35 const char mi_all_values[] = "--all-values";
36
37 extern int varobjdebug;         /* defined in varobj.c */
38
39 static int varobj_update_one (struct varobj *var,
40                               enum print_values print_values);
41
42 /* VAROBJ operations */
43
44 enum mi_cmd_result
45 mi_cmd_var_create (char *command, char **argv, int argc)
46 {
47   CORE_ADDR frameaddr = 0;
48   struct varobj *var;
49   char *name;
50   char *frame;
51   char *expr;
52   char *type;
53   struct cleanup *old_cleanups;
54   enum varobj_type var_type;
55
56   if (argc != 3)
57     {
58       /* mi_error_message = xstrprintf ("mi_cmd_var_create: Usage:
59          ...."); return MI_CMD_ERROR; */
60       error (_("mi_cmd_var_create: Usage: NAME FRAME EXPRESSION."));
61     }
62
63   name = xstrdup (argv[0]);
64   /* Add cleanup for name. Must be free_current_contents as
65      name can be reallocated */
66   old_cleanups = make_cleanup (free_current_contents, &name);
67
68   frame = xstrdup (argv[1]);
69   old_cleanups = make_cleanup (xfree, frame);
70
71   expr = xstrdup (argv[2]);
72
73   if (strcmp (name, "-") == 0)
74     {
75       xfree (name);
76       name = varobj_gen_name ();
77     }
78   else if (!isalpha (*name))
79     error (_("mi_cmd_var_create: name of object must begin with a letter"));
80
81   if (strcmp (frame, "*") == 0)
82     var_type = USE_CURRENT_FRAME;
83   else if (strcmp (frame, "@") == 0)
84     var_type = USE_SELECTED_FRAME;  
85   else
86     {
87       var_type = USE_SPECIFIED_FRAME;
88       frameaddr = string_to_core_addr (frame);
89     }
90
91   if (varobjdebug)
92     fprintf_unfiltered (gdb_stdlog,
93                     "Name=\"%s\", Frame=\"%s\" (0x%s), Expression=\"%s\"\n",
94                         name, frame, paddr (frameaddr), expr);
95
96   var = varobj_create (name, expr, frameaddr, var_type);
97
98   if (var == NULL)
99     error (_("mi_cmd_var_create: unable to create variable object"));
100
101   ui_out_field_string (uiout, "name", name);
102   ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
103   type = varobj_get_type (var);
104   if (type == NULL)
105     ui_out_field_string (uiout, "type", "");
106   else
107     {
108       ui_out_field_string (uiout, "type", type);
109       xfree (type);
110     }
111
112   do_cleanups (old_cleanups);
113   return MI_CMD_DONE;
114 }
115
116 enum mi_cmd_result
117 mi_cmd_var_delete (char *command, char **argv, int argc)
118 {
119   char *name;
120   char *expr;
121   struct varobj *var;
122   int numdel;
123   int children_only_p = 0;
124   struct cleanup *old_cleanups;
125
126   if (argc < 1 || argc > 2)
127     error (_("mi_cmd_var_delete: Usage: [-c] EXPRESSION."));
128
129   name = xstrdup (argv[0]);
130   /* Add cleanup for name. Must be free_current_contents as
131      name can be reallocated */
132   old_cleanups = make_cleanup (free_current_contents, &name);
133
134   /* If we have one single argument it cannot be '-c' or any string
135      starting with '-'. */
136   if (argc == 1)
137     {
138       if (strcmp (name, "-c") == 0)
139         error (_("mi_cmd_var_delete: Missing required argument after '-c': variable object name"));
140       if (*name == '-')
141         error (_("mi_cmd_var_delete: Illegal variable object name"));
142     }
143
144   /* If we have 2 arguments they must be '-c' followed by a string
145      which would be the variable name. */
146   if (argc == 2)
147     {
148       expr = xstrdup (argv[1]);
149       if (strcmp (name, "-c") != 0)
150         error (_("mi_cmd_var_delete: Invalid option."));
151       children_only_p = 1;
152       xfree (name);
153       name = xstrdup (expr);
154       xfree (expr);
155     }
156
157   /* If we didn't error out, now NAME contains the name of the
158      variable. */
159
160   var = varobj_get_handle (name);
161
162   if (var == NULL)
163     error (_("mi_cmd_var_delete: Variable object not found."));
164
165   numdel = varobj_delete (var, NULL, children_only_p);
166
167   ui_out_field_int (uiout, "ndeleted", numdel);
168
169   do_cleanups (old_cleanups);
170   return MI_CMD_DONE;
171 }
172
173 enum mi_cmd_result
174 mi_cmd_var_set_format (char *command, char **argv, int argc)
175 {
176   enum varobj_display_formats format;
177   int len;
178   struct varobj *var;
179   char *formspec;
180
181   if (argc != 2)
182     error (_("mi_cmd_var_set_format: Usage: NAME FORMAT."));
183
184   /* Get varobj handle, if a valid var obj name was specified */
185   var = varobj_get_handle (argv[0]);
186
187   if (var == NULL)
188     error (_("mi_cmd_var_set_format: Variable object not found"));
189
190   formspec = xstrdup (argv[1]);
191   if (formspec == NULL)
192     error (_("mi_cmd_var_set_format: Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
193
194   len = strlen (formspec);
195
196   if (strncmp (formspec, "natural", len) == 0)
197     format = FORMAT_NATURAL;
198   else if (strncmp (formspec, "binary", len) == 0)
199     format = FORMAT_BINARY;
200   else if (strncmp (formspec, "decimal", len) == 0)
201     format = FORMAT_DECIMAL;
202   else if (strncmp (formspec, "hexadecimal", len) == 0)
203     format = FORMAT_HEXADECIMAL;
204   else if (strncmp (formspec, "octal", len) == 0)
205     format = FORMAT_OCTAL;
206   else
207     error (_("mi_cmd_var_set_format: Unknown display format: must be: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
208
209   /* Set the format of VAR to given format */
210   varobj_set_display_format (var, format);
211
212   /* Report the new current format */
213   ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
214   return MI_CMD_DONE;
215 }
216
217 enum mi_cmd_result
218 mi_cmd_var_show_format (char *command, char **argv, int argc)
219 {
220   enum varobj_display_formats format;
221   struct varobj *var;
222
223   if (argc != 1)
224     error (_("mi_cmd_var_show_format: Usage: NAME."));
225
226   /* Get varobj handle, if a valid var obj name was specified */
227   var = varobj_get_handle (argv[0]);
228   if (var == NULL)
229     error (_("mi_cmd_var_show_format: Variable object not found"));
230
231   format = varobj_get_display_format (var);
232
233   /* Report the current format */
234   ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
235   return MI_CMD_DONE;
236 }
237
238 enum mi_cmd_result
239 mi_cmd_var_info_num_children (char *command, char **argv, int argc)
240 {
241   struct varobj *var;
242
243   if (argc != 1)
244     error (_("mi_cmd_var_info_num_children: Usage: NAME."));
245
246   /* Get varobj handle, if a valid var obj name was specified */
247   var = varobj_get_handle (argv[0]);
248   if (var == NULL)
249     error (_("mi_cmd_var_info_num_children: Variable object not found"));
250
251   ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
252   return MI_CMD_DONE;
253 }
254
255 /* Parse a string argument into a print_values value.  */
256
257 static enum print_values
258 mi_parse_values_option (const char *arg)
259 {
260   if (strcmp (arg, "0") == 0
261       || strcmp (arg, mi_no_values) == 0)
262     return PRINT_NO_VALUES;
263   else if (strcmp (arg, "1") == 0
264            || strcmp (arg, mi_all_values) == 0)
265     return PRINT_ALL_VALUES;
266   else if (strcmp (arg, "2") == 0
267            || strcmp (arg, mi_simple_values) == 0)
268     return PRINT_SIMPLE_VALUES;
269   else
270     error (_("Unknown value for PRINT_VALUES\n\
271 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
272            mi_no_values, mi_simple_values, mi_all_values);
273 }
274
275 /* Return 1 if given the argument PRINT_VALUES we should display
276    a value of type TYPE.  */
277
278 static int
279 mi_print_value_p (struct type *type, enum print_values print_values)
280 {
281   if (type != NULL)
282     type = check_typedef (type);
283
284   if (print_values == PRINT_NO_VALUES)
285     return 0;
286
287   if (print_values == PRINT_ALL_VALUES)
288     return 1;
289
290   /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
291      and that type is not a compound type.  */
292
293   return (TYPE_CODE (type) != TYPE_CODE_ARRAY
294           && TYPE_CODE (type) != TYPE_CODE_STRUCT
295           && TYPE_CODE (type) != TYPE_CODE_UNION);
296 }
297
298 enum mi_cmd_result
299 mi_cmd_var_list_children (char *command, char **argv, int argc)
300 {
301   struct varobj *var;
302   struct varobj **childlist;
303   struct varobj **cc;
304   struct cleanup *cleanup_children;
305   int numchild;
306   char *type;
307   enum print_values print_values;
308
309   if (argc != 1 && argc != 2)
310     error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME"));
311
312   /* Get varobj handle, if a valid var obj name was specified */
313   if (argc == 1)
314     var = varobj_get_handle (argv[0]);
315   else
316     var = varobj_get_handle (argv[1]);
317   if (var == NULL)
318     error (_("Variable object not found"));
319
320   numchild = varobj_list_children (var, &childlist);
321   ui_out_field_int (uiout, "numchild", numchild);
322   if (argc == 2)
323     print_values = mi_parse_values_option (argv[0]);
324   else
325     print_values = PRINT_NO_VALUES;
326
327   if (numchild <= 0)
328     return MI_CMD_DONE;
329
330   if (mi_version (uiout) == 1)
331     cleanup_children = make_cleanup_ui_out_tuple_begin_end (uiout, "children");
332   else
333     cleanup_children = make_cleanup_ui_out_list_begin_end (uiout, "children");
334   cc = childlist;
335   while (*cc != NULL)
336     {
337       struct cleanup *cleanup_child;
338       cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, "child");
339       ui_out_field_string (uiout, "name", varobj_get_objname (*cc));
340       ui_out_field_string (uiout, "exp", varobj_get_expression (*cc));
341       ui_out_field_int (uiout, "numchild", varobj_get_num_children (*cc));
342       if (mi_print_value_p (varobj_get_gdb_type (*cc), print_values))
343         ui_out_field_string (uiout, "value", varobj_get_value (*cc));
344       type = varobj_get_type (*cc);
345       /* C++ pseudo-variables (public, private, protected) do not have a type */
346       if (type)
347         ui_out_field_string (uiout, "type", type);
348       do_cleanups (cleanup_child);
349       cc++;
350     }
351   do_cleanups (cleanup_children);
352   xfree (childlist);
353   return MI_CMD_DONE;
354 }
355
356 enum mi_cmd_result
357 mi_cmd_var_info_type (char *command, char **argv, int argc)
358 {
359   struct varobj *var;
360
361   if (argc != 1)
362     error (_("mi_cmd_var_info_type: Usage: NAME."));
363
364   /* Get varobj handle, if a valid var obj name was specified */
365   var = varobj_get_handle (argv[0]);
366   if (var == NULL)
367     error (_("mi_cmd_var_info_type: Variable object not found"));
368
369   ui_out_field_string (uiout, "type", varobj_get_type (var));
370   return MI_CMD_DONE;
371 }
372
373 enum mi_cmd_result
374 mi_cmd_var_info_expression (char *command, char **argv, int argc)
375 {
376   enum varobj_languages lang;
377   struct varobj *var;
378
379   if (argc != 1)
380     error (_("mi_cmd_var_info_expression: Usage: NAME."));
381
382   /* Get varobj handle, if a valid var obj name was specified */
383   var = varobj_get_handle (argv[0]);
384   if (var == NULL)
385     error (_("mi_cmd_var_info_expression: Variable object not found"));
386
387   lang = varobj_get_language (var);
388
389   ui_out_field_string (uiout, "lang", varobj_language_string[(int) lang]);
390   ui_out_field_string (uiout, "exp", varobj_get_expression (var));
391   return MI_CMD_DONE;
392 }
393
394 enum mi_cmd_result
395 mi_cmd_var_show_attributes (char *command, char **argv, int argc)
396 {
397   int attr;
398   char *attstr;
399   struct varobj *var;
400
401   if (argc != 1)
402     error (_("mi_cmd_var_show_attributes: Usage: NAME."));
403
404   /* Get varobj handle, if a valid var obj name was specified */
405   var = varobj_get_handle (argv[0]);
406   if (var == NULL)
407     error (_("mi_cmd_var_show_attributes: Variable object not found"));
408
409   attr = varobj_get_attributes (var);
410   /* FIXME: define masks for attributes */
411   if (attr & 0x00000001)
412     attstr = "editable";
413   else
414     attstr = "noneditable";
415
416   ui_out_field_string (uiout, "attr", attstr);
417   return MI_CMD_DONE;
418 }
419
420 enum mi_cmd_result
421 mi_cmd_var_evaluate_expression (char *command, char **argv, int argc)
422 {
423   struct varobj *var;
424
425   if (argc != 1)
426     error (_("mi_cmd_var_evaluate_expression: Usage: NAME."));
427
428   /* Get varobj handle, if a valid var obj name was specified */
429   var = varobj_get_handle (argv[0]);
430   if (var == NULL)
431     error (_("mi_cmd_var_evaluate_expression: Variable object not found"));
432
433   ui_out_field_string (uiout, "value", varobj_get_value (var));
434   return MI_CMD_DONE;
435 }
436
437 enum mi_cmd_result
438 mi_cmd_var_assign (char *command, char **argv, int argc)
439 {
440   struct varobj *var;
441   char *expression;
442
443   if (argc != 2)
444     error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION."));
445
446   /* Get varobj handle, if a valid var obj name was specified */
447   var = varobj_get_handle (argv[0]);
448   if (var == NULL)
449     error (_("mi_cmd_var_assign: Variable object not found"));
450
451   /* FIXME: define masks for attributes */
452   if (!(varobj_get_attributes (var) & 0x00000001))
453     error (_("mi_cmd_var_assign: Variable object is not editable"));
454
455   expression = xstrdup (argv[1]);
456
457   if (!varobj_set_value (var, expression))
458     error (_("mi_cmd_var_assign: Could not assign expression to varible object"));
459
460   ui_out_field_string (uiout, "value", varobj_get_value (var));
461   return MI_CMD_DONE;
462 }
463
464 enum mi_cmd_result
465 mi_cmd_var_update (char *command, char **argv, int argc)
466 {
467   struct varobj *var;
468   struct varobj **rootlist;
469   struct varobj **cr;
470   struct cleanup *cleanup;
471   char *name;
472   int nv;
473   enum print_values print_values;
474
475   if (argc != 1 && argc != 2)
476     error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME."));
477
478   if (argc == 1)
479     name = argv[0];
480   else
481     name = (argv[1]);
482
483   if (argc == 2)
484     print_values = mi_parse_values_option (argv[0]);
485   else
486     print_values = PRINT_NO_VALUES;
487
488   /* Check if the parameter is a "*" which means that we want
489      to update all variables */
490
491   if ((*name == '*') && (*(name + 1) == '\0'))
492     {
493       nv = varobj_list (&rootlist);
494       if (mi_version (uiout) <= 1)
495         cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
496       else
497         cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
498       if (nv <= 0)
499         {
500           do_cleanups (cleanup);
501           return MI_CMD_DONE;
502         }
503       cr = rootlist;
504       while (*cr != NULL)
505         {
506           varobj_update_one (*cr, print_values);
507           cr++;
508         }
509       xfree (rootlist);
510       do_cleanups (cleanup);
511     }
512   else
513     {
514       /* Get varobj handle, if a valid var obj name was specified */
515       var = varobj_get_handle (name);
516       if (var == NULL)
517         error (_("mi_cmd_var_update: Variable object not found"));
518
519       if (mi_version (uiout) <= 1)
520         cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
521       else
522         cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
523       varobj_update_one (var, print_values);
524       do_cleanups (cleanup);
525     }
526     return MI_CMD_DONE;
527 }
528
529 /* Helper for mi_cmd_var_update() Returns 0 if the update for
530    the variable fails (usually because the variable is out of
531    scope), and 1 if it succeeds. */
532
533 static int
534 varobj_update_one (struct varobj *var, enum print_values print_values)
535 {
536   struct varobj **changelist;
537   struct varobj **cc;
538   struct cleanup *cleanup = NULL;
539   int nc;
540
541   nc = varobj_update (&var, &changelist);
542
543   /* nc == 0 means that nothing has changed.
544      nc == -1 means that an error occured in updating the variable.
545      nc == -2 means the variable has changed type. */
546   
547   if (nc == 0)
548     return 1;
549   else if (nc == -1)
550     {
551       if (mi_version (uiout) > 1)
552         cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
553       ui_out_field_string (uiout, "name", varobj_get_objname(var));
554       ui_out_field_string (uiout, "in_scope", "false");
555       if (mi_version (uiout) > 1)
556         do_cleanups (cleanup);
557       return -1;
558     }
559   else if (nc == -2)
560     {
561       if (mi_version (uiout) > 1)
562         cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
563       ui_out_field_string (uiout, "name", varobj_get_objname (var));
564       ui_out_field_string (uiout, "in_scope", "true");
565       ui_out_field_string (uiout, "new_type", varobj_get_type(var));
566       ui_out_field_int (uiout, "new_num_children", 
567                            varobj_get_num_children(var));
568       if (mi_version (uiout) > 1)
569         do_cleanups (cleanup);
570     }
571   else
572     {
573       
574       cc = changelist;
575       while (*cc != NULL)
576         {
577           if (mi_version (uiout) > 1)
578             cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
579           ui_out_field_string (uiout, "name", varobj_get_objname (*cc));
580           if (mi_print_value_p (varobj_get_gdb_type (*cc), print_values))
581             ui_out_field_string (uiout, "value", varobj_get_value (*cc));
582           ui_out_field_string (uiout, "in_scope", "true");
583           ui_out_field_string (uiout, "type_changed", "false");
584           if (mi_version (uiout) > 1)
585             do_cleanups (cleanup);
586           cc++;
587         }
588       xfree (changelist);
589       return 1;
590     }
591   return 1;
592 }