1 /****************************************************************************/
3 /* Chaine de verification */
5 /* Produit : AVT Version 1 */
6 /* Fichier : avt_util.c */
8 /* (c) copyright 1998-1999 AVERTEC */
9 /* Tous droits reserves */
10 /* Support : e-mail support@avertec.com */
12 /* Auteur(s) : AUGUSTINS Gilles */
14 /****************************************************************************/
16 #include "avt_headers.h"
18 chain_list
*SEGV_MESSAGE
= NULL
;
19 chain_list
*FPE_MESSAGE
= NULL
;
20 chain_list
*INT_MESSAGE
= NULL
;
22 chain_list
*SEGV_EXIT
= NULL
;
23 chain_list
*FPE_EXIT
= NULL
;
24 chain_list
*INT_EXIT
= NULL
;
26 chain_list
*SEGV_CODE
= NULL
;
27 chain_list
*FPE_CODE
= NULL
;
28 chain_list
*INT_CODE
= NULL
;
30 typedef void (*t_pfunc
)(int);
32 t_pfunc orig_SEGV
, orig_FPE
, orig_BUS
;
33 int trap_SEGV_count
=0, trap_FPE_count
=0;
44 size
= backtrace (array
, 40);
45 strings
= backtrace_symbols (array
, size
);
47 printf ("Obtained %zd stack frames.\n", size
);
49 for (i
= 0; i
< size
; i
++) {
50 printf ("%s\n", strings
[i
]);
58 sprintf(buf
, "/bin/pstack %ld", (long)getpid());
63 /* ------------------------------------------------------------------------ */
65 char *GetMessage (chain_list
*mlist
)
67 return (char*)mlist
->DATA
;
70 /* ------------------------------------------------------------------------ */
72 t_pfunc
GetExit (chain_list
*mlist
)
74 return (t_pfunc
)mlist
->DATA
;
77 /* ------------------------------------------------------------------------ */
78 int GetCode (chain_list
*mlist
)
80 return (int)(long)mlist
->DATA
;
83 /* ------------------------------------------------------------------------ */
85 void avt_Handler (int sig
)
98 segv_mes
= "Internal error, please contact Avertec support\n";
101 segv_mes
= GetMessage (SEGV_MESSAGE
);
104 fpe_mes
= "Internal error, please contact Avertec support\n";
106 fpe_mes
= GetMessage (FPE_MESSAGE
);
109 int_mes
= "Program aborted\n";
111 int_mes
= GetMessage (INT_MESSAGE
);
115 fprintf (stdout
, "\x1B[0m");
119 avt_errmsg(AVT_ERRMSG
,"027",-1,segv_mes
);
121 if (V_BOOL_TAB
[__AVT_TRACE_FATAL
].VALUE
== 1)
123 if (V_BOOL_TAB
[__AVT_ENABLE_CORE
].VALUE
== 1 || force_core
)
126 GetExit (SEGV_EXIT
) (GetCode (SEGV_CODE
));
132 avt_errmsg(AVT_ERRMSG
,"027",-1,segv_mes
);
134 if (V_BOOL_TAB
[__AVT_TRACE_FATAL
].VALUE
== 1)
136 if (V_BOOL_TAB
[__AVT_ENABLE_CORE
].VALUE
== 1 || force_core
)
139 GetExit (SEGV_EXIT
) (GetCode (SEGV_CODE
));
145 avt_errmsg(AVT_ERRMSG
,"027",-1,fpe_mes
);
147 if (V_BOOL_TAB
[__AVT_TRACE_FATAL
].VALUE
== 1)
149 if (V_BOOL_TAB
[__AVT_ENABLE_CORE
].VALUE
== 1 || force_core
)
152 GetExit (FPE_EXIT
) (GetCode (FPE_CODE
));
157 avt_errmsg(AVT_ERRMSG
,"027",-1,int_mes
);
160 GetExit (INT_EXIT
) (GetCode (INT_CODE
));
167 /* ------------------------------------------------------------------------ */
171 if (trap_SEGV_count
==0)
173 orig_SEGV
= signal (SIGSEGV
, avt_Handler
);
176 orig_BUS
= signal (SIGBUS
, avt_Handler
);
182 void avt_UnTrapSegV ()
185 if (trap_SEGV_count
==0)
187 signal (SIGSEGV
, orig_SEGV
);
189 signal (SIGBUS
, orig_BUS
);
195 if (trap_FPE_count
==0)
196 orig_FPE
= signal (SIGFPE
, avt_Handler
);
200 void avt_UnTrapFPE ()
203 if (trap_FPE_count
==0)
204 signal (SIGSEGV
, orig_FPE
);
209 signal (SIGINT
, avt_Handler
);
212 /* ------------------------------------------------------------------------ */
214 void avt_PushSegVExit (void (*fexit
)(int), int code
)
216 SEGV_EXIT
= addchain (SEGV_EXIT
, fexit
);
217 SEGV_CODE
= addchain (SEGV_CODE
, (void*)(long)code
);
220 void avt_PopSegVExit ()
223 SEGV_EXIT
= delchain (SEGV_EXIT
, SEGV_EXIT
);
225 SEGV_CODE
= delchain (SEGV_CODE
, SEGV_CODE
);
228 /* ------------------------------------------------------------------------ */
230 void avt_PushFPEExit (void (*fexit
)(int), int code
)
232 FPE_EXIT
= addchain (FPE_EXIT
, fexit
);
233 FPE_CODE
= addchain (FPE_CODE
, (void*)(long)code
);
236 void avt_PopFPEExit ()
239 FPE_EXIT
= delchain (FPE_EXIT
, FPE_EXIT
);
241 FPE_CODE
= delchain (FPE_CODE
, FPE_CODE
);
244 /* ------------------------------------------------------------------------ */
246 void avt_PushKillExit (void (*fexit
)(int), int code
)
248 INT_EXIT
= addchain (INT_EXIT
, fexit
);
249 INT_CODE
= addchain (INT_CODE
, (void*)(long)code
);
252 void avt_PopKillExit ()
255 INT_EXIT
= delchain (INT_EXIT
, INT_EXIT
);
257 INT_CODE
= delchain (INT_CODE
, INT_CODE
);
260 /* ------------------------------------------------------------------------ */
262 void avt_PushSegVMessage (char *message
)
264 SEGV_MESSAGE
= addchain (SEGV_MESSAGE
, message
);
267 void avt_PopSegVMessage ()
270 SEGV_MESSAGE
= delchain (SEGV_MESSAGE
, SEGV_MESSAGE
);
273 /* ------------------------------------------------------------------------ */
275 void avt_PushFPEMessage (char *message
)
277 FPE_MESSAGE
= addchain (FPE_MESSAGE
, message
);
280 void avt_PopFPEMessage ()
283 FPE_MESSAGE
= delchain (FPE_MESSAGE
, FPE_MESSAGE
);
286 /* ------------------------------------------------------------------------ */
288 void avt_PushKillMessage (char *message
)
290 INT_MESSAGE
= addchain (INT_MESSAGE
, message
);
293 void avt_PopKillMessage ()
296 INT_MESSAGE
= delchain (INT_MESSAGE
, INT_MESSAGE
);
298 /* ------------------------------------------------------------------------ */