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