1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /*************************************************************** |
---|
5 | * File: dError.cc |
---|
6 | * Purpose: implementation for debug error handling |
---|
7 | * Author: obachman (Olaf Bachmann) |
---|
8 | * Created: 9/00 |
---|
9 | *******************************************************************/ |
---|
10 | #ifndef DERROR_C |
---|
11 | #define DERROR_C |
---|
12 | |
---|
13 | #include <stdarg.h> |
---|
14 | #include <stdio.h> |
---|
15 | #include <stdlib.h> |
---|
16 | #include <strings.h> |
---|
17 | |
---|
18 | #include "config.h" |
---|
19 | |
---|
20 | static inline void malloc_free( void * ptr ) |
---|
21 | { |
---|
22 | free(ptr); |
---|
23 | } |
---|
24 | |
---|
25 | #ifdef HAVE_EXECINFO_H |
---|
26 | #include <execinfo.h> |
---|
27 | #endif |
---|
28 | |
---|
29 | #ifdef HAVE_UNISTD_H |
---|
30 | #include <unistd.h> |
---|
31 | #endif |
---|
32 | |
---|
33 | #ifdef HAVE_GCC_ABI_DEMANGLE |
---|
34 | #include <cxxabi.h> |
---|
35 | #endif |
---|
36 | |
---|
37 | |
---|
38 | #include <reporter/reporter.h> |
---|
39 | |
---|
40 | #ifdef HAVE_CONFIG_H |
---|
41 | #include <omalloc/omalloc.h> |
---|
42 | #endif |
---|
43 | |
---|
44 | #ifndef MAKE_DISTRIBUTION |
---|
45 | // dummy procedure for setting a breakpoint |
---|
46 | // within the debugger |
---|
47 | void dErrorBreak() |
---|
48 | {} |
---|
49 | #endif |
---|
50 | |
---|
51 | #ifdef __cplusplus |
---|
52 | extern "C" |
---|
53 | { |
---|
54 | #endif |
---|
55 | |
---|
56 | int dReportError(const char* fmt, ...) |
---|
57 | { |
---|
58 | #ifdef HAVE_EXECINFO_H |
---|
59 | #define SIZE 50 |
---|
60 | void *buffer[SIZE+1]; int i, j, k, ret, status; char **ptr; char *demangledName; char *s; char *ss; |
---|
61 | #endif |
---|
62 | |
---|
63 | va_list ap; |
---|
64 | va_start(ap, fmt); |
---|
65 | #ifndef MAKE_DISTRIBUTION |
---|
66 | fprintf(stderr, "\n// ***dError: "); |
---|
67 | vfprintf(stderr, fmt, ap); |
---|
68 | #if 0 |
---|
69 | if !defined(OM_NDEBUG) && defined(HAVE_CONFIG_H) |
---|
70 | #endif |
---|
71 | #if defined(HAVE_CONFIG_H) |
---|
72 | fprintf(stderr, " occured at: \n"); |
---|
73 | omPrintCurrentBackTraceMax(stderr, 8); |
---|
74 | #endif |
---|
75 | |
---|
76 | #ifdef HAVE_EXECINFO_H |
---|
77 | ret = backtrace( buffer, SIZE ); // execinfo.h |
---|
78 | fprintf(stderr, "\nExecinfo backtrace (with %zd stack frames): \n", ret); |
---|
79 | |
---|
80 | #ifndef HAVE_GCC_ABI_DEMANGLE |
---|
81 | backtrace_symbols_fd(buffer, ret, STDERR_FILENO); // execinfo.h |
---|
82 | #else |
---|
83 | ptr = backtrace_symbols( buffer, ret ); // execinfo.h |
---|
84 | |
---|
85 | for (i = 0; i < ret; i++) |
---|
86 | { |
---|
87 | status = -1; |
---|
88 | |
---|
89 | s = ptr[i]; |
---|
90 | // fprintf (stderr, " #%02d: %s\n", i, s); |
---|
91 | |
---|
92 | ss = index(s, '('); |
---|
93 | ss[0] = 0; |
---|
94 | fprintf (stderr, " #%02d: '%s': ", i, s); |
---|
95 | ss[0] = '('; s = ss + 1; |
---|
96 | |
---|
97 | ss = index(s, '+'); |
---|
98 | |
---|
99 | if ( ss != NULL ) |
---|
100 | { |
---|
101 | ss[0] = 0; |
---|
102 | demangledName = abi::__cxa_demangle( s, NULL, NULL, &status ); // cxxabi.h! |
---|
103 | if( status == 0 && demangledName != NULL ) |
---|
104 | fprintf (stderr, " '%s'", (demangledName[0] != 0)? demangledName: s); |
---|
105 | else |
---|
106 | fprintf (stderr, " '%s'", s); |
---|
107 | |
---|
108 | malloc_free( demangledName ); |
---|
109 | ss[0] = '+'; |
---|
110 | s = ss + 1; |
---|
111 | } |
---|
112 | |
---|
113 | ss = index(s, ')'); |
---|
114 | if( s != ss) |
---|
115 | { |
---|
116 | ss[0] = 0; |
---|
117 | fprintf (stderr, " + %s", s); |
---|
118 | ss[0] = ')'; |
---|
119 | } |
---|
120 | |
---|
121 | fprintf (stderr, " %s\n", ss + 2); |
---|
122 | } |
---|
123 | malloc_free (ptr); |
---|
124 | #endif |
---|
125 | |
---|
126 | #undef SIZE |
---|
127 | #endif |
---|
128 | |
---|
129 | dErrorBreak(); |
---|
130 | #else |
---|
131 | fprintf(stderr, "\n// !!! YOU HAVE FOUND A BUG IN SINGULAR::Spielwiese."); |
---|
132 | fprintf(stderr, "// !!! Please, email the input\n// and the following error message to singular@mathematik.uni-kl.de") |
---|
133 | vfprintf(stderr, fmt, ap); |
---|
134 | #endif |
---|
135 | return 0; |
---|
136 | } |
---|
137 | |
---|
138 | |
---|
139 | |
---|
140 | #ifdef __cplusplus |
---|
141 | } |
---|
142 | #endif |
---|
143 | |
---|
144 | #endif |
---|
145 | |
---|
146 | |
---|
147 | |
---|
148 | |
---|
149 | |
---|
150 | |
---|