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