* varobj.h (varobj_floating_p): Declare.
[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   int thread_id;
54
55   ui_out_field_string (uiout, "name", varobj_get_objname (var));
56   if (print_expression)
57     ui_out_field_string (uiout, "exp", varobj_get_expression (var));
58   ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
59   
60   if (mi_print_value_p (varobj_get_gdb_type (var), print_values))
61     ui_out_field_string (uiout, "value", varobj_get_value (var));
62
63   type = varobj_get_type (var);
64   if (type != NULL)
65     {
66       ui_out_field_string (uiout, "type", type);
67       xfree (type);
68     }
69
70   thread_id = varobj_get_thread_id (var);
71   if (thread_id > 0)
72     ui_out_field_int (uiout, "thread-id", thread_id);
73
74   if (varobj_get_frozen (var))
75     ui_out_field_int (uiout, "frozen", 1);
76 }
77
78 /* VAROBJ operations */
79
80 enum mi_cmd_result
81 mi_cmd_var_create (char *command, char **argv, int argc)
82 {
83   CORE_ADDR frameaddr = 0;
84   struct varobj *var;
85   char *name;
86   char *frame;
87   char *expr;
88   struct cleanup *old_cleanups;
89   enum varobj_type var_type;
90
91   if (argc != 3)
92     {
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."));
96     }
97
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);
102
103   frame = xstrdup (argv[1]);
104   make_cleanup (xfree, frame);
105
106   expr = xstrdup (argv[2]);
107   make_cleanup (xfree, expr);
108
109   if (strcmp (name, "-") == 0)
110     {
111       xfree (name);
112       name = varobj_gen_name ();
113     }
114   else if (!isalpha (*name))
115     error (_("mi_cmd_var_create: name of object must begin with a letter"));
116
117   if (strcmp (frame, "*") == 0)
118     var_type = USE_CURRENT_FRAME;
119   else if (strcmp (frame, "@") == 0)
120     var_type = USE_SELECTED_FRAME;  
121   else
122     {
123       var_type = USE_SPECIFIED_FRAME;
124       frameaddr = string_to_core_addr (frame);
125     }
126
127   if (varobjdebug)
128     fprintf_unfiltered (gdb_stdlog,
129                     "Name=\"%s\", Frame=\"%s\" (0x%s), Expression=\"%s\"\n",
130                         name, frame, paddr (frameaddr), expr);
131
132   var = varobj_create (name, expr, frameaddr, var_type);
133
134   if (var == NULL)
135     error (_("mi_cmd_var_create: unable to create variable object"));
136
137   print_varobj (var, PRINT_ALL_VALUES, 0 /* don't print expression */);
138
139   do_cleanups (old_cleanups);
140   return MI_CMD_DONE;
141 }
142
143 enum mi_cmd_result
144 mi_cmd_var_delete (char *command, char **argv, int argc)
145 {
146   char *name;
147   struct varobj *var;
148   int numdel;
149   int children_only_p = 0;
150   struct cleanup *old_cleanups;
151
152   if (argc < 1 || argc > 2)
153     error (_("mi_cmd_var_delete: Usage: [-c] EXPRESSION."));
154
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);
159
160   /* If we have one single argument it cannot be '-c' or any string
161      starting with '-'. */
162   if (argc == 1)
163     {
164       if (strcmp (name, "-c") == 0)
165         error (_("mi_cmd_var_delete: Missing required argument after '-c': variable object name"));
166       if (*name == '-')
167         error (_("mi_cmd_var_delete: Illegal variable object name"));
168     }
169
170   /* If we have 2 arguments they must be '-c' followed by a string
171      which would be the variable name. */
172   if (argc == 2)
173     {
174       if (strcmp (name, "-c") != 0)
175         error (_("mi_cmd_var_delete: Invalid option."));
176       children_only_p = 1;
177       do_cleanups (old_cleanups);
178       name = xstrdup (argv[1]);
179       make_cleanup (free_current_contents, &name);
180     }
181
182   /* If we didn't error out, now NAME contains the name of the
183      variable. */
184
185   var = varobj_get_handle (name);
186
187   if (var == NULL)
188     error (_("mi_cmd_var_delete: Variable object not found."));
189
190   numdel = varobj_delete (var, NULL, children_only_p);
191
192   ui_out_field_int (uiout, "ndeleted", numdel);
193
194   do_cleanups (old_cleanups);
195   return MI_CMD_DONE;
196 }
197
198 enum mi_cmd_result
199 mi_cmd_var_set_format (char *command, char **argv, int argc)
200 {
201   enum varobj_display_formats format;
202   int len;
203   struct varobj *var;
204   char *formspec;
205
206   if (argc != 2)
207     error (_("mi_cmd_var_set_format: Usage: NAME FORMAT."));
208
209   /* Get varobj handle, if a valid var obj name was specified */
210   var = varobj_get_handle (argv[0]);
211
212   if (var == NULL)
213     error (_("mi_cmd_var_set_format: Variable object not found"));
214
215   formspec = argv[1];
216   if (formspec == NULL)
217     error (_("mi_cmd_var_set_format: Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
218
219   len = strlen (formspec);
220
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;
231   else
232     error (_("mi_cmd_var_set_format: Unknown display format: must be: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
233
234   /* Set the format of VAR to given format */
235   varobj_set_display_format (var, format);
236
237   /* Report the new current format */
238   ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
239  
240   /* Report the value in the new format */
241   ui_out_field_string (uiout, "value", varobj_get_value (var));
242   return MI_CMD_DONE;
243 }
244
245 enum mi_cmd_result
246 mi_cmd_var_set_frozen (char *command, char **argv, int argc)
247 {
248   struct varobj *var;
249   int frozen;
250
251   if (argc != 2)
252     error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
253
254   var = varobj_get_handle (argv[0]);
255   if (var == NULL)
256     error (_("Variable object not found"));
257
258   if (strcmp (argv[1], "0") == 0)
259     frozen = 0;
260   else if (strcmp (argv[1], "1") == 0)
261     frozen = 1;
262   else
263     error (_("Invalid flag value"));
264
265   varobj_set_frozen (var, frozen);
266
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.  */
270   return MI_CMD_DONE;
271 }
272
273
274 enum mi_cmd_result
275 mi_cmd_var_show_format (char *command, char **argv, int argc)
276 {
277   enum varobj_display_formats format;
278   struct varobj *var;
279
280   if (argc != 1)
281     error (_("mi_cmd_var_show_format: Usage: NAME."));
282
283   /* Get varobj handle, if a valid var obj name was specified */
284   var = varobj_get_handle (argv[0]);
285   if (var == NULL)
286     error (_("mi_cmd_var_show_format: Variable object not found"));
287
288   format = varobj_get_display_format (var);
289
290   /* Report the current format */
291   ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
292   return MI_CMD_DONE;
293 }
294
295 enum mi_cmd_result
296 mi_cmd_var_info_num_children (char *command, char **argv, int argc)
297 {
298   struct varobj *var;
299
300   if (argc != 1)
301     error (_("mi_cmd_var_info_num_children: Usage: NAME."));
302
303   /* Get varobj handle, if a valid var obj name was specified */
304   var = varobj_get_handle (argv[0]);
305   if (var == NULL)
306     error (_("mi_cmd_var_info_num_children: Variable object not found"));
307
308   ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
309   return MI_CMD_DONE;
310 }
311
312 /* Parse a string argument into a print_values value.  */
313
314 static enum print_values
315 mi_parse_values_option (const char *arg)
316 {
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;
326   else
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);
330 }
331
332 /* Return 1 if given the argument PRINT_VALUES we should display
333    a value of type TYPE.  */
334
335 static int
336 mi_print_value_p (struct type *type, enum print_values print_values)
337 {
338
339   if (print_values == PRINT_NO_VALUES)
340     return 0;
341
342   if (print_values == PRINT_ALL_VALUES)
343     return 1;
344
345   if (type == NULL)
346     return 1;
347   else
348     {
349       type = check_typedef (type);
350
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);
356     }
357 }
358
359 enum mi_cmd_result
360 mi_cmd_var_list_children (char *command, char **argv, int argc)
361 {
362   struct varobj *var;  
363   VEC(varobj_p) *children;
364   struct varobj *child;
365   struct cleanup *cleanup_children;
366   int numchild;
367   enum print_values print_values;
368   int ix;
369
370   if (argc != 1 && argc != 2)
371     error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME"));
372
373   /* Get varobj handle, if a valid var obj name was specified */
374   if (argc == 1)
375     var = varobj_get_handle (argv[0]);
376   else
377     var = varobj_get_handle (argv[1]);
378   if (var == NULL)
379     error (_("Variable object not found"));
380
381   children = varobj_list_children (var);
382   ui_out_field_int (uiout, "numchild", VEC_length (varobj_p, children));
383   if (argc == 2)
384     print_values = mi_parse_values_option (argv[0]);
385   else
386     print_values = PRINT_NO_VALUES;
387
388   if (VEC_length (varobj_p, children) == 0)
389     return MI_CMD_DONE;
390
391   if (mi_version (uiout) == 1)
392     cleanup_children = make_cleanup_ui_out_tuple_begin_end (uiout, "children");
393   else
394     cleanup_children = make_cleanup_ui_out_list_begin_end (uiout, "children");
395   for (ix = 0; VEC_iterate (varobj_p, children, ix, child); ++ix)
396     {
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);
401     }
402   do_cleanups (cleanup_children);
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 == '@') && (*(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           if (*name == '*' || varobj_floating_p (*cr))
578             varobj_update_one (*cr, print_values, 0 /* implicit */);
579           cr++;
580         }
581       do_cleanups (cleanup);
582     }
583   else
584     {
585       /* Get varobj handle, if a valid var obj name was specified */
586       var = varobj_get_handle (name);
587       if (var == NULL)
588         error (_("mi_cmd_var_update: Variable object not found"));
589
590       if (mi_version (uiout) <= 1)
591         cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
592       else
593         cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
594       varobj_update_one (var, print_values, 1 /* explicit */);
595       do_cleanups (cleanup);
596     }
597     return MI_CMD_DONE;
598 }
599
600 /* Helper for mi_cmd_var_update().  */
601
602 static void
603 varobj_update_one (struct varobj *var, enum print_values print_values,
604                    int explicit)
605 {
606   struct varobj **changelist;
607   struct varobj **cc;
608   struct cleanup *cleanup = NULL;
609   int nc;
610
611   nc = varobj_update (&var, &changelist, explicit);
612
613   /* nc >= 0  represents the number of changes reported into changelist.
614      nc < 0   means that an error occured or the the variable has 
615               changed type (TYPE_CHANGED).  */
616   
617   if (nc == 0)
618     return;
619   else if (nc < 0)
620     {
621       if (mi_version (uiout) > 1)
622         cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
623       ui_out_field_string (uiout, "name", varobj_get_objname(var));
624
625       switch (nc)
626       {
627         case NOT_IN_SCOPE:
628           ui_out_field_string (uiout, "in_scope", "false");
629           break;
630         case INVALID:
631           ui_out_field_string (uiout, "in_scope", "invalid");
632           break;
633         case TYPE_CHANGED:
634           ui_out_field_string (uiout, "in_scope", "true");
635           ui_out_field_string (uiout, "new_type", varobj_get_type(var));
636           ui_out_field_int (uiout, "new_num_children", 
637                             varobj_get_num_children(var));
638           break;
639       }
640       if (mi_version (uiout) > 1)
641         do_cleanups (cleanup);
642     }
643   else
644     {
645       cc = changelist;
646       while (*cc != NULL)
647         {
648           if (mi_version (uiout) > 1)
649             cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
650           ui_out_field_string (uiout, "name", varobj_get_objname (*cc));
651           if (mi_print_value_p (varobj_get_gdb_type (*cc), print_values))
652             ui_out_field_string (uiout, "value", varobj_get_value (*cc));
653           ui_out_field_string (uiout, "in_scope", "true");
654           ui_out_field_string (uiout, "type_changed", "false");
655           if (mi_version (uiout) > 1)
656             do_cleanups (cleanup);
657           cc++;
658         }
659       xfree (changelist);
660     }
661 }