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