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