2007-08-03 Michael Snyder <msnyder@access-company.com>
[external/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 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 void varobj_update_one (struct varobj *var,
40                               enum print_values print_values,
41                               int explicit);
42
43 static int mi_print_value_p (struct type *type, enum print_values print_values);
44
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.  */
48 static void 
49 print_varobj (struct varobj *var, enum print_values print_values,
50               int print_expression)
51 {
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 = xstrdup (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   return MI_CMD_DONE;
235 }
236
237 enum mi_cmd_result
238 mi_cmd_var_set_frozen (char *command, char **argv, int argc)
239 {
240   struct varobj *var;
241   int frozen;
242
243   if (argc != 2)
244     error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
245
246   var = varobj_get_handle (argv[0]);
247   if (var == NULL)
248     error (_("Variable object not found"));
249
250   if (strcmp (argv[1], "0") == 0)
251     frozen = 0;
252   else if (strcmp (argv[1], "1") == 0)
253     frozen = 1;
254   else
255     error (_("Invalid flag value"));
256
257   varobj_set_frozen (var, frozen);
258
259   /* We don't automatically return the new value, or what varobjs got new
260      values during unfreezing.  If this information is required, client
261      should call -var-update explicitly.  */
262   return MI_CMD_DONE;
263 }
264
265
266 enum mi_cmd_result
267 mi_cmd_var_show_format (char *command, char **argv, int argc)
268 {
269   enum varobj_display_formats format;
270   struct varobj *var;
271
272   if (argc != 1)
273     error (_("mi_cmd_var_show_format: Usage: NAME."));
274
275   /* Get varobj handle, if a valid var obj name was specified */
276   var = varobj_get_handle (argv[0]);
277   if (var == NULL)
278     error (_("mi_cmd_var_show_format: Variable object not found"));
279
280   format = varobj_get_display_format (var);
281
282   /* Report the current format */
283   ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
284   return MI_CMD_DONE;
285 }
286
287 enum mi_cmd_result
288 mi_cmd_var_info_num_children (char *command, char **argv, int argc)
289 {
290   struct varobj *var;
291
292   if (argc != 1)
293     error (_("mi_cmd_var_info_num_children: Usage: NAME."));
294
295   /* Get varobj handle, if a valid var obj name was specified */
296   var = varobj_get_handle (argv[0]);
297   if (var == NULL)
298     error (_("mi_cmd_var_info_num_children: Variable object not found"));
299
300   ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
301   return MI_CMD_DONE;
302 }
303
304 /* Parse a string argument into a print_values value.  */
305
306 static enum print_values
307 mi_parse_values_option (const char *arg)
308 {
309   if (strcmp (arg, "0") == 0
310       || strcmp (arg, mi_no_values) == 0)
311     return PRINT_NO_VALUES;
312   else if (strcmp (arg, "1") == 0
313            || strcmp (arg, mi_all_values) == 0)
314     return PRINT_ALL_VALUES;
315   else if (strcmp (arg, "2") == 0
316            || strcmp (arg, mi_simple_values) == 0)
317     return PRINT_SIMPLE_VALUES;
318   else
319     error (_("Unknown value for PRINT_VALUES\n\
320 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
321            mi_no_values, mi_simple_values, mi_all_values);
322 }
323
324 /* Return 1 if given the argument PRINT_VALUES we should display
325    a value of type TYPE.  */
326
327 static int
328 mi_print_value_p (struct type *type, enum print_values print_values)
329 {
330   if (type != NULL)
331     type = check_typedef (type);
332
333   if (print_values == PRINT_NO_VALUES)
334     return 0;
335
336   if (print_values == PRINT_ALL_VALUES)
337     return 1;
338
339   /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
340      and that type is not a compound type.  */
341
342   return (TYPE_CODE (type) != TYPE_CODE_ARRAY
343           && TYPE_CODE (type) != TYPE_CODE_STRUCT
344           && TYPE_CODE (type) != TYPE_CODE_UNION);
345 }
346
347 enum mi_cmd_result
348 mi_cmd_var_list_children (char *command, char **argv, int argc)
349 {
350   struct varobj *var;
351   struct varobj **childlist;
352   struct varobj **cc;
353   struct cleanup *cleanup_children;
354   int numchild;
355   enum print_values print_values;
356
357   if (argc != 1 && argc != 2)
358     error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME"));
359
360   /* Get varobj handle, if a valid var obj name was specified */
361   if (argc == 1)
362     var = varobj_get_handle (argv[0]);
363   else
364     var = varobj_get_handle (argv[1]);
365   if (var == NULL)
366     error (_("Variable object not found"));
367
368   numchild = varobj_list_children (var, &childlist);
369   ui_out_field_int (uiout, "numchild", numchild);
370   if (argc == 2)
371     print_values = mi_parse_values_option (argv[0]);
372   else
373     print_values = PRINT_NO_VALUES;
374
375   if (numchild <= 0)
376     return MI_CMD_DONE;
377
378   if (mi_version (uiout) == 1)
379     cleanup_children = make_cleanup_ui_out_tuple_begin_end (uiout, "children");
380   else
381     cleanup_children = make_cleanup_ui_out_list_begin_end (uiout, "children");
382   cc = childlist;
383   while (*cc != NULL)
384     {
385       struct cleanup *cleanup_child;
386       cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, "child");
387       print_varobj (*cc, print_values, 1 /* print expression */);
388       cc++;
389       do_cleanups (cleanup_child);
390     }
391   do_cleanups (cleanup_children);
392   xfree (childlist);
393   return MI_CMD_DONE;
394 }
395
396 enum mi_cmd_result
397 mi_cmd_var_info_type (char *command, char **argv, int argc)
398 {
399   struct varobj *var;
400
401   if (argc != 1)
402     error (_("mi_cmd_var_info_type: 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_info_type: Variable object not found"));
408
409   ui_out_field_string (uiout, "type", varobj_get_type (var));
410   return MI_CMD_DONE;
411 }
412
413 enum mi_cmd_result
414 mi_cmd_var_info_expression (char *command, char **argv, int argc)
415 {
416   enum varobj_languages lang;
417   struct varobj *var;
418
419   if (argc != 1)
420     error (_("mi_cmd_var_info_expression: Usage: NAME."));
421
422   /* Get varobj handle, if a valid var obj name was specified */
423   var = varobj_get_handle (argv[0]);
424   if (var == NULL)
425     error (_("mi_cmd_var_info_expression: Variable object not found"));
426
427   lang = varobj_get_language (var);
428
429   ui_out_field_string (uiout, "lang", varobj_language_string[(int) lang]);
430   ui_out_field_string (uiout, "exp", varobj_get_expression (var));
431   return MI_CMD_DONE;
432 }
433
434 enum mi_cmd_result
435 mi_cmd_var_show_attributes (char *command, char **argv, int argc)
436 {
437   int attr;
438   char *attstr;
439   struct varobj *var;
440
441   if (argc != 1)
442     error (_("mi_cmd_var_show_attributes: Usage: NAME."));
443
444   /* Get varobj handle, if a valid var obj name was specified */
445   var = varobj_get_handle (argv[0]);
446   if (var == NULL)
447     error (_("mi_cmd_var_show_attributes: Variable object not found"));
448
449   attr = varobj_get_attributes (var);
450   /* FIXME: define masks for attributes */
451   if (attr & 0x00000001)
452     attstr = "editable";
453   else
454     attstr = "noneditable";
455
456   ui_out_field_string (uiout, "attr", attstr);
457   return MI_CMD_DONE;
458 }
459
460 enum mi_cmd_result
461 mi_cmd_var_evaluate_expression (char *command, char **argv, int argc)
462 {
463   struct varobj *var;
464
465   if (argc != 1)
466     error (_("mi_cmd_var_evaluate_expression: Usage: NAME."));
467
468   /* Get varobj handle, if a valid var obj name was specified */
469   var = varobj_get_handle (argv[0]);
470   if (var == NULL)
471     error (_("mi_cmd_var_evaluate_expression: Variable object not found"));
472
473   ui_out_field_string (uiout, "value", varobj_get_value (var));
474   return MI_CMD_DONE;
475 }
476
477 enum mi_cmd_result
478 mi_cmd_var_assign (char *command, char **argv, int argc)
479 {
480   struct varobj *var;
481   char *expression;
482
483   if (argc != 2)
484     error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION."));
485
486   /* Get varobj handle, if a valid var obj name was specified */
487   var = varobj_get_handle (argv[0]);
488   if (var == NULL)
489     error (_("mi_cmd_var_assign: Variable object not found"));
490
491   /* FIXME: define masks for attributes */
492   if (!(varobj_get_attributes (var) & 0x00000001))
493     error (_("mi_cmd_var_assign: Variable object is not editable"));
494
495   expression = xstrdup (argv[1]);
496
497   if (!varobj_set_value (var, expression))
498     error (_("mi_cmd_var_assign: Could not assign expression to variable object"));
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_update (char *command, char **argv, int argc)
506 {
507   struct varobj *var;
508   struct varobj **rootlist;
509   struct varobj **cr;
510   struct cleanup *cleanup;
511   char *name;
512   int nv;
513   enum print_values print_values;
514
515   if (argc != 1 && argc != 2)
516     error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME."));
517
518   if (argc == 1)
519     name = argv[0];
520   else
521     name = (argv[1]);
522
523   if (argc == 2)
524     print_values = mi_parse_values_option (argv[0]);
525   else
526     print_values = PRINT_NO_VALUES;
527
528   /* Check if the parameter is a "*" which means that we want
529      to update all variables */
530
531   if ((*name == '*') && (*(name + 1) == '\0'))
532     {
533       nv = varobj_list (&rootlist);
534       cleanup = make_cleanup (xfree, rootlist);
535       if (mi_version (uiout) <= 1)
536         make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
537       else
538         make_cleanup_ui_out_list_begin_end (uiout, "changelist");
539       if (nv <= 0)
540         {
541           do_cleanups (cleanup);
542           return MI_CMD_DONE;
543         }
544       cr = rootlist;
545       while (*cr != NULL)
546         {
547           varobj_update_one (*cr, print_values, 0 /* implicit */);
548           cr++;
549         }
550       do_cleanups (cleanup);
551     }
552   else
553     {
554       /* Get varobj handle, if a valid var obj name was specified */
555       var = varobj_get_handle (name);
556       if (var == NULL)
557         error (_("mi_cmd_var_update: Variable object not found"));
558
559       if (mi_version (uiout) <= 1)
560         cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
561       else
562         cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
563       varobj_update_one (var, print_values, 1 /* explicit */);
564       do_cleanups (cleanup);
565     }
566     return MI_CMD_DONE;
567 }
568
569 /* Helper for mi_cmd_var_update().  */
570
571 static void
572 varobj_update_one (struct varobj *var, enum print_values print_values,
573                    int explicit)
574 {
575   struct varobj **changelist;
576   struct varobj **cc;
577   struct cleanup *cleanup = NULL;
578   int nc;
579
580   nc = varobj_update (&var, &changelist, explicit);
581
582   /* nc >= 0  represents the number of changes reported into changelist.
583      nc < 0   means that an error occured or the the variable has 
584               changed type (TYPE_CHANGED).  */
585   
586   if (nc == 0)
587     return;
588   else if (nc < 0)
589     {
590       if (mi_version (uiout) > 1)
591         cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
592       ui_out_field_string (uiout, "name", varobj_get_objname(var));
593
594       switch (nc)
595       {
596         case NOT_IN_SCOPE:
597           ui_out_field_string (uiout, "in_scope", "false");
598           break;
599         case INVALID:
600           ui_out_field_string (uiout, "in_scope", "invalid");
601           break;
602         case TYPE_CHANGED:
603           ui_out_field_string (uiout, "in_scope", "true");
604           ui_out_field_string (uiout, "new_type", varobj_get_type(var));
605           ui_out_field_int (uiout, "new_num_children", 
606                             varobj_get_num_children(var));
607           break;
608       }
609       if (mi_version (uiout) > 1)
610         do_cleanups (cleanup);
611     }
612   else
613     {
614       cc = changelist;
615       while (*cc != NULL)
616         {
617           if (mi_version (uiout) > 1)
618             cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
619           ui_out_field_string (uiout, "name", varobj_get_objname (*cc));
620           if (mi_print_value_p (varobj_get_gdb_type (*cc), print_values))
621             ui_out_field_string (uiout, "value", varobj_get_value (*cc));
622           ui_out_field_string (uiout, "in_scope", "true");
623           ui_out_field_string (uiout, "type_changed", "false");
624           if (mi_version (uiout) > 1)
625             do_cleanups (cleanup);
626           cc++;
627         }
628       xfree (changelist);
629     }
630 }