1 /* MI Command Set - MI output generating routines for GDB.
2    Copyright (C) 2000-2024 Free Software Foundation, Inc.
3    Contributed by Cygnus Solutions (a Red Hat company).
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 #ifndef MI_MI_OUT_H
21 #define MI_MI_OUT_H
22 
23 #include <vector>
24 
25 struct ui_out;
26 struct ui_file;
27 
28 
29 class mi_ui_out : public ui_out
30 {
31 public:
32 
33   explicit mi_ui_out (int mi_version);
34   virtual ~mi_ui_out ();
35 
36   /* MI-specific */
37   void rewind ();
38   void put (struct ui_file *stream);
39 
40   /* Return the version number of the current MI.  */
41   int version ();
42 
can_emit_style_escape()43   bool can_emit_style_escape () const override
44   {
45     return false;
46   }
47 
current_stream()48   ui_file *current_stream () const override
49   { return m_streams.back (); }
50 
51 protected:
52 
53   virtual void do_table_begin (int nbrofcols, int nr_rows, const char *tblid)
54     override;
55   virtual void do_table_body () override;
56   virtual void do_table_header (int width, ui_align align,
57                                    const std::string &col_name,
58                                    const std::string &col_hdr) override;
59   virtual void do_table_end () override;
60 
61   virtual void do_begin (ui_out_type type, const char *id) override;
62   virtual void do_end (ui_out_type type) override;
63   virtual void do_field_signed (int fldno, int width, ui_align align,
64                                         const char *fldname, LONGEST value) override;
65   virtual void do_field_unsigned (int fldno, int width, ui_align align,
66                                           const char *fldname, ULONGEST value)
67     override;
68   virtual void do_field_skip (int fldno, int width, ui_align align,
69                                  const char *fldname) override;
70   virtual void do_field_string (int fldno, int width, ui_align align,
71                                         const char *fldname, const char *string,
72                                         const ui_file_style &style) override;
73   virtual void do_field_fmt (int fldno, int width, ui_align align,
74                                    const char *fldname, const ui_file_style &style,
75                                    const char *format, va_list args)
76     override ATTRIBUTE_PRINTF (7,0);
77   virtual void do_spaces (int numspaces) override;
78   virtual void do_text (const char *string) override;
79   virtual void do_message (const ui_file_style &style,
80                                  const char *format, va_list args) override
81     ATTRIBUTE_PRINTF (3,0);
82   virtual void do_wrap_hint (int indent) override;
83   virtual void do_flush () override;
84   virtual void do_redirect (struct ui_file *outstream) override;
85 
do_is_mi_like_p()86   virtual bool do_is_mi_like_p () const override
87   { return true; }
88 
89   virtual void do_progress_start () override;
90   virtual void do_progress_notify (const std::string &, const char *,
91                                            double, double) override;
92 
93   virtual void do_progress_end () override;
94 
95 private:
96 
97   void field_separator ();
98   void open (const char *name, ui_out_type type);
99   void close (ui_out_type type);
100 
101   /* The state of a recent progress_update.  */
102   struct mi_progress_info
103   {
104     /* The current state.  */
105     progress_update::state state;
106 
mi_progress_infomi_progress_info107     mi_progress_info ()
108       : state (progress_update::START)
109     {}
110   };
111 
112   /* Stack of progress info.  */
113   std::vector<mi_progress_info> m_progress_info;
114 
115   /* Convenience method that returns the MI out's string stream cast
116      to its appropriate type.  Assumes/asserts that output was not
117      redirected.  */
118   string_file *main_stream ();
119 
120   /* Helper for the constructor, deduce ui_out_flags for the given
121      MI_VERSION.  */
make_flags(int mi_version)122   static ui_out_flags make_flags (int mi_version)
123   {
124     ui_out_flags flags = 0;
125 
126     /* In MI version 2 and below, multi-location breakpoints had a wrong
127        syntax.  It is fixed in version 3.  */
128     if (mi_version >= 3)
129       flags |= fix_multi_location_breakpoint_output;
130 
131     /* In MI version 3 and below, the "script" field in breakpoint output
132        had a wrong syntax.  It is fixed in version 4.  */
133     if (mi_version >= 4)
134       flags |= fix_breakpoint_script_output;
135 
136     return flags;
137   }
138 
139   bool m_suppress_field_separator;
140   bool m_suppress_output;
141   int m_mi_version;
142   std::vector<ui_file *> m_streams;
143 };
144 
145 /* Create an MI ui-out object with MI version MI_VERSION, which should be equal
146    to one of the INTERP_MI* constants (see interps.h).
147 
148    Return nullptr if an invalid version is provided.  */
149 std::unique_ptr<mi_ui_out> mi_out_new (const char *mi_version);
150 
151 void mi_out_put (ui_out *uiout, struct ui_file *stream);
152 void mi_out_rewind (ui_out *uiout);
153 
154 #endif /* MI_MI_OUT_H */
155