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 /* This indicates that %pF should be disallowed in a printf format
58 disallow_ui_out_field
= (1 << 2),
59 fix_breakpoint_script_output
= (1 << 3),
62 DEF_ENUM_FLAGS_TYPE (ui_out_flag
, ui_out_flags
);
64 /* Prototypes for ui-out API. */
66 /* A result is a recursive data structure consisting of lists and
75 /* The possible kinds of fields. */
78 /* "FIELD_STRING" needs a funny name to avoid clashes with tokens
79 named "STRING". See PR build/25250. FIELD_SIGNED is given a
80 similar name for consistency. */
85 /* The base type of all fields that can be emitted using %pF. */
93 /* A signed integer field, to be passed to %pF in format strings. */
95 struct signed_field_s
: base_field_s
100 /* Construct a temporary signed_field_s on the caller's stack and
101 return a pointer to the constructed object. We use this because
102 it's not possible to pass a reference via va_args. */
104 static inline signed_field_s
*
105 signed_field (const char *name
, LONGEST val
,
106 signed_field_s
&&tmp
= {})
109 tmp
.kind
= field_kind::FIELD_SIGNED
;
114 /* A string field, to be passed to %pF in format strings. */
116 struct string_field_s
: base_field_s
121 /* Construct a temporary string_field_s on the caller's stack and
122 return a pointer to the constructed object. We use this because
123 it's not possible to pass a reference via va_args. */
125 static inline string_field_s
*
126 string_field (const char *name
, const char *str
,
127 string_field_s
&&tmp
= {})
130 tmp
.kind
= field_kind::FIELD_STRING
;
135 /* A styled string. */
137 struct styled_string_s
146 /* Construct a temporary styled_string_s on the caller's stack and
147 return a pointer to the constructed object. We use this because
148 it's not possible to pass a reference via va_args. */
150 static inline styled_string_s
*
151 styled_string (const ui_file_style
&style
, const char *str
,
152 styled_string_s
&&tmp
= {})
163 explicit ui_out (ui_out_flags flags
= 0);
166 void push_level (ui_out_type type
);
167 void pop_level (ui_out_type type
);
169 /* A table can be considered a special tuple/list combination with the
170 implied structure: ``table = { hdr = { header, ... } , body = [ {
171 field, ... }, ... ] }''. If NR_ROWS is negative then there is at
174 void table_begin (int nr_cols
, int nr_rows
, const std::string
&tblid
);
175 void table_header (int width
, ui_align align
, const std::string
&col_name
,
176 const std::string
&col_hdr
);
180 void begin (ui_out_type type
, const char *id
);
181 void end (ui_out_type type
);
183 void field_signed (const char *fldname
, LONGEST value
);
184 void field_fmt_signed (int width
, ui_align align
, const char *fldname
,
186 /* Like field_signed, but print an unsigned value. */
187 void field_unsigned (const char *fldname
, ULONGEST value
);
188 void field_core_addr (const char *fldname
, struct gdbarch
*gdbarch
,
190 void field_string (const char *fldname
, const char *string
,
191 const ui_file_style
&style
= ui_file_style ());
192 void field_string (const char *fldname
, const std::string
&string
,
193 const ui_file_style
&style
= ui_file_style ())
195 field_string (fldname
, string
.c_str (), style
);
197 void field_stream (const char *fldname
, string_file
&stream
,
198 const ui_file_style
&style
= ui_file_style ());
199 void field_skip (const char *fldname
);
200 void field_fmt (const char *fldname
, const char *format
, ...)
201 ATTRIBUTE_PRINTF (3, 4);
202 void field_fmt (const char *fldname
, const ui_file_style
&style
,
203 const char *format
, ...)
204 ATTRIBUTE_PRINTF (4, 5);
206 void spaces (int numspaces
);
207 void text (const char *string
);
208 void text (const std::string
&string
) { text (string
.c_str ()); }
210 /* Output a printf-style formatted string. In addition to the usual
211 printf format specs, this supports a few GDB-specific
214 - '%pF' - output a field.
216 The argument is a field, wrapped in any of the base_field_s
217 subclasses. signed_field for integer fields, string_field for
218 string fields. This is preferred over separate
219 uiout->field_signed(), uiout_>field_string() etc. calls when
220 the formatted message is translatable. E.g.:
222 uiout->message (_("\nWatchpoint %pF deleted because the program has "
223 "left the block in\n"
224 "which its expression is valid.\n"),
225 signed_field ("wpnum", b->number));
227 - '%p[' - output the following text in a specified style.
228 '%p]' - output the following text in the default style.
230 The argument to '%p[' is a ui_file_style pointer. The argument
231 to '%p]' must be nullptr.
233 This is useful when you want to output some portion of a string
234 literal in some style. E.g.:
236 uiout->message (_(" %p[<repeats %u times>%p]"),
237 metadata_style.style ().ptr (),
238 reps, repeats, nullptr);
240 - '%ps' - output a styled string.
242 The argument is the result of a call to styled_string. This is
243 useful when you want to output some runtime-generated string in
246 uiout->message (_("this is a target address %ps.\n"),
247 styled_string (address_style.style (),
248 paddress (gdbarch, pc)));
250 Note that these all "abuse" the %p printf format spec, in order
251 to be compatible with GCC's printf format checking. This is OK
252 because code in GDB that wants to print a host address should use
253 host_address_to_string instead of %p. */
254 void message (const char *format
, ...) ATTRIBUTE_PRINTF (2, 3);
255 void vmessage (const ui_file_style
&in_style
,
256 const char *format
, va_list args
) ATTRIBUTE_PRINTF (3, 0);
258 void wrap_hint (int indent
);
262 /* Redirect the output of a ui_out object temporarily. */
263 void redirect (ui_file
*outstream
);
265 ui_out_flags
test_flags (ui_out_flags mask
);
267 /* HACK: Code in GDB is currently checking to see the type of ui_out
268 builder when determining which output to produce. This function is
269 a hack to encapsulate that test. Once GDB manages to separate the
270 CLI/MI from the core of GDB the problem should just go away .... */
272 bool is_mi_like_p () const;
274 bool query_table_field (int colno
, int *width
, int *alignment
,
275 const char **col_name
);
277 /* Return true if this stream is prepared to handle style
279 virtual bool can_emit_style_escape () const = 0;
281 /* An object that starts and finishes a progress meter. */
285 /* SHOULD_PRINT indicates whether something should be printed for a tty. */
286 progress_meter (struct ui_out
*uiout
, const std::string
&name
,
290 m_uiout
->do_progress_start (name
, should_print
);
295 m_uiout
->do_progress_notify (1.0);
296 m_uiout
->do_progress_end ();
299 progress_meter (const progress_meter
&) = delete;
300 progress_meter
&operator= (const progress_meter
&) = delete;
302 /* Emit some progress for this progress meter. HOWMUCH may range
304 void progress (double howmuch
)
306 m_uiout
->do_progress_notify (howmuch
);
311 struct ui_out
*m_uiout
;
316 virtual void do_table_begin (int nbrofcols
, int nr_rows
, const char *tblid
)
318 virtual void do_table_body () = 0;
319 virtual void do_table_end () = 0;
320 virtual void do_table_header (int width
, ui_align align
,
321 const std::string
&col_name
,
322 const std::string
&col_hdr
) = 0;
324 virtual void do_begin (ui_out_type type
, const char *id
) = 0;
325 virtual void do_end (ui_out_type type
) = 0;
326 virtual void do_field_signed (int fldno
, int width
, ui_align align
,
327 const char *fldname
, LONGEST value
) = 0;
328 virtual void do_field_unsigned (int fldno
, int width
, ui_align align
,
329 const char *fldname
, ULONGEST value
) = 0;
330 virtual void do_field_skip (int fldno
, int width
, ui_align align
,
331 const char *fldname
) = 0;
332 virtual void do_field_string (int fldno
, int width
, ui_align align
,
333 const char *fldname
, const char *string
,
334 const ui_file_style
&style
) = 0;
335 virtual void do_field_fmt (int fldno
, int width
, ui_align align
,
336 const char *fldname
, const ui_file_style
&style
,
337 const char *format
, va_list args
)
338 ATTRIBUTE_PRINTF (7, 0) = 0;
339 virtual void do_spaces (int numspaces
) = 0;
340 virtual void do_text (const char *string
) = 0;
341 virtual void do_message (const ui_file_style
&style
,
342 const char *format
, va_list args
)
343 ATTRIBUTE_PRINTF (3,0) = 0;
344 virtual void do_wrap_hint (int indent
) = 0;
345 virtual void do_flush () = 0;
346 virtual void do_redirect (struct ui_file
*outstream
) = 0;
348 virtual void do_progress_start (const std::string
&, bool) = 0;
349 virtual void do_progress_notify (double) = 0;
350 virtual void do_progress_end () = 0;
352 /* Set as not MI-like by default. It is overridden in subclasses if
355 virtual bool do_is_mi_like_p () const
359 void call_do_message (const ui_file_style
&style
, const char *format
,
362 ui_out_flags m_flags
;
364 /* Vector to store and track the ui-out levels. */
365 std::vector
<std::unique_ptr
<ui_out_level
>> m_levels
;
367 /* A table, if any. At present only a single table is supported. */
368 std::unique_ptr
<ui_out_table
> m_table_up
;
370 void verify_field (int *fldno
, int *width
, ui_align
*align
);
373 ui_out_level
*current_level () const;
376 /* Start a new tuple or list on construction, and end it on
377 destruction. Normally this is used via the typedefs
378 ui_out_emit_tuple and ui_out_emit_list. */
379 template<ui_out_type Type
>
380 class ui_out_emit_type
384 ui_out_emit_type (struct ui_out
*uiout
, const char *id
)
387 uiout
->begin (Type
, id
);
395 DISABLE_COPY_AND_ASSIGN (ui_out_emit_type
<Type
>);
399 struct ui_out
*m_uiout
;
402 typedef ui_out_emit_type
<ui_out_type_tuple
> ui_out_emit_tuple
;
403 typedef ui_out_emit_type
<ui_out_type_list
> ui_out_emit_list
;
405 /* Start a new table on construction, and end the table on
407 class ui_out_emit_table
411 ui_out_emit_table (struct ui_out
*uiout
, int nr_cols
, int nr_rows
,
415 m_uiout
->table_begin (nr_cols
, nr_rows
, tblid
);
418 ~ui_out_emit_table ()
420 m_uiout
->table_end ();
423 ui_out_emit_table (const ui_out_emit_table
&) = delete;
424 ui_out_emit_table
&operator= (const ui_out_emit_table
&) = delete;
428 struct ui_out
*m_uiout
;
431 /* On construction, redirect a uiout to a given stream. On
432 destruction, pop the last redirection by calling the uiout's
433 redirect method with a NULL parameter. */
434 class ui_out_redirect_pop
438 ui_out_redirect_pop (ui_out
*uiout
, ui_file
*stream
)
441 m_uiout
->redirect (stream
);
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 */