F
Fabiano Sidler
Hi folks!
I'm using gcc 4.2.3 and glibc6 2.7.
I'm doing an exception handling implementation for a library. Although I
assume to have it done properly, with a handler set up, it exits the program
with a random exit value, and without a handler set up, it crashes with a
SIGSEGV, but as I have found out with gdb, at the same line of code:
__libc_siglongjmp (env=0xbfb935f0, val=1) at longjmp.c:29
29 {
31 _longjmp_unwind (env, val);
_longjmp_unwind (env=0xbfb935f0, val=1) at
../nptl/sysdeps/unix/sysv/linux/jmp-unwind.c:32
32 if (__libc_pthread_functions_init)
39 }
__libc_siglongjmp (env=0xbfb935f0, val=1) at longjmp.c:33
33 if (env[0].__mask_was_saved)
39 __longjmp (env[0].__jmpbuf, val ?: 1);
The types i use are:
typedef struct ExcType ExcType;
typedef struct ExcVal ExcVal;
typedef struct Exception Exception;
struct ExcType {
const char *name;
};
union ExcVal {
void* None;
int Int;
char* Char;
};
typedef enum {
NoneType,
IntType,
CharType
} Datatype;
struct Exception {
ExcType *tp;
ExcVal *val;
Datatype dt;
};
struct ExcEntry {
ExcEntry *prev,*next;
jmp_buf buf;
Exception *exc;
};
extern ExcEntry *currexc;
My macros for doing exception handling are as follows:
#define try \
{\
Exception exc;\
ExcEntry excn;\
excn.exc=&exc; excn.prev=currexc;\
currexc = excn.prev->next = &excn;\
if ( setjmp(excn.buf)==0 )
#define catch(e) \
Exception *e = &exc;\
for (;0;currexc=currexc->prev)
#define exctype(et, action) \
if ( e->tp==&et ) { action; }
#define endtry }
#define throw(t,vl) \
{currexc->exc->tp=NULL;\
currexc->exc->tp=&t;\
currexc->exc->val->Char=(char*)vl;\
currexc->exc->dt=CharType;\
jmp_buf buf;\
memcpy((void*)&buf,(void*)currexc->buf, sizeof(jmp_buf));\
longjmp(buf,1);}
A sample program could be:
#include <stdio.h>
ExcType RuntimeError;
int main(int argc, char **argv)
{
try {
throw(RuntimeError,"test error");
} catch(e) {
fprintf(stderr,"foobar!\n");
exit(42);
} endtry;
}
I don't see where the problem is. Any professional here who can help me?
Greetings,
Fabiano
I'm using gcc 4.2.3 and glibc6 2.7.
I'm doing an exception handling implementation for a library. Although I
assume to have it done properly, with a handler set up, it exits the program
with a random exit value, and without a handler set up, it crashes with a
SIGSEGV, but as I have found out with gdb, at the same line of code:
__libc_siglongjmp (env=0xbfb935f0, val=1) at longjmp.c:29
29 {
31 _longjmp_unwind (env, val);
_longjmp_unwind (env=0xbfb935f0, val=1) at
../nptl/sysdeps/unix/sysv/linux/jmp-unwind.c:32
32 if (__libc_pthread_functions_init)
39 }
__libc_siglongjmp (env=0xbfb935f0, val=1) at longjmp.c:33
33 if (env[0].__mask_was_saved)
39 __longjmp (env[0].__jmpbuf, val ?: 1);
The types i use are:
typedef struct ExcType ExcType;
typedef struct ExcVal ExcVal;
typedef struct Exception Exception;
struct ExcType {
const char *name;
};
union ExcVal {
void* None;
int Int;
char* Char;
};
typedef enum {
NoneType,
IntType,
CharType
} Datatype;
struct Exception {
ExcType *tp;
ExcVal *val;
Datatype dt;
};
struct ExcEntry {
ExcEntry *prev,*next;
jmp_buf buf;
Exception *exc;
};
extern ExcEntry *currexc;
My macros for doing exception handling are as follows:
#define try \
{\
Exception exc;\
ExcEntry excn;\
excn.exc=&exc; excn.prev=currexc;\
currexc = excn.prev->next = &excn;\
if ( setjmp(excn.buf)==0 )
#define catch(e) \
Exception *e = &exc;\
for (;0;currexc=currexc->prev)
#define exctype(et, action) \
if ( e->tp==&et ) { action; }
#define endtry }
#define throw(t,vl) \
{currexc->exc->tp=NULL;\
currexc->exc->tp=&t;\
currexc->exc->val->Char=(char*)vl;\
currexc->exc->dt=CharType;\
jmp_buf buf;\
memcpy((void*)&buf,(void*)currexc->buf, sizeof(jmp_buf));\
longjmp(buf,1);}
A sample program could be:
#include <stdio.h>
ExcType RuntimeError;
int main(int argc, char **argv)
{
try {
throw(RuntimeError,"test error");
} catch(e) {
fprintf(stderr,"foobar!\n");
exit(42);
} endtry;
}
I don't see where the problem is. Any professional here who can help me?
Greetings,
Fabiano