2006-07-29 Vladimir Prus <vladimir@codesourcery.com>
[platform/upstream/binutils.git] / gdb / mi / mi-cmd-var.c
1 /* MI Command Set - varobj commands.
2
3    Copyright (C) 2000, 2002, 2004, 2005 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 int 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_NO_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   char *type;
325   enum print_values print_values;
326
327   if (argc != 1 && argc != 2)
328     error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME"));
329
330   /* Get varobj handle, if a valid var obj name was specified */
331   if (argc == 1)
332     var = varobj_get_handle (argv[0]);
333   else
334     var = varobj_get_handle (argv[1]);
335   if (var == NULL)
336     error (_("Variable object not found"));
337
338   numchild = varobj_list_children (var, &childlist);
339   ui_out_field_int (uiout, "numchild", numchild);
340   if (argc == 2)
341     print_values = mi_parse_values_option (argv[0]);
342   else
343     print_values = PRINT_NO_VALUES;
344
345   if (numchild <= 0)
346     return MI_CMD_DONE;
347
348   if (mi_version (uiout) == 1)
349     cleanup_children = make_cleanup_ui_out_tuple_begin_end (uiout, "children");
350   else
351     cleanup_children = make_cleanup_ui_out_list_begin_end (uiout, "children");
352   cc = childlist;
353   while (*cc != NULL)
354     {
355       struct cleanup *cleanup_child;
356       cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, "child");
357       print_varobj (*cc, print_values, 1 /* print expression */);
358       cc++;
359       do_cleanups (cleanup_child);
360     }
361   do_cleanups (cleanup_children);
362   xfree (childlist);
363   return MI_CMD_DONE;
364 }
365
366 enum mi_cmd_result
367 mi_cmd_var_info_type (char *command, char **argv, int argc)
368 {
369   struct varobj *var;
370
371   if (argc != 1)
372     error (_("mi_cmd_var_info_type: Usage: NAME."));
373
374   /* Get varobj handle, if a valid var obj name was specified */
375   var = varobj_get_handle (argv[0]);
376   if (var == NULL)
377     error (_("mi_cmd_var_info_type: Variable object not found"));
378
379   ui_out_field_string (uiout, "type", varobj_get_type (var));
380   return MI_CMD_DONE;
381 }
382
383 enum mi_cmd_result
384 mi_cmd_var_info_expression (char *command, char **argv, int argc)
385 {
386   enum varobj_languages lang;
387   struct varobj *var;
388
389   if (argc != 1)
390     error (_("mi_cmd_var_info_expression: Usage: NAME."));
391
392   /* Get varobj handle, if a valid var obj name was specified */
393   var = varobj_get_handle (argv[0]);
394   if (var == NULL)
395     error (_("mi_cmd_var_info_expression: Variable object not found"));
396
397   lang = varobj_get_language (var);
398
399   ui_out_field_string (uiout, "lang", varobj_language_string[(int) lang]);
400   ui_out_field_string (uiout, "exp", varobj_get_expression (var));
401   return MI_CMD_DONE;
402 }
403
404 enum mi_cmd_result
405 mi_cmd_var_show_attributes (char *command, char **argv, int argc)
406 {
407   int attr;
408   char *attstr;
409   struct varobj *var;
410
411   if (argc != 1)
412     error (_("mi_cmd_var_show_attributes: 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_show_attributes: Variable object not found"));
418
419   attr = varobj_get_attributes (var);
420   /* FIXME: define masks for attributes */
421   if (attr & 0x00000001)
422     attstr = "editable";
423   else
424     attstr = "noneditable";
425
426   ui_out_field_string (uiout, "attr", attstr);
427   return MI_CMD_DONE;
428 }
429
430 enum mi_cmd_result
431 mi_cmd_var_evaluate_expression (char *command, char **argv, int argc)
432 {
433   struct varobj *var;
434
435   if (argc != 1)
436     error (_("mi_cmd_var_evaluate_expression: Usage: NAME."));
437
438   /* Get varobj handle, if a valid var obj name was specified */
439   var = varobj_get_handle (argv[0]);
440   if (var == NULL)
441     error (_("mi_cmd_var_evaluate_expression: Variable object not found"));
442
443   ui_out_field_string (uiout, "value", varobj_get_value (var));
444   return MI_CMD_DONE;
445 }
446
447 enum mi_cmd_result
448 mi_cmd_var_assign (char *command, char **argv, int argc)
449 {
450   struct varobj *var;
451   char *expression;
452
453   if (argc != 2)
454     error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION."));
455
456   /* Get varobj handle, if a valid var obj name was specified */
457   var = varobj_get_handle (argv[0]);
458   if (var == NULL)
459     error (_("mi_cmd_var_assign: Variable object not found"));
460
461   /* FIXME: define masks for attributes */
462   if (!(varobj_get_attributes (var) & 0x00000001))
463     error (_("mi_cmd_var_assign: Variable object is not editable"));
464
465   expression = xstrdup (argv[1]);
466
467   if (!varobj_set_value (var, expression))
468     error (_("mi_cmd_var_assign: Could not assign expression to varible object"));
469
470   ui_out_field_string (uiout, "value", varobj_get_value (var));
471   return MI_CMD_DONE;
472 }
473
474 enum mi_cmd_result
475 mi_cmd_var_update (char *command, char **argv, int argc)
476 {
477   struct varobj *var;
478   struct varobj **rootlist;
479   struct varobj **cr;
480   struct cleanup *cleanup;
481   char *name;
482   int nv;
483   enum print_values print_values;
484
485   if (argc != 1 && argc != 2)
486     error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME."));
487
488   if (argc == 1)
489     name = argv[0];
490   else
491     name = (argv[1]);
492
493   if (argc == 2)
494     print_values = mi_parse_values_option (argv[0]);
495   else
496     print_values = PRINT_NO_VALUES;
497
498   /* Check if the parameter is a "*" which means that we want
499      to update all variables */
500
501   if ((*name == '*') && (*(name + 1) == '\0'))
502     {
503       nv = varobj_list (&rootlist);
504       if (mi_version (uiout) <= 1)
505         cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
506       else
507         cleanup = 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       xfree (rootlist);
520       do_cleanups (cleanup);
521     }
522   else
523     {
524       /* Get varobj handle, if a valid var obj name was specified */
525       var = varobj_get_handle (name);
526       if (var == NULL)
527         error (_("mi_cmd_var_update: Variable object not found"));
528
529       if (mi_version (uiout) <= 1)
530         cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
531       else
532         cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
533       varobj_update_one (var, print_values);
534       do_cleanups (cleanup);
535     }
536     return MI_CMD_DONE;
537 }
538
539 /* Helper for mi_cmd_var_update() Returns 0 if the update for
540    the variable fails (usually because the variable is out of
541    scope), and 1 if it succeeds. */
542
543 static int
544 varobj_update_one (struct varobj *var, enum print_values print_values)
545 {
546   struct varobj **changelist;
547   struct varobj **cc;
548   struct cleanup *cleanup = NULL;
549   int nc;
550
551   nc = varobj_update (&var, &changelist);
552
553   /* nc == 0 means that nothing has changed.
554      nc == -1 means that an error occured in updating the variable.
555      nc == -2 means the variable has changed type. */
556   
557   if (nc == 0)
558     return 1;
559   else if (nc == -1)
560     {
561       if (mi_version (uiout) > 1)
562         cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
563       ui_out_field_string (uiout, "name", varobj_get_objname(var));
564       ui_out_field_string (uiout, "in_scope", "false");
565       if (mi_version (uiout) > 1)
566         do_cleanups (cleanup);
567       return -1;
568     }
569   else if (nc == -2)
570     {
571       if (mi_version (uiout) > 1)
572         cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
573       ui_out_field_string (uiout, "name", varobj_get_objname (var));
574       ui_out_field_string (uiout, "in_scope", "true");
575       ui_out_field_string (uiout, "new_type", varobj_get_type(var));
576       ui_out_field_int (uiout, "new_num_children", 
577                            varobj_get_num_children(var));
578       if (mi_version (uiout) > 1)
579         do_cleanups (cleanup);
580     }
581   else
582     {
583       
584       cc = changelist;
585       while (*cc != NULL)
586         {
587           if (mi_version (uiout) > 1)
588             cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
589           ui_out_field_string (uiout, "name", varobj_get_objname (*cc));
590           if (mi_print_value_p (varobj_get_gdb_type (*cc), print_values))
591             ui_out_field_string (uiout, "value", varobj_get_value (*cc));
592           ui_out_field_string (uiout, "in_scope", "true");
593           ui_out_field_string (uiout, "type_changed", "false");
594           if (mi_version (uiout) > 1)
595             do_cleanups (cleanup);
596           cc++;
597         }
598       xfree (changelist);
599       return 1;
600     }
601   return 1;
602 }