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