2006-11-28 Vladimir Prus <vladimir@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / mi / mi-cmd-var.c
1 /* MI Command Set - varobj commands.
2
3 Copyright (C) 2000, 2002, 2004, 2005 Free Software Foundation, Inc.
4
5 Contributed by Cygnus Solutions (a Red Hat company).
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
23
24 #include "defs.h"
25 #include "mi-cmds.h"
26 #include "ui-out.h"
27 #include "mi-out.h"
28 #include "varobj.h"
29 #include "value.h"
30 #include <ctype.h>
31 #include "gdb_string.h"
32
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
37 extern int varobjdebug; /* defined in varobj.c */
38
39 static int varobj_update_one (struct varobj *var,
40 enum print_values print_values);
41
42 /* VAROBJ operations */
43
44 enum mi_cmd_result
45 mi_cmd_var_create (char *command, char **argv, int argc)
46 {
47 CORE_ADDR frameaddr = 0;
48 struct varobj *var;
49 char *name;
50 char *frame;
51 char *expr;
52 char *type;
53 struct cleanup *old_cleanups;
54 enum varobj_type var_type;
55
56 if (argc != 3)
57 {
58 /* mi_error_message = xstrprintf ("mi_cmd_var_create: Usage:
59 ...."); return MI_CMD_ERROR; */
60 error (_("mi_cmd_var_create: Usage: NAME FRAME EXPRESSION."));
61 }
62
63 name = xstrdup (argv[0]);
64 /* Add cleanup for name. Must be free_current_contents as
65 name can be reallocated */
66 old_cleanups = make_cleanup (free_current_contents, &name);
67
68 frame = xstrdup (argv[1]);
69 make_cleanup (xfree, frame);
70
71 expr = xstrdup (argv[2]);
72 make_cleanup (xfree, expr);
73
74 if (strcmp (name, "-") == 0)
75 {
76 xfree (name);
77 name = varobj_gen_name ();
78 }
79 else if (!isalpha (*name))
80 error (_("mi_cmd_var_create: name of object must begin with a letter"));
81
82 if (strcmp (frame, "*") == 0)
83 var_type = USE_CURRENT_FRAME;
84 else if (strcmp (frame, "@") == 0)
85 var_type = USE_SELECTED_FRAME;
86 else
87 {
88 var_type = USE_SPECIFIED_FRAME;
89 frameaddr = string_to_core_addr (frame);
90 }
91
92 if (varobjdebug)
93 fprintf_unfiltered (gdb_stdlog,
94 "Name=\"%s\", Frame=\"%s\" (0x%s), Expression=\"%s\"\n",
95 name, frame, paddr (frameaddr), expr);
96
97 var = varobj_create (name, expr, frameaddr, var_type);
98
99 if (var == NULL)
100 error (_("mi_cmd_var_create: unable to create variable object"));
101
102 ui_out_field_string (uiout, "name", name);
103 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
104 type = varobj_get_type (var);
105 if (type == NULL)
106 ui_out_field_string (uiout, "type", "");
107 else
108 {
109 ui_out_field_string (uiout, "type", type);
110 xfree (type);
111 }
112
113 do_cleanups (old_cleanups);
114 return MI_CMD_DONE;
115 }
116
117 enum mi_cmd_result
118 mi_cmd_var_delete (char *command, char **argv, int argc)
119 {
120 char *name;
121 char *expr;
122 struct varobj *var;
123 int numdel;
124 int children_only_p = 0;
125 struct cleanup *old_cleanups;
126
127 if (argc < 1 || argc > 2)
128 error (_("mi_cmd_var_delete: Usage: [-c] EXPRESSION."));
129
130 name = xstrdup (argv[0]);
131 /* Add cleanup for name. Must be free_current_contents as
132 name can be reallocated */
133 old_cleanups = make_cleanup (free_current_contents, &name);
134
135 /* If we have one single argument it cannot be '-c' or any string
136 starting with '-'. */
137 if (argc == 1)
138 {
139 if (strcmp (name, "-c") == 0)
140 error (_("mi_cmd_var_delete: Missing required argument after '-c': variable object name"));
141 if (*name == '-')
142 error (_("mi_cmd_var_delete: Illegal variable object name"));
143 }
144
145 /* If we have 2 arguments they must be '-c' followed by a string
146 which would be the variable name. */
147 if (argc == 2)
148 {
149 expr = xstrdup (argv[1]);
150 if (strcmp (name, "-c") != 0)
151 error (_("mi_cmd_var_delete: Invalid option."));
152 children_only_p = 1;
153 xfree (name);
154 name = xstrdup (expr);
155 xfree (expr);
156 }
157
158 /* If we didn't error out, now NAME contains the name of the
159 variable. */
160
161 var = varobj_get_handle (name);
162
163 if (var == NULL)
164 error (_("mi_cmd_var_delete: Variable object not found."));
165
166 numdel = varobj_delete (var, NULL, children_only_p);
167
168 ui_out_field_int (uiout, "ndeleted", numdel);
169
170 do_cleanups (old_cleanups);
171 return MI_CMD_DONE;
172 }
173
174 enum mi_cmd_result
175 mi_cmd_var_set_format (char *command, char **argv, int argc)
176 {
177 enum varobj_display_formats format;
178 int len;
179 struct varobj *var;
180 char *formspec;
181
182 if (argc != 2)
183 error (_("mi_cmd_var_set_format: Usage: NAME FORMAT."));
184
185 /* Get varobj handle, if a valid var obj name was specified */
186 var = varobj_get_handle (argv[0]);
187
188 if (var == NULL)
189 error (_("mi_cmd_var_set_format: Variable object not found"));
190
191 formspec = xstrdup (argv[1]);
192 if (formspec == NULL)
193 error (_("mi_cmd_var_set_format: Must specify the format as: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
194
195 len = strlen (formspec);
196
197 if (strncmp (formspec, "natural", len) == 0)
198 format = FORMAT_NATURAL;
199 else if (strncmp (formspec, "binary", len) == 0)
200 format = FORMAT_BINARY;
201 else if (strncmp (formspec, "decimal", len) == 0)
202 format = FORMAT_DECIMAL;
203 else if (strncmp (formspec, "hexadecimal", len) == 0)
204 format = FORMAT_HEXADECIMAL;
205 else if (strncmp (formspec, "octal", len) == 0)
206 format = FORMAT_OCTAL;
207 else
208 error (_("mi_cmd_var_set_format: Unknown display format: must be: \"natural\", \"binary\", \"decimal\", \"hexadecimal\", or \"octal\""));
209
210 /* Set the format of VAR to given format */
211 varobj_set_display_format (var, format);
212
213 /* Report the new current format */
214 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
215 return MI_CMD_DONE;
216 }
217
218 enum mi_cmd_result
219 mi_cmd_var_show_format (char *command, char **argv, int argc)
220 {
221 enum varobj_display_formats format;
222 struct varobj *var;
223
224 if (argc != 1)
225 error (_("mi_cmd_var_show_format: Usage: NAME."));
226
227 /* Get varobj handle, if a valid var obj name was specified */
228 var = varobj_get_handle (argv[0]);
229 if (var == NULL)
230 error (_("mi_cmd_var_show_format: Variable object not found"));
231
232 format = varobj_get_display_format (var);
233
234 /* Report the current format */
235 ui_out_field_string (uiout, "format", varobj_format_string[(int) format]);
236 return MI_CMD_DONE;
237 }
238
239 enum mi_cmd_result
240 mi_cmd_var_info_num_children (char *command, char **argv, int argc)
241 {
242 struct varobj *var;
243
244 if (argc != 1)
245 error (_("mi_cmd_var_info_num_children: Usage: NAME."));
246
247 /* Get varobj handle, if a valid var obj name was specified */
248 var = varobj_get_handle (argv[0]);
249 if (var == NULL)
250 error (_("mi_cmd_var_info_num_children: Variable object not found"));
251
252 ui_out_field_int (uiout, "numchild", varobj_get_num_children (var));
253 return MI_CMD_DONE;
254 }
255
256 /* Parse a string argument into a print_values value. */
257
258 static enum print_values
259 mi_parse_values_option (const char *arg)
260 {
261 if (strcmp (arg, "0") == 0
262 || strcmp (arg, mi_no_values) == 0)
263 return PRINT_NO_VALUES;
264 else if (strcmp (arg, "1") == 0
265 || strcmp (arg, mi_all_values) == 0)
266 return PRINT_ALL_VALUES;
267 else if (strcmp (arg, "2") == 0
268 || strcmp (arg, mi_simple_values) == 0)
269 return PRINT_SIMPLE_VALUES;
270 else
271 error (_("Unknown value for PRINT_VALUES\n\
272 Must be: 0 or \"%s\", 1 or \"%s\", 2 or \"%s\""),
273 mi_no_values, mi_simple_values, mi_all_values);
274 }
275
276 /* Return 1 if given the argument PRINT_VALUES we should display
277 a value of type TYPE. */
278
279 static int
280 mi_print_value_p (struct type *type, enum print_values print_values)
281 {
282 if (type != NULL)
283 type = check_typedef (type);
284
285 if (print_values == PRINT_NO_VALUES)
286 return 0;
287
288 if (print_values == PRINT_ALL_VALUES)
289 return 1;
290
291 /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
292 and that type is not a compound type. */
293
294 return (TYPE_CODE (type) != TYPE_CODE_ARRAY
295 && TYPE_CODE (type) != TYPE_CODE_STRUCT
296 && TYPE_CODE (type) != TYPE_CODE_UNION);
297 }
298
299 enum mi_cmd_result
300 mi_cmd_var_list_children (char *command, char **argv, int argc)
301 {
302 struct varobj *var;
303 struct varobj **childlist;
304 struct varobj **cc;
305 struct cleanup *cleanup_children;
306 int numchild;
307 char *type;
308 enum print_values print_values;
309
310 if (argc != 1 && argc != 2)
311 error (_("mi_cmd_var_list_children: Usage: [PRINT_VALUES] NAME"));
312
313 /* Get varobj handle, if a valid var obj name was specified */
314 if (argc == 1)
315 var = varobj_get_handle (argv[0]);
316 else
317 var = varobj_get_handle (argv[1]);
318 if (var == NULL)
319 error (_("Variable object not found"));
320
321 numchild = varobj_list_children (var, &childlist);
322 ui_out_field_int (uiout, "numchild", numchild);
323 if (argc == 2)
324 print_values = mi_parse_values_option (argv[0]);
325 else
326 print_values = PRINT_NO_VALUES;
327
328 if (numchild <= 0)
329 return MI_CMD_DONE;
330
331 if (mi_version (uiout) == 1)
332 cleanup_children = make_cleanup_ui_out_tuple_begin_end (uiout, "children");
333 else
334 cleanup_children = make_cleanup_ui_out_list_begin_end (uiout, "children");
335 cc = childlist;
336 while (*cc != NULL)
337 {
338 struct cleanup *cleanup_child;
339 cleanup_child = make_cleanup_ui_out_tuple_begin_end (uiout, "child");
340 ui_out_field_string (uiout, "name", varobj_get_objname (*cc));
341 ui_out_field_string (uiout, "exp", varobj_get_expression (*cc));
342 ui_out_field_int (uiout, "numchild", varobj_get_num_children (*cc));
343 if (mi_print_value_p (varobj_get_gdb_type (*cc), print_values))
344 ui_out_field_string (uiout, "value", varobj_get_value (*cc));
345 type = varobj_get_type (*cc);
346 /* C++ pseudo-variables (public, private, protected) do not have a type */
347 if (type)
348 ui_out_field_string (uiout, "type", type);
349 do_cleanups (cleanup_child);
350 cc++;
351 }
352 do_cleanups (cleanup_children);
353 xfree (childlist);
354 return MI_CMD_DONE;
355 }
356
357 enum mi_cmd_result
358 mi_cmd_var_info_type (char *command, char **argv, int argc)
359 {
360 struct varobj *var;
361
362 if (argc != 1)
363 error (_("mi_cmd_var_info_type: Usage: NAME."));
364
365 /* Get varobj handle, if a valid var obj name was specified */
366 var = varobj_get_handle (argv[0]);
367 if (var == NULL)
368 error (_("mi_cmd_var_info_type: Variable object not found"));
369
370 ui_out_field_string (uiout, "type", varobj_get_type (var));
371 return MI_CMD_DONE;
372 }
373
374 enum mi_cmd_result
375 mi_cmd_var_info_expression (char *command, char **argv, int argc)
376 {
377 enum varobj_languages lang;
378 struct varobj *var;
379
380 if (argc != 1)
381 error (_("mi_cmd_var_info_expression: Usage: NAME."));
382
383 /* Get varobj handle, if a valid var obj name was specified */
384 var = varobj_get_handle (argv[0]);
385 if (var == NULL)
386 error (_("mi_cmd_var_info_expression: Variable object not found"));
387
388 lang = varobj_get_language (var);
389
390 ui_out_field_string (uiout, "lang", varobj_language_string[(int) lang]);
391 ui_out_field_string (uiout, "exp", varobj_get_expression (var));
392 return MI_CMD_DONE;
393 }
394
395 enum mi_cmd_result
396 mi_cmd_var_show_attributes (char *command, char **argv, int argc)
397 {
398 int attr;
399 char *attstr;
400 struct varobj *var;
401
402 if (argc != 1)
403 error (_("mi_cmd_var_show_attributes: Usage: NAME."));
404
405 /* Get varobj handle, if a valid var obj name was specified */
406 var = varobj_get_handle (argv[0]);
407 if (var == NULL)
408 error (_("mi_cmd_var_show_attributes: Variable object not found"));
409
410 attr = varobj_get_attributes (var);
411 /* FIXME: define masks for attributes */
412 if (attr & 0x00000001)
413 attstr = "editable";
414 else
415 attstr = "noneditable";
416
417 ui_out_field_string (uiout, "attr", attstr);
418 return MI_CMD_DONE;
419 }
420
421 enum mi_cmd_result
422 mi_cmd_var_evaluate_expression (char *command, char **argv, int argc)
423 {
424 struct varobj *var;
425
426 if (argc != 1)
427 error (_("mi_cmd_var_evaluate_expression: Usage: NAME."));
428
429 /* Get varobj handle, if a valid var obj name was specified */
430 var = varobj_get_handle (argv[0]);
431 if (var == NULL)
432 error (_("mi_cmd_var_evaluate_expression: Variable object not found"));
433
434 ui_out_field_string (uiout, "value", varobj_get_value (var));
435 return MI_CMD_DONE;
436 }
437
438 enum mi_cmd_result
439 mi_cmd_var_assign (char *command, char **argv, int argc)
440 {
441 struct varobj *var;
442 char *expression;
443
444 if (argc != 2)
445 error (_("mi_cmd_var_assign: Usage: NAME EXPRESSION."));
446
447 /* Get varobj handle, if a valid var obj name was specified */
448 var = varobj_get_handle (argv[0]);
449 if (var == NULL)
450 error (_("mi_cmd_var_assign: Variable object not found"));
451
452 /* FIXME: define masks for attributes */
453 if (!(varobj_get_attributes (var) & 0x00000001))
454 error (_("mi_cmd_var_assign: Variable object is not editable"));
455
456 expression = xstrdup (argv[1]);
457
458 if (!varobj_set_value (var, expression))
459 error (_("mi_cmd_var_assign: Could not assign expression to varible object"));
460
461 ui_out_field_string (uiout, "value", varobj_get_value (var));
462 return MI_CMD_DONE;
463 }
464
465 enum mi_cmd_result
466 mi_cmd_var_update (char *command, char **argv, int argc)
467 {
468 struct varobj *var;
469 struct varobj **rootlist;
470 struct varobj **cr;
471 struct cleanup *cleanup;
472 char *name;
473 int nv;
474 enum print_values print_values;
475
476 if (argc != 1 && argc != 2)
477 error (_("mi_cmd_var_update: Usage: [PRINT_VALUES] NAME."));
478
479 if (argc == 1)
480 name = argv[0];
481 else
482 name = (argv[1]);
483
484 if (argc == 2)
485 print_values = mi_parse_values_option (argv[0]);
486 else
487 print_values = PRINT_NO_VALUES;
488
489 /* Check if the parameter is a "*" which means that we want
490 to update all variables */
491
492 if ((*name == '*') && (*(name + 1) == '\0'))
493 {
494 nv = varobj_list (&rootlist);
495 if (mi_version (uiout) <= 1)
496 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
497 else
498 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
499 if (nv <= 0)
500 {
501 do_cleanups (cleanup);
502 return MI_CMD_DONE;
503 }
504 cr = rootlist;
505 while (*cr != NULL)
506 {
507 varobj_update_one (*cr, print_values);
508 cr++;
509 }
510 xfree (rootlist);
511 do_cleanups (cleanup);
512 }
513 else
514 {
515 /* Get varobj handle, if a valid var obj name was specified */
516 var = varobj_get_handle (name);
517 if (var == NULL)
518 error (_("mi_cmd_var_update: Variable object not found"));
519
520 if (mi_version (uiout) <= 1)
521 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, "changelist");
522 else
523 cleanup = make_cleanup_ui_out_list_begin_end (uiout, "changelist");
524 varobj_update_one (var, print_values);
525 do_cleanups (cleanup);
526 }
527 return MI_CMD_DONE;
528 }
529
530 /* Helper for mi_cmd_var_update() Returns 0 if the update for
531 the variable fails (usually because the variable is out of
532 scope), and 1 if it succeeds. */
533
534 static int
535 varobj_update_one (struct varobj *var, enum print_values print_values)
536 {
537 struct varobj **changelist;
538 struct varobj **cc;
539 struct cleanup *cleanup = NULL;
540 int nc;
541
542 nc = varobj_update (&var, &changelist);
543
544 /* nc == 0 means that nothing has changed.
545 nc == -1 means that an error occured in updating the variable.
546 nc == -2 means the variable has changed type. */
547
548 if (nc == 0)
549 return 1;
550 else if (nc == -1)
551 {
552 if (mi_version (uiout) > 1)
553 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
554 ui_out_field_string (uiout, "name", varobj_get_objname(var));
555 ui_out_field_string (uiout, "in_scope", "false");
556 if (mi_version (uiout) > 1)
557 do_cleanups (cleanup);
558 return -1;
559 }
560 else if (nc == -2)
561 {
562 if (mi_version (uiout) > 1)
563 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
564 ui_out_field_string (uiout, "name", varobj_get_objname (var));
565 ui_out_field_string (uiout, "in_scope", "true");
566 ui_out_field_string (uiout, "new_type", varobj_get_type(var));
567 ui_out_field_int (uiout, "new_num_children",
568 varobj_get_num_children(var));
569 if (mi_version (uiout) > 1)
570 do_cleanups (cleanup);
571 }
572 else
573 {
574
575 cc = changelist;
576 while (*cc != NULL)
577 {
578 if (mi_version (uiout) > 1)
579 cleanup = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
580 ui_out_field_string (uiout, "name", varobj_get_objname (*cc));
581 if (mi_print_value_p (varobj_get_gdb_type (*cc), print_values))
582 ui_out_field_string (uiout, "value", varobj_get_value (*cc));
583 ui_out_field_string (uiout, "in_scope", "true");
584 ui_out_field_string (uiout, "type_changed", "false");
585 if (mi_version (uiout) > 1)
586 do_cleanups (cleanup);
587 cc++;
588 }
589 xfree (changelist);
590 return 1;
591 }
592 return 1;
593 }
This page took 0.044903 seconds and 5 git commands to generate.