gdb: Add maint set ignore-prologue-end-flag
[binutils-gdb.git] / gdb / ui-out.h
1 /* Output generating routines for GDB.
2
3 Copyright (C) 1999-2022 Free Software Foundation, Inc.
4
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
12 the Free Software Foundation; either version 3 of the License, or
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
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23 #ifndef UI_OUT_H
24 #define UI_OUT_H 1
25
26 #include <vector>
27
28 #include "gdbsupport/enum-flags.h"
29 #include "ui-style.h"
30
31 class ui_out_level;
32 class ui_out_table;
33 struct ui_file;
34
35 /* the current ui_out */
36
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 ())
41
42 /* alignment enum */
43 enum ui_align
44 {
45 ui_left = -1,
46 ui_center,
47 ui_right,
48 ui_noalign
49 };
50
51 /* flags enum */
52 enum ui_out_flag
53 {
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)
59 };
60
61 DEF_ENUM_FLAGS_TYPE (ui_out_flag, ui_out_flags);
62
63 /* Prototypes for ui-out API. */
64
65 /* A result is a recursive data structure consisting of lists and
66 tuples. */
67
68 enum ui_out_type
69 {
70 ui_out_type_tuple,
71 ui_out_type_list
72 };
73
74 /* The possible kinds of fields. */
75 enum class field_kind
76 {
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,
82 };
83
84 /* The base type of all fields that can be emitted using %pF. */
85
86 struct base_field_s
87 {
88 const char *name;
89 field_kind kind;
90 };
91
92 /* A signed integer field, to be passed to %pF in format strings. */
93
94 struct signed_field_s : base_field_s
95 {
96 LONGEST val;
97 };
98
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. */
102
103 static inline signed_field_s *
104 signed_field (const char *name, LONGEST val,
105 signed_field_s &&tmp = {})
106 {
107 tmp.name = name;
108 tmp.kind = field_kind::FIELD_SIGNED;
109 tmp.val = val;
110 return &tmp;
111 }
112
113 /* A string field, to be passed to %pF in format strings. */
114
115 struct string_field_s : base_field_s
116 {
117 const char *str;
118 };
119
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. */
123
124 static inline string_field_s *
125 string_field (const char *name, const char *str,
126 string_field_s &&tmp = {})
127 {
128 tmp.name = name;
129 tmp.kind = field_kind::FIELD_STRING;
130 tmp.str = str;
131 return &tmp;
132 }
133
134 /* A styled string. */
135
136 struct styled_string_s
137 {
138 /* The style. */
139 ui_file_style style;
140
141 /* The string. */
142 const char *str;
143 };
144
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. */
148
149 static inline styled_string_s *
150 styled_string (const ui_file_style &style, const char *str,
151 styled_string_s &&tmp = {})
152 {
153 tmp.style = style;
154 tmp.str = str;
155 return &tmp;
156 }
157
158 class ui_out
159 {
160 public:
161
162 explicit ui_out (ui_out_flags flags = 0);
163 virtual ~ui_out ();
164
165 void push_level (ui_out_type type);
166 void pop_level (ui_out_type type);
167
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. */
172
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 ();
178
179 void begin (ui_out_type type, const char *id);
180 void end (ui_out_type type);
181
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 ())
193 {
194 field_string (fldname, string.c_str (), style);
195 }
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);
204
205 void spaces (int numspaces);
206 void text (const char *string);
207 void text (const std::string &string) { text (string.c_str ()); }
208
209 /* Output a printf-style formatted string. In addition to the usual
210 printf format specs, this supports a few GDB-specific
211 formatters:
212
213 - '%pF' - output a field.
214
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.:
220
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));
225
226 - '%p[' - output the following text in a specified style.
227 '%p]' - output the following text in the default style.
228
229 The argument to '%p[' is a ui_file_style pointer. The argument
230 to '%p]' must be nullptr.
231
232 This is useful when you want to output some portion of a string
233 literal in some style. E.g.:
234
235 uiout->message (_(" %p[<repeats %u times>%p]"),
236 metadata_style.style ().ptr (),
237 reps, repeats, nullptr);
238
239 - '%ps' - output a styled string.
240
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.:
244
245 uiout->message (_("this is a target address %ps.\n"),
246 styled_string (address_style.style (),
247 paddress (gdbarch, pc)));
248
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);
256
257 void wrap_hint (int indent);
258
259 void flush ();
260
261 /* Redirect the output of a ui_out object temporarily. */
262 void redirect (ui_file *outstream);
263
264 ui_out_flags test_flags (ui_out_flags mask);
265
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 .... */
270
271 bool is_mi_like_p () const;
272
273 bool query_table_field (int colno, int *width, int *alignment,
274 const char **col_name);
275
276 /* Return true if this stream is prepared to handle style
277 escapes. */
278 virtual bool can_emit_style_escape () const = 0;
279
280 /* An object that starts and finishes a progress meter. */
281 class progress_meter
282 {
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)
288 {
289 m_uiout->do_progress_start (name, should_print);
290 }
291
292 ~progress_meter ()
293 {
294 m_uiout->do_progress_notify (1.0);
295 m_uiout->do_progress_end ();
296 }
297
298 progress_meter (const progress_meter &) = delete;
299 progress_meter &operator= (const progress_meter &) = delete;
300
301 /* Emit some progress for this progress meter. HOWMUCH may range
302 from 0.0 to 1.0. */
303 void progress (double howmuch)
304 {
305 m_uiout->do_progress_notify (howmuch);
306 }
307
308 private:
309
310 struct ui_out *m_uiout;
311 };
312
313 protected:
314
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;
322
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;
346
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;
350
351 /* Set as not MI-like by default. It is overridden in subclasses if
352 necessary. */
353
354 virtual bool do_is_mi_like_p () const
355 { return false; }
356
357 private:
358 void call_do_message (const ui_file_style &style, const char *format,
359 ...);
360
361 ui_out_flags m_flags;
362
363 /* Vector to store and track the ui-out levels. */
364 std::vector<std::unique_ptr<ui_out_level>> m_levels;
365
366 /* A table, if any. At present only a single table is supported. */
367 std::unique_ptr<ui_out_table> m_table_up;
368
369 void verify_field (int *fldno, int *width, ui_align *align);
370
371 int level () const;
372 ui_out_level *current_level () const;
373 };
374
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
380 {
381 public:
382
383 ui_out_emit_type (struct ui_out *uiout, const char *id)
384 : m_uiout (uiout)
385 {
386 uiout->begin (Type, id);
387 }
388
389 ~ui_out_emit_type ()
390 {
391 m_uiout->end (Type);
392 }
393
394 DISABLE_COPY_AND_ASSIGN (ui_out_emit_type<Type>);
395
396 private:
397
398 struct ui_out *m_uiout;
399 };
400
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;
403
404 /* Start a new table on construction, and end the table on
405 destruction. */
406 class ui_out_emit_table
407 {
408 public:
409
410 ui_out_emit_table (struct ui_out *uiout, int nr_cols, int nr_rows,
411 const char *tblid)
412 : m_uiout (uiout)
413 {
414 m_uiout->table_begin (nr_cols, nr_rows, tblid);
415 }
416
417 ~ui_out_emit_table ()
418 {
419 m_uiout->table_end ();
420 }
421
422 ui_out_emit_table (const ui_out_emit_table &) = delete;
423 ui_out_emit_table &operator= (const ui_out_emit_table &) = delete;
424
425 private:
426
427 struct ui_out *m_uiout;
428 };
429
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
433 {
434 public:
435
436 ui_out_redirect_pop (ui_out *uiout)
437 : m_uiout (uiout)
438 {
439 }
440
441 ~ui_out_redirect_pop ()
442 {
443 m_uiout->redirect (NULL);
444 }
445
446 ui_out_redirect_pop (const ui_out_redirect_pop &) = delete;
447 ui_out_redirect_pop &operator= (const ui_out_redirect_pop &) = delete;
448
449 private:
450 struct ui_out *m_uiout;
451 };
452
453 #endif /* UI_OUT_H */