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/>. */
28 #include "gdbsupport/enum-flags.h"
35 /* the current ui_out */
37 /* FIXME: This should not be a global but something passed down from main.c
39 extern struct ui_out **current_ui_current_uiout_ptr (void);
40 #define current_uiout (*current_ui_current_uiout_ptr ())
54 ui_source_list = (1 << 0),
55 fix_multi_location_breakpoint_output = (1 << 1),
56 /* For CLI output, this flag is set if unfiltered output is desired.
57 This should only be used by low-level formatting functions. */
58 unfiltered_output = (1 << 2),
59 /* This indicates that %pF should be disallowed in a printf format
61 disallow_ui_out_field = (1 << 3)
64 DEF_ENUM_FLAGS_TYPE (ui_out_flag, ui_out_flags);
66 /* Prototypes for ui-out API. */
68 /* A result is a recursive data structure consisting of lists and
77 /* The possible kinds of fields. */
80 /* "FIELD_STRING" needs a funny name to avoid clashes with tokens
81 named "STRING". See PR build/25250. FIELD_SIGNED is given a
82 similar name for consistency. */
87 /* The base type of all fields that can be emitted using %pF. */
95 /* A signed integer field, to be passed to %pF in format strings. */
97 struct signed_field_s : base_field_s
102 /* Construct a temporary signed_field_s on the caller's stack and
103 return a pointer to the constructed object. We use this because
104 it's not possible to pass a reference via va_args. */
106 static inline signed_field_s *
107 signed_field (const char *name, LONGEST val,
108 signed_field_s &&tmp = {})
111 tmp.kind = field_kind::FIELD_SIGNED;
116 /* A string field, to be passed to %pF in format strings. */
118 struct string_field_s : base_field_s
123 /* Construct a temporary string_field_s on the caller's stack and
124 return a pointer to the constructed object. We use this because
125 it's not possible to pass a reference via va_args. */
127 static inline string_field_s *
128 string_field (const char *name, const char *str,
129 string_field_s &&tmp = {})
132 tmp.kind = field_kind::FIELD_STRING;
137 /* A styled string. */
139 struct styled_string_s
148 /* Construct a temporary styled_string_s on the caller's stack and
149 return a pointer to the constructed object. We use this because
150 it's not possible to pass a reference via va_args. */
152 static inline styled_string_s *
153 styled_string (const ui_file_style &style, const char *str,
154 styled_string_s &&tmp = {})
165 explicit ui_out (ui_out_flags flags = 0);
168 void push_level (ui_out_type type);
169 void pop_level (ui_out_type type);
171 /* A table can be considered a special tuple/list combination with the
172 implied structure: ``table = { hdr = { header, ... } , body = [ {
173 field, ... }, ... ] }''. If NR_ROWS is negative then there is at
176 void table_begin (int nr_cols, int nr_rows, const std::string &tblid);
177 void table_header (int width, ui_align align, const std::string &col_name,
178 const std::string &col_hdr);
182 void begin (ui_out_type type, const char *id);
183 void end (ui_out_type type);
185 void field_signed (const char *fldname, LONGEST value);
186 void field_fmt_signed (int width, ui_align align, const char *fldname,
188 /* Like field_signed, but print an unsigned value. */
189 void field_unsigned (const char *fldname, ULONGEST value);
190 void field_core_addr (const char *fldname, struct gdbarch *gdbarch,
192 void field_string (const char *fldname, const char *string,
193 const ui_file_style &style = ui_file_style ());
194 void field_string (const char *fldname, const std::string &string,
195 const ui_file_style &style = ui_file_style ())
197 field_string (fldname, string.c_str (), style);
199 void field_stream (const char *fldname, string_file &stream,
200 const ui_file_style &style = ui_file_style ());
201 void field_skip (const char *fldname);
202 void field_fmt (const char *fldname, const char *format, ...)
203 ATTRIBUTE_PRINTF (3, 4);
204 void field_fmt (const char *fldname, const ui_file_style &style,
205 const char *format, ...)
206 ATTRIBUTE_PRINTF (4, 5);
208 void spaces (int numspaces);
209 void text (const char *string);
210 void text (const std::string &string) { text (string.c_str ()); }
212 /* Output a printf-style formatted string. In addition to the usual
213 printf format specs, this supports a few GDB-specific
216 - '%pF' - output a field.
218 The argument is a field, wrapped in any of the base_field_s
219 subclasses. signed_field for integer fields, string_field for
220 string fields. This is preferred over separate
221 uiout->field_signed(), uiout_>field_string() etc. calls when
222 the formatted message is translatable. E.g.:
224 uiout->message (_("\nWatchpoint %pF deleted because the program has "
225 "left the block in\n"
226 "which its expression is valid.\n"),
227 signed_field ("wpnum", b->number));
229 - '%p[' - output the following text in a specified style.
230 '%p]' - output the following text in the default style.
232 The argument to '%p[' is a ui_file_style pointer. The argument
233 to '%p]' must be nullptr.
235 This is useful when you want to output some portion of a string
236 literal in some style. E.g.:
238 uiout->message (_(" %p[<repeats %u times>%p]"),
239 metadata_style.style ().ptr (),
240 reps, repeats, nullptr);
242 - '%ps' - output a styled string.
244 The argument is the result of a call to styled_string. This is
245 useful when you want to output some runtime-generated string in
248 uiout->message (_("this is a target address %ps.\n"),
249 styled_string (address_style.style (),
250 paddress (gdbarch, pc)));
252 Note that these all "abuse" the %p printf format spec, in order
253 to be compatible with GCC's printf format checking. This is OK
254 because code in GDB that wants to print a host address should use
255 host_address_to_string instead of %p. */
256 void message (const char *format, ...) ATTRIBUTE_PRINTF (2, 3);
257 void vmessage (const ui_file_style &in_style,
258 const char *format, va_list args) ATTRIBUTE_PRINTF (3, 0);
260 void wrap_hint (int indent);
264 /* Redirect the output of a ui_out object temporarily. */
265 void redirect (ui_file *outstream);
267 ui_out_flags test_flags (ui_out_flags mask);
269 /* HACK: Code in GDB is currently checking to see the type of ui_out
270 builder when determining which output to produce. This function is
271 a hack to encapsulate that test. Once GDB manages to separate the
272 CLI/MI from the core of GDB the problem should just go away .... */
274 bool is_mi_like_p () const;
276 bool query_table_field (int colno, int *width, int *alignment,
277 const char **col_name);
279 /* Return true if this stream is prepared to handle style
281 virtual bool can_emit_style_escape () const = 0;
283 /* An object that starts and finishes a progress meter. */
287 /* SHOULD_PRINT indicates whether something should be printed for a tty. */
288 progress_meter (struct ui_out *uiout, const std::string &name,
292 m_uiout->do_progress_start (name, should_print);
297 m_uiout->do_progress_notify (1.0);
298 m_uiout->do_progress_end ();
301 progress_meter (const progress_meter &) = delete;
302 progress_meter &operator= (const progress_meter &) = delete;
306 struct ui_out *m_uiout;
309 /* Emit some progress corresponding to the most recently created
310 progress meter. HOWMUCH may range from 0.0 to 1.0. */
311 void progress (double howmuch)
313 do_progress_notify (howmuch);
318 virtual void do_table_begin (int nbrofcols, int nr_rows, const char *tblid)
320 virtual void do_table_body () = 0;
321 virtual void do_table_end () = 0;
322 virtual void do_table_header (int width, ui_align align,
323 const std::string &col_name,
324 const std::string &col_hdr) = 0;
326 virtual void do_begin (ui_out_type type, const char *id) = 0;
327 virtual void do_end (ui_out_type type) = 0;
328 virtual void do_field_signed (int fldno, int width, ui_align align,
329 const char *fldname, LONGEST value) = 0;
330 virtual void do_field_unsigned (int fldno, int width, ui_align align,
331 const char *fldname, ULONGEST value) = 0;
332 virtual void do_field_skip (int fldno, int width, ui_align align,
333 const char *fldname) = 0;
334 virtual void do_field_string (int fldno, int width, ui_align align,
335 const char *fldname, const char *string,
336 const ui_file_style &style) = 0;
337 virtual void do_field_fmt (int fldno, int width, ui_align align,
338 const char *fldname, const ui_file_style &style,
339 const char *format, va_list args)
340 ATTRIBUTE_PRINTF (7, 0) = 0;
341 virtual void do_spaces (int numspaces) = 0;
342 virtual void do_text (const char *string) = 0;
343 virtual void do_message (const ui_file_style &style,
344 const char *format, va_list args)
345 ATTRIBUTE_PRINTF (3,0) = 0;
346 virtual void do_wrap_hint (int indent) = 0;
347 virtual void do_flush () = 0;
348 virtual void do_redirect (struct ui_file *outstream) = 0;
350 virtual void do_progress_start (const std::string &, bool) = 0;
351 virtual void do_progress_notify (double) = 0;
352 virtual void do_progress_end () = 0;
354 /* Set as not MI-like by default. It is overridden in subclasses if
357 virtual bool do_is_mi_like_p () const
361 void call_do_message (const ui_file_style &style, const char *format,
364 ui_out_flags m_flags;
366 /* Vector to store and track the ui-out levels. */
367 std::vector<std::unique_ptr<ui_out_level>> m_levels;
369 /* A table, if any. At present only a single table is supported. */
370 std::unique_ptr<ui_out_table> m_table_up;
372 void verify_field (int *fldno, int *width, ui_align *align);
375 ui_out_level *current_level () const;
378 /* Start a new tuple or list on construction, and end it on
379 destruction. Normally this is used via the typedefs
380 ui_out_emit_tuple and ui_out_emit_list. */
381 template<ui_out_type Type>
382 class ui_out_emit_type
386 ui_out_emit_type (struct ui_out *uiout, const char *id)
389 uiout->begin (Type, id);
397 DISABLE_COPY_AND_ASSIGN (ui_out_emit_type<Type>);
401 struct ui_out *m_uiout;
404 typedef ui_out_emit_type<ui_out_type_tuple> ui_out_emit_tuple;
405 typedef ui_out_emit_type<ui_out_type_list> ui_out_emit_list;
407 /* Start a new table on construction, and end the table on
409 class ui_out_emit_table
413 ui_out_emit_table (struct ui_out *uiout, int nr_cols, int nr_rows,
417 m_uiout->table_begin (nr_cols, nr_rows, tblid);
420 ~ui_out_emit_table ()
422 m_uiout->table_end ();
425 ui_out_emit_table (const ui_out_emit_table &) = delete;
426 ui_out_emit_table &operator= (const ui_out_emit_table &) = delete;
430 struct ui_out *m_uiout;
433 /* On destruction, pop the last redirection by calling the uiout's
434 redirect method with a NULL parameter. */
435 class ui_out_redirect_pop
439 ui_out_redirect_pop (ui_out *uiout)
444 ~ui_out_redirect_pop ()
446 m_uiout->redirect (NULL);
449 ui_out_redirect_pop (const ui_out_redirect_pop &) = delete;
450 ui_out_redirect_pop &operator= (const ui_out_redirect_pop &) = delete;
453 struct ui_out *m_uiout;
456 #endif /* UI_OUT_H */