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