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