error.c
Go to the documentation of this file.
1 
13 #define _GK_ERROR_C_ /* this is needed to properly declare the gk_jub* variables
14  as an extern function in GKlib.h */
15 
16 #include <GKlib.h>
17 
18 
19 /* These are the jmp_buf for the graceful exit in case of severe errors.
20  Multiple buffers are defined to allow for recursive invokation. */
21 #define MAX_JBUFS 128
22 __thread int gk_cur_jbufs=-1;
23 __thread jmp_buf gk_jbufs[MAX_JBUFS];
24 __thread jmp_buf gk_jbuf;
25 
26 typedef void (*gksighandler_t)(int);
27 
28 /* These are the holders of the old singal handlers for the trapped signals */
29 static __thread gksighandler_t old_SIGMEM_handler; /* Custom signal */
30 static __thread gksighandler_t old_SIGERR_handler; /* Custom signal */
31 static __thread gksighandler_t old_SIGMEM_handlers[MAX_JBUFS]; /* Custom signal */
32 static __thread gksighandler_t old_SIGERR_handlers[MAX_JBUFS]; /* Custom signal */
33 
34 /* The following is used to control if the gk_errexit() will actually abort or not.
35  There is always a single copy of this variable */
36 static int gk_exit_on_error = 1;
37 
38 
39 /*************************************************************************/
42 /*************************************************************************/
44 {
46 }
47 
48 
49 
50 /*************************************************************************/
53 /*************************************************************************/
54 void errexit(char *f_str,...)
55 {
56  va_list argp;
57 
58  va_start(argp, f_str);
59  vfprintf(stderr, f_str, argp);
60  va_end(argp);
61 
62  if (strlen(f_str) == 0 || f_str[strlen(f_str)-1] != '\n')
63  fprintf(stderr,"\n");
64  fflush(stderr);
65 
66  if (gk_exit_on_error)
67  exit(-2);
68 
69  /* abort(); */
70 }
71 
72 
73 /*************************************************************************/
76 /*************************************************************************/
77 void gk_errexit(int signum, char *f_str,...)
78 {
79  va_list argp;
80 
81  va_start(argp, f_str);
82  vfprintf(stderr, f_str, argp);
83  va_end(argp);
84 
85  fprintf(stderr,"\n");
86  fflush(stderr);
87 
88  if (gk_exit_on_error)
89  raise(signum);
90 }
91 
92 
93 /***************************************************************************/
97 /***************************************************************************/
98 int gk_sigtrap()
99 {
100  if (gk_cur_jbufs+1 >= MAX_JBUFS)
101  return 0;
102 
103  gk_cur_jbufs++;
104 
107 
108  return 1;
109 }
110 
111 
112 /***************************************************************************/
115 /***************************************************************************/
116 int gk_siguntrap()
117 {
118  if (gk_cur_jbufs == -1)
119  return 0;
120 
123 
124  gk_cur_jbufs--;
125 
126  return 1;
127 }
128 
129 
130 /*************************************************************************/
134 /*************************************************************************/
135 void gk_sigthrow(int signum)
136 {
137  longjmp(gk_jbufs[gk_cur_jbufs], signum);
138 }
139 
140 
141 /***************************************************************************
142 * This function sets a number of signal handlers and sets the return point
143 * of a longjmp
144 ****************************************************************************/
145 void gk_SetSignalHandlers()
146 {
149 }
150 
151 
152 /***************************************************************************
153 * This function sets the handlers for the signals to their default handlers
154 ****************************************************************************/
156 {
157  signal(SIGMEM, old_SIGMEM_handler);
158  signal(SIGERR, old_SIGERR_handler);
159 }
160 
161 
162 /*************************************************************************
163 * This function is the handler for SIGUSR1 that implements the cleaning up
164 * process prior to a non-local exit.
165 **************************************************************************/
166 void gk_NonLocalExit_Handler(int signum)
167 {
168  longjmp(gk_jbuf, signum);
169 }
170 
171 
172 /*************************************************************************/
174 /**************************************************************************/
175 char *gk_strerror(int errnum)
176 {
177 #if defined(WIN32) || defined(__MINGW32__)
178  return strerror(errnum);
179 #else
180 #ifndef SUNOS
181  static __thread char buf[1024];
182 
183  strerror_r(errnum, buf, 1024);
184 
185  buf[1023] = '\0';
186  return buf;
187 #else
188  return strerror(errnum);
189 #endif
190 #endif
191 }
192 
193 
194 
195 /*************************************************************************
196 * This function prints a backtrace of calling functions
197 **************************************************************************/
198 void PrintBackTrace()
199 {
200 #ifdef HAVE_EXECINFO_H
201  void *array[10];
202  int i, size;
203  char **strings;
204 
205  size = backtrace(array, 10);
206  strings = backtrace_symbols(array, size);
207 
208  printf("Obtained %d stack frames.\n", size);
209  for (i=0; i<size; i++) {
210  printf("%s\n", strings[i]);
211  }
212  free(strings);
213 #endif
214 }
gtsam.examples.DogLegOptimizerExample.int
int
Definition: DogLegOptimizerExample.py:111
setup.stderr
stderr
Definition: wrap/pybind11/setup.py:138
gk_sigtrap
int gk_sigtrap()
Definition: error.c:97
gk_jbufs
__thread jmp_buf gk_jbufs[MAX_JBUFS]
Definition: error.c:22
gk_siguntrap
int gk_siguntrap()
Definition: error.c:115
SIGMEM
#define SIGMEM
Definition: gk_defs.h:37
gk_strerror
char * gk_strerror(int errnum)
Thread-safe implementation of strerror()
Definition: error.c:174
errexit
void errexit(char *f_str,...)
Definition: error.c:53
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
gk_cur_jbufs
__thread int gk_cur_jbufs
Definition: error.c:21
old_SIGMEM_handler
static __thread gksighandler_t old_SIGMEM_handler
Definition: error.c:28
gk_sigthrow
void gk_sigthrow(int signum)
Definition: error.c:134
PrintBackTrace
void PrintBackTrace()
Definition: error.c:197
old_SIGERR_handler
static __thread gksighandler_t old_SIGERR_handler
Definition: error.c:29
gk_UnsetSignalHandlers
void gk_UnsetSignalHandlers()
Definition: error.c:154
SIGERR
#define SIGERR
Definition: gk_defs.h:38
gk_exit_on_error
static int gk_exit_on_error
Definition: error.c:35
gk_SetSignalHandlers
void gk_SetSignalHandlers()
Definition: error.c:144
old_SIGERR_handlers
static __thread gksighandler_t old_SIGERR_handlers[MAX_JBUFS]
Definition: error.c:31
array
Definition: numpy.h:821
gk_NonLocalExit_Handler
void gk_NonLocalExit_Handler(int signum)
Definition: error.c:165
GKlib.h
old_SIGMEM_handlers
static __thread gksighandler_t old_SIGMEM_handlers[MAX_JBUFS]
Definition: error.c:30
gksighandler_t
void(* gksighandler_t)(int)
Definition: error.c:25
MAX_JBUFS
#define MAX_JBUFS
Definition: error.c:20
gk_set_exit_on_error
void gk_set_exit_on_error(int value)
Definition: error.c:42
test_callbacks.value
value
Definition: test_callbacks.py:160
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
gk_errexit
void gk_errexit(int signum, char *f_str,...)
Definition: error.c:76
gk_jbuf
__thread jmp_buf gk_jbuf
Definition: error.c:23


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:11:31