使用backstrace的c ++异常不打印调用函数详细信息

时间:2018-06-13 07:07:45

标签: c++ exception callstack

我有一些异常代码,如下面的

class BaseException : public std::exception {
    public:
        BaseException(string  str) : error_string(str){
            printStackTrace();
        }
        BaseException() = default;
        virtual ~BaseException() throw() = default;
        virtual const char *what() const throw(){return error_string.c_str();}
        void printStackTrace() {
            void *array[10];
            size_t size;
            size = backtrace(array, 10);
            backtrace_symbols_fd(array, size, STDERR_FILENO);
        }
        string error_string;
};    
void baz() {
    throw BaseException("tetsingException");
} 

void bar() { baz(); }
void foo() { bar(); }  

int main() {

    try{
        foo();
    }
    catch(BaseException &e){
        cout << e.what();
    }
    return 0;
}

每当我运行它时,我得到低于输出

/home/user/sample/Debug/Exceptionwithstrace[0x4010c3]
/home/user/sample/Debug/Exceptionwithstrace[0x40104f]
/home/user/sample/Debug/Exceptionwithstrace[0x400eb5]
/home/user/sample/Debug/Exceptionwithstrace[0x400f1b]
/home/user/sample/Debug/Exceptionwithstrace[0x400f26]
/home/user/sample/Debug/Exceptionwithstrace[0x400f36]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0x7fda68538f45]
/home/user/sample/Debug/Exceptionwithstrace[0x400da9]
tetsingException

函数名称没有打印,我也需要打印函数名称,如下所示,如何得到类似下面的内容

/home/user/sample/Debug/Exceptionwithstrace[BaseException]
/home/user/sample/Debug/Exceptionwithstrace[baz]
/home/user/sample/Debug/Exceptionwithstrace[bar]
/home/user/sample/Debug/Exceptionwithstrace[foo]
/home/user/sample/Debug/Exceptionwithstrace[main]

0 个答案:

没有答案