Remove outermost loop parameter.
[official-gcc/graphite-test-results.git] / libgfortran / runtime / backtrace.c
blob4a831c0d8b25b459db322b65d31cf9f84f736982
1 /* Copyright (C) 2006, 2007, 2009 Free Software Foundation, Inc.
2 Contributed by François-Xavier Coudert
4 This file is part of the GNU Fortran 95 runtime library (libgfortran).
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.
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.
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.
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/>. */
25 #include "libgfortran.h"
27 #include <string.h>
29 #ifdef HAVE_STDLIB_H
30 #include <stdlib.h>
31 #endif
33 #ifdef HAVE_INTTYPES_H
34 #include <inttypes.h>
35 #endif
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
41 #ifdef HAVE_EXECINFO_H
42 #include <execinfo.h>
43 #endif
45 #ifdef HAVE_SYS_WAIT_H
46 #include <sys/wait.h>
47 #endif
49 #include <ctype.h>
52 /* Macros for common sets of capabilities: can we fork and exec, can
53 we use glibc-style backtrace functions, and can we use pipes. */
54 #define CAN_FORK (defined(HAVE_FORK) && defined(HAVE_EXECVP) \
55 && defined(HAVE_WAIT))
56 #define GLIBC_BACKTRACE (defined(HAVE_BACKTRACE) \
57 && defined(HAVE_BACKTRACE_SYMBOLS))
58 #define CAN_PIPE (CAN_FORK && defined(HAVE_PIPE) \
59 && defined(HAVE_DUP2) && defined(HAVE_FDOPEN) \
60 && defined(HAVE_CLOSE))
63 #if GLIBC_BACKTRACE && CAN_PIPE
64 static char *
65 local_strcasestr (const char *s1, const char *s2)
67 #ifdef HAVE_STRCASESTR
68 return strcasestr (s1, s2);
69 #else
71 const char *p = s1;
72 const size_t len = strlen (s2);
73 const char u = *s2, v = isupper((int) *s2) ? tolower((int) *s2)
74 : (islower((int) *s2) ? toupper((int) *s2)
75 : *s2);
77 while (1)
79 while (*p != u && *p != v && *p)
80 p++;
81 if (*p == 0)
82 return NULL;
83 if (strncasecmp (p, s2, len) == 0)
84 return (char *)p;
86 #endif
88 #endif
91 #if GLIBC_BACKTRACE
92 static void
93 dump_glibc_backtrace (int depth, char *str[])
95 int i;
97 for (i = 0; i < depth; i++)
98 st_printf (" + %s\n", str[i]);
100 free (str);
102 #endif
104 /* show_backtrace displays the backtrace, currently obtained by means of
105 the glibc backtrace* functions. */
106 void
107 show_backtrace (void)
109 #if GLIBC_BACKTRACE
111 #define DEPTH 50
112 #define BUFSIZE 1024
114 void *trace[DEPTH];
115 char **str;
116 int depth;
118 depth = backtrace (trace, DEPTH);
119 if (depth <= 0)
120 return;
122 str = backtrace_symbols (trace, depth);
124 #if CAN_PIPE
126 #ifndef STDIN_FILENO
127 #define STDIN_FILENO 0
128 #endif
130 #ifndef STDOUT_FILENO
131 #define STDOUT_FILENO 1
132 #endif
134 #ifndef STDERR_FILENO
135 #define STDERR_FILENO 2
136 #endif
138 /* We attempt to extract file and line information from addr2line. */
141 /* Local variables. */
142 int f[2], pid, line, i;
143 FILE *output;
144 char addr_buf[DEPTH][GFC_XTOA_BUF_SIZE], func[BUFSIZE], file[BUFSIZE];
145 char *p, *end;
146 const char *addr[DEPTH];
148 /* Write the list of addresses in hexadecimal format. */
149 for (i = 0; i < depth; i++)
150 addr[i] = gfc_xtoa ((GFC_UINTEGER_LARGEST) (intptr_t) trace[i], addr_buf[i],
151 sizeof (addr_buf[i]));
153 /* Don't output an error message if something goes wrong, we'll simply
154 fall back to the pstack and glibc backtraces. */
155 if (pipe (f) != 0)
156 break;
157 if ((pid = fork ()) == -1)
158 break;
160 if (pid == 0)
162 /* Child process. */
163 #define NUM_FIXEDARGS 5
164 char *arg[DEPTH+NUM_FIXEDARGS+1];
166 close (f[0]);
167 close (STDIN_FILENO);
168 close (STDERR_FILENO);
170 if (dup2 (f[1], STDOUT_FILENO) == -1)
171 _exit (0);
172 close (f[1]);
174 arg[0] = (char *) "addr2line";
175 arg[1] = (char *) "-e";
176 arg[2] = full_exe_path ();
177 arg[3] = (char *) "-f";
178 arg[4] = (char *) "-s";
179 for (i = 0; i < depth; i++)
180 arg[NUM_FIXEDARGS+i] = (char *) addr[i];
181 arg[NUM_FIXEDARGS+depth] = NULL;
182 execvp (arg[0], arg);
183 _exit (0);
184 #undef NUM_FIXEDARGS
187 /* Father process. */
188 close (f[1]);
189 wait (NULL);
190 output = fdopen (f[0], "r");
191 i = -1;
193 if (fgets (func, sizeof(func), output))
195 st_printf ("\nBacktrace for this error:\n");
199 if (! fgets (file, sizeof(file), output))
200 goto fallback;
202 i++;
204 for (p = func; *p != '\n' && *p != '\r'; p++)
207 *p = '\0';
209 /* Try to recognize the internal libgfortran functions. */
210 if (strncasecmp (func, "*_gfortran", 10) == 0
211 || strncasecmp (func, "_gfortran", 9) == 0
212 || strcmp (func, "main") == 0 || strcmp (func, "_start") == 0
213 || strcmp (func, "_gfortrani_handler") == 0)
214 continue;
216 if (local_strcasestr (str[i], "libgfortran.so") != NULL
217 || local_strcasestr (str[i], "libgfortran.dylib") != NULL
218 || local_strcasestr (str[i], "libgfortran.a") != NULL)
219 continue;
221 /* If we only have the address, use the glibc backtrace. */
222 if (func[0] == '?' && func[1] == '?' && file[0] == '?'
223 && file[1] == '?')
225 st_printf (" + %s\n", str[i]);
226 continue;
229 /* Extract the line number. */
230 for (end = NULL, p = file; *p; p++)
231 if (*p == ':')
232 end = p;
233 if (end != NULL)
235 *end = '\0';
236 line = atoi (++end);
238 else
239 line = -1;
241 if (strcmp (func, "MAIN__") == 0)
242 st_printf (" + in the main program\n");
243 else
244 st_printf (" + function %s (0x%s)\n", func, addr[i]);
246 if (line <= 0 && strcmp (file, "??") == 0)
247 continue;
249 if (line <= 0)
250 st_printf (" from file %s\n", file);
251 else
252 st_printf (" at line %d of file %s\n", line, file);
254 while (fgets (func, sizeof(func), output));
256 free (str);
257 return;
259 fallback:
260 st_printf ("** Something went wrong while running addr2line. **\n"
261 "** Falling back to a simpler backtrace scheme. **\n");
264 while (0);
266 #undef DEPTH
267 #undef BUFSIZE
269 #endif
270 #endif
272 #if CAN_FORK && defined(HAVE_GETPPID)
273 /* Try to call pstack. */
276 /* Local variables. */
277 int pid;
279 /* Don't output an error message if something goes wrong, we'll simply
280 fall back to the pstack and glibc backtraces. */
281 if ((pid = fork ()) == -1)
282 break;
284 if (pid == 0)
286 /* Child process. */
287 #define NUM_ARGS 2
288 char *arg[NUM_ARGS+1];
289 char buf[20];
291 st_printf ("\nBacktrace for this error:\n");
292 arg[0] = (char *) "pstack";
293 #ifdef HAVE_SNPRINTF
294 snprintf (buf, sizeof(buf), "%d", (int) getppid ());
295 #else
296 sprintf (buf, "%d", (int) getppid ());
297 #endif
298 arg[1] = buf;
299 arg[2] = NULL;
300 execvp (arg[0], arg);
301 #undef NUM_ARGS
303 /* pstack didn't work, so we fall back to dumping the glibc
304 backtrace if we can. */
305 #if GLIBC_BACKTRACE
306 dump_glibc_backtrace (depth, str);
307 #else
308 st_printf (" unable to produce a backtrace, sorry!\n");
309 #endif
311 _exit (0);
314 /* Father process. */
315 wait (NULL);
316 return;
318 while(0);
319 #endif
321 #if GLIBC_BACKTRACE
322 /* Fallback to the glibc backtrace. */
323 st_printf ("\nBacktrace for this error:\n");
324 dump_glibc_backtrace (depth, str);
325 #endif