]>
Commit | Line | Data |
---|---|---|
fb40c209 | 1 | /* MI Command Set - varobj commands. |
4a94e368 | 2 | Copyright (C) 2000-2022 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 | 36 | static void varobj_update_one (struct varobj *var, |
2b03b41d | 37 | enum print_values print_values, |
4c37490d | 38 | bool is_explicit); |
fb40c209 | 39 | |
9a2b4c1b MS |
40 | static 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 |
47 | static void |
48 | print_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 | |
8dd8c8d4 | 59 | uiout->field_string ("exp", exp); |
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 | |
8dd8c8d4 | 67 | uiout->field_string ("value", val); |
0cc7d26f | 68 | } |
a217f3f5 | 69 | |
2f408ecb PA |
70 | std::string type = varobj_get_type (var); |
71 | if (!type.empty ()) | |
8dd8c8d4 | 72 | uiout->field_string ("type", type); |
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 | 91 | void |
9f33b8b7 | 92 | mi_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) | |
6cb06a8c TT |
128 | gdb_printf (gdb_stdlog, |
129 | "Name=\"%s\", Frame=\"%s\" (%s), Expression=\"%s\"\n", | |
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 | 142 | void |
9f33b8b7 | 143 | mi_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 | ||
189 | static enum varobj_display_formats | |
190 | mi_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 | 216 | void |
9f33b8b7 | 217 | mi_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); |
8dd8c8d4 | 239 | uiout->field_string ("value", val); |
fb40c209 AC |
240 | } |
241 | ||
b6313243 | 242 | void |
9f33b8b7 | 243 | mi_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 | 258 | void |
9f33b8b7 | 259 | mi_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 | 283 | void |
9f33b8b7 | 284 | mi_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 | 302 | void |
9f33b8b7 | 303 | mi_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 | |
320 | static int | |
0cc7d26f | 321 | mi_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. */ | |
78134374 SM |
343 | return (type->code () != TYPE_CODE_ARRAY |
344 | && type->code () != TYPE_CODE_STRUCT | |
345 | && type->code () != TYPE_CODE_UNION); | |
9265acad | 346 | } |
1ecb4ee0 DJ |
347 | } |
348 | ||
ce8f13f8 | 349 | void |
9f33b8b7 | 350 | mi_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 | 413 | void |
9f33b8b7 | 414 | mi_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); |
8dd8c8d4 | 426 | uiout->field_string ("type", type_name); |
fb40c209 AC |
427 | } |
428 | ||
ce8f13f8 | 429 | void |
9f33b8b7 | 430 | mi_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 | 446 | void |
9f33b8b7 | 447 | mi_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 | ||
6f7664a9 | 461 | uiout->field_string ("lang", lang->natural_name ()); |
ca83fa81 | 462 | |
2f408ecb | 463 | std::string exp = varobj_get_expression (var); |
8dd8c8d4 | 464 | uiout->field_string ("exp", exp); |
fb40c209 AC |
465 | } |
466 | ||
ce8f13f8 | 467 | void |
9f33b8b7 | 468 | mi_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 | 491 | void |
9f33b8b7 | 492 | mi_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 | |
8dd8c8d4 | 548 | uiout->field_string ("value", val); |
0cc7d26f | 549 | } |
de051565 | 550 | else |
0cc7d26f | 551 | { |
2f408ecb | 552 | std::string val = varobj_get_value (var); |
102040f0 | 553 | |
8dd8c8d4 | 554 | uiout->field_string ("value", val); |
0cc7d26f | 555 | } |
fb40c209 AC |
556 | } |
557 | ||
ce8f13f8 | 558 | void |
9f33b8b7 | 559 | mi_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); |
8dd8c8d4 | 585 | uiout->field_string ("value", val); |
fb40c209 AC |
586 | } |
587 | ||
54333c3b JK |
588 | /* Helper for mi_cmd_var_update - update each VAR. */ |
589 | ||
590 | static void | |
d8f168dd TT |
591 | mi_cmd_var_update_iter (struct varobj *var, bool only_floating, |
592 | enum print_values print_values) | |
54333c3b | 593 | { |
00431a78 | 594 | bool thread_stopped; |
54333c3b | 595 | |
00431a78 | 596 | int thread_id = varobj_get_thread_id (var); |
54333c3b | 597 | |
00431a78 PA |
598 | if (thread_id == -1) |
599 | { | |
600 | thread_stopped = (inferior_ptid == null_ptid | |
601 | || inferior_thread ()->state == THREAD_STOPPED); | |
602 | } | |
54333c3b JK |
603 | else |
604 | { | |
00431a78 | 605 | thread_info *tp = find_thread_global_id (thread_id); |
54333c3b | 606 | |
00431a78 PA |
607 | thread_stopped = (tp == NULL |
608 | || tp->state == THREAD_STOPPED); | |
54333c3b JK |
609 | } |
610 | ||
2b03b41d | 611 | if (thread_stopped |
d8f168dd TT |
612 | && (!only_floating || varobj_floating_p (var))) |
613 | varobj_update_one (var, print_values, false /* implicit */); | |
54333c3b JK |
614 | } |
615 | ||
ce8f13f8 | 616 | void |
9f33b8b7 | 617 | mi_cmd_var_update (const char *command, char **argv, int argc) |
fb40c209 | 618 | { |
79a45e25 | 619 | struct ui_out *uiout = current_uiout; |
fb40c209 | 620 | char *name; |
1ecb4ee0 | 621 | enum print_values print_values; |
fb40c209 | 622 | |
1ecb4ee0 | 623 | if (argc != 1 && argc != 2) |
1b05df00 | 624 | error (_("-var-update: Usage: [PRINT_VALUES] NAME.")); |
1ecb4ee0 DJ |
625 | |
626 | if (argc == 1) | |
627 | name = argv[0]; | |
628 | else | |
2b03b41d | 629 | name = argv[1]; |
fb40c209 | 630 | |
1ecb4ee0 | 631 | if (argc == 2) |
87967e27 | 632 | print_values = mi_parse_print_values (argv[0]); |
1ecb4ee0 DJ |
633 | else |
634 | print_values = PRINT_NO_VALUES; | |
fb40c209 | 635 | |
c0470d48 TT |
636 | /* For historical reasons this might emit a list or a tuple, so we |
637 | construct one or the other. */ | |
638 | gdb::optional<ui_out_emit_tuple> tuple_emitter; | |
639 | gdb::optional<ui_out_emit_list> list_emitter; | |
640 | ||
6e9ef2a8 | 641 | if (mi_version (uiout) <= 1) |
c0470d48 | 642 | tuple_emitter.emplace (uiout, "changelist"); |
6e9ef2a8 | 643 | else |
c0470d48 | 644 | list_emitter.emplace (uiout, "changelist"); |
6e9ef2a8 | 645 | |
2b03b41d SS |
646 | /* Check if the parameter is a "*", which means that we want to |
647 | update all variables. */ | |
fb40c209 | 648 | |
5a413362 | 649 | if ((*name == '*' || *name == '@') && (*(name + 1) == '\0')) |
fb40c209 | 650 | { |
2b03b41d SS |
651 | /* varobj_update_one automatically updates all the children of |
652 | VAROBJ. Therefore update each VAROBJ only once by iterating | |
653 | only the root VAROBJs. */ | |
54333c3b | 654 | |
d8f168dd TT |
655 | all_root_varobjs ([=] (varobj *var) |
656 | { | |
657 | mi_cmd_var_update_iter (var, *name == '0', print_values); | |
658 | }); | |
fb40c209 AC |
659 | } |
660 | else | |
661 | { | |
2b03b41d | 662 | /* Get varobj handle, if a valid var obj name was specified. */ |
6e9ef2a8 | 663 | struct varobj *var = varobj_get_handle (name); |
fb40c209 | 664 | |
4c37490d | 665 | varobj_update_one (var, print_values, true /* explicit */); |
fb40c209 | 666 | } |
fb40c209 AC |
667 | } |
668 | ||
8756216b | 669 | /* Helper for mi_cmd_var_update(). */ |
fb40c209 | 670 | |
8756216b | 671 | static void |
25d5ea92 | 672 | varobj_update_one (struct varobj *var, enum print_values print_values, |
4c37490d | 673 | bool is_explicit) |
fb40c209 | 674 | { |
79a45e25 | 675 | struct ui_out *uiout = current_uiout; |
0604393c SM |
676 | |
677 | std::vector<varobj_update_result> changes = varobj_update (&var, is_explicit); | |
73a93a32 | 678 | |
0604393c | 679 | for (const varobj_update_result &r : changes) |
fb40c209 | 680 | { |
0cc7d26f | 681 | int from, to; |
b6313243 | 682 | |
0092b74d | 683 | gdb::optional<ui_out_emit_tuple> tuple_emitter; |
3a387118 | 684 | if (mi_version (uiout) > 1) |
0092b74d | 685 | tuple_emitter.emplace (uiout, nullptr); |
0604393c | 686 | uiout->field_string ("name", varobj_get_objname (r.varobj)); |
8756216b | 687 | |
0604393c | 688 | switch (r.status) |
f7f9ae2c VP |
689 | { |
690 | case VAROBJ_IN_SCOPE: | |
0604393c | 691 | if (mi_print_value_p (r.varobj, print_values)) |
0cc7d26f | 692 | { |
0604393c | 693 | std::string val = varobj_get_value (r.varobj); |
102040f0 | 694 | |
8dd8c8d4 | 695 | uiout->field_string ("value", val); |
0cc7d26f | 696 | } |
112e8700 | 697 | uiout->field_string ("in_scope", "true"); |
f7f9ae2c | 698 | break; |
dda83cd7 SM |
699 | case VAROBJ_NOT_IN_SCOPE: |
700 | uiout->field_string ("in_scope", "false"); | |
8756216b | 701 | break; |
dda83cd7 SM |
702 | case VAROBJ_INVALID: |
703 | uiout->field_string ("in_scope", "invalid"); | |
24b21115 | 704 | break; |
f7f9ae2c VP |
705 | } |
706 | ||
0604393c | 707 | if (r.status != VAROBJ_INVALID) |
73a93a32 | 708 | { |
0604393c | 709 | if (r.type_changed) |
112e8700 | 710 | uiout->field_string ("type_changed", "true"); |
f7f9ae2c | 711 | else |
112e8700 | 712 | uiout->field_string ("type_changed", "false"); |
73a93a32 | 713 | } |
f7f9ae2c | 714 | |
0604393c | 715 | if (r.type_changed) |
afa269ae | 716 | { |
0604393c | 717 | std::string type_name = varobj_get_type (r.varobj); |
afa269ae | 718 | |
8dd8c8d4 | 719 | uiout->field_string ("new_type", type_name); |
afa269ae | 720 | } |
0cc7d26f | 721 | |
0604393c | 722 | if (r.type_changed || r.children_changed) |
381befee TT |
723 | uiout->field_signed ("new_num_children", |
724 | varobj_get_num_children (r.varobj)); | |
b6313243 | 725 | |
9b972014 | 726 | gdb::unique_xmalloc_ptr<char> display_hint |
0604393c | 727 | = varobj_get_display_hint (r.varobj); |
b6313243 | 728 | if (display_hint) |
112e8700 | 729 | uiout->field_string ("displayhint", display_hint.get ()); |
b6313243 | 730 | |
0604393c | 731 | if (varobj_is_dynamic_p (r.varobj)) |
381befee | 732 | uiout->field_signed ("dynamic", 1); |
b6313243 | 733 | |
0604393c | 734 | varobj_get_child_range (r.varobj, &from, &to); |
381befee | 735 | uiout->field_signed ("has_more", varobj_has_more (r.varobj, to)); |
b6313243 | 736 | |
0604393c | 737 | if (!r.newobj.empty ()) |
0cc7d26f | 738 | { |
10f489e5 | 739 | ui_out_emit_list list_emitter (uiout, "new_children"); |
0604393c SM |
740 | |
741 | for (varobj *child : r.newobj) | |
b6313243 | 742 | { |
b926417a | 743 | ui_out_emit_tuple inner_tuple_emitter (uiout, NULL); |
0cc7d26f | 744 | print_varobj (child, print_values, 1 /* print_expression */); |
b6313243 | 745 | } |
b6313243 | 746 | } |
fb40c209 | 747 | } |
fb40c209 | 748 | } |
0cc7d26f TT |
749 | |
750 | void | |
9f33b8b7 | 751 | mi_cmd_enable_pretty_printing (const char *command, char **argv, int argc) |
0cc7d26f TT |
752 | { |
753 | if (argc != 0) | |
1b05df00 | 754 | error (_("-enable-pretty-printing: no arguments allowed")); |
2b03b41d | 755 | |
0cc7d26f TT |
756 | varobj_enable_pretty_printing (); |
757 | } | |
758 | ||
759 | void | |
9f33b8b7 | 760 | mi_cmd_var_set_update_range (const char *command, char **argv, int argc) |
0cc7d26f TT |
761 | { |
762 | struct varobj *var; | |
763 | int from, to; | |
764 | ||
765 | if (argc != 3) | |
1b05df00 | 766 | error (_("-var-set-update-range: Usage: VAROBJ FROM TO")); |
0cc7d26f TT |
767 | |
768 | var = varobj_get_handle (argv[0]); | |
769 | from = atoi (argv[1]); | |
770 | to = atoi (argv[2]); | |
771 | ||
772 | varobj_set_child_range (var, from, to); | |
773 | } |