免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4638 | 回复: 9
打印 上一主题 下一主题

删帖吧 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-15 21:29 |只看该作者 |倒序浏览
本帖最后由 xyfree 于 2012-01-21 03:35 编辑

论坛徽章:
0
2 [报告]
发表于 2010-01-15 22:58 |只看该作者

回复 #1 xyfree 的帖子

难道c++ on unix的异常机制不是用signal实现的?

论坛徽章:
0
3 [报告]
发表于 2010-01-16 01:22 |只看该作者
本帖最后由 xyfree 于 2012-01-21 03:36 编辑

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
4 [报告]
发表于 2010-01-16 09:10 |只看该作者
UNIX的宗旨就是,不要动不动就想到汇编这种垃圾,甚至不应该去"想"汇编(因为在UNIX的世界里,它就是垃圾)
SIGFPE信号是算术异常导致,由内核发送

论坛徽章:
0
5 [报告]
发表于 2010-01-16 17:12 |只看该作者

回复 #4 cjaizss 的帖子

原来是这样~ 学习了~

其实是因为小弟在考虑编译器如何为try块生成linux、unix下的代码
所以有用汇编的一说~
不过现在看来,倒是应该直接利用C库

论坛徽章:
0
6 [报告]
发表于 2010-01-22 13:43 |只看该作者

回复 #5 xyfree 的帖子

在《c接口和实现》那本书里就实现了一个c语言版本的TRY...EXCEPT,主要就是setjump, longjump

论坛徽章:
0
7 [报告]
发表于 2010-03-30 15:47 |只看该作者
我猜测是这样的,linux下除0会产生硬件中断,linux操作系统硬件中断处理程序会去收集CPU相关信息(哪个进程发生了什么异常),然后根据相应的异常信息给发生异常的进程发生相应信号。等CPU调度到发生异常的进程的时候会调用信号处理程序实现异常处理,当然像Page Fault等异常内核自己处理,不会给进程发送信号。
GCC编译器C++异常处理机制应该是基于以上机制的封装。

论坛徽章:
0
8 [报告]
发表于 2010-03-30 17:22 |只看该作者
>>当然像Page Fault等异常内核自己处理,不会给进程发送信号。

这个应该也处理的。

例如int *p=NULL; *p=1;这个异常可以捕获。

论坛徽章:
0
9 [报告]
发表于 2010-03-30 17:26 |只看该作者
linux下的cpp和try,catch还真不会写,网上搜了一个

  1. #include <iostream>

  2. using namespace std ;

  3. class A {
  4. public:
  5.   A() {
  6.   }
  7. } ;

  8. void
  9. f( void )
  10. {
  11.   throw A( ) ;
  12. }

  13. int
  14. main( int argc, char *argv[] )
  15. {
  16.   try {
  17.     f( ) ;
  18.   }
  19.   catch( A &e ) {
  20.     cout << "Exception occured!" << endl ;
  21.   }

  22.   return 0 ;
  23. }
复制代码
[root@mail ~]# gcc -lstdc++ 1.cpp
[root@mail ~]# ./a.out
Exception occured!

看了一下它的反汇编代码,看不懂

  1.         .file        "1.cpp"
  2.         .local        _ZSt8__ioinit
  3.         .comm        _ZSt8__ioinit,1,1
  4.         .text
  5.         .align 2
  6. .globl _Z1fv
  7.         .type        _Z1fv,@function
  8. _Z1fv:
  9. .LFB1485:
  10.         pushl        %ebp
  11. .LCFI0:
  12.         movl        %esp, %ebp
  13. .LCFI1:
  14.         pushl        %ebx
  15. .LCFI2:
  16.         subl        $4, %esp
  17. .LCFI3:
  18.         subl        $12, %esp
  19.         leal        -5(%ebp), %eax
  20.         pushl        %eax
  21. .LCFI4:
  22.         call        _ZN1AC1Ev
  23.         addl        $16, %esp
  24.         subl        $12, %esp
  25.         pushl        $1
  26.         call        __cxa_allocate_exception
  27.         addl        $16, %esp
  28.         movl        %eax, %ebx
  29.         subl        $8, %esp
  30.         leal        -5(%ebp), %eax
  31.         pushl        %eax
  32.         pushl        %ebx
  33.         call        _ZN1AC1ERKS_
  34.         addl        $16, %esp
  35.         subl        $4, %esp
  36.         pushl        $0
  37.         pushl        $_ZTI1A
  38.         pushl        %ebx
  39.         call        __cxa_throw
  40. .LFE1485:
  41. .Lfe1:
  42.         .size        _Z1fv,.Lfe1-_Z1fv
  43. .globl _Unwind_Resume
  44.         .section        .rodata
  45. .LC0:
  46.         .string        "Exception occured!"
  47.         .text
  48.         .align 2
  49. .globl main
  50.         .type        main,@function
  51. main:
  52. .LFB1487:
  53.         pushl        %ebp
  54. .LCFI5:
  55.         movl        %esp, %ebp
  56. .LCFI6:
  57.         pushl        %ebx
  58. .LCFI7:
  59.         subl        $20, %esp
  60. .LCFI8:
  61.         andl        $-16, %esp
  62.         movl        $0, %eax
  63.         subl        %eax, %esp
  64. .LEHB0:
  65.         call        _Z1fv
  66. .LEHE0:
  67.         jmp        .L5
  68. .L13:
  69.         movl        %eax, -12(%ebp)
  70.         cmpl        $1, %edx
  71.         je        .L6
  72.         subl        $12, %esp
  73.         pushl        -12(%ebp)
  74. .LEHB1:
  75. .LCFI9:
  76.         call        _Unwind_Resume
  77. .LEHE1:
  78. .L6:
  79.         subl        $12, %esp
  80.         pushl        -12(%ebp)
  81.         call        __cxa_begin_catch
  82.         addl        $16, %esp
  83.         movl        %eax, -8(%ebp)
  84.         subl        $8, %esp
  85.         pushl        $_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
  86.         subl        $12, %esp
  87.         pushl        $.LC0
  88.         pushl        $_ZSt4cout
  89. .LEHB2:
  90. .LCFI10:
  91.         call        _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
  92.         addl        $20, %esp
  93.         pushl        %eax
  94. .LCFI11:
  95.         call        _ZNSolsEPFRSoS_E
  96. .LEHE2:
  97.         addl        $16, %esp
  98.         jmp        .L8
  99. .L12:
  100.         movl        %eax, -12(%ebp)
  101.         movl        -12(%ebp), %ebx
  102. .LCFI12:
  103.         call        __cxa_end_catch
  104.         movl        %ebx, -12(%ebp)
  105.         subl        $12, %esp
  106.         pushl        -12(%ebp)
  107. .LEHB3:
  108. .LCFI13:
  109.         call        _Unwind_Resume
  110. .LEHE3:
  111. .L8:
  112. .LCFI14:
  113.         call        __cxa_end_catch
  114. .L5:
  115.         movl        $0, %eax
  116.         movl        -4(%ebp), %ebx
  117.         leave
  118.         ret
  119. .LFE1487:
  120. .Lfe2:
  121.         .size        main,.Lfe2-main
  122.         .section        .gcc_except_table,"a",@progbits
  123.         .align 4
  124. .LLSDA1487:
  125.         .byte        0xff
  126.         .byte        0x0
  127.         .uleb128 .LLSDATT1487-.LLSDATTD1487
  128. .LLSDATTD1487:
  129.         .byte        0x1
  130.         .uleb128 .LLSDACSE1487-.LLSDACSB1487
  131. .LLSDACSB1487:
  132.         .uleb128 .LEHB0-.LFB1487
  133.         .uleb128 .LEHE0-.LEHB0
  134.         .uleb128 .L13-.LFB1487
  135.         .uleb128 0x1
  136.         .uleb128 .LEHB1-.LFB1487
  137.         .uleb128 .LEHE1-.LEHB1
  138.         .uleb128 0x0
  139.         .uleb128 0x0
  140.         .uleb128 .LEHB2-.LFB1487
  141.         .uleb128 .LEHE2-.LEHB2
  142.         .uleb128 .L12-.LFB1487
  143.         .uleb128 0x0
  144.         .uleb128 .LEHB3-.LFB1487
  145.         .uleb128 .LEHE3-.LEHB3
  146.         .uleb128 0x0
  147.         .uleb128 0x0
  148. .LLSDACSE1487:
  149.         .byte        0x1
  150.         .byte        0x0
  151.         .align 4
  152.         .long        _ZTI1A
  153. .LLSDATT1487:
  154.         .text
  155.         .align 2
  156.         .type        _Z41__static_initialization_and_destruction_0ii,@function
  157. _Z41__static_initialization_and_destruction_0ii:
  158. .LFB1498:
  159.         pushl        %ebp
  160. .LCFI15:
  161.         movl        %esp, %ebp
  162. .LCFI16:
  163.         subl        $8, %esp
  164. .LCFI17:
  165.         cmpl        $65535, 12(%ebp)
  166.         jne        .L14
  167.         cmpl        $1, 8(%ebp)
  168.         jne        .L14
  169.         subl        $12, %esp
  170.         pushl        $_ZSt8__ioinit
  171. .LCFI18:
  172.         call        _ZNSt8ios_base4InitC1Ev
  173.         addl        $16, %esp
  174.         subl        $4, %esp
  175.         pushl        $__dso_handle
  176.         pushl        $0
  177.         pushl        $__tcf_0
  178.         call        __cxa_atexit
  179.         addl        $16, %esp
  180. .L14:
  181.         leave
  182.         ret
  183. .LFE1498:
  184. .Lfe3:
  185.         .size        _Z41__static_initialization_and_destruction_0ii,.Lfe3-_Z41__static_initialization_and_destruction_0ii
  186.         .section        .gnu.linkonce.t._ZN1AC1ERKS_,"ax",@progbits
  187.         .align 2
  188.         .weak        _ZN1AC1ERKS_
  189.         .type        _ZN1AC1ERKS_,@function
  190. _ZN1AC1ERKS_:
  191. .LFB1499:
  192.         pushl        %ebp
  193. .LCFI19:
  194.         movl        %esp, %ebp
  195. .LCFI20:
  196.         leave
  197.         ret
  198. .LFE1499:
  199. .Lfe4:
  200.         .size        _ZN1AC1ERKS_,.Lfe4-_ZN1AC1ERKS_
  201.         .section        .gnu.linkonce.t._ZN1AC1Ev,"ax",@progbits
  202.         .align 2
  203.         .weak        _ZN1AC1Ev
  204.         .type        _ZN1AC1Ev,@function
  205. _ZN1AC1Ev:
  206. .LFB1500:
  207.         pushl        %ebp
  208. .LCFI21:
  209.         movl        %esp, %ebp
  210. .LCFI22:
  211.         leave
  212.         ret
  213. .LFE1500:
  214. .Lfe5:
  215.         .size        _ZN1AC1Ev,.Lfe5-_ZN1AC1Ev
  216.         .text
  217.         .align 2
  218.         .type        __tcf_0,@function
  219. __tcf_0:
  220. .LFB1501:
  221.         pushl        %ebp
  222. .LCFI23:
  223.         movl        %esp, %ebp
  224. .LCFI24:
  225.         subl        $8, %esp
  226. .LCFI25:
  227.         subl        $12, %esp
  228.         pushl        $_ZSt8__ioinit
  229. .LCFI26:
  230.         call        _ZNSt8ios_base4InitD1Ev
  231.         addl        $16, %esp
  232.         leave
  233.         ret
  234. .LFE1501:
  235. .Lfe6:
  236.         .size        __tcf_0,.Lfe6-__tcf_0
  237.         .weak        _ZTI1A
  238.         .section        .gnu.linkonce.d._ZTI1A,"aw",@progbits
  239.         .align 4
  240.         .type        _ZTI1A,@object
  241.         .size        _ZTI1A,8
  242. _ZTI1A:
  243.         .long        _ZTVN10__cxxabiv117__class_type_infoE+8
  244.         .long        _ZTS1A
  245.         .weak        _ZTS1A
  246.         .section        .gnu.linkonce.r._ZTS1A,"a",@progbits
  247.         .type        _ZTS1A,@object
  248.         .size        _ZTS1A,3
  249. _ZTS1A:
  250.         .string        "1A"
  251.         .text
  252.         .align 2
  253.         .type        _GLOBAL__I__Z1fv,@function
  254. _GLOBAL__I__Z1fv:
  255. .LFB1503:
  256.         pushl        %ebp
  257. .LCFI27:
  258.         movl        %esp, %ebp
  259. .LCFI28:
  260.         subl        $8, %esp
  261. .LCFI29:
  262.         subl        $8, %esp
  263.         pushl        $65535
  264.         pushl        $1
  265. .LCFI30:
  266.         call        _Z41__static_initialization_and_destruction_0ii
  267.         addl        $16, %esp
  268.         leave
  269.         ret
  270. .LFE1503:
  271. .Lfe7:
  272.         .size        _GLOBAL__I__Z1fv,.Lfe7-_GLOBAL__I__Z1fv
  273.         .section        .ctors,"aw",@progbits
  274.         .align 4
  275.         .long        _GLOBAL__I__Z1fv
  276.         .weak        pthread_mutex_unlock
  277.         .weak        pthread_mutex_trylock
  278.         .weak        pthread_mutex_lock
  279.         .weak        pthread_create
  280.         .weak        pthread_setspecific
  281.         .weak        pthread_getspecific
  282.         .weak        pthread_key_delete
  283.         .weak        pthread_key_create
  284.         .weak        pthread_once
  285.         .section        .eh_frame,"a",@progbits
  286. .Lframe1:
  287.         .long        .LECIE1-.LSCIE1
  288. .LSCIE1:
  289.         .long        0x0
  290.         .byte        0x1
  291.         .string        "zPL"
  292.         .uleb128 0x1
  293.         .sleb128 -4
  294.         .byte        0x8
  295.         .uleb128 0x6
  296.         .byte        0x0
  297.         .long        __gxx_personality_v0
  298.         .byte        0x0
  299.         .byte        0xc
  300.         .uleb128 0x4
  301.         .uleb128 0x4
  302.         .byte        0x88
  303.         .uleb128 0x1
  304.         .align 4
  305. .LECIE1:
  306. .LSFDE1:
  307.         .long        .LEFDE1-.LASFDE1
  308. .LASFDE1:
  309.         .long        .LASFDE1-.Lframe1
  310.         .long        .LFB1485
  311.         .long        .LFE1485-.LFB1485
  312.         .uleb128 0x4
  313.         .long        0x0
  314.         .byte        0x4
  315.         .long        .LCFI0-.LFB1485
  316.         .byte        0xe
  317.         .uleb128 0x8
  318.         .byte        0x85
  319.         .uleb128 0x2
  320.         .byte        0x4
  321.         .long        .LCFI1-.LCFI0
  322.         .byte        0xd
  323.         .uleb128 0x5
  324.         .byte        0x4
  325.         .long        .LCFI3-.LCFI1
  326.         .byte        0x83
  327.         .uleb128 0x3
  328.         .byte        0x4
  329.         .long        .LCFI4-.LCFI3
  330.         .byte        0x2e
  331.         .uleb128 0x10
  332.         .align 4
  333. .LEFDE1:
  334. .LSFDE3:
  335.         .long        .LEFDE3-.LASFDE3
  336. .LASFDE3:
  337.         .long        .LASFDE3-.Lframe1
  338.         .long        .LFB1487
  339.         .long        .LFE1487-.LFB1487
  340.         .uleb128 0x4
  341.         .long        .LLSDA1487
  342.         .byte        0x4
  343.         .long        .LCFI5-.LFB1487
  344.         .byte        0xe
  345.         .uleb128 0x8
  346.         .byte        0x85
  347.         .uleb128 0x2
  348.         .byte        0x4
  349.         .long        .LCFI6-.LCFI5
  350.         .byte        0xd
  351.         .uleb128 0x5
  352.         .byte        0x4
  353.         .long        .LCFI8-.LCFI6
  354.         .byte        0x83
  355.         .uleb128 0x3
  356.         .byte        0x4
  357.         .long        .LCFI9-.LCFI8
  358.         .byte        0x2e
  359.         .uleb128 0x10
  360.         .byte        0x4
  361.         .long        .LCFI10-.LCFI9
  362.         .byte        0x2e
  363.         .uleb128 0x14
  364.         .byte        0x4
  365.         .long        .LCFI11-.LCFI10
  366.         .byte        0x2e
  367.         .uleb128 0x10
  368.         .byte        0x4
  369.         .long        .LCFI12-.LCFI11
  370.         .byte        0x2e
  371.         .uleb128 0x0
  372.         .byte        0x4
  373.         .long        .LCFI13-.LCFI12
  374.         .byte        0x2e
  375.         .uleb128 0x10
  376.         .byte        0x4
  377.         .long        .LCFI14-.LCFI13
  378.         .byte        0x2e
  379.         .uleb128 0x0
  380.         .align 4
  381. .LEFDE3:
  382. .LSFDE5:
  383.         .long        .LEFDE5-.LASFDE5
  384. .LASFDE5:
  385.         .long        .LASFDE5-.Lframe1
  386.         .long        .LFB1498
  387.         .long        .LFE1498-.LFB1498
  388.         .uleb128 0x4
  389.         .long        0x0
  390.         .byte        0x4
  391.         .long        .LCFI15-.LFB1498
  392.         .byte        0xe
  393.         .uleb128 0x8
  394.         .byte        0x85
  395.         .uleb128 0x2
  396.         .byte        0x4
  397.         .long        .LCFI16-.LCFI15
  398.         .byte        0xd
  399.         .uleb128 0x5
  400.         .byte        0x4
  401.         .long        .LCFI18-.LCFI16
  402.         .byte        0x2e
  403.         .uleb128 0x10
  404.         .align 4
  405. .LEFDE5:
  406. .LSFDE11:
  407.         .long        .LEFDE11-.LASFDE11
  408. .LASFDE11:
  409.         .long        .LASFDE11-.Lframe1
  410.         .long        .LFB1501
  411.         .long        .LFE1501-.LFB1501
  412.         .uleb128 0x4
  413.         .long        0x0
  414.         .byte        0x4
  415.         .long        .LCFI23-.LFB1501
  416.         .byte        0xe
  417.         .uleb128 0x8
  418.         .byte        0x85
  419.         .uleb128 0x2
  420.         .byte        0x4
  421.         .long        .LCFI24-.LCFI23
  422.         .byte        0xd
  423.         .uleb128 0x5
  424.         .byte        0x4
  425.         .long        .LCFI26-.LCFI24
  426.         .byte        0x2e
  427.         .uleb128 0x10
  428.         .align 4
  429. .LEFDE11:
  430. .LSFDE13:
  431.         .long        .LEFDE13-.LASFDE13
  432. .LASFDE13:
  433.         .long        .LASFDE13-.Lframe1
  434.         .long        .LFB1503
  435.         .long        .LFE1503-.LFB1503
  436.         .uleb128 0x4
  437.         .long        0x0
  438.         .byte        0x4
  439.         .long        .LCFI27-.LFB1503
  440.         .byte        0xe
  441.         .uleb128 0x8
  442.         .byte        0x85
  443.         .uleb128 0x2
  444.         .byte        0x4
  445.         .long        .LCFI28-.LCFI27
  446.         .byte        0xd
  447.         .uleb128 0x5
  448.         .byte        0x4
  449.         .long        .LCFI30-.LCFI28
  450.         .byte        0x2e
  451.         .uleb128 0x10
  452.         .align 4
  453. .LEFDE13:
  454.         .ident        "GCC: (GNU) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)"
复制代码

论坛徽章:
0
10 [报告]
发表于 2010-03-31 08:58 |只看该作者
我把在C/C++版上发的帖子转过来了~
似乎这里才算问对头的地方....

unix下没有类似Windows的SEH ,
那在 ...
xyfree 发表于 2010-01-15 05:29



    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.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP