Upgrade GDB from 7.4.1 to 7.6.1 on the vendor branch
[dragonfly.git] / contrib / gdb-7 / gdb / mi / mi-cmd-var.c
CommitLineData
5796c8dc 1/* MI Command Set - varobj commands.
ef5ccd6c 2 Copyright (C) 2000-2013 Free Software Foundation, Inc.
5796c8dc
SS
3
4 Contributed by Cygnus Solutions (a Red Hat company).
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21#include "defs.h"
22#include "mi-cmds.h"
ef5ccd6c 23#include "mi-main.h"
5796c8dc
SS
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#include "mi-getopt.h"
31#include "gdbthread.h"
32
33const char mi_no_values[] = "--no-values";
34const char mi_simple_values[] = "--simple-values";
35const char mi_all_values[] = "--all-values";
36
ef5ccd6c 37extern unsigned int varobjdebug; /* defined in varobj.c. */
5796c8dc
SS
38
39static void varobj_update_one (struct varobj *var,
ef5ccd6c
JM
40 enum print_values print_values,
41 int explicit);
5796c8dc 42
c50c785c
JM
43static int mi_print_value_p (struct varobj *var,
44 enum print_values print_values);
5796c8dc
SS
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. */
ef5ccd6c 49
5796c8dc
SS
50static void
51print_varobj (struct varobj *var, enum print_values print_values,
52 int print_expression)
53{
a45ae5f8 54 struct ui_out *uiout = current_uiout;
5796c8dc
SS
55 char *type;
56 int thread_id;
57 char *display_hint;
58
59 ui_out_field_string (uiout, "name", varobj_get_objname (var));
60 if (print_expression)
61 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
62 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
63
64 if (mi_print_value_p (var, print_values))
65 {
66 char *val = varobj_get_value (var);
cf7f2e2d 67
5796c8dc
SS
68 ui_out_field_string (uiout, "value", val);
69 xfree (val);
70 }
71
72 type = varobj_get_type (var);
73 if (type != NULL)
74 {
75 ui_out_field_string (uiout, "type", type);
76 xfree (type);
77 }
78
79 thread_id = varobj_get_thread_id (var);
80 if (thread_id > 0)
81 ui_out_field_int (uiout, "thread-id", thread_id);
82
83 if (varobj_get_frozen (var))
84 ui_out_field_int (uiout, "frozen", 1);
85
86 display_hint = varobj_get_display_hint (var);
87 if (display_hint)
88 {
89 ui_out_field_string (uiout, "displayhint", display_hint);
90 xfree (display_hint);
91 }
92
93 if (varobj_pretty_printed_p (var))
94 ui_out_field_int (uiout, "dynamic", 1);
95}
96
97/* VAROBJ operations */
98
99void
100mi_cmd_var_create (char *command, char **argv, int argc)
101{
a45ae5f8 102 struct ui_out *uiout = current_uiout;
5796c8dc
SS
103 CORE_ADDR frameaddr = 0;
104 struct varobj *var;
105 char *name;
106 char *frame;
107 char *expr;
108 struct cleanup *old_cleanups;
109 enum varobj_type var_type;
110
111 if (argc != 3)
ef5ccd6c 112 error (_("-var-create: Usage: NAME FRAME EXPRESSION."));
5796c8dc
SS
113
114 name = xstrdup (argv[0]);
ef5ccd6c
JM
115 /* Add cleanup for name. Must be free_current_contents as name can
116 be reallocated. */
5796c8dc
SS
117 old_cleanups = make_cleanup (free_current_contents, &name);
118
119 frame = xstrdup (argv[1]);
120 make_cleanup (xfree, frame);
121
122 expr = xstrdup (argv[2]);
123 make_cleanup (xfree, expr);
124
125 if (strcmp (name, "-") == 0)
126 {
127 xfree (name);
128 name = varobj_gen_name ();
129 }
130 else if (!isalpha (*name))
c50c785c 131 error (_("-var-create: name of object must begin with a letter"));
5796c8dc
SS
132
133 if (strcmp (frame, "*") == 0)
134 var_type = USE_CURRENT_FRAME;
135 else if (strcmp (frame, "@") == 0)
136 var_type = USE_SELECTED_FRAME;
137 else
138 {
139 var_type = USE_SPECIFIED_FRAME;
140 frameaddr = string_to_core_addr (frame);
141 }
142
143 if (varobjdebug)
144 fprintf_unfiltered (gdb_stdlog,
145 "Name=\"%s\", Frame=\"%s\" (%s), Expression=\"%s\"\n",
146 name, frame, hex_string (frameaddr), expr);
147
148 var = varobj_create (name, expr, frameaddr, var_type);
149
150 if (var == NULL)
c50c785c 151 error (_("-var-create: unable to create variable object"));
5796c8dc
SS
152
153 print_varobj (var, PRINT_ALL_VALUES, 0 /* don't print expression */);
154
155 ui_out_field_int (uiout, "has_more", varobj_has_more (var, 0));
156
157 do_cleanups (old_cleanups);
158}
159
160void
161mi_cmd_var_delete (char *command, char **argv, int argc)
162{
163 char *name;
164 struct varobj *var;
165 int numdel;
166 int children_only_p = 0;
167 struct cleanup *old_cleanups;
a45ae5f8 168 struct ui_out *uiout = current_uiout;
5796c8dc
SS
169
170 if (argc < 1 || argc > 2)
c50c785c 171 error (_("-var-delete: Usage: [-c] EXPRESSION."));
5796c8dc
SS
172
173 name = xstrdup (argv[0]);
ef5ccd6c
JM
174 /* Add cleanup for name. Must be free_current_contents as name can
175 be reallocated. */
5796c8dc
SS
176 old_cleanups = make_cleanup (free_current_contents, &name);
177
178 /* If we have one single argument it cannot be '-c' or any string
ef5ccd6c 179 starting with '-'. */
5796c8dc
SS
180 if (argc == 1)
181 {
182 if (strcmp (name, "-c") == 0)
c50c785c
JM
183 error (_("-var-delete: Missing required "
184 "argument after '-c': variable object name"));
5796c8dc 185 if (*name == '-')
c50c785c 186 error (_("-var-delete: Illegal variable object name"));
5796c8dc
SS
187 }
188
189 /* If we have 2 arguments they must be '-c' followed by a string
ef5ccd6c 190 which would be the variable name. */
5796c8dc
SS
191 if (argc == 2)
192 {
193 if (strcmp (name, "-c") != 0)
c50c785c 194 error (_("-var-delete: Invalid option."));
5796c8dc
SS
195 children_only_p = 1;
196 do_cleanups (old_cleanups);
197 name = xstrdup (argv[1]);
a45ae5f8 198 old_cleanups = make_cleanup (free_current_contents, &name);
5796c8dc
SS
199 }
200
201 /* If we didn't error out, now NAME contains the name of the
ef5ccd6c 202 variable. */
5796c8dc
SS
203
204 var = varobj_get_handle (name);
205
206 numdel = varobj_delete (var, NULL, children_only_p);
207
208 ui_out_field_int (uiout, "ndeleted", numdel);
209
210 do_cleanups (old_cleanups);
211}
212
213/* Parse a string argument into a format value. */
214
215static enum varobj_display_formats
216mi_parse_format (const char *arg)
217{
218 if (arg != NULL)
219 {
220 int len;
221
222 len = strlen (arg);
223
224 if (strncmp (arg, "natural", len) == 0)
225 return FORMAT_NATURAL;
226 else if (strncmp (arg, "binary", len) == 0)
227 return FORMAT_BINARY;
228 else if (strncmp (arg, "decimal", len) == 0)
229 return FORMAT_DECIMAL;
230 else if (strncmp (arg, "hexadecimal", len) == 0)
231 return FORMAT_HEXADECIMAL;
232 else if (strncmp (arg, "octal", len) == 0)
233 return FORMAT_OCTAL;
234 }
235
c50c785c
JM
236 error (_("Must specify the format as: \"natural\", "
237 "\"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
5796c8dc
SS
238}
239
240void
241mi_cmd_var_set_format (char *command, char **argv, int argc)
242{
243 enum varobj_display_formats format;
244 struct varobj *var;
245 char *val;
a45ae5f8 246 struct ui_out *uiout = current_uiout;
5796c8dc
SS
247
248 if (argc != 2)
c50c785c 249 error (_("-var-set-format: Usage: NAME FORMAT."));
5796c8dc 250
ef5ccd6c 251 /* Get varobj handle, if a valid var obj name was specified. */
5796c8dc
SS
252 var = varobj_get_handle (argv[0]);
253
254 format = mi_parse_format (argv[1]);
255
ef5ccd6c 256 /* Set the format of VAR to the given format. */
5796c8dc
SS
257 varobj_set_display_format (var, format);
258
ef5ccd6c 259 /* Report the new current format. */
5796c8dc
SS
260 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
261
ef5ccd6c 262 /* Report the value in the new format. */
5796c8dc
SS
263 val = varobj_get_value (var);
264 ui_out_field_string (uiout, "value", val);
265 xfree (val);
266}
267
268void
269mi_cmd_var_set_visualizer (char *command, char **argv, int argc)
270{
271 struct varobj *var;
272
273 if (argc != 2)
c50c785c 274 error (_("Usage: NAME VISUALIZER_FUNCTION."));
5796c8dc
SS
275
276 var = varobj_get_handle (argv[0]);
277
278 if (var == NULL)
c50c785c 279 error (_("Variable object not found"));
5796c8dc
SS
280
281 varobj_set_visualizer (var, argv[1]);
282}
283
284void
285mi_cmd_var_set_frozen (char *command, char **argv, int argc)
286{
287 struct varobj *var;
288 int frozen;
289
290 if (argc != 2)
291 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
292
293 var = varobj_get_handle (argv[0]);
294
295 if (strcmp (argv[1], "0") == 0)
296 frozen = 0;
297 else if (strcmp (argv[1], "1") == 0)
298 frozen = 1;
299 else
300 error (_("Invalid flag value"));
301
302 varobj_set_frozen (var, frozen);
303
ef5ccd6c
JM
304 /* We don't automatically return the new value, or what varobjs got
305 new values during unfreezing. If this information is required,
306 client should call -var-update explicitly. */
5796c8dc
SS
307}
308
5796c8dc
SS
309void
310mi_cmd_var_show_format (char *command, char **argv, int argc)
311{
a45ae5f8 312 struct ui_out *uiout = current_uiout;
5796c8dc
SS
313 enum varobj_display_formats format;
314 struct varobj *var;
315
316 if (argc != 1)
c50c785c 317 error (_("-var-show-format: Usage: NAME."));
5796c8dc 318
ef5ccd6c 319 /* Get varobj handle, if a valid var obj name was specified. */
5796c8dc
SS
320 var = varobj_get_handle (argv[0]);
321
322 format = varobj_get_display_format (var);
323
ef5ccd6c 324 /* Report the current format. */
5796c8dc
SS
325 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
326}
327
328void
329mi_cmd_var_info_num_children (char *command, char **argv, int argc)
330{
a45ae5f8 331 struct ui_out *uiout = current_uiout;
5796c8dc
SS
332 struct varobj *var;
333
334 if (argc != 1)
c50c785c 335 error (_("-var-info-num-children: Usage: NAME."));
5796c8dc 336
ef5ccd6c 337 /* Get varobj handle, if a valid var obj name was specified. */
5796c8dc
SS
338 var = varobj_get_handle (argv[0]);
339
340 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
341}
342
343/* Parse a string argument into a print_values value. */
344
345static enum print_values
346mi_parse_values_option (const char *arg)
347{
348 if (strcmp (arg, "0") == 0
349 || strcmp (arg, mi_no_values) == 0)
350 return PRINT_NO_VALUES;
351 else if (strcmp (arg, "1") == 0
352 || strcmp (arg, mi_all_values) == 0)
353 return PRINT_ALL_VALUES;
354 else if (strcmp (arg, "2") == 0
355 || strcmp (arg, mi_simple_values) == 0)
356 return PRINT_SIMPLE_VALUES;
357 else
358 error (_("Unknown value for PRINT_VALUES\n\
359Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
360 mi_no_values, mi_simple_values, mi_all_values);
361}
362
363/* Return 1 if given the argument PRINT_VALUES we should display
364 the varobj VAR. */
365
366static int
367mi_print_value_p (struct varobj *var, enum print_values print_values)
368{
369 struct type *type;
370
371 if (print_values == PRINT_NO_VALUES)
372 return 0;
373
374 if (print_values == PRINT_ALL_VALUES)
375 return 1;
376
377 if (varobj_pretty_printed_p (var))
378 return 1;
379
380 type = varobj_get_gdb_type (var);
381 if (type == NULL)
382 return 1;
383 else
384 {
385 type = check_typedef (type);
386
387 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
388 and that type is not a compound type. */
389 return (TYPE_CODE (type) != TYPE_CODE_ARRAY
390 && TYPE_CODE (type) != TYPE_CODE_STRUCT
391 && TYPE_CODE (type) != TYPE_CODE_UNION);
392 }
393}
394
395void
396mi_cmd_var_list_children (char *command, char **argv, int argc)
397{
a45ae5f8 398 struct ui_out *uiout = current_uiout;
5796c8dc
SS
399 struct varobj *var;
400 VEC(varobj_p) *children;
401 struct varobj *child;
5796c8dc
SS
402 enum print_values print_values;
403 int ix;
404 int from, to;
405 char *display_hint;
406
407 if (argc < 1 || argc > 4)
c50c785c
JM
408 error (_("-var-list-children: Usage: "
409 "[PRINT_VALUES] NAME [FROM TO]"));
5796c8dc 410
ef5ccd6c 411 /* Get varobj handle, if a valid var obj name was specified. */
5796c8dc
SS
412 if (argc == 1 || argc == 3)
413 var = varobj_get_handle (argv[0]);
414 else
415 var = varobj_get_handle (argv[1]);
416
417 if (argc > 2)
418 {
419 from = atoi (argv[argc - 2]);
420 to = atoi (argv[argc - 1]);
421 }
422 else
423 {
424 from = -1;
425 to = -1;
426 }
427
428 children = varobj_list_children (var, &from, &to);
429 ui_out_field_int (uiout, "numchild", to - from);
430 if (argc == 2 || argc == 4)
431 print_values = mi_parse_values_option (argv[0]);
432 else
433 print_values = PRINT_NO_VALUES;
434
435 display_hint = varobj_get_display_hint (var);
436 if (display_hint)
437 {
438 ui_out_field_string (uiout, "displayhint", display_hint);
439 xfree (display_hint);
440 }
441
442 if (from < to)
443 {
444 struct cleanup *cleanup_children;
cf7f2e2d 445
5796c8dc
SS
446 if (mi_version (uiout) == 1)
447 cleanup_children
448 = make_cleanup_ui_out_tuple_begin_end (uiout, "children");
449 else
450 cleanup_children
451 = make_cleanup_ui_out_list_begin_end (uiout, "children");
452 for (ix = from;
453 ix < to && VEC_iterate (varobj_p, children, ix, child);
454 ++ix)
455 {
456 struct cleanup *cleanup_child;
cf7f2e2d 457
5796c8dc
SS
458 cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, "child");
459 print_varobj (child, print_values, 1 /* print expression */);
460 do_cleanups (cleanup_child);
461 }
462 do_cleanups (cleanup_children);
463 }
464
465 ui_out_field_int (uiout, "has_more", varobj_has_more (var, to));
466}
467
468void
469mi_cmd_var_info_type (char *command, char **argv, int argc)
470{
a45ae5f8 471 struct ui_out *uiout = current_uiout;
5796c8dc
SS
472 struct varobj *var;
473
474 if (argc != 1)
c50c785c 475 error (_("-var-info-type: Usage: NAME."));
5796c8dc 476
ef5ccd6c 477 /* Get varobj handle, if a valid var obj name was specified. */
5796c8dc
SS
478 var = varobj_get_handle (argv[0]);
479
480 ui_out_field_string (uiout, "type", varobj_get_type (var));
481}
482
483void
484mi_cmd_var_info_path_expression (char *command, char **argv, int argc)
485{
a45ae5f8 486 struct ui_out *uiout = current_uiout;
5796c8dc
SS
487 struct varobj *var;
488 char *path_expr;
489
490 if (argc != 1)
491 error (_("Usage: NAME."));
492
493 /* Get varobj handle, if a valid var obj name was specified. */
494 var = varobj_get_handle (argv[0]);
495
496 path_expr = varobj_get_path_expr (var);
497
498 ui_out_field_string (uiout, "path_expr", path_expr);
499}
500
501void
502mi_cmd_var_info_expression (char *command, char **argv, int argc)
503{
a45ae5f8 504 struct ui_out *uiout = current_uiout;
5796c8dc
SS
505 enum varobj_languages lang;
506 struct varobj *var;
507
508 if (argc != 1)
c50c785c 509 error (_("-var-info-expression: Usage: NAME."));
5796c8dc 510
ef5ccd6c 511 /* Get varobj handle, if a valid var obj name was specified. */
5796c8dc
SS
512 var = varobj_get_handle (argv[0]);
513
514 lang = varobj_get_language (var);
515
516 ui_out_field_string (uiout, "lang", varobj_language_string[(int) lang]);
517 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
518}
519
520void
521mi_cmd_var_show_attributes (char *command, char **argv, int argc)
522{
a45ae5f8 523 struct ui_out *uiout = current_uiout;
5796c8dc
SS
524 int attr;
525 char *attstr;
526 struct varobj *var;
527
528 if (argc != 1)
c50c785c 529 error (_("-var-show-attributes: Usage: NAME."));
5796c8dc
SS
530
531 /* Get varobj handle, if a valid var obj name was specified */
532 var = varobj_get_handle (argv[0]);
533
534 attr = varobj_get_attributes (var);
535 /* FIXME: define masks for attributes */
536 if (attr & 0x00000001)
537 attstr = "editable";
538 else
539 attstr = "noneditable";
540
541 ui_out_field_string (uiout, "attr", attstr);
542}
543
544void
545mi_cmd_var_evaluate_expression (char *command, char **argv, int argc)
546{
a45ae5f8 547 struct ui_out *uiout = current_uiout;
5796c8dc
SS
548 struct varobj *var;
549
550 enum varobj_display_formats format;
551 int formatFound;
ef5ccd6c
JM
552 int oind;
553 char *oarg;
5796c8dc
SS
554
555 enum opt
5796c8dc 556 {
ef5ccd6c 557 OP_FORMAT
5796c8dc 558 };
ef5ccd6c
JM
559 static const struct mi_opt opts[] =
560 {
561 {"f", OP_FORMAT, 1},
562 { 0, 0, 0 }
563 };
5796c8dc 564
ef5ccd6c 565 /* Parse arguments. */
5796c8dc
SS
566 format = FORMAT_NATURAL;
567 formatFound = 0;
ef5ccd6c 568 oind = 0;
5796c8dc
SS
569 while (1)
570 {
cf7f2e2d 571 int opt = mi_getopt ("-var-evaluate-expression", argc, argv,
ef5ccd6c 572 opts, &oind, &oarg);
cf7f2e2d 573
5796c8dc
SS
574 if (opt < 0)
575 break;
576 switch ((enum opt) opt)
ef5ccd6c 577 {
5796c8dc
SS
578 case OP_FORMAT:
579 if (formatFound)
580 error (_("Cannot specify format more than once"));
581
ef5ccd6c 582 format = mi_parse_format (oarg);
5796c8dc
SS
583 formatFound = 1;
584 break;
ef5ccd6c 585 }
5796c8dc
SS
586 }
587
ef5ccd6c 588 if (oind >= argc)
5796c8dc
SS
589 error (_("Usage: [-f FORMAT] NAME"));
590
ef5ccd6c 591 if (oind < argc - 1)
5796c8dc
SS
592 error (_("Garbage at end of command"));
593
ef5ccd6c
JM
594 /* Get varobj handle, if a valid var obj name was specified. */
595 var = varobj_get_handle (argv[oind]);
5796c8dc
SS
596
597 if (formatFound)
598 {
599 char *val = varobj_get_formatted_value (var, format);
cf7f2e2d 600
5796c8dc
SS
601 ui_out_field_string (uiout, "value", val);
602 xfree (val);
603 }
604 else
605 {
606 char *val = varobj_get_value (var);
cf7f2e2d 607
5796c8dc
SS
608 ui_out_field_string (uiout, "value", val);
609 xfree (val);
610 }
611}
612
613void
614mi_cmd_var_assign (char *command, char **argv, int argc)
615{
a45ae5f8 616 struct ui_out *uiout = current_uiout;
5796c8dc
SS
617 struct varobj *var;
618 char *expression, *val;
ef5ccd6c 619 struct cleanup *cleanup;
5796c8dc
SS
620
621 if (argc != 2)
c50c785c 622 error (_("-var-assign: Usage: NAME EXPRESSION."));
5796c8dc 623
ef5ccd6c 624 /* Get varobj handle, if a valid var obj name was specified. */
5796c8dc
SS
625 var = varobj_get_handle (argv[0]);
626
627 if (!varobj_editable_p (var))
c50c785c 628 error (_("-var-assign: Variable object is not editable"));
5796c8dc
SS
629
630 expression = xstrdup (argv[1]);
631
ef5ccd6c
JM
632 /* MI command '-var-assign' may write memory, so suppress memory
633 changed notification if it does. */
634 cleanup
635 = make_cleanup_restore_integer (&mi_suppress_notification.memory);
636 mi_suppress_notification.memory = 1;
637
5796c8dc 638 if (!varobj_set_value (var, expression))
c50c785c
JM
639 error (_("-var-assign: Could not assign "
640 "expression to variable object"));
5796c8dc
SS
641
642 val = varobj_get_value (var);
643 ui_out_field_string (uiout, "value", val);
644 xfree (val);
ef5ccd6c
JM
645
646 do_cleanups (cleanup);
5796c8dc
SS
647}
648
649/* Type used for parameters passing to mi_cmd_var_update_iter. */
650
651struct mi_cmd_var_update
652 {
653 int only_floating;
654 enum print_values print_values;
655 };
656
657/* Helper for mi_cmd_var_update - update each VAR. */
658
659static void
660mi_cmd_var_update_iter (struct varobj *var, void *data_pointer)
661{
662 struct mi_cmd_var_update *data = data_pointer;
663 int thread_id, thread_stopped;
664
665 thread_id = varobj_get_thread_id (var);
666
667 if (thread_id == -1 && is_stopped (inferior_ptid))
668 thread_stopped = 1;
669 else
670 {
671 struct thread_info *tp = find_thread_id (thread_id);
672
673 if (tp)
674 thread_stopped = is_stopped (tp->ptid);
675 else
676 thread_stopped = 1;
677 }
678
ef5ccd6c
JM
679 if (thread_stopped
680 && (!data->only_floating || varobj_floating_p (var)))
681 varobj_update_one (var, data->print_values, 0 /* implicit */);
5796c8dc
SS
682}
683
684void
685mi_cmd_var_update (char *command, char **argv, int argc)
686{
a45ae5f8 687 struct ui_out *uiout = current_uiout;
5796c8dc
SS
688 struct cleanup *cleanup;
689 char *name;
690 enum print_values print_values;
691
692 if (argc != 1 && argc != 2)
c50c785c 693 error (_("-var-update: Usage: [PRINT_VALUES] NAME."));
5796c8dc
SS
694
695 if (argc == 1)
696 name = argv[0];
697 else
ef5ccd6c 698 name = argv[1];
5796c8dc
SS
699
700 if (argc == 2)
701 print_values = mi_parse_values_option (argv[0]);
702 else
703 print_values = PRINT_NO_VALUES;
704
705 if (mi_version (uiout) <= 1)
706 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
707 else
708 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
709
ef5ccd6c
JM
710 /* Check if the parameter is a "*", which means that we want to
711 update all variables. */
5796c8dc
SS
712
713 if ((*name == '*' || *name == '@') && (*(name + 1) == '\0'))
714 {
715 struct mi_cmd_var_update data;
716
ef5ccd6c 717 data.only_floating = (*name == '@');
5796c8dc
SS
718 data.print_values = print_values;
719
ef5ccd6c
JM
720 /* varobj_update_one automatically updates all the children of
721 VAROBJ. Therefore update each VAROBJ only once by iterating
722 only the root VAROBJs. */
5796c8dc
SS
723
724 all_root_varobjs (mi_cmd_var_update_iter, &data);
725 }
726 else
727 {
ef5ccd6c 728 /* Get varobj handle, if a valid var obj name was specified. */
5796c8dc
SS
729 struct varobj *var = varobj_get_handle (name);
730
731 varobj_update_one (var, print_values, 1 /* explicit */);
732 }
733
734 do_cleanups (cleanup);
735}
736
737/* Helper for mi_cmd_var_update(). */
738
739static void
740varobj_update_one (struct varobj *var, enum print_values print_values,
741 int explicit)
742{
a45ae5f8 743 struct ui_out *uiout = current_uiout;
5796c8dc
SS
744 struct cleanup *cleanup = NULL;
745 VEC (varobj_update_result) *changes;
746 varobj_update_result *r;
747 int i;
748
749 changes = varobj_update (&var, explicit);
750
751 for (i = 0; VEC_iterate (varobj_update_result, changes, i, r); ++i)
752 {
753 char *display_hint;
754 int from, to;
755
756 if (mi_version (uiout) > 1)
757 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
758 ui_out_field_string (uiout, "name", varobj_get_objname (r->varobj));
759
760 switch (r->status)
761 {
762 case VAROBJ_IN_SCOPE:
763 if (mi_print_value_p (r->varobj, print_values))
764 {
765 char *val = varobj_get_value (r->varobj);
cf7f2e2d 766
5796c8dc
SS
767 ui_out_field_string (uiout, "value", val);
768 xfree (val);
769 }
770 ui_out_field_string (uiout, "in_scope", "true");
771 break;
772 case VAROBJ_NOT_IN_SCOPE:
773 ui_out_field_string (uiout, "in_scope", "false");
774 break;
775 case VAROBJ_INVALID:
776 ui_out_field_string (uiout, "in_scope", "invalid");
777 break;
778 }
779
780 if (r->status != VAROBJ_INVALID)
781 {
782 if (r->type_changed)
783 ui_out_field_string (uiout, "type_changed", "true");
784 else
785 ui_out_field_string (uiout, "type_changed", "false");
786 }
787
788 if (r->type_changed)
789 ui_out_field_string (uiout, "new_type", varobj_get_type (r->varobj));
790
791 if (r->type_changed || r->children_changed)
792 ui_out_field_int (uiout, "new_num_children",
793 varobj_get_num_children (r->varobj));
794
ef5ccd6c 795 display_hint = varobj_get_display_hint (r->varobj);
5796c8dc
SS
796 if (display_hint)
797 {
798 ui_out_field_string (uiout, "displayhint", display_hint);
799 xfree (display_hint);
800 }
801
ef5ccd6c 802 if (varobj_pretty_printed_p (r->varobj))
5796c8dc
SS
803 ui_out_field_int (uiout, "dynamic", 1);
804
805 varobj_get_child_range (r->varobj, &from, &to);
806 ui_out_field_int (uiout, "has_more",
807 varobj_has_more (r->varobj, to));
808
809 if (r->new)
810 {
811 int j;
812 varobj_p child;
813 struct cleanup *cleanup;
814
815 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "new_children");
816 for (j = 0; VEC_iterate (varobj_p, r->new, j, child); ++j)
817 {
818 struct cleanup *cleanup_child;
cf7f2e2d 819
c50c785c
JM
820 cleanup_child
821 = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
5796c8dc
SS
822 print_varobj (child, print_values, 1 /* print_expression */);
823 do_cleanups (cleanup_child);
824 }
825
826 do_cleanups (cleanup);
827 VEC_free (varobj_p, r->new);
828 r->new = NULL; /* Paranoia. */
829 }
830
831 if (mi_version (uiout) > 1)
832 do_cleanups (cleanup);
833 }
834 VEC_free (varobj_update_result, changes);
835}
836
837void
838mi_cmd_enable_pretty_printing (char *command, char **argv, int argc)
839{
840 if (argc != 0)
c50c785c 841 error (_("-enable-pretty-printing: no arguments allowed"));
ef5ccd6c 842
5796c8dc
SS
843 varobj_enable_pretty_printing ();
844}
845
846void
847mi_cmd_var_set_update_range (char *command, char **argv, int argc)
848{
849 struct varobj *var;
850 int from, to;
851
852 if (argc != 3)
c50c785c 853 error (_("-var-set-update-range: Usage: VAROBJ FROM TO"));
5796c8dc
SS
854
855 var = varobj_get_handle (argv[0]);
856 from = atoi (argv[1]);
857 to = atoi (argv[2]);
858
859 varobj_set_child_range (var, from, to);
860}