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