删帖吧
本帖最后由 xyfree 于 2012-01-21 03:35 编辑滚
回复 #1 xyfree 的帖子
难道c++ on unix的异常机制不是用signal实现的? 本帖最后由 xyfree 于 2012-01-21 03:36 编辑滚 UNIX的宗旨就是,不要动不动就想到汇编这种垃圾,甚至不应该去"想"汇编(因为在UNIX的世界里,它就是垃圾)
SIGFPE信号是算术异常导致,由内核发送
回复 #4 cjaizss 的帖子
原来是这样~ 学习了~其实是因为小弟在考虑编译器如何为try块生成linux、unix下的代码
所以有用汇编的一说~
不过现在看来,倒是应该直接利用C库
回复 #5 xyfree 的帖子
在《c接口和实现》那本书里就实现了一个c语言版本的TRY...EXCEPT,主要就是setjump, longjump 我猜测是这样的,linux下除0会产生硬件中断,linux操作系统硬件中断处理程序会去收集CPU相关信息(哪个进程发生了什么异常),然后根据相应的异常信息给发生异常的进程发生相应信号。等CPU调度到发生异常的进程的时候会调用信号处理程序实现异常处理,当然像Page Fault等异常内核自己处理,不会给进程发送信号。GCC编译器C++异常处理机制应该是基于以上机制的封装。 >>当然像Page Fault等异常内核自己处理,不会给进程发送信号。
这个应该也处理的。
例如int *p=NULL; *p=1;这个异常可以捕获。 linux下的cpp和try,catch还真不会写,网上搜了一个
#include <iostream>
using namespace std ;
class A {
public:
A() {
}
} ;
void
f( void )
{
throw A( ) ;
}
int
main( int argc, char *argv[] )
{
try {
f( ) ;
}
catch( A &e ) {
cout << "Exception occured!" << endl ;
}
return 0 ;
}
# gcc -lstdc++ 1.cpp
# ./a.out
Exception occured!
看了一下它的反汇编代码,看不懂
.file "1.cpp"
.local _ZSt8__ioinit
.comm _ZSt8__ioinit,1,1
.text
.align 2
.globl _Z1fv
.type _Z1fv,@function
_Z1fv:
.LFB1485:
pushl %ebp
.LCFI0:
movl %esp, %ebp
.LCFI1:
pushl %ebx
.LCFI2:
subl $4, %esp
.LCFI3:
subl $12, %esp
leal -5(%ebp), %eax
pushl %eax
.LCFI4:
call _ZN1AC1Ev
addl $16, %esp
subl $12, %esp
pushl $1
call __cxa_allocate_exception
addl $16, %esp
movl %eax, %ebx
subl $8, %esp
leal -5(%ebp), %eax
pushl %eax
pushl %ebx
call _ZN1AC1ERKS_
addl $16, %esp
subl $4, %esp
pushl $0
pushl $_ZTI1A
pushl %ebx
call __cxa_throw
.LFE1485:
.Lfe1:
.size _Z1fv,.Lfe1-_Z1fv
.globl _Unwind_Resume
.section .rodata
.LC0:
.string "Exception occured!"
.text
.align 2
.globl main
.type main,@function
main:
.LFB1487:
pushl %ebp
.LCFI5:
movl %esp, %ebp
.LCFI6:
pushl %ebx
.LCFI7:
subl $20, %esp
.LCFI8:
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
.LEHB0:
call _Z1fv
.LEHE0:
jmp .L5
.L13:
movl %eax, -12(%ebp)
cmpl $1, %edx
je .L6
subl $12, %esp
pushl -12(%ebp)
.LEHB1:
.LCFI9:
call _Unwind_Resume
.LEHE1:
.L6:
subl $12, %esp
pushl -12(%ebp)
call __cxa_begin_catch
addl $16, %esp
movl %eax, -8(%ebp)
subl $8, %esp
pushl $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
subl $12, %esp
pushl $.LC0
pushl $_ZSt4cout
.LEHB2:
.LCFI10:
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
addl $20, %esp
pushl %eax
.LCFI11:
call _ZNSolsEPFRSoS_E
.LEHE2:
addl $16, %esp
jmp .L8
.L12:
movl %eax, -12(%ebp)
movl -12(%ebp), %ebx
.LCFI12:
call __cxa_end_catch
movl %ebx, -12(%ebp)
subl $12, %esp
pushl -12(%ebp)
.LEHB3:
.LCFI13:
call _Unwind_Resume
.LEHE3:
.L8:
.LCFI14:
call __cxa_end_catch
.L5:
movl $0, %eax
movl -4(%ebp), %ebx
leave
ret
.LFE1487:
.Lfe2:
.size main,.Lfe2-main
.section .gcc_except_table,"a",@progbits
.align 4
.LLSDA1487:
.byte 0xff
.byte 0x0
.uleb128 .LLSDATT1487-.LLSDATTD1487
.LLSDATTD1487:
.byte 0x1
.uleb128 .LLSDACSE1487-.LLSDACSB1487
.LLSDACSB1487:
.uleb128 .LEHB0-.LFB1487
.uleb128 .LEHE0-.LEHB0
.uleb128 .L13-.LFB1487
.uleb128 0x1
.uleb128 .LEHB1-.LFB1487
.uleb128 .LEHE1-.LEHB1
.uleb128 0x0
.uleb128 0x0
.uleb128 .LEHB2-.LFB1487
.uleb128 .LEHE2-.LEHB2
.uleb128 .L12-.LFB1487
.uleb128 0x0
.uleb128 .LEHB3-.LFB1487
.uleb128 .LEHE3-.LEHB3
.uleb128 0x0
.uleb128 0x0
.LLSDACSE1487:
.byte 0x1
.byte 0x0
.align 4
.long _ZTI1A
.LLSDATT1487:
.text
.align 2
.type _Z41__static_initialization_and_destruction_0ii,@function
_Z41__static_initialization_and_destruction_0ii:
.LFB1498:
pushl %ebp
.LCFI15:
movl %esp, %ebp
.LCFI16:
subl $8, %esp
.LCFI17:
cmpl $65535, 12(%ebp)
jne .L14
cmpl $1, 8(%ebp)
jne .L14
subl $12, %esp
pushl $_ZSt8__ioinit
.LCFI18:
call _ZNSt8ios_base4InitC1Ev
addl $16, %esp
subl $4, %esp
pushl $__dso_handle
pushl $0
pushl $__tcf_0
call __cxa_atexit
addl $16, %esp
.L14:
leave
ret
.LFE1498:
.Lfe3:
.size _Z41__static_initialization_and_destruction_0ii,.Lfe3-_Z41__static_initialization_and_destruction_0ii
.section .gnu.linkonce.t._ZN1AC1ERKS_,"ax",@progbits
.align 2
.weak _ZN1AC1ERKS_
.type _ZN1AC1ERKS_,@function
_ZN1AC1ERKS_:
.LFB1499:
pushl %ebp
.LCFI19:
movl %esp, %ebp
.LCFI20:
leave
ret
.LFE1499:
.Lfe4:
.size _ZN1AC1ERKS_,.Lfe4-_ZN1AC1ERKS_
.section .gnu.linkonce.t._ZN1AC1Ev,"ax",@progbits
.align 2
.weak _ZN1AC1Ev
.type _ZN1AC1Ev,@function
_ZN1AC1Ev:
.LFB1500:
pushl %ebp
.LCFI21:
movl %esp, %ebp
.LCFI22:
leave
ret
.LFE1500:
.Lfe5:
.size _ZN1AC1Ev,.Lfe5-_ZN1AC1Ev
.text
.align 2
.type __tcf_0,@function
__tcf_0:
.LFB1501:
pushl %ebp
.LCFI23:
movl %esp, %ebp
.LCFI24:
subl $8, %esp
.LCFI25:
subl $12, %esp
pushl $_ZSt8__ioinit
.LCFI26:
call _ZNSt8ios_base4InitD1Ev
addl $16, %esp
leave
ret
.LFE1501:
.Lfe6:
.size __tcf_0,.Lfe6-__tcf_0
.weak _ZTI1A
.section .gnu.linkonce.d._ZTI1A,"aw",@progbits
.align 4
.type _ZTI1A,@object
.size _ZTI1A,8
_ZTI1A:
.long _ZTVN10__cxxabiv117__class_type_infoE+8
.long _ZTS1A
.weak _ZTS1A
.section .gnu.linkonce.r._ZTS1A,"a",@progbits
.type _ZTS1A,@object
.size _ZTS1A,3
_ZTS1A:
.string "1A"
.text
.align 2
.type _GLOBAL__I__Z1fv,@function
_GLOBAL__I__Z1fv:
.LFB1503:
pushl %ebp
.LCFI27:
movl %esp, %ebp
.LCFI28:
subl $8, %esp
.LCFI29:
subl $8, %esp
pushl $65535
pushl $1
.LCFI30:
call _Z41__static_initialization_and_destruction_0ii
addl $16, %esp
leave
ret
.LFE1503:
.Lfe7:
.size _GLOBAL__I__Z1fv,.Lfe7-_GLOBAL__I__Z1fv
.section .ctors,"aw",@progbits
.align 4
.long _GLOBAL__I__Z1fv
.weak pthread_mutex_unlock
.weak pthread_mutex_trylock
.weak pthread_mutex_lock
.weak pthread_create
.weak pthread_setspecific
.weak pthread_getspecific
.weak pthread_key_delete
.weak pthread_key_create
.weak pthread_once
.section .eh_frame,"a",@progbits
.Lframe1:
.long .LECIE1-.LSCIE1
.LSCIE1:
.long 0x0
.byte 0x1
.string "zPL"
.uleb128 0x1
.sleb128 -4
.byte 0x8
.uleb128 0x6
.byte 0x0
.long __gxx_personality_v0
.byte 0x0
.byte 0xc
.uleb128 0x4
.uleb128 0x4
.byte 0x88
.uleb128 0x1
.align 4
.LECIE1:
.LSFDE1:
.long .LEFDE1-.LASFDE1
.LASFDE1:
.long .LASFDE1-.Lframe1
.long .LFB1485
.long .LFE1485-.LFB1485
.uleb128 0x4
.long 0x0
.byte 0x4
.long .LCFI0-.LFB1485
.byte 0xe
.uleb128 0x8
.byte 0x85
.uleb128 0x2
.byte 0x4
.long .LCFI1-.LCFI0
.byte 0xd
.uleb128 0x5
.byte 0x4
.long .LCFI3-.LCFI1
.byte 0x83
.uleb128 0x3
.byte 0x4
.long .LCFI4-.LCFI3
.byte 0x2e
.uleb128 0x10
.align 4
.LEFDE1:
.LSFDE3:
.long .LEFDE3-.LASFDE3
.LASFDE3:
.long .LASFDE3-.Lframe1
.long .LFB1487
.long .LFE1487-.LFB1487
.uleb128 0x4
.long .LLSDA1487
.byte 0x4
.long .LCFI5-.LFB1487
.byte 0xe
.uleb128 0x8
.byte 0x85
.uleb128 0x2
.byte 0x4
.long .LCFI6-.LCFI5
.byte 0xd
.uleb128 0x5
.byte 0x4
.long .LCFI8-.LCFI6
.byte 0x83
.uleb128 0x3
.byte 0x4
.long .LCFI9-.LCFI8
.byte 0x2e
.uleb128 0x10
.byte 0x4
.long .LCFI10-.LCFI9
.byte 0x2e
.uleb128 0x14
.byte 0x4
.long .LCFI11-.LCFI10
.byte 0x2e
.uleb128 0x10
.byte 0x4
.long .LCFI12-.LCFI11
.byte 0x2e
.uleb128 0x0
.byte 0x4
.long .LCFI13-.LCFI12
.byte 0x2e
.uleb128 0x10
.byte 0x4
.long .LCFI14-.LCFI13
.byte 0x2e
.uleb128 0x0
.align 4
.LEFDE3:
.LSFDE5:
.long .LEFDE5-.LASFDE5
.LASFDE5:
.long .LASFDE5-.Lframe1
.long .LFB1498
.long .LFE1498-.LFB1498
.uleb128 0x4
.long 0x0
.byte 0x4
.long .LCFI15-.LFB1498
.byte 0xe
.uleb128 0x8
.byte 0x85
.uleb128 0x2
.byte 0x4
.long .LCFI16-.LCFI15
.byte 0xd
.uleb128 0x5
.byte 0x4
.long .LCFI18-.LCFI16
.byte 0x2e
.uleb128 0x10
.align 4
.LEFDE5:
.LSFDE11:
.long .LEFDE11-.LASFDE11
.LASFDE11:
.long .LASFDE11-.Lframe1
.long .LFB1501
.long .LFE1501-.LFB1501
.uleb128 0x4
.long 0x0
.byte 0x4
.long .LCFI23-.LFB1501
.byte 0xe
.uleb128 0x8
.byte 0x85
.uleb128 0x2
.byte 0x4
.long .LCFI24-.LCFI23
.byte 0xd
.uleb128 0x5
.byte 0x4
.long .LCFI26-.LCFI24
.byte 0x2e
.uleb128 0x10
.align 4
.LEFDE11:
.LSFDE13:
.long .LEFDE13-.LASFDE13
.LASFDE13:
.long .LASFDE13-.Lframe1
.long .LFB1503
.long .LFE1503-.LFB1503
.uleb128 0x4
.long 0x0
.byte 0x4
.long .LCFI27-.LFB1503
.byte 0xe
.uleb128 0x8
.byte 0x85
.uleb128 0x2
.byte 0x4
.long .LCFI28-.LCFI27
.byte 0xd
.uleb128 0x5
.byte 0x4
.long .LCFI30-.LCFI28
.byte 0x2e
.uleb128 0x10
.align 4
.LEFDE13:
.ident "GCC: (GNU) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)"
我把在C/C++版上发的帖子转过来了~
似乎这里才算问对头的地方....
unix下没有类似Windows的SEH ,
那在 ...
xyfree 发表于 2010-01-15 05:29 http://linux.chinaunix.net/bbs/images/common/back.gif
yong setjmp/longjmp he signal lai mo ni SEH shi ke yi shi xian de, dan dou shi trick, bu zhi de fei zhe ge li qi, xia xia zhi ce. exception de xiang xi xin xi ke yi yong sigaction lai na dao.
页:
[1]