GetFEM  5.4.3
dal_backtrace.cc
1 /*===========================================================================
2 
3  Copyright (C) 1995-2020 Yves Renard
4 
5  This file is a part of GetFEM
6 
7  GetFEM is free software; you can redistribute it and/or modify it
8  under the terms of the GNU Lesser General Public License as published
9  by the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version along with the GCC Runtime Library
11  Exception either version 3.1 or (at your option) any later version.
12  This program is distributed in the hope that it will be useful, but
13  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15  License and GCC Runtime Library Exception for more details.
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program; if not, write to the Free Software Foundation,
18  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20 ===========================================================================*/
21 
22 #include "getfem/dal_backtrace.h"
23 #ifdef GETFEM_HAVE_BACKTRACE
24 # include <execinfo.h>
25 #endif
26 #ifdef GETFEM_HAVE_CXXABI_H
27 # include <cxxabi.h>
28 #endif
29 namespace dal {
30  /* demangles a c++ mangled function name, such as the ones
31  returned by backtrace_symbols or typeid(x).name()
32  If you call this function with a non-mangled name (i.e. "main"),
33  you will get strange results.
34  */
35 #ifdef GETFEM_HAVE_CXXABI_H
36  std::string demangle(const char *s) {
37  int status;
38  /* documented in http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/namespaceabi.html */
39  char *sd = abi::__cxa_demangle(s, NULL, NULL, &status);
40  if (sd == NULL || status) {
41  if (sd)
42  free(sd);
43  return std::string(""); // + " [could not be demangled]";
44  } else {
45  std::string res(sd);
46  free(sd);
47  return res;
48  }
49  }
50 #else
51  std::string demangle(const char *) { return std::string(""); }
52 #endif
53 
54 #ifdef GETFEM_HAVE_BACKTRACE
55  void dump_glibc_backtrace() {
56  static int cnt = 0;
57  int i,n;
58  void* trace[256];
59  char** strings;
60  if (cnt++ == 0) {
61  n = backtrace(trace, 256);
62  strings = backtrace_symbols (trace, n);
63  if (strings == NULL) {
64  fprintf(stderr, "backtrace unavailable ... no more memory ?\n"); return;
65  }
66  fprintf(stderr,"Backtrace dump follows:\n");
67  for (i = 0; i < n; ++i) {
68  char s[256]; strncpy(s,strings[i],256);s[255]=0;
69  char *a = strchr(s,'('), *b = 0;
70  if (a) b = strchr(a,'+');
71  if (!a || !b) {
72  fprintf(stderr,"%2d : %s\n", i, s);
73  } else {
74  *a = 0; *b = 0;
75  fprintf(stderr,"%2d : %s(%s+%s %s\n",
76  i, s, a+1, b+1, demangle(a+1).c_str());
77  }
78  }
79  free (strings);
80  cnt--;
81  } else { /* on n'est jamais trop prudent */
82  fprintf(stderr, "ooops, a recursive bug in dump_glibc_backtrace\n");
83  }
84  }
85 #endif
86 }
Get debug information.
Dynamic Array Library.