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