Add NEWS entry.
[deliverable/binutils-gdb.git] / gdb / ui-out.h
CommitLineData
8b93c638 1/* Output generating routines for GDB.
bee0189a 2
3666a048 3 Copyright (C) 1999-2021 Free Software Foundation, Inc.
bee0189a 4
8b93c638
JM
5 Contributed by Cygnus Solutions.
6 Written by Fernando Nasser for Cygnus.
7
8 This file is part of GDB.
9
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
a9762ec7 12 the Free Software Foundation; either version 3 of the License, or
8b93c638
JM
13 (at your option) any later version.
14
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.
19
20 You should have received a copy of the GNU General Public License
a9762ec7 21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
8b93c638
JM
22
23#ifndef UI_OUT_H
24#define UI_OUT_H 1
25
112e8700 26#include <vector>
bef721e2 27
268a13a5 28#include "gdbsupport/enum-flags.h"
e43b10e1 29#include "ui-style.h"
8b93c638 30
112e8700
SM
31class ui_out_level;
32class ui_out_table;
e6e5e94c 33struct ui_file;
8b93c638
JM
34
35/* the current ui_out */
36
37/* FIXME: This should not be a global but something passed down from main.c
581e13c1 38 or top.c. */
b6dcde57
PA
39extern struct ui_out **current_ui_current_uiout_ptr (void);
40#define current_uiout (*current_ui_current_uiout_ptr ())
8b93c638
JM
41
42/* alignment enum */
43enum ui_align
44 {
45 ui_left = -1,
46 ui_center,
47 ui_right,
48 ui_noalign
49 };
50
51/* flags enum */
bef721e2 52enum ui_out_flag
13674803
SM
53{
54 ui_source_list = (1 << 0),
55 fix_multi_location_breakpoint_output = (1 << 1),
2a3c1174
PA
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
60 string. */
61 disallow_ui_out_field = (1 << 3)
13674803 62};
8b93c638 63
bef721e2 64DEF_ENUM_FLAGS_TYPE (ui_out_flag, ui_out_flags);
8b93c638 65
581e13c1 66/* Prototypes for ui-out API. */
8b93c638 67
631ec795 68/* A result is a recursive data structure consisting of lists and
581e13c1 69 tuples. */
631ec795
AC
70
71enum ui_out_type
72 {
666547aa 73 ui_out_type_tuple,
631ec795
AC
74 ui_out_type_list
75 };
76
2a3c1174
PA
77/* The possible kinds of fields. */
78enum class field_kind
79 {
2032eb7e
TT
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. */
28ce7b07
TT
83 FIELD_SIGNED,
84 FIELD_STRING,
2a3c1174
PA
85 };
86
87/* The base type of all fields that can be emitted using %pF. */
88
89struct base_field_s
90{
91 const char *name;
92 field_kind kind;
93};
94
95/* A signed integer field, to be passed to %pF in format strings. */
96
97struct signed_field_s : base_field_s
98{
99 LONGEST val;
100};
101
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. */
105
106static inline signed_field_s *
107signed_field (const char *name, LONGEST val,
108 signed_field_s &&tmp = {})
109{
110 tmp.name = name;
28ce7b07 111 tmp.kind = field_kind::FIELD_SIGNED;
2a3c1174
PA
112 tmp.val = val;
113 return &tmp;
114}
115
116/* A string field, to be passed to %pF in format strings. */
117
118struct string_field_s : base_field_s
119{
120 const char *str;
121};
122
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. */
126
127static inline string_field_s *
128string_field (const char *name, const char *str,
129 string_field_s &&tmp = {})
130{
131 tmp.name = name;
28ce7b07 132 tmp.kind = field_kind::FIELD_STRING;
2a3c1174
PA
133 tmp.str = str;
134 return &tmp;
135}
136
137/* A styled string. */
138
139struct styled_string_s
140{
141 /* The style. */
142 ui_file_style style;
143
144 /* The string. */
145 const char *str;
146};
147
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. */
151
152static inline styled_string_s *
153styled_string (const ui_file_style &style, const char *str,
154 styled_string_s &&tmp = {})
155{
156 tmp.style = style;
157 tmp.str = str;
158 return &tmp;
159}
160
112e8700
SM
161class ui_out
162{
163 public:
164
165 explicit ui_out (ui_out_flags flags = 0);
166 virtual ~ui_out ();
167
168 void push_level (ui_out_type type);
169 void pop_level (ui_out_type type);
170
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
174 least one row. */
175
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);
179 void table_body ();
180 void table_end ();
181
182 void begin (ui_out_type type, const char *id);
183 void end (ui_out_type type);
184
381befee
TT
185 void field_signed (const char *fldname, LONGEST value);
186 void field_fmt_signed (int width, ui_align align, const char *fldname,
187 LONGEST value);
188 /* Like field_signed, but print an unsigned value. */
1f77b012 189 void field_unsigned (const char *fldname, ULONGEST value);
112e8700
SM
190 void field_core_addr (const char *fldname, struct gdbarch *gdbarch,
191 CORE_ADDR address);
cbe56571 192 void field_string (const char *fldname, const char *string,
e43b10e1 193 const ui_file_style &style = ui_file_style ());
6fb16ce6 194 void field_string (const char *fldname, const std::string &string);
cbe56571 195 void field_stream (const char *fldname, string_file &stream,
e43b10e1 196 const ui_file_style &style = ui_file_style ());
112e8700
SM
197 void field_skip (const char *fldname);
198 void field_fmt (const char *fldname, const char *format, ...)
199 ATTRIBUTE_PRINTF (3, 4);
7f6aba03
TT
200 void field_fmt (const char *fldname, const ui_file_style &style,
201 const char *format, ...)
202 ATTRIBUTE_PRINTF (4, 5);
112e8700
SM
203
204 void spaces (int numspaces);
205 void text (const char *string);
2a3c1174
PA
206
207 /* Output a printf-style formatted string. In addition to the usual
208 printf format specs, this supports a few GDB-specific
209 formatters:
210
211 - '%pF' - output a field.
212
213 The argument is a field, wrapped in any of the base_field_s
214 subclasses. signed_field for integer fields, string_field for
215 string fields. This is preferred over separate
216 uiout->field_signed(), uiout_>field_string() etc. calls when
217 the formatted message is translatable. E.g.:
218
dda83cd7
SM
219 uiout->message (_("\nWatchpoint %pF deleted because the program has "
220 "left the block in\n"
221 "which its expression is valid.\n"),
222 signed_field ("wpnum", b->number));
2a3c1174
PA
223
224 - '%p[' - output the following text in a specified style.
225 '%p]' - output the following text in the default style.
226
227 The argument to '%p[' is a ui_file_style pointer. The argument
228 to '%p]' must be nullptr.
229
230 This is useful when you want to output some portion of a string
231 literal in some style. E.g.:
232
233 uiout->message (_(" %p[<repeats %u times>%p]"),
234 metadata_style.style ().ptr (),
235 reps, repeats, nullptr);
236
237 - '%ps' - output a styled string.
238
239 The argument is the result of a call to styled_string. This is
240 useful when you want to output some runtime-generated string in
241 some style. E.g.:
242
243 uiout->message (_("this is a target address %ps.\n"),
244 styled_string (address_style.style (),
245 paddress (gdbarch, pc)));
246
247 Note that these all "abuse" the %p printf format spec, in order
248 to be compatible with GCC's printf format checking. This is OK
249 because code in GDB that wants to print a host address should use
250 host_address_to_string instead of %p. */
112e8700 251 void message (const char *format, ...) ATTRIBUTE_PRINTF (2, 3);
2a3c1174
PA
252 void vmessage (const ui_file_style &in_style,
253 const char *format, va_list args) ATTRIBUTE_PRINTF (3, 0);
254
112e8700
SM
255 void wrap_hint (const char *identstring);
256
257 void flush ();
258
259 /* Redirect the output of a ui_out object temporarily. */
7becfd03 260 void redirect (ui_file *outstream);
112e8700
SM
261
262 ui_out_flags test_flags (ui_out_flags mask);
263
264 /* HACK: Code in GDB is currently checking to see the type of ui_out
265 builder when determining which output to produce. This function is
266 a hack to encapsulate that test. Once GDB manages to separate the
267 CLI/MI from the core of GDB the problem should just go away .... */
268
4904c3c6 269 bool is_mi_like_p () const;
112e8700
SM
270
271 bool query_table_field (int colno, int *width, int *alignment,
272 const char **col_name);
273
046bebe1
TT
274 /* Return true if this stream is prepared to handle style
275 escapes. */
276 virtual bool can_emit_style_escape () const = 0;
277
2f228731
TT
278 /* An object that starts and finishes a progress meter. */
279 class progress_meter
280 {
281 public:
282 /* SHOULD_PRINT indicates whether something should be printed for a tty. */
283 progress_meter (struct ui_out *uiout, const std::string &name,
284 bool should_print)
285 : m_uiout (uiout)
286 {
287 m_uiout->do_progress_start (name, should_print);
288 }
289
290 ~progress_meter ()
291 {
292 m_uiout->do_progress_notify (1.0);
293 m_uiout->do_progress_end ();
294 }
295
296 progress_meter (const progress_meter &) = delete;
297 progress_meter &operator= (const progress_meter &) = delete;
298
299 private:
300
301 struct ui_out *m_uiout;
302 };
303
304 /* Emit some progress corresponding to the most recently created
305 progress meter. HOWMUCH may range from 0.0 to 1.0. */
306 void progress (double howmuch)
307 {
308 do_progress_notify (howmuch);
309 }
310
112e8700
SM
311 protected:
312
313 virtual void do_table_begin (int nbrofcols, int nr_rows, const char *tblid)
314 = 0;
315 virtual void do_table_body () = 0;
316 virtual void do_table_end () = 0;
317 virtual void do_table_header (int width, ui_align align,
318 const std::string &col_name,
319 const std::string &col_hdr) = 0;
320
321 virtual void do_begin (ui_out_type type, const char *id) = 0;
322 virtual void do_end (ui_out_type type) = 0;
381befee
TT
323 virtual void do_field_signed (int fldno, int width, ui_align align,
324 const char *fldname, LONGEST value) = 0;
1f77b012
TT
325 virtual void do_field_unsigned (int fldno, int width, ui_align align,
326 const char *fldname, ULONGEST value) = 0;
112e8700
SM
327 virtual void do_field_skip (int fldno, int width, ui_align align,
328 const char *fldname) = 0;
329 virtual void do_field_string (int fldno, int width, ui_align align,
cbe56571 330 const char *fldname, const char *string,
e43b10e1 331 const ui_file_style &style) = 0;
112e8700 332 virtual void do_field_fmt (int fldno, int width, ui_align align,
7f6aba03
TT
333 const char *fldname, const ui_file_style &style,
334 const char *format, va_list args)
335 ATTRIBUTE_PRINTF (7, 0) = 0;
112e8700
SM
336 virtual void do_spaces (int numspaces) = 0;
337 virtual void do_text (const char *string) = 0;
2a3c1174
PA
338 virtual void do_message (const ui_file_style &style,
339 const char *format, va_list args)
340 ATTRIBUTE_PRINTF (3,0) = 0;
112e8700
SM
341 virtual void do_wrap_hint (const char *identstring) = 0;
342 virtual void do_flush () = 0;
7becfd03 343 virtual void do_redirect (struct ui_file *outstream) = 0;
112e8700 344
2f228731
TT
345 virtual void do_progress_start (const std::string &, bool) = 0;
346 virtual void do_progress_notify (double) = 0;
347 virtual void do_progress_end () = 0;
348
112e8700
SM
349 /* Set as not MI-like by default. It is overridden in subclasses if
350 necessary. */
351
4904c3c6 352 virtual bool do_is_mi_like_p () const
112e8700
SM
353 { return false; }
354
355 private:
2a3c1174
PA
356 void call_do_message (const ui_file_style &style, const char *format,
357 ...);
112e8700
SM
358
359 ui_out_flags m_flags;
360
361 /* Vector to store and track the ui-out levels. */
362 std::vector<std::unique_ptr<ui_out_level>> m_levels;
363
364 /* A table, if any. At present only a single table is supported. */
365 std::unique_ptr<ui_out_table> m_table_up;
366
367 void verify_field (int *fldno, int *width, ui_align *align);
368
369 int level () const;
370 ui_out_level *current_level () const;
371};
0fac0b41 372
296bd123
TT
373/* Start a new tuple or list on construction, and end it on
374 destruction. Normally this is used via the typedefs
375 ui_out_emit_tuple and ui_out_emit_list. */
d4b0bb18
TT
376template<ui_out_type Type>
377class ui_out_emit_type
378{
379public:
380
381 ui_out_emit_type (struct ui_out *uiout, const char *id)
382 : m_uiout (uiout)
383 {
384 uiout->begin (Type, id);
385 }
386
387 ~ui_out_emit_type ()
388 {
389 m_uiout->end (Type);
390 }
391
d6541620 392 DISABLE_COPY_AND_ASSIGN (ui_out_emit_type<Type>);
d4b0bb18
TT
393
394private:
395
396 struct ui_out *m_uiout;
397};
398
399typedef ui_out_emit_type<ui_out_type_tuple> ui_out_emit_tuple;
400typedef ui_out_emit_type<ui_out_type_list> ui_out_emit_list;
401
dc9fe180
TT
402/* Start a new table on construction, and end the table on
403 destruction. */
4a2b031d
TT
404class ui_out_emit_table
405{
406public:
407
408 ui_out_emit_table (struct ui_out *uiout, int nr_cols, int nr_rows,
409 const char *tblid)
410 : m_uiout (uiout)
411 {
412 m_uiout->table_begin (nr_cols, nr_rows, tblid);
413 }
414
415 ~ui_out_emit_table ()
416 {
417 m_uiout->table_end ();
418 }
419
420 ui_out_emit_table (const ui_out_emit_table &) = delete;
421 ui_out_emit_table &operator= (const ui_out_emit_table &) = delete;
422
423private:
424
425 struct ui_out *m_uiout;
426};
427
ca5909c7
TT
428/* On destruction, pop the last redirection by calling the uiout's
429 redirect method with a NULL parameter. */
430class ui_out_redirect_pop
431{
432public:
433
434 ui_out_redirect_pop (ui_out *uiout)
435 : m_uiout (uiout)
436 {
437 }
438
439 ~ui_out_redirect_pop ()
440 {
441 m_uiout->redirect (NULL);
442 }
443
444 ui_out_redirect_pop (const ui_out_redirect_pop &) = delete;
445 ui_out_redirect_pop &operator= (const ui_out_redirect_pop &) = delete;
446
447private:
448 struct ui_out *m_uiout;
449};
450
8b93c638 451#endif /* UI_OUT_H */
This page took 2.153882 seconds and 4 git commands to generate.