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