Replace VEC (varobj_update_result) with std::vector
[external/binutils.git] / gdb / mi / mi-cmd-var.c
1 /* MI Command Set - varobj commands.
2    Copyright (C) 2000-2017 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
35 extern unsigned int varobjdebug;                /* defined in varobj.c.  */
36
37 static void varobj_update_one (struct varobj *var,
38                                enum print_values print_values,
39                                int is_explicit);
40
41 static int mi_print_value_p (struct varobj *var,
42                              enum print_values print_values);
43
44 /* Print variable object VAR.  The PRINT_VALUES parameter controls
45    if the value should be printed.  The PRINT_EXPRESSION parameter
46    controls if the expression should be printed.  */
47
48 static void 
49 print_varobj (struct varobj *var, enum print_values print_values,
50               int print_expression)
51 {
52   struct ui_out *uiout = current_uiout;
53   int thread_id;
54
55   uiout->field_string ("name", varobj_get_objname (var));
56   if (print_expression)
57     {
58       std::string exp = varobj_get_expression (var);
59
60       uiout->field_string ("exp", exp.c_str ());
61     }
62   uiout->field_int ("numchild", varobj_get_num_children (var));
63   
64   if (mi_print_value_p (var, print_values))
65     {
66       std::string val = varobj_get_value (var);
67
68       uiout->field_string ("value", val.c_str ());
69     }
70
71   std::string type = varobj_get_type (var);
72   if (!type.empty ())
73     uiout->field_string ("type", type.c_str ());
74
75   thread_id = varobj_get_thread_id (var);
76   if (thread_id > 0)
77     uiout->field_int ("thread-id", thread_id);
78
79   if (varobj_get_frozen (var))
80     uiout->field_int ("frozen", 1);
81
82   gdb::unique_xmalloc_ptr<char> display_hint = varobj_get_display_hint (var);
83   if (display_hint)
84     uiout->field_string ("displayhint", display_hint.get ());
85
86   if (varobj_is_dynamic_p (var))
87     uiout->field_int ("dynamic", 1);
88 }
89
90 /* VAROBJ operations */
91
92 void
93 mi_cmd_var_create (const char *command, char **argv, int argc)
94 {
95   struct ui_out *uiout = current_uiout;
96   CORE_ADDR frameaddr = 0;
97   struct varobj *var;
98   char *frame;
99   char *expr;
100   enum varobj_type var_type;
101
102   if (argc != 3)
103     error (_("-var-create: Usage: NAME FRAME EXPRESSION."));
104
105   frame = argv[1];
106   expr = argv[2];
107
108   const char *name = argv[0];
109   std::string gen_name;
110   if (strcmp (name, "-") == 0)
111     {
112       gen_name = varobj_gen_name ();
113       name = gen_name.c_str ();
114     }
115   else if (!isalpha (name[0]))
116     error (_("-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\" (%s), Expression=\"%s\"\n",
131                         name, frame, hex_string (frameaddr), expr);
132
133   var = varobj_create (name, expr, frameaddr, var_type);
134
135   if (var == NULL)
136     error (_("-var-create: unable to create variable object"));
137
138   print_varobj (var, PRINT_ALL_VALUES, 0 /* don't print expression */);
139
140   uiout->field_int ("has_more", varobj_has_more (var, 0));
141 }
142
143 void
144 mi_cmd_var_delete (const 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 ui_out *uiout = current_uiout;
151
152   if (argc < 1 || argc > 2)
153     error (_("-var-delete: Usage: [-c] EXPRESSION."));
154
155   name = argv[0];
156
157   /* If we have one single argument it cannot be '-c' or any string
158      starting with '-'.  */
159   if (argc == 1)
160     {
161       if (strcmp (name, "-c") == 0)
162         error (_("-var-delete: Missing required "
163                  "argument after '-c': variable object name"));
164       if (*name == '-')
165         error (_("-var-delete: Illegal variable object name"));
166     }
167
168   /* If we have 2 arguments they must be '-c' followed by a string
169      which would be the variable name.  */
170   if (argc == 2)
171     {
172       if (strcmp (name, "-c") != 0)
173         error (_("-var-delete: Invalid option."));
174       children_only_p = 1;
175       name = argv[1];
176     }
177
178   /* If we didn't error out, now NAME contains the name of the
179      variable.  */
180
181   var = varobj_get_handle (name);
182
183   numdel = varobj_delete (var, children_only_p);
184
185   uiout->field_int ("ndeleted", numdel);
186 }
187
188 /* Parse a string argument into a format value.  */
189
190 static enum varobj_display_formats
191 mi_parse_format (const char *arg)
192 {
193   if (arg != NULL)
194     {
195       int len;
196
197       len = strlen (arg);
198
199       if (strncmp (arg, "natural", len) == 0)
200         return FORMAT_NATURAL;
201       else if (strncmp (arg, "binary", len) == 0)
202         return FORMAT_BINARY;
203       else if (strncmp (arg, "decimal", len) == 0)
204         return FORMAT_DECIMAL;
205       else if (strncmp (arg, "hexadecimal", len) == 0)
206         return FORMAT_HEXADECIMAL;
207       else if (strncmp (arg, "octal", len) == 0)
208         return FORMAT_OCTAL;
209       else if (strncmp (arg, "zero-hexadecimal", len) == 0)
210         return FORMAT_ZHEXADECIMAL;
211     }
212
213   error (_("Must specify the format as: \"natural\", "
214            "\"binary\", \"decimal\", \"hexadecimal\", \"octal\" or \"zero-hexadecimal\""));
215 }
216
217 void
218 mi_cmd_var_set_format (const char *command, char **argv, int argc)
219 {
220   enum varobj_display_formats format;
221   struct varobj *var;
222   struct ui_out *uiout = current_uiout;
223
224   if (argc != 2)
225     error (_("-var-set-format: Usage: NAME FORMAT."));
226
227   /* Get varobj handle, if a valid var obj name was specified.  */
228   var = varobj_get_handle (argv[0]);
229
230   format = mi_parse_format (argv[1]);
231   
232   /* Set the format of VAR to the given format.  */
233   varobj_set_display_format (var, format);
234
235   /* Report the new current format.  */
236   uiout->field_string ("format", varobj_format_string[(int) format]);
237  
238   /* Report the value in the new format.  */
239   std::string val = varobj_get_value (var);
240   uiout->field_string ("value", val.c_str ());
241 }
242
243 void
244 mi_cmd_var_set_visualizer (const char *command, char **argv, int argc)
245 {
246   struct varobj *var;
247
248   if (argc != 2)
249     error (_("Usage: NAME VISUALIZER_FUNCTION."));
250
251   var = varobj_get_handle (argv[0]);
252
253   if (var == NULL)
254     error (_("Variable object not found"));
255
256   varobj_set_visualizer (var, argv[1]);
257 }
258
259 void
260 mi_cmd_var_set_frozen (const char *command, char **argv, int argc)
261 {
262   struct varobj *var;
263   int frozen;
264
265   if (argc != 2)
266     error (_("-var-set-format: Usage: NAME FROZEN_FLAG."));
267
268   var = varobj_get_handle (argv[0]);
269
270   if (strcmp (argv[1], "0") == 0)
271     frozen = 0;
272   else if (strcmp (argv[1], "1") == 0)
273     frozen = 1;
274   else
275     error (_("Invalid flag value"));
276
277   varobj_set_frozen (var, frozen);
278
279   /* We don't automatically return the new value, or what varobjs got
280      new values during unfreezing.  If this information is required,
281      client should call -var-update explicitly.  */
282 }
283
284 void
285 mi_cmd_var_show_format (const char *command, char **argv, int argc)
286 {
287   struct ui_out *uiout = current_uiout;
288   enum varobj_display_formats format;
289   struct varobj *var;
290
291   if (argc != 1)
292     error (_("-var-show-format: Usage: NAME."));
293
294   /* Get varobj handle, if a valid var obj name was specified.  */
295   var = varobj_get_handle (argv[0]);
296
297   format = varobj_get_display_format (var);
298
299   /* Report the current format.  */
300   uiout->field_string ("format", varobj_format_string[(int) format]);
301 }
302
303 void
304 mi_cmd_var_info_num_children (const char *command, char **argv, int argc)
305 {
306   struct ui_out *uiout = current_uiout;
307   struct varobj *var;
308
309   if (argc != 1)
310     error (_("-var-info-num-children: Usage: NAME."));
311
312   /* Get varobj handle, if a valid var obj name was specified.  */
313   var = varobj_get_handle (argv[0]);
314
315   uiout->field_int ("numchild", varobj_get_num_children (var));
316 }
317
318 /* Return 1 if given the argument PRINT_VALUES we should display
319    the varobj VAR.  */
320
321 static int
322 mi_print_value_p (struct varobj *var, enum print_values print_values)
323 {
324   struct type *type;
325
326   if (print_values == PRINT_NO_VALUES)
327     return 0;
328
329   if (print_values == PRINT_ALL_VALUES)
330     return 1;
331
332   if (varobj_is_dynamic_p (var))
333     return 1;
334
335   type = varobj_get_gdb_type (var);
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 (const char *command, char **argv, int argc)
352 {
353   struct ui_out *uiout = current_uiout;
354   struct varobj *var;  
355   enum print_values print_values;
356   int from, to;
357
358   if (argc < 1 || argc > 4)
359     error (_("-var-list-children: Usage: "
360              "[PRINT_VALUES] NAME [FROM TO]"));
361
362   /* Get varobj handle, if a valid var obj name was specified.  */
363   if (argc == 1 || argc == 3)
364     var = varobj_get_handle (argv[0]);
365   else
366     var = varobj_get_handle (argv[1]);
367
368   if (argc > 2)
369     {
370       from = atoi (argv[argc - 2]);
371       to = atoi (argv[argc - 1]);
372     }
373   else
374     {
375       from = -1;
376       to = -1;
377     }
378
379   const std::vector<varobj *> &children
380     = varobj_list_children (var, &from, &to);
381
382   uiout->field_int ("numchild", to - from);
383   if (argc == 2 || argc == 4)
384     print_values = mi_parse_print_values (argv[0]);
385   else
386     print_values = PRINT_NO_VALUES;
387
388   gdb::unique_xmalloc_ptr<char> display_hint = varobj_get_display_hint (var);
389   if (display_hint)
390     uiout->field_string ("displayhint", display_hint.get ());
391
392   if (from < to)
393     {
394       /* For historical reasons this might emit a list or a tuple, so
395          we construct one or the other.  */
396       gdb::optional<ui_out_emit_tuple> tuple_emitter;
397       gdb::optional<ui_out_emit_list> list_emitter;
398
399       if (mi_version (uiout) == 1)
400         tuple_emitter.emplace (uiout, "children");
401       else
402         list_emitter.emplace (uiout, "children");
403       for (int ix = from; ix < to && ix < children.size (); ix++)
404         {
405           ui_out_emit_tuple child_emitter (uiout, "child");
406
407           print_varobj (children[ix], print_values, 1 /* print expression */);
408         }
409     }
410
411   uiout->field_int ("has_more", varobj_has_more (var, to));
412 }
413
414 void
415 mi_cmd_var_info_type (const char *command, char **argv, int argc)
416 {
417   struct ui_out *uiout = current_uiout;
418   struct varobj *var;
419
420   if (argc != 1)
421     error (_("-var-info-type: Usage: NAME."));
422
423   /* Get varobj handle, if a valid var obj name was specified.  */
424   var = varobj_get_handle (argv[0]);
425
426   std::string type_name = varobj_get_type (var);
427   uiout->field_string ("type", type_name.c_str ());
428 }
429
430 void
431 mi_cmd_var_info_path_expression (const char *command, char **argv, int argc)
432 {
433   struct ui_out *uiout = current_uiout;
434   struct varobj *var;
435
436   if (argc != 1)
437     error (_("Usage: NAME."));
438
439   /* Get varobj handle, if a valid var obj name was specified.  */
440   var = varobj_get_handle (argv[0]);
441   
442   const char *path_expr = varobj_get_path_expr (var);
443
444   uiout->field_string ("path_expr", path_expr);
445 }
446
447 void
448 mi_cmd_var_info_expression (const char *command, char **argv, int argc)
449 {
450   struct ui_out *uiout = current_uiout;
451   const struct language_defn *lang;
452   struct varobj *var;
453
454   if (argc != 1)
455     error (_("-var-info-expression: Usage: NAME."));
456
457   /* Get varobj handle, if a valid var obj name was specified.  */
458   var = varobj_get_handle (argv[0]);
459
460   lang = varobj_get_language (var);
461
462   uiout->field_string ("lang", lang->la_natural_name);
463
464   std::string exp = varobj_get_expression (var);
465   uiout->field_string ("exp", exp.c_str ());
466 }
467
468 void
469 mi_cmd_var_show_attributes (const char *command, char **argv, int argc)
470 {
471   struct ui_out *uiout = current_uiout;
472   int attr;
473   const char *attstr;
474   struct varobj *var;
475
476   if (argc != 1)
477     error (_("-var-show-attributes: Usage: NAME."));
478
479   /* Get varobj handle, if a valid var obj name was specified */
480   var = varobj_get_handle (argv[0]);
481
482   attr = varobj_get_attributes (var);
483   /* FIXME: define masks for attributes */
484   if (attr & 0x00000001)
485     attstr = "editable";
486   else
487     attstr = "noneditable";
488
489   uiout->field_string ("attr", attstr);
490 }
491
492 void
493 mi_cmd_var_evaluate_expression (const char *command, char **argv, int argc)
494 {
495   struct ui_out *uiout = current_uiout;
496   struct varobj *var;
497
498   enum varobj_display_formats format;
499   int formatFound;
500   int oind;
501   char *oarg;
502     
503   enum opt
504   {
505     OP_FORMAT
506   };
507   static const struct mi_opt opts[] =
508     {
509       {"f", OP_FORMAT, 1},
510       { 0, 0, 0 }
511     };
512
513   /* Parse arguments.  */
514   format = FORMAT_NATURAL;
515   formatFound = 0;
516   oind = 0;
517   while (1)
518     {
519       int opt = mi_getopt ("-var-evaluate-expression", argc, argv,
520                            opts, &oind, &oarg);
521
522       if (opt < 0)
523         break;
524       switch ((enum opt) opt)
525         {
526         case OP_FORMAT:
527           if (formatFound)
528             error (_("Cannot specify format more than once"));
529    
530           format = mi_parse_format (oarg);
531           formatFound = 1;
532           break;
533         }
534     }
535
536   if (oind >= argc)
537     error (_("Usage: [-f FORMAT] NAME"));
538    
539   if (oind < argc - 1)
540     error (_("Garbage at end of command"));
541  
542   /* Get varobj handle, if a valid var obj name was specified.  */
543   var = varobj_get_handle (argv[oind]);
544    
545   if (formatFound)
546     {
547       std::string val = varobj_get_formatted_value (var, format);
548
549       uiout->field_string ("value", val.c_str ());
550     }
551   else
552     {
553       std::string val = varobj_get_value (var);
554
555       uiout->field_string ("value", val.c_str ());
556     }
557 }
558
559 void
560 mi_cmd_var_assign (const char *command, char **argv, int argc)
561 {
562   struct ui_out *uiout = current_uiout;
563   struct varobj *var;
564
565   if (argc != 2)
566     error (_("-var-assign: Usage: NAME EXPRESSION."));
567
568   /* Get varobj handle, if a valid var obj name was specified.  */
569   var = varobj_get_handle (argv[0]);
570
571   if (!varobj_editable_p (var))
572     error (_("-var-assign: Variable object is not editable"));
573
574   const char *expression = argv[1];
575
576   /* MI command '-var-assign' may write memory, so suppress memory
577      changed notification if it does.  */
578   scoped_restore save_suppress
579     = make_scoped_restore (&mi_suppress_notification.memory, 1);
580
581   if (!varobj_set_value (var, expression))
582     error (_("-var-assign: Could not assign "
583              "expression to variable object"));
584
585   std::string val = varobj_get_value (var);
586   uiout->field_string ("value", val.c_str ());
587 }
588
589 /* Type used for parameters passing to mi_cmd_var_update_iter.  */
590
591 struct mi_cmd_var_update
592   {
593     int only_floating;
594     enum print_values print_values;
595   };
596
597 /* Helper for mi_cmd_var_update - update each VAR.  */
598
599 static void
600 mi_cmd_var_update_iter (struct varobj *var, void *data_pointer)
601 {
602   struct mi_cmd_var_update *data = (struct mi_cmd_var_update *) data_pointer;
603   int thread_id, thread_stopped;
604
605   thread_id = varobj_get_thread_id (var);
606
607   if (thread_id == -1
608       && (ptid_equal (inferior_ptid, null_ptid)
609           || is_stopped (inferior_ptid)))
610     thread_stopped = 1;
611   else
612     {
613       struct thread_info *tp = find_thread_global_id (thread_id);
614
615       if (tp)
616         thread_stopped = is_stopped (tp->ptid);
617       else
618         thread_stopped = 1;
619     }
620
621   if (thread_stopped
622       && (!data->only_floating || varobj_floating_p (var)))
623     varobj_update_one (var, data->print_values, 0 /* 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, 1 /* 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                    int 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 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 }