Pre-read DWARF section data
[binutils-gdb.git] / gdb / ui-file.c
1 /* UI_FILE - a generic STDIO like output stream.
2
3 Copyright (C) 1999-2022 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* Implement the ``struct ui_file'' object. */
21
22 #include "defs.h"
23 #include "ui-file.h"
24 #include "gdbsupport/gdb_obstack.h"
25 #include "gdbsupport/gdb_select.h"
26 #include "gdbsupport/filestuff.h"
27 #include "cli-out.h"
28 #include "cli/cli-style.h"
29 #include <chrono>
30
31 null_file null_stream;
32
33 ui_file::ui_file ()
34 {}
35
36 ui_file::~ui_file ()
37 {}
38
39 void
40 ui_file::printf (const char *format, ...)
41 {
42 va_list args;
43
44 va_start (args, format);
45 vprintf (format, args);
46 va_end (args);
47 }
48
49 void
50 ui_file::putstr (const char *str, int quoter)
51 {
52 while (*str)
53 printchar (*str++, quoter, false);
54 }
55
56 void
57 ui_file::putstrn (const char *str, int n, int quoter, bool async_safe)
58 {
59 for (int i = 0; i < n; i++)
60 printchar (str[i], quoter, async_safe);
61 }
62
63 int
64 ui_file::putc (int c)
65 {
66 char copy = (char) c;
67 write (&copy, 1);
68 return c;
69 }
70
71 void
72 ui_file::vprintf (const char *format, va_list args)
73 {
74 ui_out_flags flags = disallow_ui_out_field;
75 cli_ui_out (this, flags).vmessage (m_applied_style, format, args);
76 }
77
78 /* See ui-file.h. */
79
80 void
81 ui_file::emit_style_escape (const ui_file_style &style)
82 {
83 if (can_emit_style_escape () && style != m_applied_style)
84 {
85 m_applied_style = style;
86 this->puts (style.to_ansi ().c_str ());
87 }
88 }
89
90 /* See ui-file.h. */
91
92 void
93 ui_file::reset_style ()
94 {
95 if (can_emit_style_escape ())
96 {
97 m_applied_style = ui_file_style ();
98 this->puts (m_applied_style.to_ansi ().c_str ());
99 }
100 }
101
102 /* See ui-file.h. */
103
104 void
105 ui_file::printchar (int c, int quoter, bool async_safe)
106 {
107 char buf[4];
108 int out = 0;
109
110 c &= 0xFF; /* Avoid sign bit follies */
111
112 if (c < 0x20 /* Low control chars */
113 || (c >= 0x7F && c < 0xA0) /* DEL, High controls */
114 || (sevenbit_strings && c >= 0x80))
115 { /* high order bit set */
116 buf[out++] = '\\';
117
118 switch (c)
119 {
120 case '\n':
121 buf[out++] = 'n';
122 break;
123 case '\b':
124 buf[out++] = 'b';
125 break;
126 case '\t':
127 buf[out++] = 't';
128 break;
129 case '\f':
130 buf[out++] = 'f';
131 break;
132 case '\r':
133 buf[out++] = 'r';
134 break;
135 case '\033':
136 buf[out++] = 'e';
137 break;
138 case '\007':
139 buf[out++] = 'a';
140 break;
141 default:
142 {
143 buf[out++] = '0' + ((c >> 6) & 0x7);
144 buf[out++] = '0' + ((c >> 3) & 0x7);
145 buf[out++] = '0' + ((c >> 0) & 0x7);
146 break;
147 }
148 }
149 }
150 else
151 {
152 if (quoter != 0 && (c == '\\' || c == quoter))
153 buf[out++] = '\\';
154 buf[out++] = c;
155 }
156
157 if (async_safe)
158 this->write_async_safe (buf, out);
159 else
160 this->write (buf, out);
161 }
162
163 \f
164
165 void
166 null_file::write (const char *buf, long sizeof_buf)
167 {
168 /* Discard the request. */
169 }
170
171 void
172 null_file::puts (const char *)
173 {
174 /* Discard the request. */
175 }
176
177 void
178 null_file::write_async_safe (const char *buf, long sizeof_buf)
179 {
180 /* Discard the request. */
181 }
182
183 \f
184
185 /* true if the gdb terminal supports styling, and styling is enabled. */
186
187 static bool
188 term_cli_styling ()
189 {
190 if (!cli_styling)
191 return false;
192
193 const char *term = getenv ("TERM");
194 /* Windows doesn't by default define $TERM, but can support styles
195 regardless. */
196 #ifndef _WIN32
197 if (term == nullptr || !strcmp (term, "dumb"))
198 return false;
199 #else
200 /* But if they do define $TERM, let us behave the same as on Posix
201 platforms, for the benefit of programs which invoke GDB as their
202 back-end. */
203 if (term && !strcmp (term, "dumb"))
204 return false;
205 #endif
206 return true;
207 }
208
209 \f
210
211 string_file::~string_file ()
212 {}
213
214 void
215 string_file::write (const char *buf, long length_buf)
216 {
217 m_string.append (buf, length_buf);
218 }
219
220 /* See ui-file.h. */
221
222 bool
223 string_file::term_out ()
224 {
225 return m_term_out;
226 }
227
228 /* See ui-file.h. */
229
230 bool
231 string_file::can_emit_style_escape ()
232 {
233 return m_term_out && term_cli_styling ();
234 }
235
236 \f
237
238 stdio_file::stdio_file (FILE *file, bool close_p)
239 {
240 set_stream (file);
241 m_close_p = close_p;
242 }
243
244 stdio_file::stdio_file ()
245 : m_file (NULL),
246 m_fd (-1),
247 m_close_p (false)
248 {}
249
250 stdio_file::~stdio_file ()
251 {
252 if (m_close_p)
253 fclose (m_file);
254 }
255
256 void
257 stdio_file::set_stream (FILE *file)
258 {
259 m_file = file;
260 m_fd = fileno (file);
261 }
262
263 bool
264 stdio_file::open (const char *name, const char *mode)
265 {
266 /* Close the previous stream, if we own it. */
267 if (m_close_p)
268 {
269 fclose (m_file);
270 m_close_p = false;
271 }
272
273 gdb_file_up f = gdb_fopen_cloexec (name, mode);
274
275 if (f == NULL)
276 return false;
277
278 set_stream (f.release ());
279 m_close_p = true;
280
281 return true;
282 }
283
284 void
285 stdio_file::flush ()
286 {
287 fflush (m_file);
288 }
289
290 long
291 stdio_file::read (char *buf, long length_buf)
292 {
293 /* Wait until at least one byte of data is available, or we get
294 interrupted with Control-C. */
295 {
296 fd_set readfds;
297
298 FD_ZERO (&readfds);
299 FD_SET (m_fd, &readfds);
300 if (interruptible_select (m_fd + 1, &readfds, NULL, NULL, NULL) == -1)
301 return -1;
302 }
303
304 return ::read (m_fd, buf, length_buf);
305 }
306
307 void
308 stdio_file::write (const char *buf, long length_buf)
309 {
310 /* Calling error crashes when we are called from the exception framework. */
311 if (fwrite (buf, length_buf, 1, m_file))
312 {
313 /* Nothing. */
314 }
315 }
316
317 void
318 stdio_file::write_async_safe (const char *buf, long length_buf)
319 {
320 /* This is written the way it is to avoid a warning from gcc about not using the
321 result of write (since it can be declared with attribute warn_unused_result).
322 Alas casting to void doesn't work for this. */
323 if (::write (m_fd, buf, length_buf))
324 {
325 /* Nothing. */
326 }
327 }
328
329 void
330 stdio_file::puts (const char *linebuffer)
331 {
332 /* This host-dependent function (with implementations in
333 posix-hdep.c and mingw-hdep.c) is given the opportunity to
334 process the output first in host-dependent way. If it does, it
335 should return non-zero, to avoid calling fputs below. */
336 if (gdb_console_fputs (linebuffer, m_file))
337 return;
338 /* Calling error crashes when we are called from the exception framework. */
339 if (fputs (linebuffer, m_file))
340 {
341 /* Nothing. */
342 }
343 }
344
345 bool
346 stdio_file::isatty ()
347 {
348 return ::isatty (m_fd);
349 }
350
351 /* See ui-file.h. */
352
353 bool
354 stdio_file::can_emit_style_escape ()
355 {
356 return (this->isatty ()
357 && term_cli_styling ());
358 }
359
360 \f
361
362 /* This is the implementation of ui_file method 'write' for stderr.
363 gdb_stdout is flushed before writing to gdb_stderr. */
364
365 void
366 stderr_file::write (const char *buf, long length_buf)
367 {
368 gdb_stdout->flush ();
369 stdio_file::write (buf, length_buf);
370 }
371
372 /* This is the implementation of ui_file method 'puts' for stderr.
373 gdb_stdout is flushed before writing to gdb_stderr. */
374
375 void
376 stderr_file::puts (const char *linebuffer)
377 {
378 gdb_stdout->flush ();
379 stdio_file::puts (linebuffer);
380 }
381
382 stderr_file::stderr_file (FILE *stream)
383 : stdio_file (stream)
384 {}
385
386 \f
387
388 tee_file::tee_file (ui_file *one, ui_file_up &&two)
389 : m_one (one),
390 m_two (std::move (two))
391 {}
392
393 tee_file::~tee_file ()
394 {
395 }
396
397 void
398 tee_file::flush ()
399 {
400 m_one->flush ();
401 m_two->flush ();
402 }
403
404 void
405 tee_file::write (const char *buf, long length_buf)
406 {
407 m_one->write (buf, length_buf);
408 m_two->write (buf, length_buf);
409 }
410
411 void
412 tee_file::write_async_safe (const char *buf, long length_buf)
413 {
414 m_one->write_async_safe (buf, length_buf);
415 m_two->write_async_safe (buf, length_buf);
416 }
417
418 void
419 tee_file::puts (const char *linebuffer)
420 {
421 m_one->puts (linebuffer);
422 m_two->puts (linebuffer);
423 }
424
425 bool
426 tee_file::isatty ()
427 {
428 return m_one->isatty ();
429 }
430
431 /* See ui-file.h. */
432
433 bool
434 tee_file::term_out ()
435 {
436 return m_one->term_out ();
437 }
438
439 /* See ui-file.h. */
440
441 bool
442 tee_file::can_emit_style_escape ()
443 {
444 return (m_one->term_out ()
445 && term_cli_styling ());
446 }
447
448 /* See ui-file.h. */
449
450 void
451 no_terminal_escape_file::write (const char *buf, long length_buf)
452 {
453 std::string copy (buf, length_buf);
454 this->puts (copy.c_str ());
455 }
456
457 /* See ui-file.h. */
458
459 void
460 no_terminal_escape_file::puts (const char *buf)
461 {
462 while (*buf != '\0')
463 {
464 const char *esc = strchr (buf, '\033');
465 if (esc == nullptr)
466 break;
467
468 int n_read = 0;
469 if (!skip_ansi_escape (esc, &n_read))
470 ++esc;
471
472 this->stdio_file::write (buf, esc - buf);
473 buf = esc + n_read;
474 }
475
476 if (*buf != '\0')
477 this->stdio_file::write (buf, strlen (buf));
478 }
479
480 void
481 timestamped_file::write (const char *buf, long len)
482 {
483 if (debug_timestamp)
484 {
485 /* Print timestamp if previous print ended with a \n. */
486 if (m_needs_timestamp)
487 {
488 using namespace std::chrono;
489
490 steady_clock::time_point now = steady_clock::now ();
491 seconds s = duration_cast<seconds> (now.time_since_epoch ());
492 microseconds us = duration_cast<microseconds> (now.time_since_epoch () - s);
493 std::string timestamp = string_printf ("%ld.%06ld ",
494 (long) s.count (),
495 (long) us.count ());
496 m_stream->puts (timestamp.c_str ());
497 }
498
499 /* Print the message. */
500 m_stream->write (buf, len);
501
502 m_needs_timestamp = (len > 0 && buf[len - 1] == '\n');
503 }
504 else
505 m_stream->write (buf, len);
506 }