Retro68/gcc/libgfortran/runtime/backtrace.c

180 lines
4.5 KiB
C
Raw Normal View History

/* Copyright (C) 2006-2017 Free Software Foundation, Inc.
2012-03-27 23:13:14 +00:00
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"
2017-04-10 11:32:00 +00:00
#include <gthr.h>
2012-03-27 23:13:14 +00:00
#include <string.h>
2017-04-10 11:32:00 +00:00
#include <errno.h>
2012-03-27 23:13:14 +00:00
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
2017-04-10 11:32:00 +00:00
#include "backtrace-supported.h"
#include "backtrace.h"
2012-03-27 23:13:14 +00:00
2017-04-10 11:32:00 +00:00
/* Store our own state while backtracing. */
struct mystate
{
int frame;
bool try_simple;
bool in_signal_handler;
};
2012-03-27 23:13:14 +00:00
2017-04-10 11:32:00 +00:00
/* 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. */
2012-03-27 23:13:14 +00:00
2017-04-10 11:32:00 +00:00
static int
has_gfortran_prefix (const char *s)
2012-03-27 23:13:14 +00:00
{
2017-04-10 11:32:00 +00:00
if (!s)
return 0;
2012-03-27 23:13:14 +00:00
2017-04-10 11:32:00 +00:00
while (*s == '_')
s++;
2012-03-27 23:13:14 +00:00
2017-04-10 11:32:00 +00:00
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] == '_')));
}
2012-03-27 23:13:14 +00:00
2017-04-10 11:32:00 +00:00
static void
error_callback (void *data, const char *msg, int errnum)
2012-03-27 23:13:14 +00:00
{
2017-04-10 11:32:00 +00:00
struct mystate *state = (struct mystate *) data;
#define ERRHDR "\nCould not print backtrace: "
if (errnum < 0)
{
state->try_simple = true;
return;
}
else if (errnum == 0)
2012-03-27 23:13:14 +00:00
{
2017-04-10 11:32:00 +00:00
estr_write (ERRHDR);
estr_write (msg);
estr_write ("\n");
}
else
{
char errbuf[256];
if (state->in_signal_handler)
2012-03-27 23:13:14 +00:00
{
2017-04-10 11:32:00 +00:00
estr_write (ERRHDR);
estr_write (msg);
estr_write (", errno: ");
const char *p = gfc_itoa (errnum, errbuf, sizeof (errbuf));
estr_write (p);
estr_write ("\n");
2012-03-27 23:13:14 +00:00
}
else
2017-04-10 11:32:00 +00:00
st_printf (ERRHDR "%s: %s\n", msg,
gf_strerror (errnum, errbuf, sizeof (errbuf)));
2012-03-27 23:13:14 +00:00
}
}
2017-04-10 11:32:00 +00:00
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;
}
2012-03-27 23:13:14 +00:00
2017-04-10 11:32:00 +00:00
static int
full_callback (void *data, uintptr_t pc, const char *filename,
int lineno, const char *function)
2012-03-27 23:13:14 +00:00
{
2017-04-10 11:32:00 +00:00
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;
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
/* Display the backtrace. */
void
show_backtrace (bool in_signal_handler)
2012-03-27 23:13:14 +00:00
{
2017-04-10 11:32:00 +00:00
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;
2012-03-27 23:13:14 +00:00
2017-04-10 11:32:00 +00:00
if (!BACKTRACE_SUPPORTED || (in_signal_handler && BACKTRACE_USES_MALLOC))
2012-03-27 23:13:14 +00:00
{
2017-04-10 11:32:00 +00:00
/* 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);
2012-03-27 23:13:14 +00:00
}
else
{
2017-04-10 11:32:00 +00:00
/* 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. */
2012-03-27 23:13:14 +00:00
2017-04-10 11:32:00 +00:00
backtrace_full (lbstate, 0, full_callback, error_callback, &state);
if (state.try_simple)
backtrace_simple (lbstate, 0, simple_callback, error_callback, &state);
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00
}
2012-03-27 23:13:14 +00:00
2017-04-10 11:32:00 +00:00
/* Function called by the front-end translating the BACKTRACE intrinsic. */
2012-03-27 23:13:14 +00:00
2017-04-10 11:32:00 +00:00
extern void backtrace (void);
export_proto (backtrace);
2012-03-27 23:13:14 +00:00
void
2014-09-21 17:33:12 +00:00
backtrace (void)
2012-03-27 23:13:14 +00:00
{
2017-04-10 11:32:00 +00:00
show_backtrace (false);
2012-03-27 23:13:14 +00:00
}
2017-04-10 11:32:00 +00:00