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