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