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