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