类结构图
message 保存异常信息的字符串
stack_ 异常发生的时候,调用栈的回溯信息
fillStackTrace() 私有函数,在构造函数中调用,用来记录异常发生时,栈回溯的信息。
backtrace ,栈回溯,保存各个栈帧的地址。数组指针,数组元素是函数地址。
backtrace_sympols 函数地址,转换成函数字符串。指针数组。指针数组需要调用者来释放。
示例:
#include <muduo/base/Exception.h>
#include <stdio.h>
class Bar
{
public:
void test()
{
throw muduo::Exception("oops");
}
};
void foo()
{
Bar b;
b.test();
}
int main()
{
try
{
foo();
}
catch (const muduo::Exception& ex)
{
printf("reason: %s\n", ex.what());
printf("stack trace: %s\n", ex.stackTrace());
}
}
name mangling C++ 会对函数名做名字改变.
C++ 对函数 name mangling 后的还原。
string Exception::demangle(const char* symbol)
{
size_t size;
int status;
char temp[128];
char* demangled;
//first, try to demangle a c++ name
if (1 == sscanf(symbol, "%*[^(]%*[^_]%127[^)+]", temp)) {
if (NULL != (demangled = abi::__cxa_demangle(temp, NULL, &size, &status))) {
string result(demangled);
free(demangled);
return result;
}
}
//if that didn't work, try to get a regular c symbol
if (1 == sscanf(symbol, "%127s", temp)) {
return temp;
}
//if all else fails, just return the symbol
return symbol;
}