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