Update release README with new version numbers
[binutils-gdb.git] / gdb / ui-out.h
blob9e6ff9a29bf0c773c7f7558003b54fbba2159e1c
1 /* Output generating routines for GDB.
3 Copyright (C) 1999-2022 Free Software Foundation, Inc.
5 Contributed by Cygnus Solutions.
6 Written by Fernando Nasser for Cygnus.
8 This file is part of GDB.
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.
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.
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/>. */
23 #ifndef UI_OUT_H
24 #define UI_OUT_H 1
26 #include <vector>
28 #include "gdbsupport/enum-flags.h"
29 #include "ui-style.h"
31 class ui_out_level;
32 class ui_out_table;
33 struct ui_file;
35 /* the current ui_out */
37 /* FIXME: This should not be a global but something passed down from main.c
38 or top.c. */
39 extern struct ui_out **current_ui_current_uiout_ptr (void);
40 #define current_uiout (*current_ui_current_uiout_ptr ())
42 /* alignment enum */
43 enum ui_align
45 ui_left = -1,
46 ui_center,
47 ui_right,
48 ui_noalign
51 /* flags enum */
52 enum ui_out_flag
54 ui_source_list = (1 << 0),
55 fix_multi_location_breakpoint_output = (1 << 1),
56 /* This indicates that %pF should be disallowed in a printf format
57 string. */
58 disallow_ui_out_field = (1 << 2)
61 DEF_ENUM_FLAGS_TYPE (ui_out_flag, ui_out_flags);
63 /* Prototypes for ui-out API. */
65 /* A result is a recursive data structure consisting of lists and
66 tuples. */
68 enum ui_out_type
70 ui_out_type_tuple,
71 ui_out_type_list
74 /* The possible kinds of fields. */
75 enum class field_kind
77 /* "FIELD_STRING" needs a funny name to avoid clashes with tokens
78 named "STRING". See PR build/25250. FIELD_SIGNED is given a
79 similar name for consistency. */
80 FIELD_SIGNED,
81 FIELD_STRING,
84 /* The base type of all fields that can be emitted using %pF. */
86 struct base_field_s
88 const char *name;
89 field_kind kind;
92 /* A signed integer field, to be passed to %pF in format strings. */
94 struct signed_field_s : base_field_s
96 LONGEST val;
99 /* Construct a temporary signed_field_s on the caller's stack and
100 return a pointer to the constructed object. We use this because
101 it's not possible to pass a reference via va_args. */
103 static inline signed_field_s *
104 signed_field (const char *name, LONGEST val,
105 signed_field_s &&tmp = {})
107 tmp.name = name;
108 tmp.kind = field_kind::FIELD_SIGNED;
109 tmp.val = val;
110 return &tmp;
113 /* A string field, to be passed to %pF in format strings. */
115 struct string_field_s : base_field_s
117 const char *str;
120 /* Construct a temporary string_field_s on the caller's stack and
121 return a pointer to the constructed object. We use this because
122 it's not possible to pass a reference via va_args. */
124 static inline string_field_s *
125 string_field (const char *name, const char *str,
126 string_field_s &&tmp = {})
128 tmp.name = name;
129 tmp.kind = field_kind::FIELD_STRING;
130 tmp.str = str;
131 return &tmp;
134 /* A styled string. */
136 struct styled_string_s
138 /* The style. */
139 ui_file_style style;
141 /* The string. */
142 const char *str;
145 /* Construct a temporary styled_string_s on the caller's stack and
146 return a pointer to the constructed object. We use this because
147 it's not possible to pass a reference via va_args. */
149 static inline styled_string_s *
150 styled_string (const ui_file_style &style, const char *str,
151 styled_string_s &&tmp = {})
153 tmp.style = style;
154 tmp.str = str;
155 return &tmp;
158 class ui_out
160 public:
162 explicit ui_out (ui_out_flags flags = 0);
163 virtual ~ui_out ();
165 void push_level (ui_out_type type);
166 void pop_level (ui_out_type type);
168 /* A table can be considered a special tuple/list combination with the
169 implied structure: ``table = { hdr = { header, ... } , body = [ {
170 field, ... }, ... ] }''. If NR_ROWS is negative then there is at
171 least one row. */
173 void table_begin (int nr_cols, int nr_rows, const std::string &tblid);
174 void table_header (int width, ui_align align, const std::string &col_name,
175 const std::string &col_hdr);
176 void table_body ();
177 void table_end ();
179 void begin (ui_out_type type, const char *id);
180 void end (ui_out_type type);
182 void field_signed (const char *fldname, LONGEST value);
183 void field_fmt_signed (int width, ui_align align, const char *fldname,
184 LONGEST value);
185 /* Like field_signed, but print an unsigned value. */
186 void field_unsigned (const char *fldname, ULONGEST value);
187 void field_core_addr (const char *fldname, struct gdbarch *gdbarch,
188 CORE_ADDR address);
189 void field_string (const char *fldname, const char *string,
190 const ui_file_style &style = ui_file_style ());
191 void field_string (const char *fldname, const std::string &string,
192 const ui_file_style &style = ui_file_style ())
194 field_string (fldname, string.c_str (), style);
196 void field_stream (const char *fldname, string_file &stream,
197 const ui_file_style &style = ui_file_style ());
198 void field_skip (const char *fldname);
199 void field_fmt (const char *fldname, const char *format, ...)
200 ATTRIBUTE_PRINTF (3, 4);
201 void field_fmt (const char *fldname, const ui_file_style &style,
202 const char *format, ...)
203 ATTRIBUTE_PRINTF (4, 5);
205 void spaces (int numspaces);
206 void text (const char *string);
207 void text (const std::string &string) { text (string.c_str ()); }
209 /* Output a printf-style formatted string. In addition to the usual
210 printf format specs, this supports a few GDB-specific
211 formatters:
213 - '%pF' - output a field.
215 The argument is a field, wrapped in any of the base_field_s
216 subclasses. signed_field for integer fields, string_field for
217 string fields. This is preferred over separate
218 uiout->field_signed(), uiout_>field_string() etc. calls when
219 the formatted message is translatable. E.g.:
221 uiout->message (_("\nWatchpoint %pF deleted because the program has "
222 "left the block in\n"
223 "which its expression is valid.\n"),
224 signed_field ("wpnum", b->number));
226 - '%p[' - output the following text in a specified style.
227 '%p]' - output the following text in the default style.
229 The argument to '%p[' is a ui_file_style pointer. The argument
230 to '%p]' must be nullptr.
232 This is useful when you want to output some portion of a string
233 literal in some style. E.g.:
235 uiout->message (_(" %p[<repeats %u times>%p]"),
236 metadata_style.style ().ptr (),
237 reps, repeats, nullptr);
239 - '%ps' - output a styled string.
241 The argument is the result of a call to styled_string. This is
242 useful when you want to output some runtime-generated string in
243 some style. E.g.:
245 uiout->message (_("this is a target address %ps.\n"),
246 styled_string (address_style.style (),
247 paddress (gdbarch, pc)));
249 Note that these all "abuse" the %p printf format spec, in order
250 to be compatible with GCC's printf format checking. This is OK
251 because code in GDB that wants to print a host address should use
252 host_address_to_string instead of %p. */
253 void message (const char *format, ...) ATTRIBUTE_PRINTF (2, 3);
254 void vmessage (const ui_file_style &in_style,
255 const char *format, va_list args) ATTRIBUTE_PRINTF (3, 0);
257 void wrap_hint (int indent);
259 void flush ();
261 /* Redirect the output of a ui_out object temporarily. */
262 void redirect (ui_file *outstream);
264 ui_out_flags test_flags (ui_out_flags mask);
266 /* HACK: Code in GDB is currently checking to see the type of ui_out
267 builder when determining which output to produce. This function is
268 a hack to encapsulate that test. Once GDB manages to separate the
269 CLI/MI from the core of GDB the problem should just go away .... */
271 bool is_mi_like_p () const;
273 bool query_table_field (int colno, int *width, int *alignment,
274 const char **col_name);
276 /* Return true if this stream is prepared to handle style
277 escapes. */
278 virtual bool can_emit_style_escape () const = 0;
280 /* An object that starts and finishes a progress meter. */
281 class progress_meter
283 public:
284 /* SHOULD_PRINT indicates whether something should be printed for a tty. */
285 progress_meter (struct ui_out *uiout, const std::string &name,
286 bool should_print)
287 : m_uiout (uiout)
289 m_uiout->do_progress_start (name, should_print);
292 ~progress_meter ()
294 m_uiout->do_progress_notify (1.0);
295 m_uiout->do_progress_end ();
298 progress_meter (const progress_meter &) = delete;
299 progress_meter &operator= (const progress_meter &) = delete;
301 /* Emit some progress for this progress meter. HOWMUCH may range
302 from 0.0 to 1.0. */
303 void progress (double howmuch)
305 m_uiout->do_progress_notify (howmuch);
308 private:
310 struct ui_out *m_uiout;
313 protected:
315 virtual void do_table_begin (int nbrofcols, int nr_rows, const char *tblid)
316 = 0;
317 virtual void do_table_body () = 0;
318 virtual void do_table_end () = 0;
319 virtual void do_table_header (int width, ui_align align,
320 const std::string &col_name,
321 const std::string &col_hdr) = 0;
323 virtual void do_begin (ui_out_type type, const char *id) = 0;
324 virtual void do_end (ui_out_type type) = 0;
325 virtual void do_field_signed (int fldno, int width, ui_align align,
326 const char *fldname, LONGEST value) = 0;
327 virtual void do_field_unsigned (int fldno, int width, ui_align align,
328 const char *fldname, ULONGEST value) = 0;
329 virtual void do_field_skip (int fldno, int width, ui_align align,
330 const char *fldname) = 0;
331 virtual void do_field_string (int fldno, int width, ui_align align,
332 const char *fldname, const char *string,
333 const ui_file_style &style) = 0;
334 virtual void do_field_fmt (int fldno, int width, ui_align align,
335 const char *fldname, const ui_file_style &style,
336 const char *format, va_list args)
337 ATTRIBUTE_PRINTF (7, 0) = 0;
338 virtual void do_spaces (int numspaces) = 0;
339 virtual void do_text (const char *string) = 0;
340 virtual void do_message (const ui_file_style &style,
341 const char *format, va_list args)
342 ATTRIBUTE_PRINTF (3,0) = 0;
343 virtual void do_wrap_hint (int indent) = 0;
344 virtual void do_flush () = 0;
345 virtual void do_redirect (struct ui_file *outstream) = 0;
347 virtual void do_progress_start (const std::string &, bool) = 0;
348 virtual void do_progress_notify (double) = 0;
349 virtual void do_progress_end () = 0;
351 /* Set as not MI-like by default. It is overridden in subclasses if
352 necessary. */
354 virtual bool do_is_mi_like_p () const
355 { return false; }
357 private:
358 void call_do_message (const ui_file_style &style, const char *format,
359 ...);
361 ui_out_flags m_flags;
363 /* Vector to store and track the ui-out levels. */
364 std::vector<std::unique_ptr<ui_out_level>> m_levels;
366 /* A table, if any. At present only a single table is supported. */
367 std::unique_ptr<ui_out_table> m_table_up;
369 void verify_field (int *fldno, int *width, ui_align *align);
371 int level () const;
372 ui_out_level *current_level () const;
375 /* Start a new tuple or list on construction, and end it on
376 destruction. Normally this is used via the typedefs
377 ui_out_emit_tuple and ui_out_emit_list. */
378 template<ui_out_type Type>
379 class ui_out_emit_type
381 public:
383 ui_out_emit_type (struct ui_out *uiout, const char *id)
384 : m_uiout (uiout)
386 uiout->begin (Type, id);
389 ~ui_out_emit_type ()
391 m_uiout->end (Type);
394 DISABLE_COPY_AND_ASSIGN (ui_out_emit_type<Type>);
396 private:
398 struct ui_out *m_uiout;
401 typedef ui_out_emit_type<ui_out_type_tuple> ui_out_emit_tuple;
402 typedef ui_out_emit_type<ui_out_type_list> ui_out_emit_list;
404 /* Start a new table on construction, and end the table on
405 destruction. */
406 class ui_out_emit_table
408 public:
410 ui_out_emit_table (struct ui_out *uiout, int nr_cols, int nr_rows,
411 const char *tblid)
412 : m_uiout (uiout)
414 m_uiout->table_begin (nr_cols, nr_rows, tblid);
417 ~ui_out_emit_table ()
419 m_uiout->table_end ();
422 ui_out_emit_table (const ui_out_emit_table &) = delete;
423 ui_out_emit_table &operator= (const ui_out_emit_table &) = delete;
425 private:
427 struct ui_out *m_uiout;
430 /* On destruction, pop the last redirection by calling the uiout's
431 redirect method with a NULL parameter. */
432 class ui_out_redirect_pop
434 public:
436 ui_out_redirect_pop (ui_out *uiout)
437 : m_uiout (uiout)
441 ~ui_out_redirect_pop ()
443 m_uiout->redirect (NULL);
446 ui_out_redirect_pop (const ui_out_redirect_pop &) = delete;
447 ui_out_redirect_pop &operator= (const ui_out_redirect_pop &) = delete;
449 private:
450 struct ui_out *m_uiout;
453 #endif /* UI_OUT_H */