Update copyright year range in all GDB files.
[external/binutils.git] / gdb / mi / mi-cmd-var.c
1 /* MI Command Set - varobj commands.
2    Copyright (C) 2000-2019 Free Software Foundation, Inc.
3
4    Contributed by Cygnus Solutions (a Red Hat company).
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
10    the Free Software Foundation; either version 3 of the License, or
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
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "mi-cmds.h"
23 #include "mi-main.h"
24 #include "ui-out.h"
25 #include "mi-out.h"
26 #include "varobj.h"
27 #include "language.h"
28 #include "value.h"
29 #include <ctype.h>
30 #include "mi-getopt.h"
31 #include "gdbthread.h"
32 #include "mi-parse.h"
33 #include "common/gdb_optional.h"
34 #include "inferior.h"
35
36 extern unsigned int varobjdebug;                /* defined in varobj.c.  */
37
38 static void varobj_update_one (struct varobj *var,
39                                enum print_values print_values,
40                                bool is_explicit);
41
42 static int mi_print_value_p (struct varobj *var,
43                              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
49 static void 
50 print_varobj (struct varobj *var, enum print_values print_values,
51               int print_expression)
52 {
53   struct ui_out *uiout = current_uiout;
54   int thread_id;
55
56   uiout->field_string ("name", varobj_get_objname (var));
57   if (print_expression)
58     {
59       std::string exp = varobj_get_expression (var);
60
61       uiout->field_string ("exp", exp.c_str ());
62     }
63   uiout->field_int ("numchild", varobj_get_num_children (var));
64   
65   if (mi_print_value_p (var, print_values))
66     {
67       std::string val = varobj_get_value (var);
68
69       uiout->field_string ("value", val.c_str ());
70     }
71
72   std::string type = varobj_get_type (var);
73   if (!type.empty ())
74     uiout->field_string ("type", type.c_str ());
75
76   thread_id = varobj_get_thread_id (var);
77   if (thread_id > 0)
78     uiout->field_int ("thread-id", thread_id);
79
80   if (varobj_get_frozen (var))
81     uiout->field_int ("frozen", 1);
82
83   gdb::unique_xmalloc_ptr<char> display_hint = varobj_get_display_hint (var);
84   if (display_hint)
85     uiout->field_string ("displayhint", display_hint.get ());
86
87   if (varobj_is_dynamic_p (var))
88     uiout->field_int ("dynamic", 1);
89 }
90
91 /* VAROBJ operations */
92
93 void
94 mi_cmd_var_create (const char *command, char **argv, int argc)
95 {
96   struct ui_out *uiout = current_uiout;
97   CORE_ADDR frameaddr = 0;
98   struct varobj *var;
99   char *frame;
100   char *expr;
101   enum varobj_type var_type;
102
103   if (argc != 3)
104     error (_("-var-create: Usage: NAME FRAME EXPRESSION."));
105
106   frame = argv[1];
107   expr = argv[2];
108
109   const char *name = argv[0];
110   std::string gen_name;
111   if (strcmp (name, "-") == 0)
112     {
113       gen_name = varobj_gen_name ();
114       name = gen_name.c_str ();
115     }
116   else if (!isalpha (name[0]))
117     error (_("-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\" (%s), Expression=\"%s\"\n",
132                         name, frame, hex_string (frameaddr), expr);
133
134   var = varobj_create (name, expr, frameaddr, var_type);
135
136   if (var == NULL)
137     error (_("-var-create: unable to create variable object"));
138
139   print_varobj (var, PRINT_ALL_VALUES, 0 /* don't print expression */);
140
141   uiout->field_int ("has_more", varobj_has_more (var, 0));
142 }
143
144 void
145 mi_cmd_var_delete (const 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 ui_out *uiout = current_uiout;
152
153   if (argc < 1 || argc > 2)
154     error (_("-var-delete: Usage: [-c] EXPRESSION."));
155
156   name = argv[0];
157
158   /* If we have one single argument it cannot be '-c' or any string
159      starting with '-'.  */
160   if (argc == 1)
161     {
162       if (strcmp (name, "-c") == 0)
163         error (_("-var-delete: Missing required "
164                  "argument after '-c': variable object name"));
165       if (*name == '-')
166         error (_("-var-delete: Illegal variable object name"));
167     }
168
169   /* If we have 2 arguments they must be '-c' followed by a string
170      which would be the variable name.  */
171   if (argc == 2)
172     {
173       if (strcmp (name, "-c") != 0)
174         error (_("-var-delete: Invalid option."));
175       children_only_p = 1;
176       name = argv[1];
177     }
178
179   /* If we didn't error out, now NAME contains the name of the
180      variable.  */
181
182   var = varobj_get_handle (name);
183
184   numdel = varobj_delete (var, children_only_p);
185
186   uiout->field_int ("ndeleted", numdel);
187 }
188
189 /* Parse a string argument into a format value.  */
190
191 static enum varobj_display_formats
192 mi_parse_format (const char *arg)
193 {
194   if (arg != NULL)
195     {
196       int len;
197
198       len = strlen (arg);
199
200       if (strncmp (arg, "natural", len) == 0)
201         return FORMAT_NATURAL;
202       else if (strncmp (arg, "binary", len) == 0)
203         return FORMAT_BINARY;
204       else if (strncmp (arg, "decimal", len) == 0)
205         return FORMAT_DECIMAL;
206       else if (strncmp (arg, "hexadecimal", len) == 0)
207         return FORMAT_HEXADECIMAL;
208       else if (strncmp (arg, "octal", len) == 0)
209         return FORMAT_OCTAL;
210       else if (strncmp (arg, "zero-hexadecimal", len) == 0)
211         return FORMAT_ZHEXADECIMAL;
212     }
213
214   error (_("Must specify the format as: \"natural\", "
215            "\"binary\", \"decimal\", \"hexadecimal\", \"octal\" or \"zero-hexadecimal\""));
216 }
217
218 void
219 mi_cmd_var_set_format (const char *command, char **argv, int argc)
220 {
221   enum varobj_display_formats format;
222   struct varobj *var;
223   struct ui_out *uiout = current_uiout;
224
225   if (argc != 2)
226     error (_("-var-set-format: Usage: NAME FORMAT."));
227
228   /* Get varobj handle, if a valid var obj name was specified.  */
229   var = varobj_get_handle (argv[0]);
230
231   format = mi_parse_format (argv[1]);
232   
233   /* Set the format of VAR to the given format.  */
234   varobj_set_display_format (var, format);
235
236   /* Report the new current format.  */
237   uiout->field_string ("format", varobj_format_string[(int) format]);
238  
239   /* Report the value in the new format.  */
240   std::string val = varobj_get_value (var);
241   uiout->field_string ("value", val.c_str ());
242 }
243
244 void
245 mi_cmd_var_set_visualizer (const char *command, char **argv, int argc)
246 {
247   struct varobj *var;
248
249   if (argc != 2)
250     error (_("Usage: NAME VISUALIZER_FUNCTION."));
251
252   var = varobj_get_handle (argv[0]);
253
254   if (var == NULL)
255     error (_("Variable object not found"));
256
257   varobj_set_visualizer (var, argv[1]);
258 }
259
260 void
261 mi_cmd_var_set_frozen (const char *command, char **argv, int argc)
262 {
263   struct varobj *var;
264   bool frozen;
265
266   if (argc != 2)
267     error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
268
269   var = varobj_get_handle (argv[0]);
270
271   if (strcmp (argv[1], "0") == 0)
272     frozen = false;
273   else if (strcmp (argv[1], "1") == 0)
274     frozen = true;
275   else
276     error (_("Invalid flag value"));
277
278   varobj_set_frozen (var, frozen);
279
280   /* We don't automatically return the new value, or what varobjs got
281      new values during unfreezing.  If this information is required,
282      client should call -var-update explicitly.  */
283 }
284
285 void
286 mi_cmd_var_show_format (const char *command, char **argv, int argc)
287 {
288   struct ui_out *uiout = current_uiout;
289   enum varobj_display_formats format;
290   struct varobj *var;
291
292   if (argc != 1)
293     error (_("-var-show-format: Usage: NAME."));
294
295   /* Get varobj handle, if a valid var obj name was specified.  */
296   var = varobj_get_handle (argv[0]);
297
298   format = varobj_get_display_format (var);
299
300   /* Report the current format.  */
301   uiout->field_string ("format", varobj_format_string[(int) format]);
302 }
303
304 void
305 mi_cmd_var_info_num_children (const char *command, char **argv, int argc)
306 {
307   struct ui_out *uiout = current_uiout;
308   struct varobj *var;
309
310   if (argc != 1)
311     error (_("-var-info-num-children: Usage: NAME."));
312
313   /* Get varobj handle, if a valid var obj name was specified.  */
314   var = varobj_get_handle (argv[0]);
315
316   uiout->field_int ("numchild", varobj_get_num_children (var));
317 }
318
319 /* Return 1 if given the argument PRINT_VALUES we should display
320    the varobj VAR.  */
321
322 static int
323 mi_print_value_p (struct varobj *var, enum print_values print_values)
324 {
325   struct type *type;
326
327   if (print_values == PRINT_NO_VALUES)
328     return 0;
329
330   if (print_values == PRINT_ALL_VALUES)
331     return 1;
332
333   if (varobj_is_dynamic_p (var))
334     return 1;
335
336   type = varobj_get_gdb_type (var);
337   if (type == NULL)
338     return 1;
339   else
340     {
341       type = check_typedef (type);
342
343       /* For PRINT_SIMPLE_VALUES, only print the value if it has a type
344          and that type is not a compound type.  */
345       return (TYPE_CODE (type) != TYPE_CODE_ARRAY
346               && TYPE_CODE (type) != TYPE_CODE_STRUCT
347               && TYPE_CODE (type) != TYPE_CODE_UNION);
348     }
349 }
350
351 void
352 mi_cmd_var_list_children (const char *command, char **argv, int argc)
353 {
354   struct ui_out *uiout = current_uiout;
355   struct varobj *var;  
356   enum print_values print_values;
357   int from, to;
358
359   if (argc < 1 || argc > 4)
360     error (_("-var-list-children: Usage: "
361              "[PRINT_VALUES] NAME [FROM TO]"));
362
363   /* Get varobj handle, if a valid var obj name was specified.  */
364   if (argc == 1 || argc == 3)
365     var = varobj_get_handle (argv[0]);
366   else
367     var = varobj_get_handle (argv[1]);
368
369   if (argc > 2)
370     {
371       from = atoi (argv[argc - 2]);
372       to = atoi (argv[argc - 1]);
373     }
374   else
375     {
376       from = -1;
377       to = -1;
378     }
379
380   const std::vector<varobj *> &children
381     = varobj_list_children (var, &from, &to);
382
383   uiout->field_int ("numchild", to - from);
384   if (argc == 2 || argc == 4)
385     print_values = mi_parse_print_values (argv[0]);
386   else
387     print_values = PRINT_NO_VALUES;
388
389   gdb::unique_xmalloc_ptr<char> display_hint = varobj_get_display_hint (var);
390   if (display_hint)
391     uiout->field_string ("displayhint", display_hint.get ());
392
393   if (from < to)
394     {
395       /* For historical reasons this might emit a list or a tuple, so
396          we construct one or the other.  */
397       gdb::optional<ui_out_emit_tuple> tuple_emitter;
398       gdb::optional<ui_out_emit_list> list_emitter;
399
400       if (mi_version (uiout) == 1)
401         tuple_emitter.emplace (uiout, "children");
402       else
403         list_emitter.emplace (uiout, "children");
404       for (int ix = from; ix < to && ix < children.size (); ix++)
405         {
406           ui_out_emit_tuple child_emitter (uiout, "child");
407
408           print_varobj (children[ix], print_values, 1 /* print expression */);
409         }
410     }
411
412   uiout->field_int ("has_more", varobj_has_more (var, to));
413 }
414
415 void
416 mi_cmd_var_info_type (const char *command, char **argv, int argc)
417 {
418   struct ui_out *uiout = current_uiout;
419   struct varobj *var;
420
421   if (argc != 1)
422     error (_("-var-info-type: Usage: NAME."));
423
424   /* Get varobj handle, if a valid var obj name was specified.  */
425   var = varobj_get_handle (argv[0]);
426
427   std::string type_name = varobj_get_type (var);
428   uiout->field_string ("type", type_name.c_str ());
429 }
430
431 void
432 mi_cmd_var_info_path_expression (const char *command, char **argv, int argc)
433 {
434   struct ui_out *uiout = current_uiout;
435   struct varobj *var;
436
437   if (argc != 1)
438     error (_("Usage: NAME."));
439
440   /* Get varobj handle, if a valid var obj name was specified.  */
441   var = varobj_get_handle (argv[0]);
442   
443   const char *path_expr = varobj_get_path_expr (var);
444
445   uiout->field_string ("path_expr", path_expr);
446 }
447
448 void
449 mi_cmd_var_info_expression (const char *command, char **argv, int argc)
450 {
451   struct ui_out *uiout = current_uiout;
452   const struct language_defn *lang;
453   struct varobj *var;
454
455   if (argc != 1)
456     error (_("-var-info-expression: Usage: NAME."));
457
458   /* Get varobj handle, if a valid var obj name was specified.  */
459   var = varobj_get_handle (argv[0]);
460
461   lang = varobj_get_language (var);
462
463   uiout->field_string ("lang", lang->la_natural_name);
464
465   std::string exp = varobj_get_expression (var);
466   uiout->field_string ("exp", exp.c_str ());
467 }
468
469 void
470 mi_cmd_var_show_attributes (const char *command, char **argv, int argc)
471 {
472   struct ui_out *uiout = current_uiout;
473   int attr;
474   const char *attstr;
475   struct varobj *var;
476
477   if (argc != 1)
478     error (_("-var-show-attributes: Usage: NAME."));
479
480   /* Get varobj handle, if a valid var obj name was specified */
481   var = varobj_get_handle (argv[0]);
482
483   attr = varobj_get_attributes (var);
484   /* FIXME: define masks for attributes */
485   if (attr & 0x00000001)
486     attstr = "editable";
487   else
488     attstr = "noneditable";
489
490   uiout->field_string ("attr", attstr);
491 }
492
493 void
494 mi_cmd_var_evaluate_expression (const char *command, char **argv, int argc)
495 {
496   struct ui_out *uiout = current_uiout;
497   struct varobj *var;
498
499   enum varobj_display_formats format;
500   int formatFound;
501   int oind;
502   char *oarg;
503     
504   enum opt
505   {
506     OP_FORMAT
507   };
508   static const struct mi_opt opts[] =
509     {
510       {"f", OP_FORMAT, 1},
511       { 0, 0, 0 }
512     };
513
514   /* Parse arguments.  */
515   format = FORMAT_NATURAL;
516   formatFound = 0;
517   oind = 0;
518   while (1)
519     {
520       int opt = mi_getopt ("-var-evaluate-expression", argc, argv,
521                            opts, &oind, &oarg);
522
523       if (opt < 0)
524         break;
525       switch ((enum opt) opt)
526         {
527         case OP_FORMAT:
528           if (formatFound)
529             error (_("Cannot specify format more than once"));
530    
531           format = mi_parse_format (oarg);
532           formatFound = 1;
533           break;
534         }
535     }
536
537   if (oind >= argc)
538     error (_("Usage: [-f FORMAT] NAME"));
539    
540   if (oind < argc - 1)
541     error (_("Garbage at end of command"));
542  
543   /* Get varobj handle, if a valid var obj name was specified.  */
544   var = varobj_get_handle (argv[oind]);
545    
546   if (formatFound)
547     {
548       std::string val = varobj_get_formatted_value (var, format);
549
550       uiout->field_string ("value", val.c_str ());
551     }
552   else
553     {
554       std::string val = varobj_get_value (var);
555
556       uiout->field_string ("value", val.c_str ());
557     }
558 }
559
560 void
561 mi_cmd_var_assign (const char *command, char **argv, int argc)
562 {
563   struct ui_out *uiout = current_uiout;
564   struct varobj *var;
565
566   if (argc != 2)
567     error (_("-var-assign: Usage: NAME EXPRESSION."));
568
569   /* Get varobj handle, if a valid var obj name was specified.  */
570   var = varobj_get_handle (argv[0]);
571
572   if (!varobj_editable_p (var))
573     error (_("-var-assign: Variable object is not editable"));
574
575   const char *expression = argv[1];
576
577   /* MI command '-var-assign' may write memory, so suppress memory
578      changed notification if it does.  */
579   scoped_restore save_suppress
580     = make_scoped_restore (&mi_suppress_notification.memory, 1);
581
582   if (!varobj_set_value (var, expression))
583     error (_("-var-assign: Could not assign "
584              "expression to variable object"));
585
586   std::string val = varobj_get_value (var);
587   uiout->field_string ("value", val.c_str ());
588 }
589
590 /* Type used for parameters passing to mi_cmd_var_update_iter.  */
591
592 struct mi_cmd_var_update
593   {
594     int only_floating;
595     enum print_values print_values;
596   };
597
598 /* Helper for mi_cmd_var_update - update each VAR.  */
599
600 static void
601 mi_cmd_var_update_iter (struct varobj *var, void *data_pointer)
602 {
603   struct mi_cmd_var_update *data = (struct mi_cmd_var_update *) data_pointer;
604   bool thread_stopped;
605
606   int thread_id = varobj_get_thread_id (var);
607
608   if (thread_id == -1)
609     {
610       thread_stopped = (inferior_ptid == null_ptid
611                         || inferior_thread ()->state == THREAD_STOPPED);
612     }
613   else
614     {
615       thread_info *tp = find_thread_global_id (thread_id);
616
617       thread_stopped = (tp == NULL
618                         || tp->state == THREAD_STOPPED);
619     }
620
621   if (thread_stopped
622       && (!data->only_floating || varobj_floating_p (var)))
623     varobj_update_one (var, data->print_values, false /* implicit */);
624 }
625
626 void
627 mi_cmd_var_update (const char *command, char **argv, int argc)
628 {
629   struct ui_out *uiout = current_uiout;
630   char *name;
631   enum print_values print_values;
632
633   if (argc != 1 && argc != 2)
634     error (_("-var-update: Usage: [PRINT_VALUES] NAME."));
635
636   if (argc == 1)
637     name = argv[0];
638   else
639     name = argv[1];
640
641   if (argc == 2)
642     print_values = mi_parse_print_values (argv[0]);
643   else
644     print_values = PRINT_NO_VALUES;
645
646   /* For historical reasons this might emit a list or a tuple, so we
647      construct one or the other.  */
648   gdb::optional<ui_out_emit_tuple> tuple_emitter;
649   gdb::optional<ui_out_emit_list> list_emitter;
650
651   if (mi_version (uiout) <= 1)
652     tuple_emitter.emplace (uiout, "changelist");
653   else
654     list_emitter.emplace (uiout, "changelist");
655
656   /* Check if the parameter is a "*", which means that we want to
657      update all variables.  */
658
659   if ((*name == '*' || *name == '@') && (*(name + 1) == '\0'))
660     {
661       struct mi_cmd_var_update data;
662
663       data.only_floating = (*name == '@');
664       data.print_values = print_values;
665
666       /* varobj_update_one automatically updates all the children of
667          VAROBJ.  Therefore update each VAROBJ only once by iterating
668          only the root VAROBJs.  */
669
670       all_root_varobjs (mi_cmd_var_update_iter, &data);
671     }
672   else
673     {
674       /* Get varobj handle, if a valid var obj name was specified.  */
675       struct varobj *var = varobj_get_handle (name);
676
677       varobj_update_one (var, print_values, true /* explicit */);
678     }
679 }
680
681 /* Helper for mi_cmd_var_update().  */
682
683 static void
684 varobj_update_one (struct varobj *var, enum print_values print_values,
685                    bool is_explicit)
686 {
687   struct ui_out *uiout = current_uiout;
688
689   std::vector<varobj_update_result> changes = varobj_update (&var, is_explicit);
690   
691   for (const varobj_update_result &r : changes)
692     {
693       int from, to;
694
695       gdb::optional<ui_out_emit_tuple> tuple_emitter;
696       if (mi_version (uiout) > 1)
697         tuple_emitter.emplace (uiout, nullptr);
698       uiout->field_string ("name", varobj_get_objname (r.varobj));
699
700       switch (r.status)
701         {
702         case VAROBJ_IN_SCOPE:
703           if (mi_print_value_p (r.varobj, print_values))
704             {
705               std::string val = varobj_get_value (r.varobj);
706
707               uiout->field_string ("value", val.c_str ());
708             }
709           uiout->field_string ("in_scope", "true");
710           break;
711         case VAROBJ_NOT_IN_SCOPE:
712           uiout->field_string ("in_scope", "false");
713           break;
714         case VAROBJ_INVALID:
715           uiout->field_string ("in_scope", "invalid");
716           break;
717         }
718
719       if (r.status != VAROBJ_INVALID)
720         {
721           if (r.type_changed)
722             uiout->field_string ("type_changed", "true");
723           else
724             uiout->field_string ("type_changed", "false");
725         }
726
727       if (r.type_changed)
728         {
729           std::string type_name = varobj_get_type (r.varobj);
730
731           uiout->field_string ("new_type", type_name.c_str ());
732         }
733
734       if (r.type_changed || r.children_changed)
735         uiout->field_int ("new_num_children",
736                           varobj_get_num_children (r.varobj));
737
738       gdb::unique_xmalloc_ptr<char> display_hint
739         = varobj_get_display_hint (r.varobj);
740       if (display_hint)
741         uiout->field_string ("displayhint", display_hint.get ());
742
743       if (varobj_is_dynamic_p (r.varobj))
744         uiout->field_int ("dynamic", 1);
745
746       varobj_get_child_range (r.varobj, &from, &to);
747       uiout->field_int ("has_more", varobj_has_more (r.varobj, to));
748
749       if (!r.newobj.empty ())
750         {
751           ui_out_emit_list list_emitter (uiout, "new_children");
752
753           for (varobj *child : r.newobj)
754             {
755               ui_out_emit_tuple inner_tuple_emitter (uiout, NULL);
756               print_varobj (child, print_values, 1 /* print_expression */);
757             }
758         }
759     }
760 }
761
762 void
763 mi_cmd_enable_pretty_printing (const char *command, char **argv, int argc)
764 {
765   if (argc != 0)
766     error (_("-enable-pretty-printing: no arguments allowed"));
767
768   varobj_enable_pretty_printing ();
769 }
770
771 void
772 mi_cmd_var_set_update_range (const char *command, char **argv, int argc)
773 {
774   struct varobj *var;
775   int from, to;
776
777   if (argc != 3)
778     error (_("-var-set-update-range: Usage: VAROBJ FROM TO"));
779   
780   var = varobj_get_handle (argv[0]);
781   from = atoi (argv[1]);
782   to = atoi (argv[2]);
783
784   varobj_set_child_range (var, from, to);
785 }