GetFEM  5.4.2
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  std::string demangle(const char *
36 #ifdef GETFEM_HAVE_CXXABI_H
37  s
38 #endif
39  ) {
40 #ifdef GETFEM_HAVE_CXXABI_H
41  int status;
42  /* documented in http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/namespaceabi.html */
43  char *sd = abi::__cxa_demangle(s, NULL, NULL, &status);
44  if (sd == NULL || status) {
45  if (sd) free(sd);
46  return std::string(""); // + " [could not be demangled]";
47  } else {
48  std::string res(sd); free(sd); return res;
49  }
50 #else
51  return std::string("");
52 #endif
53  }
54 
55 #ifdef GETFEM_HAVE_BACKTRACE
56  void dump_glibc_backtrace() {
57  static int cnt = 0;
58  int i,n;
59  void* trace[256];
60  char** strings;
61  if (cnt++ == 0) {
62  n = backtrace(trace, 256);
63  strings = backtrace_symbols (trace, n);
64  if (strings == NULL) {
65  fprintf(stderr, "backtrace unavailable ... no more memory ?\n"); return;
66  }
67  fprintf(stderr,"Backtrace dump follows:\n");
68  for (i = 0; i < n; ++i) {
69  char s[256]; strncpy(s,strings[i],256);s[255]=0;
70  char *a = strchr(s,'('), *b = 0;
71  if (a) b = strchr(a,'+');
72  if (!a || !b) {
73  fprintf(stderr,"%2d : %s\n", i, s);
74  } else {
75  *a = 0; *b = 0;
76  fprintf(stderr,"%2d : %s(%s+%s %s\n",
77  i, s, a+1, b+1, demangle(a+1).c_str());
78  }
79  }
80  free (strings);
81  cnt--;
82  } else { /* on n'est jamais trop prudent */
83  fprintf(stderr, "ooops, a recursive bug in dump_glibc_backtrace\n");
84  }
85  }
86 #endif
87 }
dal_backtrace.h
Get debug information.
dal
Dynamic Array Library.
Definition: dal_backtrace.cc:29