Commit | Line | Data |
---|---|---|
8b93c638 | 1 | /* Output generating routines for GDB. |
bee0189a | 2 | |
42a4f53d | 3 | Copyright (C) 1999-2019 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 |
31 | class ui_out_level; |
32 | class ui_out_table; | |
e6e5e94c | 33 | struct 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 |
39 | extern struct ui_out **current_ui_current_uiout_ptr (void); |
40 | #define current_uiout (*current_ui_current_uiout_ptr ()) | |
8b93c638 JM |
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 */ | |
bef721e2 | 52 | enum 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 | 64 | DEF_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 | |
71 | enum 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. */ |
78 | enum class field_kind | |
79 | { | |
80 | SIGNED, | |
81 | 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::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::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 | ||
112e8700 SM |
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 | ||
381befee TT |
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. */ | |
1f77b012 | 186 | void field_unsigned (const char *fldname, ULONGEST value); |
112e8700 SM |
187 | void field_core_addr (const char *fldname, struct gdbarch *gdbarch, |
188 | CORE_ADDR address); | |
cbe56571 | 189 | void field_string (const char *fldname, const char *string, |
e43b10e1 | 190 | const ui_file_style &style = ui_file_style ()); |
6fb16ce6 | 191 | void field_string (const char *fldname, const std::string &string); |
cbe56571 | 192 | void field_stream (const char *fldname, string_file &stream, |
e43b10e1 | 193 | const ui_file_style &style = ui_file_style ()); |
112e8700 SM |
194 | void field_skip (const char *fldname); |
195 | void field_fmt (const char *fldname, const char *format, ...) | |
196 | ATTRIBUTE_PRINTF (3, 4); | |
7f6aba03 TT |
197 | void field_fmt (const char *fldname, const ui_file_style &style, |
198 | const char *format, ...) | |
199 | ATTRIBUTE_PRINTF (4, 5); | |
112e8700 SM |
200 | |
201 | void spaces (int numspaces); | |
202 | void text (const char *string); | |
2a3c1174 PA |
203 | |
204 | /* Output a printf-style formatted string. In addition to the usual | |
205 | printf format specs, this supports a few GDB-specific | |
206 | formatters: | |
207 | ||
208 | - '%pF' - output a field. | |
209 | ||
210 | The argument is a field, wrapped in any of the base_field_s | |
211 | subclasses. signed_field for integer fields, string_field for | |
212 | string fields. This is preferred over separate | |
213 | uiout->field_signed(), uiout_>field_string() etc. calls when | |
214 | the formatted message is translatable. E.g.: | |
215 | ||
216 | uiout->message (_("\nWatchpoint %pF deleted because the program has " | |
217 | "left the block in\n" | |
218 | "which its expression is valid.\n"), | |
219 | signed_field ("wpnum", b->number)); | |
220 | ||
221 | - '%p[' - output the following text in a specified style. | |
222 | '%p]' - output the following text in the default style. | |
223 | ||
224 | The argument to '%p[' is a ui_file_style pointer. The argument | |
225 | to '%p]' must be nullptr. | |
226 | ||
227 | This is useful when you want to output some portion of a string | |
228 | literal in some style. E.g.: | |
229 | ||
230 | uiout->message (_(" %p[<repeats %u times>%p]"), | |
231 | metadata_style.style ().ptr (), | |
232 | reps, repeats, nullptr); | |
233 | ||
234 | - '%ps' - output a styled string. | |
235 | ||
236 | The argument is the result of a call to styled_string. This is | |
237 | useful when you want to output some runtime-generated string in | |
238 | some style. E.g.: | |
239 | ||
240 | uiout->message (_("this is a target address %ps.\n"), | |
241 | styled_string (address_style.style (), | |
242 | paddress (gdbarch, pc))); | |
243 | ||
244 | Note that these all "abuse" the %p printf format spec, in order | |
245 | to be compatible with GCC's printf format checking. This is OK | |
246 | because code in GDB that wants to print a host address should use | |
247 | host_address_to_string instead of %p. */ | |
112e8700 | 248 | void message (const char *format, ...) ATTRIBUTE_PRINTF (2, 3); |
2a3c1174 PA |
249 | void vmessage (const ui_file_style &in_style, |
250 | const char *format, va_list args) ATTRIBUTE_PRINTF (3, 0); | |
251 | ||
112e8700 SM |
252 | void wrap_hint (const char *identstring); |
253 | ||
254 | void flush (); | |
255 | ||
256 | /* Redirect the output of a ui_out object temporarily. */ | |
7becfd03 | 257 | void redirect (ui_file *outstream); |
112e8700 SM |
258 | |
259 | ui_out_flags test_flags (ui_out_flags mask); | |
260 | ||
261 | /* HACK: Code in GDB is currently checking to see the type of ui_out | |
262 | builder when determining which output to produce. This function is | |
263 | a hack to encapsulate that test. Once GDB manages to separate the | |
264 | CLI/MI from the core of GDB the problem should just go away .... */ | |
265 | ||
4904c3c6 | 266 | bool is_mi_like_p () const; |
112e8700 SM |
267 | |
268 | bool query_table_field (int colno, int *width, int *alignment, | |
269 | const char **col_name); | |
270 | ||
046bebe1 TT |
271 | /* Return true if this stream is prepared to handle style |
272 | escapes. */ | |
273 | virtual bool can_emit_style_escape () const = 0; | |
274 | ||
112e8700 SM |
275 | protected: |
276 | ||
277 | virtual void do_table_begin (int nbrofcols, int nr_rows, const char *tblid) | |
278 | = 0; | |
279 | virtual void do_table_body () = 0; | |
280 | virtual void do_table_end () = 0; | |
281 | virtual void do_table_header (int width, ui_align align, | |
282 | const std::string &col_name, | |
283 | const std::string &col_hdr) = 0; | |
284 | ||
285 | virtual void do_begin (ui_out_type type, const char *id) = 0; | |
286 | virtual void do_end (ui_out_type type) = 0; | |
381befee TT |
287 | virtual void do_field_signed (int fldno, int width, ui_align align, |
288 | const char *fldname, LONGEST value) = 0; | |
1f77b012 TT |
289 | virtual void do_field_unsigned (int fldno, int width, ui_align align, |
290 | const char *fldname, ULONGEST value) = 0; | |
112e8700 SM |
291 | virtual void do_field_skip (int fldno, int width, ui_align align, |
292 | const char *fldname) = 0; | |
293 | virtual void do_field_string (int fldno, int width, ui_align align, | |
cbe56571 | 294 | const char *fldname, const char *string, |
e43b10e1 | 295 | const ui_file_style &style) = 0; |
112e8700 | 296 | virtual void do_field_fmt (int fldno, int width, ui_align align, |
7f6aba03 TT |
297 | const char *fldname, const ui_file_style &style, |
298 | const char *format, va_list args) | |
299 | ATTRIBUTE_PRINTF (7, 0) = 0; | |
112e8700 SM |
300 | virtual void do_spaces (int numspaces) = 0; |
301 | virtual void do_text (const char *string) = 0; | |
2a3c1174 PA |
302 | virtual void do_message (const ui_file_style &style, |
303 | const char *format, va_list args) | |
304 | ATTRIBUTE_PRINTF (3,0) = 0; | |
112e8700 SM |
305 | virtual void do_wrap_hint (const char *identstring) = 0; |
306 | virtual void do_flush () = 0; | |
7becfd03 | 307 | virtual void do_redirect (struct ui_file *outstream) = 0; |
112e8700 SM |
308 | |
309 | /* Set as not MI-like by default. It is overridden in subclasses if | |
310 | necessary. */ | |
311 | ||
4904c3c6 | 312 | virtual bool do_is_mi_like_p () const |
112e8700 SM |
313 | { return false; } |
314 | ||
315 | private: | |
2a3c1174 PA |
316 | void call_do_message (const ui_file_style &style, const char *format, |
317 | ...); | |
112e8700 SM |
318 | |
319 | ui_out_flags m_flags; | |
320 | ||
321 | /* Vector to store and track the ui-out levels. */ | |
322 | std::vector<std::unique_ptr<ui_out_level>> m_levels; | |
323 | ||
324 | /* A table, if any. At present only a single table is supported. */ | |
325 | std::unique_ptr<ui_out_table> m_table_up; | |
326 | ||
327 | void verify_field (int *fldno, int *width, ui_align *align); | |
328 | ||
329 | int level () const; | |
330 | ui_out_level *current_level () const; | |
331 | }; | |
0fac0b41 | 332 | |
296bd123 TT |
333 | /* Start a new tuple or list on construction, and end it on |
334 | destruction. Normally this is used via the typedefs | |
335 | ui_out_emit_tuple and ui_out_emit_list. */ | |
d4b0bb18 TT |
336 | template<ui_out_type Type> |
337 | class ui_out_emit_type | |
338 | { | |
339 | public: | |
340 | ||
341 | ui_out_emit_type (struct ui_out *uiout, const char *id) | |
342 | : m_uiout (uiout) | |
343 | { | |
344 | uiout->begin (Type, id); | |
345 | } | |
346 | ||
347 | ~ui_out_emit_type () | |
348 | { | |
349 | m_uiout->end (Type); | |
350 | } | |
351 | ||
d6541620 | 352 | DISABLE_COPY_AND_ASSIGN (ui_out_emit_type<Type>); |
d4b0bb18 TT |
353 | |
354 | private: | |
355 | ||
356 | struct ui_out *m_uiout; | |
357 | }; | |
358 | ||
359 | typedef ui_out_emit_type<ui_out_type_tuple> ui_out_emit_tuple; | |
360 | typedef ui_out_emit_type<ui_out_type_list> ui_out_emit_list; | |
361 | ||
dc9fe180 TT |
362 | /* Start a new table on construction, and end the table on |
363 | destruction. */ | |
4a2b031d TT |
364 | class ui_out_emit_table |
365 | { | |
366 | public: | |
367 | ||
368 | ui_out_emit_table (struct ui_out *uiout, int nr_cols, int nr_rows, | |
369 | const char *tblid) | |
370 | : m_uiout (uiout) | |
371 | { | |
372 | m_uiout->table_begin (nr_cols, nr_rows, tblid); | |
373 | } | |
374 | ||
375 | ~ui_out_emit_table () | |
376 | { | |
377 | m_uiout->table_end (); | |
378 | } | |
379 | ||
380 | ui_out_emit_table (const ui_out_emit_table &) = delete; | |
381 | ui_out_emit_table &operator= (const ui_out_emit_table &) = delete; | |
382 | ||
383 | private: | |
384 | ||
385 | struct ui_out *m_uiout; | |
386 | }; | |
387 | ||
ca5909c7 TT |
388 | /* On destruction, pop the last redirection by calling the uiout's |
389 | redirect method with a NULL parameter. */ | |
390 | class ui_out_redirect_pop | |
391 | { | |
392 | public: | |
393 | ||
394 | ui_out_redirect_pop (ui_out *uiout) | |
395 | : m_uiout (uiout) | |
396 | { | |
397 | } | |
398 | ||
399 | ~ui_out_redirect_pop () | |
400 | { | |
401 | m_uiout->redirect (NULL); | |
402 | } | |
403 | ||
404 | ui_out_redirect_pop (const ui_out_redirect_pop &) = delete; | |
405 | ui_out_redirect_pop &operator= (const ui_out_redirect_pop &) = delete; | |
406 | ||
407 | private: | |
408 | struct ui_out *m_uiout; | |
409 | }; | |
410 | ||
8b93c638 | 411 | #endif /* UI_OUT_H */ |