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