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