How to break when a specific exception type is thrown in GDB?

后端 未结 9 1198
迷失自我
迷失自我 2020-12-02 07:40

According to the documentation I can break on specific exception type by using conditional breakpoints. However the syntax for the condition isn\'t very clear to me:

9条回答
  •  情书的邮戳
    2020-12-02 08:05

    When gdb command 'catch throw' fails, try this workaround :
    (tested with Linux g++ 4.4.5/gdb 6.6)
    1/ Add this code anywhere in the program to debug :

    #include 
    #include 
    #include 
    
    struct __cxa_exception {
        std::type_info *inf;
    };
    struct __cxa_eh_globals {
        __cxa_exception *exc;
    };
    extern "C" __cxa_eh_globals* __cxa_get_globals();
    const char* what_exc() {
        __cxa_eh_globals* eh = __cxa_get_globals();
        if (eh && eh->exc && eh->exc->inf)
            return eh->exc->inf->name();
        return NULL;
    }
    

    2/ In gdb you will then be able to filter exceptions with :

    (gdb) break __cxa_begin_catch  
    (gdb) cond N (what_exc()?strstr(what_exc(),"exception_name"):0!=0)  
    

    where N is the breakpoint number, and exception_name is the name of exception for which we wish to break.

提交回复
热议问题