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