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