forked from Imagelibrary/binutils-gdb
Enabling "set debug lin-lwp 1" with the MI interpreter doesn't work.
When the sigchld_handler function wants to print a debug output
("sigchld\n"), it uses ui_file_write_async_safe. This ends up in the
default implementation of ui_file::write_async_safe, which aborts GDB.
This patch implements the write_async_safe method for mi_console_file.
The "normal" MI output is line buffered, which means the output
accumulates in m_buffer until a \n is written, at which point it's
flushed in m_raw. The implementation of write_async_safe provided by
this patch bypasses this buffer and writes directly to m_raw. There are
two reasons for this:
(1) Appending to m_buffer (therefore to an std::string) is probably not
async-safe, as it may allocate memory.
(2) We may have a partial output already in m_buffer, so that would lead
to some nested MI output, not so great.
There is probably still a chance to have bad MI output, if
sigchld_handler is invoked in the middle of mi_console_file's flush, and
the line being flushed is only partially sent to m_raw. The solution
would probably be to block signals during flushing. Since this is only
used for debug output, I don't know if it's worth the effort to do that.
To implement write_async_safe, I needed to use the fputstrn_unfiltered,
which does the necessary escaping (e.g. replace \n with \\n). I started
by adding printchar's callback parameters to fputstrn_unfiltered, to be
able to pass async-safe versions of them. It's not easy to provide an
async-safe version of do_fprintf, but it turns out that we can easily
replace printchar's callbacks with a single do_fputc quite easily. The
async-safe version of do_fputc simply calls the underlying ui_file's
write_async_safe method.
gdb/ChangeLog:
PR mi/22299
* mi/mi-console.c (do_fputc_async_safe): New.
(mi_console_file::write_async_safe): New.
(mi_console_file::flush): Adjust calls to fputstrn_unfiltered.
* mi/mi-console.h (class mi_console_file) <write_async_safe>:
New.
* ui-file.c (ui_file::putstrn): Adjust call to
fputstrn_unfiltered.
* utils.c (printchar): Replace do_fputs and do_fprintf
parameters by do_fputc.
(fputstr_filtered): Adjust call to printchar.
(fputstr_unfiltered): Likewise.
(fputstrn_filtered): Likewise.
(fputstrn_unfiltered): Add do_fputc parameter, pass to
printchar.
* utils.h (do_fputc_ftype): New typedef.
(fputstrn_unfiltered): Add do_fputc parameter.
119 lines
3.2 KiB
C
119 lines
3.2 KiB
C
/* MI Console code.
|
|
|
|
Copyright (C) 2000-2018 Free Software Foundation, Inc.
|
|
|
|
Contributed by Cygnus Solutions (a Red Hat company).
|
|
|
|
This file is part of GDB.
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
/* An MI console is a kind of ui_file stream that sends output to
|
|
stdout, but encapsulated and prefixed with a distinctive string;
|
|
for instance, error output is normally identified by a leading
|
|
"&". */
|
|
|
|
#include "defs.h"
|
|
#include "mi-console.h"
|
|
|
|
/* Create a console that wraps the given output stream RAW with the
|
|
string PREFIX and quoting it with QUOTE. */
|
|
|
|
mi_console_file::mi_console_file (ui_file *raw, const char *prefix, char quote)
|
|
: m_raw (raw),
|
|
m_prefix (prefix),
|
|
m_quote (quote)
|
|
{}
|
|
|
|
void
|
|
mi_console_file::write (const char *buf, long length_buf)
|
|
{
|
|
size_t prev_size = m_buffer.size ();
|
|
/* Append the text to our internal buffer. */
|
|
m_buffer.write (buf, length_buf);
|
|
/* Flush when an embedded newline is present anywhere in the
|
|
buffer. */
|
|
if (strchr (m_buffer.c_str () + prev_size, '\n') != NULL)
|
|
this->flush ();
|
|
}
|
|
|
|
/* Write C to STREAM's in an async-safe way. */
|
|
|
|
static int
|
|
do_fputc_async_safe (int c, ui_file *stream)
|
|
{
|
|
char ch = c;
|
|
stream->write_async_safe (&ch, 1);
|
|
return c;
|
|
}
|
|
|
|
void
|
|
mi_console_file::write_async_safe (const char *buf, long length_buf)
|
|
{
|
|
m_raw->write_async_safe (m_prefix, strlen (m_prefix));
|
|
if (m_quote)
|
|
{
|
|
m_raw->write_async_safe (&m_quote, 1);
|
|
fputstrn_unfiltered (buf, length_buf, m_quote, do_fputc_async_safe,
|
|
m_raw);
|
|
m_raw->write_async_safe (&m_quote, 1);
|
|
}
|
|
else
|
|
fputstrn_unfiltered (buf, length_buf, 0, do_fputc_async_safe, m_raw);
|
|
|
|
char nl = '\n';
|
|
m_raw->write_async_safe (&nl, 1);
|
|
}
|
|
|
|
void
|
|
mi_console_file::flush ()
|
|
{
|
|
const std::string &str = m_buffer.string ();
|
|
|
|
/* Transform a byte sequence into a console output packet. */
|
|
if (!str.empty ())
|
|
{
|
|
size_t length_buf = str.size ();
|
|
const char *buf = str.data ();
|
|
|
|
fputs_unfiltered (m_prefix, m_raw);
|
|
if (m_quote)
|
|
{
|
|
fputc_unfiltered (m_quote, m_raw);
|
|
fputstrn_unfiltered (buf, length_buf, m_quote, fputc_unfiltered,
|
|
m_raw);
|
|
fputc_unfiltered (m_quote, m_raw);
|
|
fputc_unfiltered ('\n', m_raw);
|
|
}
|
|
else
|
|
{
|
|
fputstrn_unfiltered (buf, length_buf, 0, fputc_unfiltered, m_raw);
|
|
fputc_unfiltered ('\n', m_raw);
|
|
}
|
|
gdb_flush (m_raw);
|
|
}
|
|
|
|
m_buffer.clear ();
|
|
}
|
|
|
|
/* Change the underlying stream of the console directly; this is
|
|
useful as a minimum-impact way to reflect external changes like
|
|
logging enable/disable. */
|
|
|
|
void
|
|
mi_console_file::set_raw (ui_file *raw)
|
|
{
|
|
m_raw = raw;
|
|
}
|