Home | History | Annotate | Line # | Download | only in gdb
ui-file.h revision 1.12
      1   1.1  christos /* UI_FILE - a generic STDIO like output stream.
      2  1.11  christos    Copyright (C) 1999-2024 Free Software Foundation, Inc.
      3   1.1  christos 
      4   1.1  christos    This file is part of GDB.
      5   1.1  christos 
      6   1.1  christos    This program is free software; you can redistribute it and/or modify
      7   1.1  christos    it under the terms of the GNU General Public License as published by
      8   1.1  christos    the Free Software Foundation; either version 3 of the License, or
      9   1.1  christos    (at your option) any later version.
     10   1.1  christos 
     11   1.1  christos    This program is distributed in the hope that it will be useful,
     12   1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13   1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14   1.1  christos    GNU General Public License for more details.
     15   1.1  christos 
     16   1.1  christos    You should have received a copy of the GNU General Public License
     17   1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     18   1.1  christos 
     19  1.12  christos #ifndef GDB_UI_FILE_H
     20  1.12  christos #define GDB_UI_FILE_H
     21   1.1  christos 
     22   1.7  christos #include <string>
     23   1.8  christos #include "ui-style.h"
     24   1.1  christos 
     25   1.7  christos /* The abstract ui_file base class.  */
     26   1.1  christos 
     27   1.7  christos class ui_file
     28   1.7  christos {
     29   1.7  christos public:
     30   1.7  christos   ui_file ();
     31   1.7  christos   virtual ~ui_file () = 0;
     32   1.1  christos 
     33  1.11  christos   ui_file (ui_file &&other) = default;
     34  1.11  christos 
     35   1.7  christos   /* Public non-virtual API.  */
     36   1.1  christos 
     37   1.7  christos   void printf (const char *, ...) ATTRIBUTE_PRINTF (2, 3);
     38   1.1  christos 
     39  1.10  christos   /* Print a NUL-terminated string whose delimiter is QUOTER.  Note
     40  1.10  christos      that these routines should only be called for printing things
     41  1.10  christos      which are independent of the language of the program being
     42  1.10  christos      debugged.
     43  1.10  christos 
     44  1.10  christos      This will normally escape backslashes and instances of QUOTER.
     45  1.10  christos      If QUOTER is 0, it won't escape backslashes or any quoting
     46  1.10  christos      character.  As a side effect, if you pass the backslash character
     47  1.10  christos      as the QUOTER, this will escape backslashes as usual, but not any
     48  1.10  christos      other quoting character.  */
     49   1.7  christos   void putstr (const char *str, int quoter);
     50   1.1  christos 
     51  1.10  christos   /* Like putstr, but only print the first N characters of STR.  If
     52  1.10  christos      ASYNC_SAFE is true, then the output is done via the
     53  1.10  christos      write_async_safe method.  */
     54  1.10  christos   void putstrn (const char *str, int n, int quoter, bool async_safe = false);
     55   1.1  christos 
     56  1.10  christos   void putc (int c);
     57   1.1  christos 
     58   1.7  christos   void vprintf (const char *, va_list) ATTRIBUTE_PRINTF (2, 0);
     59   1.1  christos 
     60   1.7  christos   /* Methods below are both public, and overridable by ui_file
     61   1.7  christos      subclasses.  */
     62   1.7  christos 
     63   1.7  christos   virtual void write (const char *buf, long length_buf) = 0;
     64   1.7  christos 
     65   1.7  christos   /* This version of "write" is safe for use in signal handlers.  It's
     66   1.7  christos      not guaranteed that all existing output will have been flushed
     67   1.7  christos      first.  Implementations are also free to ignore some or all of
     68   1.7  christos      the request.  puts_async is not provided as the async versions
     69   1.7  christos      are rarely used, no point in having both for a rarely used
     70   1.7  christos      interface.  */
     71   1.7  christos   virtual void write_async_safe (const char *buf, long length_buf)
     72   1.7  christos   { gdb_assert_not_reached ("write_async_safe"); }
     73   1.7  christos 
     74   1.7  christos   /* Some ui_files override this to provide a efficient implementation
     75   1.7  christos      that avoids a strlen.  */
     76   1.7  christos   virtual void puts (const char *str)
     77   1.7  christos   { this->write (str, strlen (str)); }
     78   1.1  christos 
     79   1.7  christos   virtual long read (char *buf, long length_buf)
     80   1.7  christos   { gdb_assert_not_reached ("can't read from this file type"); }
     81   1.1  christos 
     82   1.7  christos   virtual bool isatty ()
     83   1.7  christos   { return false; }
     84   1.3  christos 
     85  1.12  christos   /* true indicates terminal output behavior such as cli_styling.
     86   1.9  christos      This default implementation indicates to do terminal output
     87  1.12  christos      behavior if the UI_FILE is a tty.  A derived class can override
     88  1.12  christos      TERM_OUT to have cli_styling behavior without being a tty.  */
     89   1.9  christos   virtual bool term_out ()
     90   1.9  christos   { return isatty (); }
     91   1.9  christos 
     92   1.9  christos   /* true if ANSI escapes can be used on STREAM.  */
     93   1.9  christos   virtual bool can_emit_style_escape ()
     94   1.9  christos   { return false; }
     95   1.9  christos 
     96   1.7  christos   virtual void flush ()
     97   1.7  christos   {}
     98  1.10  christos 
     99  1.10  christos   /* If this object has an underlying file descriptor, then return it.
    100  1.10  christos      Otherwise, return -1.  */
    101  1.10  christos   virtual int fd () const
    102  1.10  christos   { return -1; }
    103  1.10  christos 
    104  1.10  christos   /* Indicate that if the next sequence of characters overflows the
    105  1.10  christos      line, a newline should be inserted here rather than when it hits
    106  1.10  christos      the end.  If INDENT is non-zero, it is a number of spaces to be
    107  1.10  christos      printed to indent the wrapped part on the next line.
    108  1.10  christos 
    109  1.10  christos      If the line is already overfull, we immediately print a newline and
    110  1.10  christos      the indentation, and disable further wrapping.
    111  1.10  christos 
    112  1.10  christos      If we don't know the width of lines, but we know the page height,
    113  1.10  christos      we must not wrap words, but should still keep track of newlines
    114  1.10  christos      that were explicitly printed.
    115  1.10  christos 
    116  1.10  christos      This routine is guaranteed to force out any output which has been
    117  1.10  christos      squirreled away in the wrap_buffer, so wrap_here (0) can be
    118  1.10  christos      used to force out output from the wrap_buffer.  */
    119  1.10  christos   virtual void wrap_here (int indent)
    120  1.10  christos   {
    121  1.10  christos   }
    122  1.10  christos 
    123  1.10  christos   /* Emit an ANSI style escape for STYLE.  */
    124  1.10  christos   virtual void emit_style_escape (const ui_file_style &style);
    125  1.10  christos 
    126  1.10  christos   /* Rest the current output style to the empty style.  */
    127  1.10  christos   virtual void reset_style ();
    128  1.10  christos 
    129  1.10  christos   /* Print STR, bypassing any paging that might be done by this
    130  1.10  christos      ui_file.  Note that nearly no code should call this -- it's
    131  1.10  christos      intended for use by gdb_printf, but nothing else.  */
    132  1.10  christos   virtual void puts_unfiltered (const char *str)
    133  1.10  christos   {
    134  1.10  christos     this->puts (str);
    135  1.10  christos   }
    136  1.10  christos 
    137  1.10  christos protected:
    138  1.10  christos 
    139  1.10  christos   /* The currently applied style.  */
    140  1.10  christos   ui_file_style m_applied_style;
    141  1.10  christos 
    142  1.10  christos private:
    143  1.10  christos 
    144  1.10  christos   /* Helper function for putstr and putstrn.  Print the character C on
    145  1.10  christos      this stream as part of the contents of a literal string whose
    146  1.10  christos      delimiter is QUOTER.  */
    147  1.10  christos   void printchar (int c, int quoter, bool async_safe);
    148   1.7  christos };
    149   1.3  christos 
    150   1.7  christos typedef std::unique_ptr<ui_file> ui_file_up;
    151   1.1  christos 
    152   1.7  christos /* A ui_file that writes to nowhere.  */
    153   1.1  christos 
    154   1.7  christos class null_file : public ui_file
    155   1.7  christos {
    156   1.7  christos public:
    157   1.7  christos   void write (const char *buf, long length_buf) override;
    158   1.7  christos   void write_async_safe (const char *buf, long sizeof_buf) override;
    159   1.7  christos   void puts (const char *str) override;
    160   1.7  christos };
    161   1.1  christos 
    162   1.7  christos /* A preallocated null_file stream.  */
    163   1.7  christos extern null_file null_stream;
    164   1.1  christos 
    165   1.8  christos extern int gdb_console_fputs (const char *, FILE *);
    166   1.8  christos 
    167   1.7  christos /* A std::string-based ui_file.  Can be used as a scratch buffer for
    168   1.7  christos    collecting output.  */
    169   1.1  christos 
    170   1.7  christos class string_file : public ui_file
    171   1.7  christos {
    172   1.7  christos public:
    173   1.9  christos   /* Construct a string_file to collect 'raw' output, i.e. without
    174  1.12  christos      'terminal' behavior such as cli_styling.  */
    175   1.9  christos   string_file () : m_term_out (false) {};
    176  1.12  christos   /* If TERM_OUT, construct a string_file with terminal output behavior
    177   1.9  christos      such as cli_styling)
    178   1.9  christos      else collect 'raw' output like the previous constructor.  */
    179   1.9  christos   explicit string_file (bool term_out) : m_term_out (term_out) {};
    180   1.7  christos   ~string_file () override;
    181   1.7  christos 
    182  1.11  christos   string_file (string_file &&other) = default;
    183  1.11  christos 
    184   1.7  christos   /* Override ui_file methods.  */
    185   1.7  christos 
    186   1.7  christos   void write (const char *buf, long length_buf) override;
    187   1.7  christos 
    188   1.7  christos   long read (char *buf, long length_buf) override
    189   1.7  christos   { gdb_assert_not_reached ("a string_file is not readable"); }
    190   1.7  christos 
    191   1.9  christos   bool term_out () override;
    192   1.9  christos   bool can_emit_style_escape () override;
    193   1.9  christos 
    194   1.7  christos   /* string_file-specific public API.  */
    195   1.7  christos 
    196   1.7  christos   /* Accesses the std::string containing the entire output collected
    197  1.10  christos      so far.  */
    198  1.10  christos   const std::string &string () { return m_string; }
    199   1.7  christos 
    200  1.10  christos   /* Return an std::string containing the entire output collected so far.
    201   1.7  christos 
    202  1.10  christos      The internal buffer is cleared, such that it's ready to build a new
    203  1.10  christos      string.  */
    204  1.10  christos   std::string release ()
    205  1.10  christos   {
    206  1.10  christos     std::string ret = std::move (m_string);
    207  1.10  christos     m_string.clear ();
    208  1.10  christos     return ret;
    209  1.10  christos   }
    210  1.10  christos 
    211  1.10  christos   /* Set the internal buffer contents to STR.  Any existing contents are
    212  1.10  christos      discarded.  */
    213  1.10  christos   string_file &operator= (std::string &&str)
    214  1.10  christos   {
    215  1.10  christos     m_string = std::move (str);
    216  1.10  christos     return *this;
    217  1.10  christos   }
    218   1.7  christos 
    219   1.7  christos   /* Provide a few convenience methods with the same API as the
    220   1.7  christos      underlying std::string.  */
    221   1.7  christos   const char *data () const { return m_string.data (); }
    222   1.7  christos   const char *c_str () const { return m_string.c_str (); }
    223   1.7  christos   size_t size () const { return m_string.size (); }
    224   1.7  christos   bool empty () const { return m_string.empty (); }
    225   1.7  christos   void clear () { return m_string.clear (); }
    226   1.7  christos 
    227   1.7  christos private:
    228   1.7  christos   /* The internal buffer.  */
    229   1.7  christos   std::string m_string;
    230   1.9  christos 
    231   1.9  christos   bool m_term_out;
    232   1.7  christos };
    233   1.7  christos 
    234   1.7  christos /* A ui_file implementation that maps directly onto <stdio.h>'s FILE.
    235   1.7  christos    A stdio_file can either own its underlying file, or not.  If it
    236   1.7  christos    owns the file, then destroying the stdio_file closes the underlying
    237   1.7  christos    file, otherwise it is left open.  */
    238   1.7  christos 
    239   1.7  christos class stdio_file : public ui_file
    240   1.7  christos {
    241   1.7  christos public:
    242   1.7  christos   /* Create a ui_file from a previously opened FILE.  CLOSE_P
    243   1.7  christos      indicates whether the underlying file should be closed when the
    244   1.7  christos      stdio_file is destroyed.  */
    245   1.7  christos   explicit stdio_file (FILE *file, bool close_p = false);
    246   1.7  christos 
    247   1.7  christos   /* Create an stdio_file that is not managing any file yet.  Call
    248   1.7  christos      open to actually open something.  */
    249   1.7  christos   stdio_file ();
    250   1.7  christos 
    251   1.7  christos   ~stdio_file () override;
    252   1.7  christos 
    253   1.7  christos   /* Open NAME in mode MODE, and own the resulting file.  Returns true
    254   1.7  christos      on success, false otherwise.  If the stdio_file previously owned
    255   1.7  christos      a file, it is closed.  */
    256   1.7  christos   bool open (const char *name, const char *mode);
    257   1.7  christos 
    258   1.7  christos   void flush () override;
    259   1.7  christos 
    260   1.7  christos   void write (const char *buf, long length_buf) override;
    261   1.7  christos 
    262   1.7  christos   void write_async_safe (const char *buf, long length_buf) override;
    263   1.7  christos 
    264   1.7  christos   void puts (const char *) override;
    265   1.7  christos 
    266   1.7  christos   long read (char *buf, long length_buf) override;
    267   1.7  christos 
    268   1.7  christos   bool isatty () override;
    269   1.7  christos 
    270   1.9  christos   bool can_emit_style_escape () override;
    271   1.9  christos 
    272  1.10  christos   /* Return the underlying file descriptor.  */
    273  1.10  christos   int fd () const override
    274  1.10  christos   { return m_fd; }
    275  1.10  christos 
    276   1.7  christos private:
    277   1.7  christos   /* Sets the internal stream to FILE, and saves the FILE's file
    278   1.7  christos      descriptor in M_FD.  */
    279   1.7  christos   void set_stream (FILE *file);
    280   1.7  christos 
    281   1.7  christos   /* The file.  */
    282   1.7  christos   FILE *m_file;
    283   1.7  christos 
    284   1.7  christos   /* The associated file descriptor is extracted ahead of time for
    285   1.7  christos      stdio_file::write_async_safe's benefit, in case fileno isn't
    286   1.7  christos      async-safe.  */
    287   1.7  christos   int m_fd;
    288   1.7  christos 
    289   1.7  christos   /* If true, M_FILE is closed on destruction.  */
    290   1.7  christos   bool m_close_p;
    291   1.7  christos };
    292   1.7  christos 
    293   1.7  christos typedef std::unique_ptr<stdio_file> stdio_file_up;
    294   1.7  christos 
    295   1.7  christos /* Like stdio_file, but specifically for stderr.
    296   1.7  christos 
    297   1.7  christos    This exists because there is no real line-buffering on Windows, see
    298   1.7  christos    <http://msdn.microsoft.com/en-us/library/86cebhfs%28v=vs.71%29.aspx>
    299   1.7  christos    so the stdout is either fully-buffered or non-buffered.  We can't
    300   1.7  christos    make stdout non-buffered, because of two concerns:
    301   1.7  christos 
    302   1.7  christos     1. Non-buffering hurts performance.
    303   1.7  christos     2. Non-buffering may change GDB's behavior when it is interacting
    304   1.7  christos        with a front-end, such as Emacs.
    305   1.7  christos 
    306   1.7  christos    We leave stdout as fully buffered, but flush it first when
    307   1.7  christos    something is written to stderr.
    308   1.7  christos 
    309   1.7  christos    Note that the 'write_async_safe' method is not overridden, because
    310   1.7  christos    there's no way to flush a stream in an async-safe manner.
    311   1.7  christos    Fortunately, it doesn't really matter, because:
    312   1.7  christos 
    313   1.7  christos     1. That method is only used for printing internal debug output
    314   1.7  christos        from signal handlers.
    315   1.7  christos 
    316   1.7  christos     2. Windows hosts don't have a concept of async-safeness.  Signal
    317   1.7  christos        handlers run in a separate thread, so they can call the regular
    318   1.7  christos        non-async-safe output routines freely.
    319   1.7  christos */
    320   1.7  christos class stderr_file : public stdio_file
    321   1.7  christos {
    322   1.7  christos public:
    323   1.7  christos   explicit stderr_file (FILE *stream);
    324   1.7  christos 
    325   1.7  christos   /* Override the output routines to flush gdb_stdout before deferring
    326   1.7  christos      to stdio_file for the actual outputting.  */
    327   1.7  christos   void write (const char *buf, long length_buf) override;
    328   1.7  christos   void puts (const char *linebuffer) override;
    329   1.7  christos };
    330   1.7  christos 
    331   1.7  christos /* A ui_file implementation that maps onto two ui-file objects.  */
    332   1.7  christos 
    333   1.7  christos class tee_file : public ui_file
    334   1.7  christos {
    335   1.7  christos public:
    336  1.10  christos   /* Create a file which writes to both ONE and TWO.  Ownership of
    337  1.10  christos      both files is up to the user.  */
    338  1.10  christos   tee_file (ui_file *one, ui_file *two);
    339   1.7  christos   ~tee_file () override;
    340   1.7  christos 
    341   1.7  christos   void write (const char *buf, long length_buf) override;
    342   1.7  christos   void write_async_safe (const char *buf, long length_buf) override;
    343   1.7  christos   void puts (const char *) override;
    344   1.7  christos 
    345   1.7  christos   bool isatty () override;
    346   1.9  christos   bool term_out () override;
    347   1.9  christos   bool can_emit_style_escape () override;
    348   1.7  christos   void flush () override;
    349   1.7  christos 
    350  1.10  christos   void emit_style_escape (const ui_file_style &style) override
    351  1.10  christos   {
    352  1.10  christos     m_one->emit_style_escape (style);
    353  1.10  christos     m_two->emit_style_escape (style);
    354  1.10  christos   }
    355  1.10  christos 
    356  1.10  christos   void reset_style () override
    357  1.10  christos   {
    358  1.10  christos     m_one->reset_style ();
    359  1.10  christos     m_two->reset_style ();
    360  1.10  christos   }
    361  1.10  christos 
    362  1.10  christos   void puts_unfiltered (const char *str) override
    363  1.10  christos   {
    364  1.10  christos     m_one->puts_unfiltered (str);
    365  1.10  christos     m_two->puts_unfiltered (str);
    366  1.10  christos   }
    367  1.10  christos 
    368   1.7  christos private:
    369   1.9  christos   /* The two underlying ui_files.  */
    370   1.9  christos   ui_file *m_one;
    371  1.10  christos   ui_file *m_two;
    372   1.9  christos };
    373   1.9  christos 
    374   1.9  christos /* A ui_file implementation that filters out terminal escape
    375   1.9  christos    sequences.  */
    376   1.9  christos 
    377   1.9  christos class no_terminal_escape_file : public stdio_file
    378   1.9  christos {
    379   1.9  christos public:
    380   1.9  christos   no_terminal_escape_file ()
    381   1.9  christos   {
    382   1.9  christos   }
    383   1.9  christos 
    384   1.9  christos   /* Like the stdio_file methods, but these filter out terminal escape
    385   1.9  christos      sequences.  */
    386   1.9  christos   void write (const char *buf, long length_buf) override;
    387   1.9  christos   void puts (const char *linebuffer) override;
    388  1.10  christos 
    389  1.10  christos   void emit_style_escape (const ui_file_style &style) override
    390  1.10  christos   {
    391  1.10  christos   }
    392  1.10  christos 
    393  1.10  christos   void reset_style () override
    394  1.10  christos   {
    395  1.10  christos   }
    396  1.10  christos };
    397  1.10  christos 
    398  1.10  christos /* A base class for ui_file types that wrap another ui_file.  */
    399  1.10  christos 
    400  1.10  christos class wrapped_file : public ui_file
    401  1.10  christos {
    402  1.10  christos public:
    403  1.10  christos 
    404  1.10  christos   bool isatty () override
    405  1.10  christos   { return m_stream->isatty (); }
    406  1.10  christos 
    407  1.10  christos   bool term_out () override
    408  1.10  christos   { return m_stream->term_out (); }
    409  1.10  christos 
    410  1.10  christos   bool can_emit_style_escape () override
    411  1.10  christos   { return m_stream->can_emit_style_escape (); }
    412  1.10  christos 
    413  1.10  christos   void flush () override
    414  1.10  christos   { m_stream->flush (); }
    415  1.10  christos 
    416  1.10  christos   void wrap_here (int indent) override
    417  1.10  christos   { m_stream->wrap_here (indent); }
    418  1.10  christos 
    419  1.10  christos   void emit_style_escape (const ui_file_style &style) override
    420  1.10  christos   { m_stream->emit_style_escape (style); }
    421  1.10  christos 
    422  1.10  christos   /* Rest the current output style to the empty style.  */
    423  1.10  christos   void reset_style () override
    424  1.10  christos   { m_stream->reset_style (); }
    425  1.10  christos 
    426  1.10  christos   int fd () const override
    427  1.10  christos   { return m_stream->fd (); }
    428  1.10  christos 
    429  1.10  christos   void puts_unfiltered (const char *str) override
    430  1.10  christos   { m_stream->puts_unfiltered (str); }
    431  1.10  christos 
    432  1.10  christos   void write_async_safe (const char *buf, long length_buf) override
    433  1.10  christos   { return m_stream->write_async_safe (buf, length_buf); }
    434  1.10  christos 
    435  1.10  christos protected:
    436  1.10  christos 
    437  1.10  christos   /* Note that this class does not assume ownership of the stream.
    438  1.10  christos      However, a subclass may choose to, by adding a 'delete' to its
    439  1.10  christos      destructor.  */
    440  1.10  christos   explicit wrapped_file (ui_file *stream)
    441  1.10  christos     : m_stream (stream)
    442  1.10  christos   {
    443  1.10  christos   }
    444  1.10  christos 
    445  1.10  christos   /* The underlying stream.  */
    446  1.10  christos   ui_file *m_stream;
    447  1.10  christos };
    448  1.10  christos 
    449  1.10  christos /* A ui_file that optionally puts a timestamp at the start of each
    450  1.10  christos    line of output.  */
    451  1.10  christos 
    452  1.10  christos class timestamped_file : public wrapped_file
    453  1.10  christos {
    454  1.10  christos public:
    455  1.10  christos   explicit timestamped_file (ui_file *stream)
    456  1.10  christos     : wrapped_file (stream)
    457  1.10  christos   {
    458  1.10  christos   }
    459  1.10  christos 
    460  1.10  christos   DISABLE_COPY_AND_ASSIGN (timestamped_file);
    461  1.10  christos 
    462  1.10  christos   void write (const char *buf, long len) override;
    463  1.10  christos 
    464  1.10  christos private:
    465  1.10  christos 
    466  1.10  christos   /* True if the next output should be timestamped.  */
    467  1.10  christos   bool m_needs_timestamp = true;
    468   1.7  christos };
    469   1.1  christos 
    470  1.12  christos #endif /* GDB_UI_FILE_H */
    471