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