PR 53379 Print backtrace on error termination.
[gcc.git] / libgfortran / runtime / backtrace.c
1 /* Copyright (C) 2006-2015 Free Software Foundation, Inc.
2 Contributed by François-Xavier Coudert
3
4 This file is part of the GNU Fortran runtime library (libgfortran).
5
6 Libgfortran is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 Libgfortran is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
19
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
24
25 #include "libgfortran.h"
26
27 #include <string.h>
28 #include <stdlib.h>
29 #include <errno.h>
30
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34
35 #include "backtrace-supported.h"
36 #include "backtrace.h"
37
38
39 /* Store our own state while backtracing. */
40 struct mystate
41 {
42 int frame;
43 bool try_simple;
44 bool in_signal_handler;
45 };
46
47
48 /* Does a function name have "_gfortran_" or "_gfortrani_" prefix, possibly
49 with additional underscore(s) at the beginning? Cannot use strncmp()
50 because we might be called from a signal handler. */
51
52 static int
53 has_gfortran_prefix (const char *s)
54 {
55 if (!s)
56 return 0;
57
58 while (*s == '_')
59 s++;
60
61 return (s[0] == 'g' && s[1] == 'f' && s[2] == 'o' && s[3] == 'r'
62 && s[4] == 't' && s[5] == 'r' && s[6] == 'a' && s[7] == 'n'
63 && (s[8] == '_' || (s[8] == 'i' && s[9] == '_')));
64 }
65
66 static void
67 error_callback (void *data, const char *msg, int errnum)
68 {
69 struct mystate *state = (struct mystate *) data;
70 #define ERRHDR "\nCould not print backtrace: "
71
72 if (errnum < 0)
73 {
74 state->try_simple = true;
75 return;
76 }
77 else if (errnum == 0)
78 {
79 estr_write (ERRHDR);
80 estr_write (msg);
81 estr_write ("\n");
82 }
83 else
84 {
85 char errbuf[256];
86 if (state->in_signal_handler)
87 {
88 estr_write (ERRHDR);
89 estr_write (msg);
90 estr_write (", errno: ");
91 const char *p = gfc_itoa (errnum, errbuf, sizeof (errbuf));
92 estr_write (p);
93 estr_write ("\n");
94 }
95 else
96 st_printf (ERRHDR "%s: %s\n", msg,
97 gf_strerror (errnum, errbuf, sizeof (errbuf)));
98 }
99 }
100
101 static int
102 simple_callback (void *data, uintptr_t pc)
103 {
104 struct mystate *state = (struct mystate *) data;
105 st_printf ("#%d 0x%lx\n", state->frame, (unsigned long) pc);
106 (state->frame)++;
107 return 0;
108 }
109
110 static int
111 full_callback (void *data, uintptr_t pc, const char *filename,
112 int lineno, const char *function)
113 {
114 struct mystate *state = (struct mystate *) data;
115
116 if (has_gfortran_prefix (function))
117 return 0;
118
119 st_printf ("#%d 0x%lx in %s\n", state->frame,
120 (unsigned long) pc, function == NULL ? "???" : function);
121 if (filename || lineno != 0)
122 st_printf ("\tat %s:%d\n", filename == NULL ? "???" : filename, lineno);
123 (state->frame)++;
124
125 if (function != NULL && strcmp (function, "main") == 0)
126 return 1;
127
128 return 0;
129 }
130
131
132 /* Display the backtrace. */
133
134 void
135 show_backtrace (bool in_signal_handler)
136 {
137 struct backtrace_state *lbstate;
138 struct mystate state = { 0, false, in_signal_handler };
139
140 lbstate = backtrace_create_state (NULL, 1, error_callback, NULL);
141
142 if (!BACKTRACE_SUPPORTED || (in_signal_handler && BACKTRACE_USES_MALLOC))
143 {
144 /* If symbolic backtrace is not supported on this target, or would
145 require malloc() and we are in a signal handler, go with a
146 simple backtrace. */
147
148 backtrace_simple (lbstate, 0, simple_callback, error_callback, &state);
149 }
150 else
151 {
152 /* libbacktrace uses mmap, which is safe to call from a signal handler
153 (in practice, if not in theory). Thus we can generate a symbolic
154 backtrace, if debug symbols are available. */
155
156 backtrace_full (lbstate, 0, full_callback, error_callback, &state);
157 if (state.try_simple)
158 backtrace_simple (lbstate, 0, simple_callback, error_callback, &state);
159 }
160 }
161
162
163
164 /* Function called by the front-end translating the BACKTRACE intrinsic. */
165
166 extern void backtrace (void);
167 export_proto (backtrace);
168
169 void
170 backtrace (void)
171 {
172 show_backtrace (false);
173 }
174