1 /* Copyright (C) 2006, 2007, 2009, 2011 Free Software Foundation, Inc.
2 Contributed by François-Xavier Coudert
4 This file is part of the GNU Fortran 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)
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"
33 #ifdef HAVE_INTTYPES_H
41 #ifdef HAVE_EXECINFO_H
45 #ifdef HAVE_SYS_WAIT_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
65 local_strcasestr (const char *s1
, const char *s2
)
67 #ifdef HAVE_STRCASESTR
68 return strcasestr (s1
, s2
);
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
)
79 while (*p
!= u
&& *p
!= v
&& *p
)
83 if (strncasecmp (p
, s2
, len
) == 0)
93 dump_glibc_backtrace (int depth
, char *str
[])
97 for (i
= 0; i
< depth
; i
++)
108 /* show_backtrace displays the backtrace, currently obtained by means of
109 the glibc backtrace* functions. */
111 show_backtrace (void)
122 depth
= backtrace (trace
, DEPTH
);
126 str
= backtrace_symbols (trace
, depth
);
131 #define STDIN_FILENO 0
134 #ifndef STDOUT_FILENO
135 #define STDOUT_FILENO 1
138 #ifndef STDERR_FILENO
139 #define STDERR_FILENO 2
142 /* We attempt to extract file and line information from addr2line. */
145 /* Local variables. */
146 int f
[2], pid
, line
, i
;
148 char addr_buf
[DEPTH
][GFC_XTOA_BUF_SIZE
], func
[BUFSIZE
], file
[BUFSIZE
];
150 const char *addr
[DEPTH
];
152 /* Write the list of addresses in hexadecimal format. */
153 for (i
= 0; i
< depth
; i
++)
154 addr
[i
] = gfc_xtoa ((GFC_UINTEGER_LARGEST
) (intptr_t) trace
[i
], addr_buf
[i
],
155 sizeof (addr_buf
[i
]));
157 /* Don't output an error message if something goes wrong, we'll simply
158 fall back to the pstack and glibc backtraces. */
161 if ((pid
= fork ()) == -1)
167 #define NUM_FIXEDARGS 5
168 char *arg
[DEPTH
+NUM_FIXEDARGS
+1];
171 close (STDIN_FILENO
);
172 close (STDERR_FILENO
);
174 if (dup2 (f
[1], STDOUT_FILENO
) == -1)
178 arg
[0] = (char *) "addr2line";
179 arg
[1] = (char *) "-e";
180 arg
[2] = full_exe_path ();
181 arg
[3] = (char *) "-f";
182 arg
[4] = (char *) "-s";
183 for (i
= 0; i
< depth
; i
++)
184 arg
[NUM_FIXEDARGS
+i
] = (char *) addr
[i
];
185 arg
[NUM_FIXEDARGS
+depth
] = NULL
;
186 execvp (arg
[0], arg
);
191 /* Father process. */
194 output
= fdopen (f
[0], "r");
197 if (fgets (func
, sizeof(func
), output
))
199 estr_write ("\nBacktrace for this error:\n");
203 if (! fgets (file
, sizeof(file
), output
))
208 for (p
= func
; *p
!= '\n' && *p
!= '\r'; p
++)
213 /* Try to recognize the internal libgfortran functions. */
214 if (strncasecmp (func
, "*_gfortran", 10) == 0
215 || strncasecmp (func
, "_gfortran", 9) == 0
216 || strcmp (func
, "main") == 0 || strcmp (func
, "_start") == 0
217 || strcmp (func
, "_gfortrani_handler") == 0)
220 if (local_strcasestr (str
[i
], "libgfortran.so") != NULL
221 || local_strcasestr (str
[i
], "libgfortran.dylib") != NULL
222 || local_strcasestr (str
[i
], "libgfortran.a") != NULL
)
225 /* If we only have the address, use the glibc backtrace. */
226 if (func
[0] == '?' && func
[1] == '?' && file
[0] == '?'
235 /* Extract the line number. */
236 for (end
= NULL
, p
= file
; *p
; p
++)
247 if (strcmp (func
, "MAIN__") == 0)
248 estr_write (" + in the main program\n");
251 estr_write (" + function ");
254 estr_write (addr
[i
]);
258 if (line
<= 0 && strcmp (file
, "??") == 0)
263 estr_write (" from file ");
268 st_printf (" at line %d of file %s\n", line
, file
);
270 while (fgets (func
, sizeof(func
), output
));
276 estr_write ("** Something went wrong while running addr2line. **\n"
277 "** Falling back to a simpler backtrace scheme. **\n");
288 #if CAN_FORK && defined(HAVE_GETPPID)
289 /* Try to call pstack. */
292 /* Local variables. */
295 /* Don't output an error message if something goes wrong, we'll simply
296 fall back to the pstack and glibc backtraces. */
297 if ((pid
= fork ()) == -1)
304 char *arg
[NUM_ARGS
+1];
307 estr_write ("\nBacktrace for this error:\n");
308 arg
[0] = (char *) "pstack";
309 snprintf (buf
, sizeof(buf
), "%d", (int) getppid ());
312 execvp (arg
[0], arg
);
315 /* pstack didn't work, so we fall back to dumping the glibc
316 backtrace if we can. */
318 dump_glibc_backtrace (depth
, str
);
320 estr_write (" unable to produce a backtrace, sorry!\n");
326 /* Father process. */
334 /* Fallback to the glibc backtrace. */
335 estr_write ("\nBacktrace for this error:\n");
336 dump_glibc_backtrace (depth
, str
);