gcc/libgfortran/runtime/backtrace.c
Janne Blomqvist edaaef601d Use vectored writes when reporting errors and warnings.
When producing error and warning messages, libgfortran writes a
message by using many system calls.  By using vectored writes (the
POSIX writev function) when available and feasible to use without
major surgery, we reduce the chance that output gets intermingled with
other output to stderr.

In practice, this is done by introducing a new function estr_writev in
addition to the existing estr_write.  In order to use this, the old
st_vprintf is removed, replaced by direct calls of vsnprintf, allowing
more message batching.

Regtested on x86_64-pc-linux-gnu.

libgfortran/ChangeLog:

2018-09-21  Janne Blomqvist  <jb@gcc.gnu.org>

	* config.h.in: Regenerated.
	* configure: Regenerated.
	* configure.ac: Check for writev and sys/uio.h.
	* libgfortran.h: Include sys/uio.h.
	(st_vprintf): Remove prototype.
	(struct iovec): Define if not available.
	(estr_writev): New prototype.
	* runtime/backtrace.c (error_callback): Use estr_writev.
	* runtime/error.c (ST_VPRINTF_SIZE): Remove.
	(estr_writev): New function.
	(st_vprintf): Remove.
	(gf_vsnprintf): New function.
	(ST_ERRBUF_SIZE): New macro.
	(st_printf): Use vsnprintf.
	(os_error): Use estr_writev.
	(runtime_error): Use vsnprintf and estr_writev.
	(runtime_error_at): Likewise.
	(runtime_warning_at): Likewise.
	(internal_error): Use estr_writev.
	(generate_error_common): Likewise.
	(generate_warning): Likewise.
	(notify_std): Likewise.
	* runtime/pause.c (pause_string): Likewise.
	* runtime/stop.c (report_exception): Likewise.
	(stop_string): Likewise.
	(error_stop_string): Likewise.

From-SVN: r264487
2018-09-21 21:12:59 +03:00

191 lines
4.9 KiB
C

/* Copyright (C) 2006-2018 Free Software Foundation, Inc.
Contributed by François-Xavier Coudert
This file is part of the GNU Fortran runtime library (libgfortran).
Libgfortran 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, or (at your option)
any later version.
Libgfortran 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.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */
#include "libgfortran.h"
#include <gthr.h>
#include <string.h>
#include <errno.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "backtrace-supported.h"
#include "backtrace.h"
/* Store our own state while backtracing. */
struct mystate
{
int frame;
bool try_simple;
bool in_signal_handler;
};
/* Does a function name have "_gfortran_" or "_gfortrani_" prefix, possibly
with additional underscore(s) at the beginning? Cannot use strncmp()
because we might be called from a signal handler. */
static int
has_gfortran_prefix (const char *s)
{
if (!s)
return 0;
while (*s == '_')
s++;
return (s[0] == 'g' && s[1] == 'f' && s[2] == 'o' && s[3] == 'r'
&& s[4] == 't' && s[5] == 'r' && s[6] == 'a' && s[7] == 'n'
&& (s[8] == '_' || (s[8] == 'i' && s[9] == '_')));
}
static void
error_callback (void *data, const char *msg, int errnum)
{
struct mystate *state = (struct mystate *) data;
struct iovec iov[5];
#define ERRHDR "\nCould not print backtrace: "
if (errnum < 0)
{
state->try_simple = true;
return;
}
else if (errnum == 0)
{
iov[0].iov_base = (char*) ERRHDR;
iov[0].iov_len = strlen (ERRHDR);
iov[1].iov_base = (char*) msg;
iov[1].iov_len = strlen (msg);
iov[2].iov_base = (char*) "\n";
iov[2].iov_len = 1;
estr_writev (iov, 3);
}
else
{
char errbuf[256];
if (state->in_signal_handler)
{
iov[0].iov_base = (char*) ERRHDR;
iov[0].iov_len = strlen (ERRHDR);
iov[1].iov_base = (char*) msg;
iov[1].iov_len = strlen (msg);
iov[2].iov_base = (char*) ", errno: ";
iov[2].iov_len = strlen (iov[2].iov_base);
const char *p = gfc_itoa (errnum, errbuf, sizeof (errbuf));
iov[3].iov_base = (char*) p;
iov[3].iov_len = strlen (p);
iov[4].iov_base = (char*) "\n";
iov[4].iov_len = 1;
estr_writev (iov, 5);
}
else
st_printf (ERRHDR "%s: %s\n", msg,
gf_strerror (errnum, errbuf, sizeof (errbuf)));
}
}
static int
simple_callback (void *data, uintptr_t pc)
{
struct mystate *state = (struct mystate *) data;
st_printf ("#%d 0x%lx\n", state->frame, (unsigned long) pc);
(state->frame)++;
return 0;
}
static int
full_callback (void *data, uintptr_t pc, const char *filename,
int lineno, const char *function)
{
struct mystate *state = (struct mystate *) data;
if (has_gfortran_prefix (function))
return 0;
st_printf ("#%d 0x%lx in %s\n", state->frame,
(unsigned long) pc, function == NULL ? "???" : function);
if (filename || lineno != 0)
st_printf ("\tat %s:%d\n", filename == NULL ? "???" : filename, lineno);
(state->frame)++;
if (function != NULL && strcmp (function, "main") == 0)
return 1;
return 0;
}
/* Display the backtrace. */
void
show_backtrace (bool in_signal_handler)
{
struct backtrace_state *lbstate;
struct mystate state = { 0, false, in_signal_handler };
lbstate = backtrace_create_state (NULL, __gthread_active_p (),
error_callback, NULL);
if (lbstate == NULL)
return;
if (!BACKTRACE_SUPPORTED || (in_signal_handler && BACKTRACE_USES_MALLOC))
{
/* If symbolic backtrace is not supported on this target, or would
require malloc() and we are in a signal handler, go with a
simple backtrace. */
backtrace_simple (lbstate, 0, simple_callback, error_callback, &state);
}
else
{
/* libbacktrace uses mmap, which is safe to call from a signal handler
(in practice, if not in theory). Thus we can generate a symbolic
backtrace, if debug symbols are available. */
backtrace_full (lbstate, 0, full_callback, error_callback, &state);
if (state.try_simple)
backtrace_simple (lbstate, 0, simple_callback, error_callback, &state);
}
}
/* Function called by the front-end translating the BACKTRACE intrinsic. */
extern void backtrace (void);
export_proto (backtrace);
void
backtrace (void)
{
show_backtrace (false);
}