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