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)
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
++)
98 st_printf (" + %s\n", str
[i
]);
104 /* show_backtrace displays the backtrace, currently obtained by means of
105 the glibc backtrace* functions. */
107 show_backtrace (void)
118 depth
= backtrace (trace
, DEPTH
);
122 str
= backtrace_symbols (trace
, depth
);
127 #define STDIN_FILENO 0
130 #ifndef STDOUT_FILENO
131 #define STDOUT_FILENO 1
134 #ifndef STDERR_FILENO
135 #define STDERR_FILENO 2
138 /* We attempt to extract file and line information from addr2line. */
141 /* Local variables. */
142 int f
[2], pid
, line
, i
;
144 char addr_buf
[DEPTH
][GFC_XTOA_BUF_SIZE
], func
[BUFSIZE
], file
[BUFSIZE
];
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. */
157 if ((pid
= fork ()) == -1)
163 #define NUM_FIXEDARGS 5
164 char *arg
[DEPTH
+NUM_FIXEDARGS
+1];
167 close (STDIN_FILENO
);
168 close (STDERR_FILENO
);
170 if (dup2 (f
[1], STDOUT_FILENO
) == -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
);
187 /* Father process. */
190 output
= fdopen (f
[0], "r");
193 if (fgets (func
, sizeof(func
), output
))
195 st_printf ("\nBacktrace for this error:\n");
199 if (! fgets (file
, sizeof(file
), output
))
204 for (p
= func
; *p
!= '\n' && *p
!= '\r'; p
++)
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)
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
)
221 /* If we only have the address, use the glibc backtrace. */
222 if (func
[0] == '?' && func
[1] == '?' && file
[0] == '?'
225 st_printf (" + %s\n", str
[i
]);
229 /* Extract the line number. */
230 for (end
= NULL
, p
= file
; *p
; p
++)
241 if (strcmp (func
, "MAIN__") == 0)
242 st_printf (" + in the main program\n");
244 st_printf (" + function %s (0x%s)\n", func
, addr
[i
]);
246 if (line
<= 0 && strcmp (file
, "??") == 0)
250 st_printf (" from file %s\n", file
);
252 st_printf (" at line %d of file %s\n", line
, file
);
254 while (fgets (func
, sizeof(func
), output
));
260 st_printf ("** Something went wrong while running addr2line. **\n"
261 "** Falling back to a simpler backtrace scheme. **\n");
272 #if CAN_FORK && defined(HAVE_GETPPID)
273 /* Try to call pstack. */
276 /* Local variables. */
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)
288 char *arg
[NUM_ARGS
+1];
291 st_printf ("\nBacktrace for this error:\n");
292 arg
[0] = (char *) "pstack";
294 snprintf (buf
, sizeof(buf
), "%d", (int) getppid ());
296 sprintf (buf
, "%d", (int) getppid ());
300 execvp (arg
[0], arg
);
303 /* pstack didn't work, so we fall back to dumping the glibc
304 backtrace if we can. */
306 dump_glibc_backtrace (depth
, str
);
308 st_printf (" unable to produce a backtrace, sorry!\n");
314 /* Father process. */
322 /* Fallback to the glibc backtrace. */
323 st_printf ("\nBacktrace for this error:\n");
324 dump_glibc_backtrace (depth
, str
);