Commit | Line | Data |
---|---|---|
fb40c209 | 1 | /* MI Command Set - varobj commands. |
349c5d5f | 2 | |
9b254dd1 DJ |
3 | Copyright (C) 2000, 2002, 2004, 2005, 2007, 2008 |
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" |
fb40c209 | 32 | |
1ecb4ee0 DJ |
33 | const char mi_no_values[] = "--no-values"; |
34 | const char mi_simple_values[] = "--simple-values"; | |
35 | const char mi_all_values[] = "--all-values"; | |
36 | ||
8756216b | 37 | extern int varobjdebug; /* defined in varobj.c. */ |
fb40c209 | 38 | |
8756216b | 39 | static void varobj_update_one (struct varobj *var, |
25d5ea92 VP |
40 | enum print_values print_values, |
41 | int explicit); | |
fb40c209 | 42 | |
a217f3f5 VP |
43 | static int mi_print_value_p (struct type *type, enum print_values print_values); |
44 | ||
45 | /* Print variable object VAR. The PRINT_VALUES parameter controls | |
46 | if the value should be printed. The PRINT_EXPRESSION parameter | |
47 | controls if the expression should be printed. */ | |
48 | static void | |
49 | print_varobj (struct varobj *var, enum print_values print_values, | |
50 | int print_expression) | |
51 | { | |
bccc275a | 52 | struct type *gdb_type; |
a217f3f5 | 53 | char *type; |
c5b48eac | 54 | int thread_id; |
a217f3f5 VP |
55 | |
56 | ui_out_field_string (uiout, "name", varobj_get_objname (var)); | |
57 | if (print_expression) | |
58 | ui_out_field_string (uiout, "exp", varobj_get_expression (var)); | |
59 | ui_out_field_int (uiout, "numchild", varobj_get_num_children (var)); | |
60 | ||
9265acad | 61 | if (mi_print_value_p (varobj_get_gdb_type (var), print_values)) |
a217f3f5 VP |
62 | ui_out_field_string (uiout, "value", varobj_get_value (var)); |
63 | ||
64 | type = varobj_get_type (var); | |
65 | if (type != NULL) | |
66 | { | |
67 | ui_out_field_string (uiout, "type", type); | |
68 | xfree (type); | |
69 | } | |
25d5ea92 | 70 | |
c5b48eac VP |
71 | thread_id = varobj_get_thread_id (var); |
72 | if (thread_id > 0) | |
73 | ui_out_field_int (uiout, "thread-id", thread_id); | |
74 | ||
25d5ea92 VP |
75 | if (varobj_get_frozen (var)) |
76 | ui_out_field_int (uiout, "frozen", 1); | |
a217f3f5 VP |
77 | } |
78 | ||
fb40c209 AC |
79 | /* VAROBJ operations */ |
80 | ||
81 | enum mi_cmd_result | |
82 | mi_cmd_var_create (char *command, char **argv, int argc) | |
83 | { | |
73a93a32 | 84 | CORE_ADDR frameaddr = 0; |
fb40c209 AC |
85 | struct varobj *var; |
86 | char *name; | |
87 | char *frame; | |
88 | char *expr; | |
fb40c209 | 89 | struct cleanup *old_cleanups; |
73a93a32 | 90 | enum varobj_type var_type; |
fb40c209 AC |
91 | |
92 | if (argc != 3) | |
93 | { | |
c6902d46 AC |
94 | /* mi_error_message = xstrprintf ("mi_cmd_var_create: Usage: |
95 | ...."); return MI_CMD_ERROR; */ | |
8a3fe4f8 | 96 | error (_("mi_cmd_var_create: Usage: NAME FRAME EXPRESSION.")); |
fb40c209 AC |
97 | } |
98 | ||
99 | name = xstrdup (argv[0]); | |
100 | /* Add cleanup for name. Must be free_current_contents as | |
101 | name can be reallocated */ | |
47cf603e | 102 | old_cleanups = make_cleanup (free_current_contents, &name); |
fb40c209 AC |
103 | |
104 | frame = xstrdup (argv[1]); | |
51b57b6b | 105 | make_cleanup (xfree, frame); |
fb40c209 AC |
106 | |
107 | expr = xstrdup (argv[2]); | |
51b57b6b | 108 | make_cleanup (xfree, expr); |
fb40c209 AC |
109 | |
110 | if (strcmp (name, "-") == 0) | |
111 | { | |
b8c9b27d | 112 | xfree (name); |
fb40c209 AC |
113 | name = varobj_gen_name (); |
114 | } | |
115 | else if (!isalpha (*name)) | |
8a3fe4f8 | 116 | error (_("mi_cmd_var_create: name of object must begin with a letter")); |
fb40c209 AC |
117 | |
118 | if (strcmp (frame, "*") == 0) | |
73a93a32 JI |
119 | var_type = USE_CURRENT_FRAME; |
120 | else if (strcmp (frame, "@") == 0) | |
121 | var_type = USE_SELECTED_FRAME; | |
fb40c209 | 122 | else |
73a93a32 JI |
123 | { |
124 | var_type = USE_SPECIFIED_FRAME; | |
1bd34ded | 125 | frameaddr = string_to_core_addr (frame); |
73a93a32 | 126 | } |
fb40c209 AC |
127 | |
128 | if (varobjdebug) | |
129 | fprintf_unfiltered (gdb_stdlog, | |
130 | "Name=\"%s\", Frame=\"%s\" (0x%s), Expression=\"%s\"\n", | |
131 | name, frame, paddr (frameaddr), expr); | |
132 | ||
73a93a32 | 133 | var = varobj_create (name, expr, frameaddr, var_type); |
fb40c209 AC |
134 | |
135 | if (var == NULL) | |
8a3fe4f8 | 136 | error (_("mi_cmd_var_create: unable to create variable object")); |
fb40c209 | 137 | |
224e4ca7 | 138 | print_varobj (var, PRINT_ALL_VALUES, 0 /* don't print expression */); |
fb40c209 AC |
139 | |
140 | do_cleanups (old_cleanups); | |
141 | return MI_CMD_DONE; | |
142 | } | |
143 | ||
144 | enum mi_cmd_result | |
145 | mi_cmd_var_delete (char *command, char **argv, int argc) | |
146 | { | |
147 | char *name; | |
fb40c209 AC |
148 | struct varobj *var; |
149 | int numdel; | |
150 | int children_only_p = 0; | |
151 | struct cleanup *old_cleanups; | |
152 | ||
153 | if (argc < 1 || argc > 2) | |
8a3fe4f8 | 154 | error (_("mi_cmd_var_delete: Usage: [-c] EXPRESSION.")); |
fb40c209 AC |
155 | |
156 | name = xstrdup (argv[0]); | |
157 | /* Add cleanup for name. Must be free_current_contents as | |
158 | name can be reallocated */ | |
47cf603e | 159 | old_cleanups = make_cleanup (free_current_contents, &name); |
fb40c209 AC |
160 | |
161 | /* If we have one single argument it cannot be '-c' or any string | |
162 | starting with '-'. */ | |
163 | if (argc == 1) | |
164 | { | |
165 | if (strcmp (name, "-c") == 0) | |
8a3fe4f8 | 166 | error (_("mi_cmd_var_delete: Missing required argument after '-c': variable object name")); |
fb40c209 | 167 | if (*name == '-') |
8a3fe4f8 | 168 | error (_("mi_cmd_var_delete: Illegal variable object name")); |
fb40c209 AC |
169 | } |
170 | ||
171 | /* If we have 2 arguments they must be '-c' followed by a string | |
172 | which would be the variable name. */ | |
173 | if (argc == 2) | |
174 | { | |
fb40c209 | 175 | if (strcmp (name, "-c") != 0) |
8a3fe4f8 | 176 | error (_("mi_cmd_var_delete: Invalid option.")); |
fb40c209 | 177 | children_only_p = 1; |
474d0d0c MS |
178 | do_cleanups (old_cleanups); |
179 | name = xstrdup (argv[1]); | |
180 | make_cleanup (free_current_contents, &name); | |
fb40c209 AC |
181 | } |
182 | ||
183 | /* If we didn't error out, now NAME contains the name of the | |
184 | variable. */ | |
185 | ||
186 | var = varobj_get_handle (name); | |
187 | ||
188 | if (var == NULL) | |
8a3fe4f8 | 189 | error (_("mi_cmd_var_delete: Variable object not found.")); |
fb40c209 AC |
190 | |
191 | numdel = varobj_delete (var, NULL, children_only_p); | |
192 | ||
193 | ui_out_field_int (uiout, "ndeleted", numdel); | |
194 | ||
195 | do_cleanups (old_cleanups); | |
196 | return MI_CMD_DONE; | |
197 | } | |
198 | ||
de051565 MK |
199 | /* Parse a string argument into a format value. */ |
200 | ||
201 | static enum varobj_display_formats | |
202 | mi_parse_format (const char *arg) | |
203 | { | |
204 | if (arg != NULL) | |
205 | { | |
206 | int len; | |
207 | ||
208 | len = strlen (arg); | |
209 | ||
210 | if (strncmp (arg, "natural", len) == 0) | |
211 | return FORMAT_NATURAL; | |
212 | else if (strncmp (arg, "binary", len) == 0) | |
213 | return FORMAT_BINARY; | |
214 | else if (strncmp (arg, "decimal", len) == 0) | |
215 | return FORMAT_DECIMAL; | |
216 | else if (strncmp (arg, "hexadecimal", len) == 0) | |
217 | return FORMAT_HEXADECIMAL; | |
218 | else if (strncmp (arg, "octal", len) == 0) | |
219 | return FORMAT_OCTAL; | |
220 | } | |
221 | ||
222 | error (_("Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\"")); | |
223 | } | |
224 | ||
fb40c209 AC |
225 | enum mi_cmd_result |
226 | mi_cmd_var_set_format (char *command, char **argv, int argc) | |
227 | { | |
228 | enum varobj_display_formats format; | |
fb40c209 | 229 | struct varobj *var; |
fb40c209 AC |
230 | |
231 | if (argc != 2) | |
8a3fe4f8 | 232 | error (_("mi_cmd_var_set_format: Usage: NAME FORMAT.")); |
fb40c209 AC |
233 | |
234 | /* Get varobj handle, if a valid var obj name was specified */ | |
235 | var = varobj_get_handle (argv[0]); | |
236 | ||
237 | if (var == NULL) | |
8a3fe4f8 | 238 | error (_("mi_cmd_var_set_format: Variable object not found")); |
fb40c209 | 239 | |
de051565 MK |
240 | format = mi_parse_format (argv[1]); |
241 | ||
fb40c209 AC |
242 | /* Set the format of VAR to given format */ |
243 | varobj_set_display_format (var, format); | |
244 | ||
245 | /* Report the new current format */ | |
246 | ui_out_field_string (uiout, "format", varobj_format_string[(int) format]); | |
00ee6348 NR |
247 | |
248 | /* Report the value in the new format */ | |
249 | ui_out_field_string (uiout, "value", varobj_get_value (var)); | |
fb40c209 AC |
250 | return MI_CMD_DONE; |
251 | } | |
252 | ||
25d5ea92 VP |
253 | enum mi_cmd_result |
254 | mi_cmd_var_set_frozen (char *command, char **argv, int argc) | |
255 | { | |
256 | struct varobj *var; | |
257 | int frozen; | |
258 | ||
259 | if (argc != 2) | |
260 | error (_("-var-set-format: Usage: NAME FROZEN_FLAG.")); | |
261 | ||
262 | var = varobj_get_handle (argv[0]); | |
263 | if (var == NULL) | |
264 | error (_("Variable object not found")); | |
265 | ||
266 | if (strcmp (argv[1], "0") == 0) | |
267 | frozen = 0; | |
268 | else if (strcmp (argv[1], "1") == 0) | |
269 | frozen = 1; | |
270 | else | |
271 | error (_("Invalid flag value")); | |
272 | ||
273 | varobj_set_frozen (var, frozen); | |
274 | ||
275 | /* We don't automatically return the new value, or what varobjs got new | |
276 | values during unfreezing. If this information is required, client | |
277 | should call -var-update explicitly. */ | |
278 | return MI_CMD_DONE; | |
279 | } | |
280 | ||
281 | ||
fb40c209 AC |
282 | enum mi_cmd_result |
283 | mi_cmd_var_show_format (char *command, char **argv, int argc) | |
284 | { | |
285 | enum varobj_display_formats format; | |
286 | struct varobj *var; | |
287 | ||
288 | if (argc != 1) | |
8a3fe4f8 | 289 | error (_("mi_cmd_var_show_format: Usage: NAME.")); |
fb40c209 AC |
290 | |
291 | /* Get varobj handle, if a valid var obj name was specified */ | |
292 | var = varobj_get_handle (argv[0]); | |
293 | if (var == NULL) | |
8a3fe4f8 | 294 | error (_("mi_cmd_var_show_format: Variable object not found")); |
fb40c209 AC |
295 | |
296 | format = varobj_get_display_format (var); | |
297 | ||
298 | /* Report the current format */ | |
299 | ui_out_field_string (uiout, "format", varobj_format_string[(int) format]); | |
300 | return MI_CMD_DONE; | |
301 | } | |
302 | ||
303 | enum mi_cmd_result | |
304 | mi_cmd_var_info_num_children (char *command, char **argv, int argc) | |
305 | { | |
306 | struct varobj *var; | |
307 | ||
308 | if (argc != 1) | |
8a3fe4f8 | 309 | error (_("mi_cmd_var_info_num_children: Usage: NAME.")); |
fb40c209 AC |
310 | |
311 | /* Get varobj handle, if a valid var obj name was specified */ | |
312 | var = varobj_get_handle (argv[0]); | |
313 | if (var == NULL) | |
8a3fe4f8 | 314 | error (_("mi_cmd_var_info_num_children: Variable object not found")); |
fb40c209 AC |
315 | |
316 | ui_out_field_int (uiout, "numchild", varobj_get_num_children (var)); | |
317 | return MI_CMD_DONE; | |
318 | } | |
319 | ||
1ecb4ee0 DJ |
320 | /* Parse a string argument into a print_values value. */ |
321 | ||
322 | static enum print_values | |
323 | mi_parse_values_option (const char *arg) | |
324 | { | |
325 | if (strcmp (arg, "0") == 0 | |
326 | || strcmp (arg, mi_no_values) == 0) | |
327 | return PRINT_NO_VALUES; | |
328 | else if (strcmp (arg, "1") == 0 | |
329 | || strcmp (arg, mi_all_values) == 0) | |
330 | return PRINT_ALL_VALUES; | |
331 | else if (strcmp (arg, "2") == 0 | |
332 | || strcmp (arg, mi_simple_values) == 0) | |
333 | return PRINT_SIMPLE_VALUES; | |
334 | else | |
335 | error (_("Unknown value for PRINT_VALUES\n\ | |
336 | Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""), | |
337 | mi_no_values, mi_simple_values, mi_all_values); | |
338 | } | |
339 | ||
340 | /* Return 1 if given the argument PRINT_VALUES we should display | |
341 | a value of type TYPE. */ | |
342 | ||
343 | static int | |
344 | mi_print_value_p (struct type *type, enum print_values print_values) | |
345 | { | |
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 | ||
9265acad NR |
353 | if (type == NULL) |
354 | return 1; | |
355 | else | |
356 | { | |
357 | type = check_typedef (type); | |
1ecb4ee0 | 358 | |
9265acad NR |
359 | /* For PRINT_SIMPLE_VALUES, only print the value if it has a type |
360 | and that type is not a compound type. */ | |
361 | return (TYPE_CODE (type) != TYPE_CODE_ARRAY | |
362 | && TYPE_CODE (type) != TYPE_CODE_STRUCT | |
363 | && TYPE_CODE (type) != TYPE_CODE_UNION); | |
364 | } | |
1ecb4ee0 DJ |
365 | } |
366 | ||
fb40c209 AC |
367 | enum mi_cmd_result |
368 | mi_cmd_var_list_children (char *command, char **argv, int argc) | |
369 | { | |
d56d46f5 VP |
370 | struct varobj *var; |
371 | VEC(varobj_p) *children; | |
372 | struct varobj *child; | |
6ad4a2cf | 373 | struct cleanup *cleanup_children; |
fb40c209 | 374 | int numchild; |
c9e1f0fc | 375 | enum print_values print_values; |
d56d46f5 | 376 | int ix; |
fb40c209 | 377 | |
c9e1f0fc | 378 | if (argc != 1 && argc != 2) |
8a3fe4f8 | 379 | error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME")); |
fb40c209 AC |
380 | |
381 | /* Get varobj handle, if a valid var obj name was specified */ | |
1ecb4ee0 DJ |
382 | if (argc == 1) |
383 | var = varobj_get_handle (argv[0]); | |
384 | else | |
385 | var = varobj_get_handle (argv[1]); | |
fb40c209 | 386 | if (var == NULL) |
8a3fe4f8 | 387 | error (_("Variable object not found")); |
fb40c209 | 388 | |
d56d46f5 VP |
389 | children = varobj_list_children (var); |
390 | ui_out_field_int (uiout, "numchild", VEC_length (varobj_p, children)); | |
c9e1f0fc | 391 | if (argc == 2) |
1ecb4ee0 DJ |
392 | print_values = mi_parse_values_option (argv[0]); |
393 | else | |
394 | print_values = PRINT_NO_VALUES; | |
fb40c209 | 395 | |
d56d46f5 VP |
396 | if (VEC_length (varobj_p, children) == 0) |
397 | return MI_CMD_DONE; | |
fb40c209 | 398 | |
e7494ffb AC |
399 | if (mi_version (uiout) == 1) |
400 | cleanup_children = make_cleanup_ui_out_tuple_begin_end (uiout, "children"); | |
401 | else | |
402 | cleanup_children = make_cleanup_ui_out_list_begin_end (uiout, "children"); | |
d56d46f5 | 403 | for (ix = 0; VEC_iterate (varobj_p, children, ix, child); ++ix) |
fb40c209 | 404 | { |
6ad4a2cf JJ |
405 | struct cleanup *cleanup_child; |
406 | cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, "child"); | |
d56d46f5 | 407 | print_varobj (child, print_values, 1 /* print expression */); |
a217f3f5 | 408 | do_cleanups (cleanup_child); |
fb40c209 | 409 | } |
6ad4a2cf | 410 | do_cleanups (cleanup_children); |
fb40c209 AC |
411 | return MI_CMD_DONE; |
412 | } | |
413 | ||
414 | enum mi_cmd_result | |
415 | mi_cmd_var_info_type (char *command, char **argv, int argc) | |
416 | { | |
417 | struct varobj *var; | |
418 | ||
419 | if (argc != 1) | |
8a3fe4f8 | 420 | error (_("mi_cmd_var_info_type: Usage: NAME.")); |
fb40c209 AC |
421 | |
422 | /* Get varobj handle, if a valid var obj name was specified */ | |
423 | var = varobj_get_handle (argv[0]); | |
424 | if (var == NULL) | |
8a3fe4f8 | 425 | error (_("mi_cmd_var_info_type: Variable object not found")); |
fb40c209 AC |
426 | |
427 | ui_out_field_string (uiout, "type", varobj_get_type (var)); | |
428 | return MI_CMD_DONE; | |
429 | } | |
430 | ||
02142340 VP |
431 | enum mi_cmd_result |
432 | mi_cmd_var_info_path_expression (char *command, char **argv, int argc) | |
433 | { | |
434 | struct varobj *var; | |
435 | char *path_expr; | |
436 | ||
437 | if (argc != 1) | |
438 | error (_("Usage: NAME.")); | |
439 | ||
440 | /* Get varobj handle, if a valid var obj name was specified. */ | |
441 | var = varobj_get_handle (argv[0]); | |
442 | if (var == NULL) | |
443 | error (_("Variable object not found")); | |
444 | ||
445 | path_expr = varobj_get_path_expr (var); | |
446 | ||
447 | ui_out_field_string (uiout, "path_expr", path_expr); | |
448 | ||
449 | return MI_CMD_DONE; | |
450 | } | |
451 | ||
fb40c209 AC |
452 | enum mi_cmd_result |
453 | mi_cmd_var_info_expression (char *command, char **argv, int argc) | |
454 | { | |
455 | enum varobj_languages lang; | |
456 | struct varobj *var; | |
457 | ||
458 | if (argc != 1) | |
8a3fe4f8 | 459 | error (_("mi_cmd_var_info_expression: Usage: NAME.")); |
fb40c209 AC |
460 | |
461 | /* Get varobj handle, if a valid var obj name was specified */ | |
462 | var = varobj_get_handle (argv[0]); | |
463 | if (var == NULL) | |
8a3fe4f8 | 464 | error (_("mi_cmd_var_info_expression: Variable object not found")); |
fb40c209 AC |
465 | |
466 | lang = varobj_get_language (var); | |
467 | ||
468 | ui_out_field_string (uiout, "lang", varobj_language_string[(int) lang]); | |
469 | ui_out_field_string (uiout, "exp", varobj_get_expression (var)); | |
470 | return MI_CMD_DONE; | |
471 | } | |
472 | ||
473 | enum mi_cmd_result | |
474 | mi_cmd_var_show_attributes (char *command, char **argv, int argc) | |
475 | { | |
476 | int attr; | |
477 | char *attstr; | |
478 | struct varobj *var; | |
479 | ||
480 | if (argc != 1) | |
8a3fe4f8 | 481 | error (_("mi_cmd_var_show_attributes: Usage: NAME.")); |
fb40c209 AC |
482 | |
483 | /* Get varobj handle, if a valid var obj name was specified */ | |
484 | var = varobj_get_handle (argv[0]); | |
485 | if (var == NULL) | |
8a3fe4f8 | 486 | error (_("mi_cmd_var_show_attributes: Variable object not found")); |
fb40c209 AC |
487 | |
488 | attr = varobj_get_attributes (var); | |
489 | /* FIXME: define masks for attributes */ | |
490 | if (attr & 0x00000001) | |
491 | attstr = "editable"; | |
492 | else | |
493 | attstr = "noneditable"; | |
494 | ||
495 | ui_out_field_string (uiout, "attr", attstr); | |
496 | return MI_CMD_DONE; | |
497 | } | |
498 | ||
499 | enum mi_cmd_result | |
500 | mi_cmd_var_evaluate_expression (char *command, char **argv, int argc) | |
501 | { | |
502 | struct varobj *var; | |
503 | ||
de051565 MK |
504 | enum varobj_display_formats format; |
505 | int formatFound; | |
506 | int optind; | |
507 | char *optarg; | |
508 | ||
509 | enum opt | |
510 | { | |
511 | OP_FORMAT | |
512 | }; | |
513 | static struct mi_opt opts[] = | |
514 | { | |
515 | {"f", OP_FORMAT, 1}, | |
516 | { 0, 0, 0 } | |
517 | }; | |
518 | ||
519 | /* Parse arguments */ | |
520 | format = FORMAT_NATURAL; | |
521 | formatFound = 0; | |
522 | optind = 0; | |
523 | while (1) | |
524 | { | |
525 | int opt = mi_getopt ("-var-evaluate-expression", argc, argv, opts, &optind, &optarg); | |
526 | if (opt < 0) | |
527 | break; | |
528 | switch ((enum opt) opt) | |
529 | { | |
530 | case OP_FORMAT: | |
531 | if (formatFound) | |
532 | error (_("Cannot specify format more than once")); | |
533 | ||
534 | format = mi_parse_format (optarg); | |
535 | formatFound = 1; | |
536 | break; | |
537 | } | |
538 | } | |
fb40c209 | 539 | |
de051565 MK |
540 | if (optind >= argc) |
541 | error (_("Usage: [-f FORMAT] NAME")); | |
542 | ||
543 | if (optind < argc - 1) | |
544 | error (_("Garbage at end of command")); | |
545 | ||
546 | /* Get varobj handle, if a valid var obj name was specified */ | |
547 | var = varobj_get_handle (argv[optind]); | |
fb40c209 | 548 | if (var == NULL) |
de051565 MK |
549 | error (_("Variable object not found")); |
550 | ||
551 | if (formatFound) | |
552 | ui_out_field_string (uiout, "value", varobj_get_formatted_value (var, format)); | |
553 | else | |
554 | ui_out_field_string (uiout, "value", varobj_get_value (var)); | |
fb40c209 | 555 | |
fb40c209 AC |
556 | return MI_CMD_DONE; |
557 | } | |
558 | ||
559 | enum mi_cmd_result | |
560 | mi_cmd_var_assign (char *command, char **argv, int argc) | |
561 | { | |
562 | struct varobj *var; | |
563 | char *expression; | |
564 | ||
565 | if (argc != 2) | |
8a3fe4f8 | 566 | error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION.")); |
fb40c209 AC |
567 | |
568 | /* Get varobj handle, if a valid var obj name was specified */ | |
569 | var = varobj_get_handle (argv[0]); | |
570 | if (var == NULL) | |
8a3fe4f8 | 571 | error (_("mi_cmd_var_assign: Variable object not found")); |
fb40c209 | 572 | |
d2562500 | 573 | if (!varobj_editable_p (var)) |
8a3fe4f8 | 574 | error (_("mi_cmd_var_assign: Variable object is not editable")); |
fb40c209 AC |
575 | |
576 | expression = xstrdup (argv[1]); | |
577 | ||
578 | if (!varobj_set_value (var, expression)) | |
98a29c7e | 579 | error (_("mi_cmd_var_assign: Could not assign expression to variable object")); |
fb40c209 AC |
580 | |
581 | ui_out_field_string (uiout, "value", varobj_get_value (var)); | |
582 | return MI_CMD_DONE; | |
583 | } | |
584 | ||
585 | enum mi_cmd_result | |
586 | mi_cmd_var_update (char *command, char **argv, int argc) | |
587 | { | |
588 | struct varobj *var; | |
589 | struct varobj **rootlist; | |
590 | struct varobj **cr; | |
3a387118 | 591 | struct cleanup *cleanup; |
fb40c209 AC |
592 | char *name; |
593 | int nv; | |
1ecb4ee0 | 594 | enum print_values print_values; |
fb40c209 | 595 | |
1ecb4ee0 DJ |
596 | if (argc != 1 && argc != 2) |
597 | error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME.")); | |
598 | ||
599 | if (argc == 1) | |
600 | name = argv[0]; | |
601 | else | |
602 | name = (argv[1]); | |
fb40c209 | 603 | |
1ecb4ee0 DJ |
604 | if (argc == 2) |
605 | print_values = mi_parse_values_option (argv[0]); | |
606 | else | |
607 | print_values = PRINT_NO_VALUES; | |
fb40c209 AC |
608 | |
609 | /* Check if the parameter is a "*" which means that we want | |
610 | to update all variables */ | |
611 | ||
5a413362 | 612 | if ((*name == '*' || *name == '@') && (*(name + 1) == '\0')) |
fb40c209 AC |
613 | { |
614 | nv = varobj_list (&rootlist); | |
db9a518b | 615 | cleanup = make_cleanup (xfree, rootlist); |
3a387118 | 616 | if (mi_version (uiout) <= 1) |
db9a518b | 617 | make_cleanup_ui_out_tuple_begin_end (uiout, "changelist"); |
3a387118 | 618 | else |
db9a518b | 619 | make_cleanup_ui_out_list_begin_end (uiout, "changelist"); |
fb40c209 AC |
620 | if (nv <= 0) |
621 | { | |
3a387118 | 622 | do_cleanups (cleanup); |
fb40c209 AC |
623 | return MI_CMD_DONE; |
624 | } | |
625 | cr = rootlist; | |
626 | while (*cr != NULL) | |
627 | { | |
5a413362 VP |
628 | if (*name == '*' || varobj_floating_p (*cr)) |
629 | varobj_update_one (*cr, print_values, 0 /* implicit */); | |
fb40c209 AC |
630 | cr++; |
631 | } | |
3a387118 | 632 | do_cleanups (cleanup); |
fb40c209 AC |
633 | } |
634 | else | |
635 | { | |
636 | /* Get varobj handle, if a valid var obj name was specified */ | |
637 | var = varobj_get_handle (name); | |
638 | if (var == NULL) | |
8a3fe4f8 | 639 | error (_("mi_cmd_var_update: Variable object not found")); |
fb40c209 | 640 | |
3a387118 JJ |
641 | if (mi_version (uiout) <= 1) |
642 | cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist"); | |
643 | else | |
644 | cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist"); | |
25d5ea92 | 645 | varobj_update_one (var, print_values, 1 /* explicit */); |
3a387118 | 646 | do_cleanups (cleanup); |
fb40c209 | 647 | } |
73a93a32 | 648 | return MI_CMD_DONE; |
fb40c209 AC |
649 | } |
650 | ||
8756216b | 651 | /* Helper for mi_cmd_var_update(). */ |
fb40c209 | 652 | |
8756216b | 653 | static void |
25d5ea92 VP |
654 | varobj_update_one (struct varobj *var, enum print_values print_values, |
655 | int explicit) | |
fb40c209 AC |
656 | { |
657 | struct varobj **changelist; | |
658 | struct varobj **cc; | |
3a387118 | 659 | struct cleanup *cleanup = NULL; |
fb40c209 AC |
660 | int nc; |
661 | ||
25d5ea92 | 662 | nc = varobj_update (&var, &changelist, explicit); |
fb40c209 | 663 | |
8756216b DP |
664 | /* nc >= 0 represents the number of changes reported into changelist. |
665 | nc < 0 means that an error occured or the the variable has | |
666 | changed type (TYPE_CHANGED). */ | |
73a93a32 JI |
667 | |
668 | if (nc == 0) | |
8756216b DP |
669 | return; |
670 | else if (nc < 0) | |
fb40c209 | 671 | { |
3a387118 JJ |
672 | if (mi_version (uiout) > 1) |
673 | cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); | |
73a93a32 | 674 | ui_out_field_string (uiout, "name", varobj_get_objname(var)); |
8756216b DP |
675 | |
676 | switch (nc) | |
677 | { | |
678 | case NOT_IN_SCOPE: | |
8756216b DP |
679 | ui_out_field_string (uiout, "in_scope", "false"); |
680 | break; | |
681 | case INVALID: | |
682 | ui_out_field_string (uiout, "in_scope", "invalid"); | |
683 | break; | |
684 | case TYPE_CHANGED: | |
685 | ui_out_field_string (uiout, "in_scope", "true"); | |
686 | ui_out_field_string (uiout, "new_type", varobj_get_type(var)); | |
687 | ui_out_field_int (uiout, "new_num_children", | |
688 | varobj_get_num_children(var)); | |
689 | break; | |
690 | } | |
3a387118 JJ |
691 | if (mi_version (uiout) > 1) |
692 | do_cleanups (cleanup); | |
73a93a32 JI |
693 | } |
694 | else | |
695 | { | |
73a93a32 JI |
696 | cc = changelist; |
697 | while (*cc != NULL) | |
698 | { | |
3a387118 JJ |
699 | if (mi_version (uiout) > 1) |
700 | cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL); | |
73a93a32 | 701 | ui_out_field_string (uiout, "name", varobj_get_objname (*cc)); |
1ecb4ee0 DJ |
702 | if (mi_print_value_p (varobj_get_gdb_type (*cc), print_values)) |
703 | ui_out_field_string (uiout, "value", varobj_get_value (*cc)); | |
73a93a32 JI |
704 | ui_out_field_string (uiout, "in_scope", "true"); |
705 | ui_out_field_string (uiout, "type_changed", "false"); | |
3a387118 JJ |
706 | if (mi_version (uiout) > 1) |
707 | do_cleanups (cleanup); | |
73a93a32 JI |
708 | cc++; |
709 | } | |
b8c9b27d | 710 | xfree (changelist); |
fb40c209 | 711 | } |
fb40c209 | 712 | } |