异常处理崩溃或退出 [英] Exception handling crashes or exits
问题描述
嗨大家好!
我正在使用gcc 4.2.3和glibc6 2.7。
我正在做一个异常处理实现图书馆。虽然我假设它已经正确完成,但是设置了一个处理程序,它会以随机退出值退出程序
,并且没有设置处理程序,它会崩溃用
SIGSEGV,但正如我在gdb中发现的那样,在同一行代码中:
__libc_siglongjmp(env = 0xbfb935f0,val = 1) at longjmp.c:29
29 {
31 _longjmp_unwind(env,val);
_longjmp_unwind(env = 0xbfb935f0,val = 1)在
../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);
我使用的类型是:
typedef struct ExcType ExcType;
typedef struct ExcVal ExcVal;
typedef struct Exception Exception;
struct ExcType {
const char * name; < br $> b $ b};
union ExcVal {
void * None;
int Int;
char * Char;
};
typedef enum {
NoneType,
IntType,
CharType
}数据类型; < br $>
struct Exception {
ExcType * tp;
ExcVal * val;
数据类型dt;
};
struct ExcEntry {
ExcEntry * prev,* next;
jmp_buf buf;
例外* exc;
};
extern ExcEntry * currexc;
我做异常处理的宏如下:
#define试试\
{\
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);}
示例程序可以是:
#include< stdio.h>
ExcType RuntimeError;
int main (int argc,char ** argv)
{
try {
throw(RuntimeError," test error");
} catch(e){
fprintf(stderr,foobar!\ n);
退出(42);
} endtry;
}
我不知道问题出在哪里。有谁可以帮助我吗?
问候,
Fabiano
< blockquote> 2008-09-08,Fabiano Sidler< fa *********** @ my-mail.chwrote:
嗨伙伴们!
我正在使用gcc 4.2.3和glibc6 2.7。
我正在为库执行异常处理实现。虽然我假设它已经正确完成,但是设置了一个处理程序,它会以随机退出值退出程序
,并且没有设置处理程序,它会崩溃用
SIGSEGV,但正如我在gdb中发现的那样,在同一行代码中:
__libc_siglongjmp(env = 0xbfb935f0,val = 1) at longjmp.c:29
29 {
31 _longjmp_unwind(env,val);
_longjmp_unwind(env = 0xbfb935f0,val = 1)在
../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);
我使用的类型是:
typedef struct ExcType ExcType;
typedef struct ExcVal ExcVal;
typedef struct Exception Exception;
struct ExcType {
const char *姓名;
};
union ExcVal {
void *无;
int Int;
char * Char;
};
typedef enum {
NoneType,
IntType,
CharType
}数据类型;
struct异常{
ExcType * tp;
ExcVal * val;
数据类型dt ;
};
struct ExcEntry {
ExcEntry * prev,* next;
jmp_buf buf;
异常* exc;
};
extern ExcEntry * currexc;
其中是这个定义的?您似乎在没有定义它的情况下使用它。该程序是否成功链接?
>
我做异常处理的宏如下:
#define尝试\
{\
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);}
示例程序可以是:
#include< stdio.h>
ExcType RuntimeError;
int main(int argc,char ** argv)
{
try {
throw(RuntimeError," test error") ;
} catch(e){
fprintf(stderr," foobar!\ n");
退出(42);
} endtry;
}
我不知道问题出在哪里。这里有谁可以帮助我?
问候,
Fabiano
-
Andrew Poelstra ap*******@wpsoftware.com
要给我发电子邮件,请使用以上电子邮件地址,.com设置为.net
On 2008-09-08,Andrew Poelstra< ap *** ****@supernova.homewrote:
这个定义在哪里?
jmp_buf在setjmp.h中定义,而setjmp.h又是ANSI C头。
也许我应该写这个.. 。在我的程序中,它包括在内。
此程序是否成功链接?
< setjmp.h必须包括在内,定义(和声明)
必须在main()之前完成,但之后呢确实如此,否则它不会是SEGSEGV
这里......;)
问候,
法比亚诺
2008-09-08,Fabiano Sidler< fa *********** @ my-mail.chwrote:
2008-09-08,Andrew Poelstra< ap ******* @ supernova.homewrote:
> Where是这个定义的?
jmp_buf在setjmp.h中定义,反过来又是ANSI C头。
也许我应该写这个..在我的程序中,它包括在内。
当然,但是jmp_buf只是currexc对象的一个成员,对于
哪个存储是永远不会分配(因为这个对象不是
定义,据我所知)。
>此程序是否成功链接?
< setjmp.h必须包括在内,定义(和声明)
必须在main()之前完成,但是它确实如此,否则它这里没有SEGSEGV
...;)
-
Andrew Poelstra ap ******* @ wpsoftware.com
要给我发电子邮件,请使用上面的电子邮件地址.com设置为.net
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
On 2008-09-08, Fabiano Sidler <fa***********@my-mail.chwrote: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;
Where is this defined? You appear to use it without ever
defining it. Does this program link successfully?>
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
--
Andrew Poelstra ap*******@wpsoftware.com
To email me, use the above email addresss with .com set to .net
On 2008-09-08, Andrew Poelstra <ap*******@supernova.homewrote:Where is this defined?jmp_buf is defined in setjmp.h which is, in turn, an ANSI C header.
Maybe i should have written this...in my program, it is included.
Does this program link successfully?<setjmp.hmust be included and the definitions (and the declaration)
must be done before main(), but then it does, otherwise it couldn''t SEGSEGV
here...;)
Greetings,
Fabiano
On 2008-09-08, Fabiano Sidler <fa***********@my-mail.chwrote:On 2008-09-08, Andrew Poelstra <ap*******@supernova.homewrote:>Where is this defined?
jmp_buf is defined in setjmp.h which is, in turn, an ANSI C header.
Maybe i should have written this...in my program, it is included.
Sure, but jmp_buf is just a member of the currexc object, for
which storage is never allocated (since this object is not
defined, as far as I can tell).
>Does this program link successfully?
<setjmp.hmust be included and the definitions (and the declaration)
must be done before main(), but then it does, otherwise it couldn''t SEGSEGV
here...;)
--
Andrew Poelstra ap*******@wpsoftware.com
To email me, use the above email addresss with .com set to .net
这篇关于异常处理崩溃或退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!