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