Fix @-varobjs.
[deliverable/binutils-gdb.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 #include "mi-getopt.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 struct type *gdb_type;
53 char *type;
54 int thread_id;
55
56 ui_out_field_string (uiout, "name", varobj_get_objname (var));
57 if (print_expression)
58 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
59 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
60
61 if (mi_print_value_p (varobj_get_gdb_type (var), print_values))
62 ui_out_field_string (uiout, "value", varobj_get_value (var));
63
64 type = varobj_get_type (var);
65 if (type != NULL)
66 {
67 ui_out_field_string (uiout, "type", type);
68 xfree (type);
69 }
70
71 thread_id = varobj_get_thread_id (var);
72 if (thread_id > 0)
73 ui_out_field_int (uiout, "thread-id", thread_id);
74
75 if (varobj_get_frozen (var))
76 ui_out_field_int (uiout, "frozen", 1);
77 }
78
79 /* VAROBJ operations */
80
81 enum mi_cmd_result
82 mi_cmd_var_create (char *command, char **argv, int argc)
83 {
84 CORE_ADDR frameaddr = 0;
85 struct varobj *var;
86 char *name;
87 char *frame;
88 char *expr;
89 struct cleanup *old_cleanups;
90 enum varobj_type var_type;
91
92 if (argc != 3)
93 {
94 /* mi_error_message = xstrprintf ("mi_cmd_var_create: Usage:
95 ...."); return MI_CMD_ERROR; */
96 error (_("mi_cmd_var_create: Usage: NAME FRAME EXPRESSION."));
97 }
98
99 name = xstrdup (argv[0]);
100 /* Add cleanup for name. Must be free_current_contents as
101 name can be reallocated */
102 old_cleanups = make_cleanup (free_current_contents, &name);
103
104 frame = xstrdup (argv[1]);
105 make_cleanup (xfree, frame);
106
107 expr = xstrdup (argv[2]);
108 make_cleanup (xfree, expr);
109
110 if (strcmp (name, "-") == 0)
111 {
112 xfree (name);
113 name = varobj_gen_name ();
114 }
115 else if (!isalpha (*name))
116 error (_("mi_cmd_var_create: name of object must begin with a letter"));
117
118 if (strcmp (frame, "*") == 0)
119 var_type = USE_CURRENT_FRAME;
120 else if (strcmp (frame, "@") == 0)
121 var_type = USE_SELECTED_FRAME;
122 else
123 {
124 var_type = USE_SPECIFIED_FRAME;
125 frameaddr = string_to_core_addr (frame);
126 }
127
128 if (varobjdebug)
129 fprintf_unfiltered (gdb_stdlog,
130 "Name=\"%s\", Frame=\"%s\" (0x%s), Expression=\"%s\"\n",
131 name, frame, paddr (frameaddr), expr);
132
133 var = varobj_create (name, expr, frameaddr, var_type);
134
135 if (var == NULL)
136 error (_("mi_cmd_var_create: unable to create variable object"));
137
138 print_varobj (var, PRINT_ALL_VALUES, 0 /* don't print expression */);
139
140 do_cleanups (old_cleanups);
141 return MI_CMD_DONE;
142 }
143
144 enum mi_cmd_result
145 mi_cmd_var_delete (char *command, char **argv, int argc)
146 {
147 char *name;
148 struct varobj *var;
149 int numdel;
150 int children_only_p = 0;
151 struct cleanup *old_cleanups;
152
153 if (argc < 1 || argc > 2)
154 error (_("mi_cmd_var_delete: Usage: [-c] EXPRESSION."));
155
156 name = xstrdup (argv[0]);
157 /* Add cleanup for name. Must be free_current_contents as
158 name can be reallocated */
159 old_cleanups = make_cleanup (free_current_contents, &name);
160
161 /* If we have one single argument it cannot be '-c' or any string
162 starting with '-'. */
163 if (argc == 1)
164 {
165 if (strcmp (name, "-c") == 0)
166 error (_("mi_cmd_var_delete: Missing required argument after '-c': variable object name"));
167 if (*name == '-')
168 error (_("mi_cmd_var_delete: Illegal variable object name"));
169 }
170
171 /* If we have 2 arguments they must be '-c' followed by a string
172 which would be the variable name. */
173 if (argc == 2)
174 {
175 if (strcmp (name, "-c") != 0)
176 error (_("mi_cmd_var_delete: Invalid option."));
177 children_only_p = 1;
178 do_cleanups (old_cleanups);
179 name = xstrdup (argv[1]);
180 make_cleanup (free_current_contents, &name);
181 }
182
183 /* If we didn't error out, now NAME contains the name of the
184 variable. */
185
186 var = varobj_get_handle (name);
187
188 if (var == NULL)
189 error (_("mi_cmd_var_delete: Variable object not found."));
190
191 numdel = varobj_delete (var, NULL, children_only_p);
192
193 ui_out_field_int (uiout, "ndeleted", numdel);
194
195 do_cleanups (old_cleanups);
196 return MI_CMD_DONE;
197 }
198
199 /* Parse a string argument into a format value. */
200
201 static enum varobj_display_formats
202 mi_parse_format (const char *arg)
203 {
204 if (arg != NULL)
205 {
206 int len;
207
208 len = strlen (arg);
209
210 if (strncmp (arg, "natural", len) == 0)
211 return FORMAT_NATURAL;
212 else if (strncmp (arg, "binary", len) == 0)
213 return FORMAT_BINARY;
214 else if (strncmp (arg, "decimal", len) == 0)
215 return FORMAT_DECIMAL;
216 else if (strncmp (arg, "hexadecimal", len) == 0)
217 return FORMAT_HEXADECIMAL;
218 else if (strncmp (arg, "octal", len) == 0)
219 return FORMAT_OCTAL;
220 }
221
222 error (_("Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
223 }
224
225 enum mi_cmd_result
226 mi_cmd_var_set_format (char *command, char **argv, int argc)
227 {
228 enum varobj_display_formats format;
229 struct varobj *var;
230
231 if (argc != 2)
232 error (_("mi_cmd_var_set_format: Usage: NAME FORMAT."));
233
234 /* Get varobj handle, if a valid var obj name was specified */
235 var = varobj_get_handle (argv[0]);
236
237 if (var == NULL)
238 error (_("mi_cmd_var_set_format: Variable object not found"));
239
240 format = mi_parse_format (argv[1]);
241
242 /* Set the format of VAR to given format */
243 varobj_set_display_format (var, format);
244
245 /* Report the new current format */
246 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
247
248 /* Report the value in the new format */
249 ui_out_field_string (uiout, "value", varobj_get_value (var));
250 return MI_CMD_DONE;
251 }
252
253 enum mi_cmd_result
254 mi_cmd_var_set_frozen (char *command, char **argv, int argc)
255 {
256 struct varobj *var;
257 int frozen;
258
259 if (argc != 2)
260 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
261
262 var = varobj_get_handle (argv[0]);
263 if (var == NULL)
264 error (_("Variable object not found"));
265
266 if (strcmp (argv[1], "0") == 0)
267 frozen = 0;
268 else if (strcmp (argv[1], "1") == 0)
269 frozen = 1;
270 else
271 error (_("Invalid flag value"));
272
273 varobj_set_frozen (var, frozen);
274
275 /* We don't automatically return the new value, or what varobjs got new
276 values during unfreezing. If this information is required, client
277 should call -var-update explicitly. */
278 return MI_CMD_DONE;
279 }
280
281
282 enum mi_cmd_result
283 mi_cmd_var_show_format (char *command, char **argv, int argc)
284 {
285 enum varobj_display_formats format;
286 struct varobj *var;
287
288 if (argc != 1)
289 error (_("mi_cmd_var_show_format: Usage: NAME."));
290
291 /* Get varobj handle, if a valid var obj name was specified */
292 var = varobj_get_handle (argv[0]);
293 if (var == NULL)
294 error (_("mi_cmd_var_show_format: Variable object not found"));
295
296 format = varobj_get_display_format (var);
297
298 /* Report the current format */
299 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
300 return MI_CMD_DONE;
301 }
302
303 enum mi_cmd_result
304 mi_cmd_var_info_num_children (char *command, char **argv, int argc)
305 {
306 struct varobj *var;
307
308 if (argc != 1)
309 error (_("mi_cmd_var_info_num_children: Usage: NAME."));
310
311 /* Get varobj handle, if a valid var obj name was specified */
312 var = varobj_get_handle (argv[0]);
313 if (var == NULL)
314 error (_("mi_cmd_var_info_num_children: Variable object not found"));
315
316 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
317 return MI_CMD_DONE;
318 }
319
320 /* Parse a string argument into a print_values value. */
321
322 static enum print_values
323 mi_parse_values_option (const char *arg)
324 {
325 if (strcmp (arg, "0") == 0
326 || strcmp (arg, mi_no_values) == 0)
327 return PRINT_NO_VALUES;
328 else if (strcmp (arg, "1") == 0
329 || strcmp (arg, mi_all_values) == 0)
330 return PRINT_ALL_VALUES;
331 else if (strcmp (arg, "2") == 0
332 || strcmp (arg, mi_simple_values) == 0)
333 return PRINT_SIMPLE_VALUES;
334 else
335 error (_("Unknown value for PRINT_VALUES\n\
336 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
337 mi_no_values, mi_simple_values, mi_all_values);
338 }
339
340 /* Return 1 if given the argument PRINT_VALUES we should display
341 a value of type TYPE. */
342
343 static int
344 mi_print_value_p (struct type *type, enum print_values print_values)
345 {
346
347 if (print_values == PRINT_NO_VALUES)
348 return 0;
349
350 if (print_values == PRINT_ALL_VALUES)
351 return 1;
352
353 if (type == NULL)
354 return 1;
355 else
356 {
357 type = check_typedef (type);
358
359 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
360 and that type is not a compound type. */
361 return (TYPE_CODE (type) != TYPE_CODE_ARRAY
362 && TYPE_CODE (type) != TYPE_CODE_STRUCT
363 && TYPE_CODE (type) != TYPE_CODE_UNION);
364 }
365 }
366
367 enum mi_cmd_result
368 mi_cmd_var_list_children (char *command, char **argv, int argc)
369 {
370 struct varobj *var;
371 VEC(varobj_p) *children;
372 struct varobj *child;
373 struct cleanup *cleanup_children;
374 int numchild;
375 enum print_values print_values;
376 int ix;
377
378 if (argc != 1 && argc != 2)
379 error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME"));
380
381 /* Get varobj handle, if a valid var obj name was specified */
382 if (argc == 1)
383 var = varobj_get_handle (argv[0]);
384 else
385 var = varobj_get_handle (argv[1]);
386 if (var == NULL)
387 error (_("Variable object not found"));
388
389 children = varobj_list_children (var);
390 ui_out_field_int (uiout, "numchild", VEC_length (varobj_p, children));
391 if (argc == 2)
392 print_values = mi_parse_values_option (argv[0]);
393 else
394 print_values = PRINT_NO_VALUES;
395
396 if (VEC_length (varobj_p, children) == 0)
397 return MI_CMD_DONE;
398
399 if (mi_version (uiout) == 1)
400 cleanup_children = make_cleanup_ui_out_tuple_begin_end (uiout, "children");
401 else
402 cleanup_children = make_cleanup_ui_out_list_begin_end (uiout, "children");
403 for (ix = 0; VEC_iterate (varobj_p, children, ix, child); ++ix)
404 {
405 struct cleanup *cleanup_child;
406 cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, "child");
407 print_varobj (child, print_values, 1 /* print expression */);
408 do_cleanups (cleanup_child);
409 }
410 do_cleanups (cleanup_children);
411 return MI_CMD_DONE;
412 }
413
414 enum mi_cmd_result
415 mi_cmd_var_info_type (char *command, char **argv, int argc)
416 {
417 struct varobj *var;
418
419 if (argc != 1)
420 error (_("mi_cmd_var_info_type: 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_type: Variable object not found"));
426
427 ui_out_field_string (uiout, "type", varobj_get_type (var));
428 return MI_CMD_DONE;
429 }
430
431 enum mi_cmd_result
432 mi_cmd_var_info_path_expression (char *command, char **argv, int argc)
433 {
434 struct varobj *var;
435 char *path_expr;
436
437 if (argc != 1)
438 error (_("Usage: NAME."));
439
440 /* Get varobj handle, if a valid var obj name was specified. */
441 var = varobj_get_handle (argv[0]);
442 if (var == NULL)
443 error (_("Variable object not found"));
444
445 path_expr = varobj_get_path_expr (var);
446
447 ui_out_field_string (uiout, "path_expr", path_expr);
448
449 return MI_CMD_DONE;
450 }
451
452 enum mi_cmd_result
453 mi_cmd_var_info_expression (char *command, char **argv, int argc)
454 {
455 enum varobj_languages lang;
456 struct varobj *var;
457
458 if (argc != 1)
459 error (_("mi_cmd_var_info_expression: Usage: NAME."));
460
461 /* Get varobj handle, if a valid var obj name was specified */
462 var = varobj_get_handle (argv[0]);
463 if (var == NULL)
464 error (_("mi_cmd_var_info_expression: Variable object not found"));
465
466 lang = varobj_get_language (var);
467
468 ui_out_field_string (uiout, "lang", varobj_language_string[(int) lang]);
469 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
470 return MI_CMD_DONE;
471 }
472
473 enum mi_cmd_result
474 mi_cmd_var_show_attributes (char *command, char **argv, int argc)
475 {
476 int attr;
477 char *attstr;
478 struct varobj *var;
479
480 if (argc != 1)
481 error (_("mi_cmd_var_show_attributes: Usage: NAME."));
482
483 /* Get varobj handle, if a valid var obj name was specified */
484 var = varobj_get_handle (argv[0]);
485 if (var == NULL)
486 error (_("mi_cmd_var_show_attributes: Variable object not found"));
487
488 attr = varobj_get_attributes (var);
489 /* FIXME: define masks for attributes */
490 if (attr & 0x00000001)
491 attstr = "editable";
492 else
493 attstr = "noneditable";
494
495 ui_out_field_string (uiout, "attr", attstr);
496 return MI_CMD_DONE;
497 }
498
499 enum mi_cmd_result
500 mi_cmd_var_evaluate_expression (char *command, char **argv, int argc)
501 {
502 struct varobj *var;
503
504 enum varobj_display_formats format;
505 int formatFound;
506 int optind;
507 char *optarg;
508
509 enum opt
510 {
511 OP_FORMAT
512 };
513 static struct mi_opt opts[] =
514 {
515 {"f", OP_FORMAT, 1},
516 { 0, 0, 0 }
517 };
518
519 /* Parse arguments */
520 format = FORMAT_NATURAL;
521 formatFound = 0;
522 optind = 0;
523 while (1)
524 {
525 int opt = mi_getopt ("-var-evaluate-expression", argc, argv, opts, &optind, &optarg);
526 if (opt < 0)
527 break;
528 switch ((enum opt) opt)
529 {
530 case OP_FORMAT:
531 if (formatFound)
532 error (_("Cannot specify format more than once"));
533
534 format = mi_parse_format (optarg);
535 formatFound = 1;
536 break;
537 }
538 }
539
540 if (optind >= argc)
541 error (_("Usage: [-f FORMAT] NAME"));
542
543 if (optind < argc - 1)
544 error (_("Garbage at end of command"));
545
546 /* Get varobj handle, if a valid var obj name was specified */
547 var = varobj_get_handle (argv[optind]);
548 if (var == NULL)
549 error (_("Variable object not found"));
550
551 if (formatFound)
552 ui_out_field_string (uiout, "value", varobj_get_formatted_value (var, format));
553 else
554 ui_out_field_string (uiout, "value", varobj_get_value (var));
555
556 return MI_CMD_DONE;
557 }
558
559 enum mi_cmd_result
560 mi_cmd_var_assign (char *command, char **argv, int argc)
561 {
562 struct varobj *var;
563 char *expression;
564
565 if (argc != 2)
566 error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION."));
567
568 /* Get varobj handle, if a valid var obj name was specified */
569 var = varobj_get_handle (argv[0]);
570 if (var == NULL)
571 error (_("mi_cmd_var_assign: Variable object not found"));
572
573 if (!varobj_editable_p (var))
574 error (_("mi_cmd_var_assign: Variable object is not editable"));
575
576 expression = xstrdup (argv[1]);
577
578 if (!varobj_set_value (var, expression))
579 error (_("mi_cmd_var_assign: Could not assign expression to variable object"));
580
581 ui_out_field_string (uiout, "value", varobj_get_value (var));
582 return MI_CMD_DONE;
583 }
584
585 enum mi_cmd_result
586 mi_cmd_var_update (char *command, char **argv, int argc)
587 {
588 struct varobj *var;
589 struct varobj **rootlist;
590 struct varobj **cr;
591 struct cleanup *cleanup;
592 char *name;
593 int nv;
594 enum print_values print_values;
595
596 if (argc != 1 && argc != 2)
597 error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME."));
598
599 if (argc == 1)
600 name = argv[0];
601 else
602 name = (argv[1]);
603
604 if (argc == 2)
605 print_values = mi_parse_values_option (argv[0]);
606 else
607 print_values = PRINT_NO_VALUES;
608
609 /* Check if the parameter is a "*" which means that we want
610 to update all variables */
611
612 if ((*name == '*' || *name == '@') && (*(name + 1) == '\0'))
613 {
614 nv = varobj_list (&rootlist);
615 cleanup = make_cleanup (xfree, rootlist);
616 if (mi_version (uiout) <= 1)
617 make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
618 else
619 make_cleanup_ui_out_list_begin_end (uiout, "changelist");
620 if (nv <= 0)
621 {
622 do_cleanups (cleanup);
623 return MI_CMD_DONE;
624 }
625 cr = rootlist;
626 while (*cr != NULL)
627 {
628 if (*name == '*' || varobj_floating_p (*cr))
629 varobj_update_one (*cr, print_values, 0 /* implicit */);
630 cr++;
631 }
632 do_cleanups (cleanup);
633 }
634 else
635 {
636 /* Get varobj handle, if a valid var obj name was specified */
637 var = varobj_get_handle (name);
638 if (var == NULL)
639 error (_("mi_cmd_var_update: Variable object not found"));
640
641 if (mi_version (uiout) <= 1)
642 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
643 else
644 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
645 varobj_update_one (var, print_values, 1 /* explicit */);
646 do_cleanups (cleanup);
647 }
648 return MI_CMD_DONE;
649 }
650
651 /* Helper for mi_cmd_var_update(). */
652
653 static void
654 varobj_update_one (struct varobj *var, enum print_values print_values,
655 int explicit)
656 {
657 struct varobj **changelist;
658 struct varobj **cc;
659 struct cleanup *cleanup = NULL;
660 int nc;
661
662 nc = varobj_update (&var, &changelist, explicit);
663
664 /* nc >= 0 represents the number of changes reported into changelist.
665 nc < 0 means that an error occured or the the variable has
666 changed type (TYPE_CHANGED). */
667
668 if (nc == 0)
669 return;
670 else if (nc < 0)
671 {
672 if (mi_version (uiout) > 1)
673 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
674 ui_out_field_string (uiout, "name", varobj_get_objname(var));
675
676 switch (nc)
677 {
678 case NOT_IN_SCOPE:
679 ui_out_field_string (uiout, "in_scope", "false");
680 break;
681 case INVALID:
682 ui_out_field_string (uiout, "in_scope", "invalid");
683 break;
684 case TYPE_CHANGED:
685 ui_out_field_string (uiout, "in_scope", "true");
686 ui_out_field_string (uiout, "type_changed", "true");
687 ui_out_field_string (uiout, "new_type", varobj_get_type(var));
688 ui_out_field_int (uiout, "new_num_children",
689 varobj_get_num_children(var));
690 break;
691 }
692 if (mi_version (uiout) > 1)
693 do_cleanups (cleanup);
694 }
695 else
696 {
697 cc = changelist;
698 while (*cc != NULL)
699 {
700 if (mi_version (uiout) > 1)
701 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
702 ui_out_field_string (uiout, "name", varobj_get_objname (*cc));
703 if (mi_print_value_p (varobj_get_gdb_type (*cc), print_values))
704 ui_out_field_string (uiout, "value", varobj_get_value (*cc));
705 ui_out_field_string (uiout, "in_scope", "true");
706 ui_out_field_string (uiout, "type_changed", "false");
707 if (mi_version (uiout) > 1)
708 do_cleanups (cleanup);
709 cc++;
710 }
711 xfree (changelist);
712 }
713 }
This page took 0.044983 seconds and 5 git commands to generate.