Use thread_info and inferior pointers more throughout
[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"
00431a78 34#include "inferior.h"
1ecb4ee0 35
ccce17b0 36extern unsigned int varobjdebug; /* defined in varobj.c. */
fb40c209 37
8756216b 38static void varobj_update_one (struct varobj *var,
2b03b41d 39 enum print_values print_values,
4c37490d 40 bool is_explicit);
fb40c209 41
9a2b4c1b
MS
42static int mi_print_value_p (struct varobj *var,
43 enum print_values print_values);
a217f3f5
VP
44
45/* Print variable object VAR. The PRINT_VALUES parameter controls
46 if the value should be printed. The PRINT_EXPRESSION parameter
47 controls if the expression should be printed. */
2b03b41d 48
a217f3f5
VP
49static void
50print_varobj (struct varobj *var, enum print_values print_values,
51 int print_expression)
52{
79a45e25 53 struct ui_out *uiout = current_uiout;
c5b48eac 54 int thread_id;
a217f3f5 55
112e8700 56 uiout->field_string ("name", varobj_get_objname (var));
a217f3f5 57 if (print_expression)
ca83fa81 58 {
2f408ecb 59 std::string exp = varobj_get_expression (var);
ca83fa81 60
112e8700 61 uiout->field_string ("exp", exp.c_str ());
ca83fa81 62 }
112e8700 63 uiout->field_int ("numchild", varobj_get_num_children (var));
a217f3f5 64
0cc7d26f
TT
65 if (mi_print_value_p (var, print_values))
66 {
2f408ecb 67 std::string val = varobj_get_value (var);
102040f0 68
112e8700 69 uiout->field_string ("value", val.c_str ());
0cc7d26f 70 }
a217f3f5 71
2f408ecb
PA
72 std::string type = varobj_get_type (var);
73 if (!type.empty ())
112e8700 74 uiout->field_string ("type", type.c_str ());
25d5ea92 75
c5b48eac
VP
76 thread_id = varobj_get_thread_id (var);
77 if (thread_id > 0)
112e8700 78 uiout->field_int ("thread-id", thread_id);
c5b48eac 79
25d5ea92 80 if (varobj_get_frozen (var))
112e8700 81 uiout->field_int ("frozen", 1);
0cc7d26f 82
9b972014 83 gdb::unique_xmalloc_ptr<char> display_hint = varobj_get_display_hint (var);
0cc7d26f 84 if (display_hint)
112e8700 85 uiout->field_string ("displayhint", display_hint.get ());
0cc7d26f 86
cde5ef40 87 if (varobj_is_dynamic_p (var))
112e8700 88 uiout->field_int ("dynamic", 1);
a217f3f5
VP
89}
90
fb40c209
AC
91/* VAROBJ operations */
92
ce8f13f8 93void
9f33b8b7 94mi_cmd_var_create (const char *command, char **argv, int argc)
fb40c209 95{
79a45e25 96 struct ui_out *uiout = current_uiout;
73a93a32 97 CORE_ADDR frameaddr = 0;
fb40c209 98 struct varobj *var;
fb40c209
AC
99 char *frame;
100 char *expr;
73a93a32 101 enum varobj_type var_type;
fb40c209
AC
102
103 if (argc != 3)
2b03b41d 104 error (_("-var-create: Usage: NAME FRAME EXPRESSION."));
fb40c209 105
2d6960b4
TT
106 frame = argv[1];
107 expr = argv[2];
fb40c209 108
2d6960b4
TT
109 const char *name = argv[0];
110 std::string gen_name;
fb40c209
AC
111 if (strcmp (name, "-") == 0)
112 {
2d6960b4
TT
113 gen_name = varobj_gen_name ();
114 name = gen_name.c_str ();
fb40c209 115 }
2d6960b4 116 else if (!isalpha (name[0]))
1b05df00 117 error (_("-var-create: name of object must begin with a letter"));
fb40c209
AC
118
119 if (strcmp (frame, "*") == 0)
73a93a32
JI
120 var_type = USE_CURRENT_FRAME;
121 else if (strcmp (frame, "@") == 0)
122 var_type = USE_SELECTED_FRAME;
fb40c209 123 else
73a93a32
JI
124 {
125 var_type = USE_SPECIFIED_FRAME;
1bd34ded 126 frameaddr = string_to_core_addr (frame);
73a93a32 127 }
fb40c209
AC
128
129 if (varobjdebug)
130 fprintf_unfiltered (gdb_stdlog,
2d6960b4 131 "Name=\"%s\", Frame=\"%s\" (%s), Expression=\"%s\"\n",
5af949e3 132 name, frame, hex_string (frameaddr), expr);
fb40c209 133
73a93a32 134 var = varobj_create (name, expr, frameaddr, var_type);
fb40c209
AC
135
136 if (var == NULL)
1b05df00 137 error (_("-var-create: unable to create variable object"));
fb40c209 138
224e4ca7 139 print_varobj (var, PRINT_ALL_VALUES, 0 /* don't print expression */);
fb40c209 140
112e8700 141 uiout->field_int ("has_more", varobj_has_more (var, 0));
fb40c209
AC
142}
143
ce8f13f8 144void
9f33b8b7 145mi_cmd_var_delete (const char *command, char **argv, int argc)
fb40c209
AC
146{
147 char *name;
fb40c209
AC
148 struct varobj *var;
149 int numdel;
150 int children_only_p = 0;
79a45e25 151 struct ui_out *uiout = current_uiout;
fb40c209
AC
152
153 if (argc < 1 || argc > 2)
1b05df00 154 error (_("-var-delete: Usage: [-c] EXPRESSION."));
fb40c209 155
2d6960b4 156 name = argv[0];
fb40c209
AC
157
158 /* If we have one single argument it cannot be '-c' or any string
2b03b41d 159 starting with '-'. */
fb40c209
AC
160 if (argc == 1)
161 {
162 if (strcmp (name, "-c") == 0)
1b05df00 163 error (_("-var-delete: Missing required "
9a2b4c1b 164 "argument after '-c': variable object name"));
fb40c209 165 if (*name == '-')
1b05df00 166 error (_("-var-delete: Illegal variable object name"));
fb40c209
AC
167 }
168
169 /* If we have 2 arguments they must be '-c' followed by a string
2b03b41d 170 which would be the variable name. */
fb40c209
AC
171 if (argc == 2)
172 {
fb40c209 173 if (strcmp (name, "-c") != 0)
1b05df00 174 error (_("-var-delete: Invalid option."));
fb40c209 175 children_only_p = 1;
2d6960b4 176 name = argv[1];
fb40c209
AC
177 }
178
179 /* If we didn't error out, now NAME contains the name of the
2b03b41d 180 variable. */
fb40c209
AC
181
182 var = varobj_get_handle (name);
183
30914ca8 184 numdel = varobj_delete (var, children_only_p);
fb40c209 185
112e8700 186 uiout->field_int ("ndeleted", numdel);
fb40c209
AC
187}
188
de051565
MK
189/* Parse a string argument into a format value. */
190
191static enum varobj_display_formats
192mi_parse_format (const char *arg)
193{
194 if (arg != NULL)
195 {
196 int len;
197
198 len = strlen (arg);
199
200 if (strncmp (arg, "natural", len) == 0)
201 return FORMAT_NATURAL;
202 else if (strncmp (arg, "binary", len) == 0)
203 return FORMAT_BINARY;
204 else if (strncmp (arg, "decimal", len) == 0)
205 return FORMAT_DECIMAL;
206 else if (strncmp (arg, "hexadecimal", len) == 0)
207 return FORMAT_HEXADECIMAL;
208 else if (strncmp (arg, "octal", len) == 0)
209 return FORMAT_OCTAL;
1c35a88f
LM
210 else if (strncmp (arg, "zero-hexadecimal", len) == 0)
211 return FORMAT_ZHEXADECIMAL;
de051565
MK
212 }
213
9a2b4c1b 214 error (_("Must specify the format as: \"natural\", "
1c35a88f 215 "\"binary\", \"decimal\", \"hexadecimal\", \"octal\" or \"zero-hexadecimal\""));
de051565
MK
216}
217
ce8f13f8 218void
9f33b8b7 219mi_cmd_var_set_format (const char *command, char **argv, int argc)
fb40c209
AC
220{
221 enum varobj_display_formats format;
fb40c209 222 struct varobj *var;
79a45e25 223 struct ui_out *uiout = current_uiout;
fb40c209
AC
224
225 if (argc != 2)
1b05df00 226 error (_("-var-set-format: Usage: NAME FORMAT."));
fb40c209 227
2b03b41d 228 /* Get varobj handle, if a valid var obj name was specified. */
fb40c209
AC
229 var = varobj_get_handle (argv[0]);
230
de051565
MK
231 format = mi_parse_format (argv[1]);
232
2b03b41d 233 /* Set the format of VAR to the given format. */
fb40c209
AC
234 varobj_set_display_format (var, format);
235
2b03b41d 236 /* Report the new current format. */
112e8700 237 uiout->field_string ("format", varobj_format_string[(int) format]);
00ee6348 238
2b03b41d 239 /* Report the value in the new format. */
2f408ecb 240 std::string val = varobj_get_value (var);
112e8700 241 uiout->field_string ("value", val.c_str ());
fb40c209
AC
242}
243
b6313243 244void
9f33b8b7 245mi_cmd_var_set_visualizer (const char *command, char **argv, int argc)
b6313243
TT
246{
247 struct varobj *var;
248
249 if (argc != 2)
9b20d036 250 error (_("Usage: NAME VISUALIZER_FUNCTION."));
b6313243
TT
251
252 var = varobj_get_handle (argv[0]);
253
254 if (var == NULL)
9b20d036 255 error (_("Variable object not found"));
b6313243
TT
256
257 varobj_set_visualizer (var, argv[1]);
258}
259
ce8f13f8 260void
9f33b8b7 261mi_cmd_var_set_frozen (const char *command, char **argv, int argc)
25d5ea92
VP
262{
263 struct varobj *var;
4c37490d 264 bool frozen;
25d5ea92
VP
265
266 if (argc != 2)
267 error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
268
269 var = varobj_get_handle (argv[0]);
25d5ea92
VP
270
271 if (strcmp (argv[1], "0") == 0)
4c37490d 272 frozen = false;
25d5ea92 273 else if (strcmp (argv[1], "1") == 0)
4c37490d 274 frozen = true;
25d5ea92
VP
275 else
276 error (_("Invalid flag value"));
277
278 varobj_set_frozen (var, frozen);
279
2b03b41d
SS
280 /* We don't automatically return the new value, or what varobjs got
281 new values during unfreezing. If this information is required,
282 client should call -var-update explicitly. */
25d5ea92
VP
283}
284
ce8f13f8 285void
9f33b8b7 286mi_cmd_var_show_format (const char *command, char **argv, int argc)
fb40c209 287{
79a45e25 288 struct ui_out *uiout = current_uiout;
fb40c209
AC
289 enum varobj_display_formats format;
290 struct varobj *var;
291
292 if (argc != 1)
1b05df00 293 error (_("-var-show-format: Usage: NAME."));
fb40c209 294
2b03b41d 295 /* Get varobj handle, if a valid var obj name was specified. */
fb40c209 296 var = varobj_get_handle (argv[0]);
fb40c209
AC
297
298 format = varobj_get_display_format (var);
299
2b03b41d 300 /* Report the current format. */
112e8700 301 uiout->field_string ("format", varobj_format_string[(int) format]);
fb40c209
AC
302}
303
ce8f13f8 304void
9f33b8b7 305mi_cmd_var_info_num_children (const char *command, char **argv, int argc)
fb40c209 306{
79a45e25 307 struct ui_out *uiout = current_uiout;
fb40c209
AC
308 struct varobj *var;
309
310 if (argc != 1)
1b05df00 311 error (_("-var-info-num-children: Usage: NAME."));
fb40c209 312
2b03b41d 313 /* Get varobj handle, if a valid var obj name was specified. */
fb40c209 314 var = varobj_get_handle (argv[0]);
fb40c209 315
112e8700 316 uiout->field_int ("numchild", varobj_get_num_children (var));
fb40c209
AC
317}
318
1ecb4ee0 319/* Return 1 if given the argument PRINT_VALUES we should display
0cc7d26f 320 the varobj VAR. */
1ecb4ee0
DJ
321
322static int
0cc7d26f 323mi_print_value_p (struct varobj *var, enum print_values print_values)
1ecb4ee0 324{
0cc7d26f 325 struct type *type;
1ecb4ee0
DJ
326
327 if (print_values == PRINT_NO_VALUES)
328 return 0;
329
330 if (print_values == PRINT_ALL_VALUES)
331 return 1;
332
cde5ef40 333 if (varobj_is_dynamic_p (var))
0cc7d26f
TT
334 return 1;
335
336 type = varobj_get_gdb_type (var);
9265acad
NR
337 if (type == NULL)
338 return 1;
339 else
340 {
341 type = check_typedef (type);
1ecb4ee0 342
9265acad
NR
343 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
344 and that type is not a compound type. */
345 return (TYPE_CODE (type) != TYPE_CODE_ARRAY
346 && TYPE_CODE (type) != TYPE_CODE_STRUCT
347 && TYPE_CODE (type) != TYPE_CODE_UNION);
348 }
1ecb4ee0
DJ
349}
350
ce8f13f8 351void
9f33b8b7 352mi_cmd_var_list_children (const char *command, char **argv, int argc)
fb40c209 353{
79a45e25 354 struct ui_out *uiout = current_uiout;
d56d46f5 355 struct varobj *var;
c9e1f0fc 356 enum print_values print_values;
0cc7d26f 357 int from, to;
fb40c209 358
0cc7d26f 359 if (argc < 1 || argc > 4)
1b05df00 360 error (_("-var-list-children: Usage: "
9a2b4c1b 361 "[PRINT_VALUES] NAME [FROM TO]"));
fb40c209 362
2b03b41d 363 /* Get varobj handle, if a valid var obj name was specified. */
0cc7d26f 364 if (argc == 1 || argc == 3)
1ecb4ee0
DJ
365 var = varobj_get_handle (argv[0]);
366 else
367 var = varobj_get_handle (argv[1]);
fb40c209 368
0cc7d26f
TT
369 if (argc > 2)
370 {
371 from = atoi (argv[argc - 2]);
372 to = atoi (argv[argc - 1]);
373 }
374 else
375 {
376 from = -1;
377 to = -1;
378 }
379
ddf0ea08
SM
380 const std::vector<varobj *> &children
381 = varobj_list_children (var, &from, &to);
382
112e8700 383 uiout->field_int ("numchild", to - from);
0cc7d26f 384 if (argc == 2 || argc == 4)
87967e27 385 print_values = mi_parse_print_values (argv[0]);
1ecb4ee0
DJ
386 else
387 print_values = PRINT_NO_VALUES;
fb40c209 388
9b972014 389 gdb::unique_xmalloc_ptr<char> display_hint = varobj_get_display_hint (var);
b6313243 390 if (display_hint)
112e8700 391 uiout->field_string ("displayhint", display_hint.get ());
b6313243 392
0cc7d26f 393 if (from < to)
fb40c209 394 {
c0470d48
TT
395 /* For historical reasons this might emit a list or a tuple, so
396 we construct one or the other. */
397 gdb::optional<ui_out_emit_tuple> tuple_emitter;
398 gdb::optional<ui_out_emit_list> list_emitter;
102040f0 399
0cc7d26f 400 if (mi_version (uiout) == 1)
c0470d48 401 tuple_emitter.emplace (uiout, "children");
0cc7d26f 402 else
c0470d48 403 list_emitter.emplace (uiout, "children");
ddf0ea08 404 for (int ix = from; ix < to && ix < children.size (); ix++)
0cc7d26f 405 {
2e783024 406 ui_out_emit_tuple child_emitter (uiout, "child");
102040f0 407
ddf0ea08 408 print_varobj (children[ix], print_values, 1 /* print expression */);
0cc7d26f 409 }
fb40c209 410 }
0cc7d26f 411
112e8700 412 uiout->field_int ("has_more", varobj_has_more (var, to));
fb40c209
AC
413}
414
ce8f13f8 415void
9f33b8b7 416mi_cmd_var_info_type (const char *command, char **argv, int argc)
fb40c209 417{
79a45e25 418 struct ui_out *uiout = current_uiout;
fb40c209
AC
419 struct varobj *var;
420
421 if (argc != 1)
1b05df00 422 error (_("-var-info-type: Usage: NAME."));
fb40c209 423
2b03b41d 424 /* Get varobj handle, if a valid var obj name was specified. */
fb40c209 425 var = varobj_get_handle (argv[0]);
afa269ae 426
2f408ecb 427 std::string type_name = varobj_get_type (var);
112e8700 428 uiout->field_string ("type", type_name.c_str ());
fb40c209
AC
429}
430
ce8f13f8 431void
9f33b8b7 432mi_cmd_var_info_path_expression (const char *command, char **argv, int argc)
02142340 433{
79a45e25 434 struct ui_out *uiout = current_uiout;
02142340 435 struct varobj *var;
02142340
VP
436
437 if (argc != 1)
438 error (_("Usage: NAME."));
439
440 /* Get varobj handle, if a valid var obj name was specified. */
441 var = varobj_get_handle (argv[0]);
02142340 442
2f408ecb 443 const char *path_expr = varobj_get_path_expr (var);
02142340 444
112e8700 445 uiout->field_string ("path_expr", path_expr);
02142340
VP
446}
447
ce8f13f8 448void
9f33b8b7 449mi_cmd_var_info_expression (const char *command, char **argv, int argc)
fb40c209 450{
79a45e25 451 struct ui_out *uiout = current_uiout;
fa4d0c40 452 const struct language_defn *lang;
fb40c209
AC
453 struct varobj *var;
454
455 if (argc != 1)
1b05df00 456 error (_("-var-info-expression: Usage: NAME."));
fb40c209 457
2b03b41d 458 /* Get varobj handle, if a valid var obj name was specified. */
fb40c209 459 var = varobj_get_handle (argv[0]);
fb40c209
AC
460
461 lang = varobj_get_language (var);
462
112e8700 463 uiout->field_string ("lang", lang->la_natural_name);
ca83fa81 464
2f408ecb 465 std::string exp = varobj_get_expression (var);
112e8700 466 uiout->field_string ("exp", exp.c_str ());
fb40c209
AC
467}
468
ce8f13f8 469void
9f33b8b7 470mi_cmd_var_show_attributes (const char *command, char **argv, int argc)
fb40c209 471{
79a45e25 472 struct ui_out *uiout = current_uiout;
fb40c209 473 int attr;
a121b7c1 474 const char *attstr;
fb40c209
AC
475 struct varobj *var;
476
477 if (argc != 1)
1b05df00 478 error (_("-var-show-attributes: Usage: NAME."));
fb40c209
AC
479
480 /* Get varobj handle, if a valid var obj name was specified */
481 var = varobj_get_handle (argv[0]);
fb40c209
AC
482
483 attr = varobj_get_attributes (var);
484 /* FIXME: define masks for attributes */
485 if (attr & 0x00000001)
486 attstr = "editable";
487 else
488 attstr = "noneditable";
489
112e8700 490 uiout->field_string ("attr", attstr);
fb40c209
AC
491}
492
ce8f13f8 493void
9f33b8b7 494mi_cmd_var_evaluate_expression (const char *command, char **argv, int argc)
fb40c209 495{
79a45e25 496 struct ui_out *uiout = current_uiout;
fb40c209
AC
497 struct varobj *var;
498
de051565
MK
499 enum varobj_display_formats format;
500 int formatFound;
54dc8297
AS
501 int oind;
502 char *oarg;
de051565
MK
503
504 enum opt
de051565 505 {
2b03b41d 506 OP_FORMAT
de051565 507 };
2b03b41d
SS
508 static const struct mi_opt opts[] =
509 {
510 {"f", OP_FORMAT, 1},
511 { 0, 0, 0 }
512 };
de051565 513
2b03b41d 514 /* Parse arguments. */
de051565
MK
515 format = FORMAT_NATURAL;
516 formatFound = 0;
54dc8297 517 oind = 0;
de051565
MK
518 while (1)
519 {
102040f0 520 int opt = mi_getopt ("-var-evaluate-expression", argc, argv,
54dc8297 521 opts, &oind, &oarg);
102040f0 522
de051565
MK
523 if (opt < 0)
524 break;
525 switch ((enum opt) opt)
2b03b41d 526 {
de051565
MK
527 case OP_FORMAT:
528 if (formatFound)
529 error (_("Cannot specify format more than once"));
530
54dc8297 531 format = mi_parse_format (oarg);
de051565
MK
532 formatFound = 1;
533 break;
2b03b41d 534 }
de051565 535 }
fb40c209 536
54dc8297 537 if (oind >= argc)
de051565
MK
538 error (_("Usage: [-f FORMAT] NAME"));
539
54dc8297 540 if (oind < argc - 1)
de051565
MK
541 error (_("Garbage at end of command"));
542
2b03b41d 543 /* Get varobj handle, if a valid var obj name was specified. */
54dc8297 544 var = varobj_get_handle (argv[oind]);
de051565
MK
545
546 if (formatFound)
0cc7d26f 547 {
2f408ecb 548 std::string val = varobj_get_formatted_value (var, format);
102040f0 549
112e8700 550 uiout->field_string ("value", val.c_str ());
0cc7d26f 551 }
de051565 552 else
0cc7d26f 553 {
2f408ecb 554 std::string val = varobj_get_value (var);
102040f0 555
112e8700 556 uiout->field_string ("value", val.c_str ());
0cc7d26f 557 }
fb40c209
AC
558}
559
ce8f13f8 560void
9f33b8b7 561mi_cmd_var_assign (const char *command, char **argv, int argc)
fb40c209 562{
79a45e25 563 struct ui_out *uiout = current_uiout;
fb40c209 564 struct varobj *var;
fb40c209
AC
565
566 if (argc != 2)
1b05df00 567 error (_("-var-assign: Usage: NAME EXPRESSION."));
fb40c209 568
2b03b41d 569 /* Get varobj handle, if a valid var obj name was specified. */
fb40c209 570 var = varobj_get_handle (argv[0]);
fb40c209 571
d2562500 572 if (!varobj_editable_p (var))
1b05df00 573 error (_("-var-assign: Variable object is not editable"));
fb40c209 574
2f408ecb 575 const char *expression = argv[1];
fb40c209 576
8de0566d
YQ
577 /* MI command '-var-assign' may write memory, so suppress memory
578 changed notification if it does. */
b7b633e9
TT
579 scoped_restore save_suppress
580 = make_scoped_restore (&mi_suppress_notification.memory, 1);
8de0566d 581
fb40c209 582 if (!varobj_set_value (var, expression))
1b05df00 583 error (_("-var-assign: Could not assign "
9a2b4c1b 584 "expression to variable object"));
fb40c209 585
2f408ecb 586 std::string val = varobj_get_value (var);
112e8700 587 uiout->field_string ("value", val.c_str ());
fb40c209
AC
588}
589
54333c3b
JK
590/* Type used for parameters passing to mi_cmd_var_update_iter. */
591
592struct mi_cmd_var_update
593 {
594 int only_floating;
595 enum print_values print_values;
596 };
597
598/* Helper for mi_cmd_var_update - update each VAR. */
599
600static void
601mi_cmd_var_update_iter (struct varobj *var, void *data_pointer)
602{
19ba03f4 603 struct mi_cmd_var_update *data = (struct mi_cmd_var_update *) data_pointer;
00431a78 604 bool thread_stopped;
54333c3b 605
00431a78 606 int thread_id = varobj_get_thread_id (var);
54333c3b 607
00431a78
PA
608 if (thread_id == -1)
609 {
610 thread_stopped = (inferior_ptid == null_ptid
611 || inferior_thread ()->state == THREAD_STOPPED);
612 }
54333c3b
JK
613 else
614 {
00431a78 615 thread_info *tp = find_thread_global_id (thread_id);
54333c3b 616
00431a78
PA
617 thread_stopped = (tp == NULL
618 || tp->state == THREAD_STOPPED);
54333c3b
JK
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.791834 seconds and 4 git commands to generate.