12muduo_base库源码分析(三)
来源:互联网 发布:数据分析工程师 外企 编辑:程序博客网 时间:2024/06/02 00:19
1.Exception类实现
(1)backtrace,栈回溯,保存各个栈帧的地址
(2)backtrace_symbols,根据地址,转成相应的函数符号
(3)abi::__cxa_demangle
2.代码
Exception.h
// Use of this source code is governed by a BSD-style license// that can be found in the License file.//// Author: Shuo Chen (chenshuo at chenshuo dot com)#ifndef MUDUO_BASE_EXCEPTION_H#define MUDUO_BASE_EXCEPTION_H#include <muduo/base/Types.h>#include <exception>namespace muduo{class Exception : public std::exception{ public: explicit Exception(const char* what); explicit Exception(const string& what); virtual ~Exception() throw(); virtual const char* what() const throw(); const char* stackTrace() const throw(); private: void fillStackTrace(); string demangle(const char* symbol); string message_; string stack_;};}#endif // MUDUO_BASE_EXCEPTION_H
Exception.cc
// Use of this source code is governed by a BSD-style license// that can be found in the License file.//// Author: Shuo Chen (chenshuo at chenshuo dot com)#include <muduo/base/Exception.h>#include <cxxabi.h>#include <execinfo.h>#include <stdlib.h>#include <stdio.h>using namespace muduo;Exception::Exception(const char* msg) : message_(msg){ fillStackTrace();}Exception::Exception(const string& msg) : message_(msg){ fillStackTrace();}Exception::~Exception() throw (){}const char* Exception::what() const throw(){ return message_.c_str();}const char* Exception::stackTrace() const throw(){ return stack_.c_str();}void Exception::fillStackTrace(){ const int len = 200; void* buffer[len]; int nptrs = ::backtrace(buffer, len); char** strings = ::backtrace_symbols(buffer, nptrs); if (strings) { for (int i = 0; i < nptrs; ++i) { // TODO demangle funcion name with abi::__cxa_demangle //stack_.append(strings[i]); stack_.append(demangle(strings[i])); stack_.push_back('\n'); } free(strings); }}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;}
Exception_test.cpp
#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()); }}
执行结果
阅读全文
1 0
- 12muduo_base库源码分析(三)
- 11muduo_base库源码分析(二)
- 13muduo_base库源码分析(四)
- 15muduo_base库源码分析(六)
- 16muduo_base库源码分析(七)
- 17muduo_base库源码分析(八)
- 18muduo_base库源码分析(九)
- 19muduo_base库源码分析(十)
- 20muduo_base库源码分析(十一)
- muduo_base 源码分析:Timestamp
- muduo_base 源码分析:AtomicIntegerT
- 高并发服务器架构笔记(3)——muduo_base 源码分析
- 高并发服务器架构笔记(3)——muduo_base 源码分析
- 27muduo_net库源码分析(三)
- JUnit源码分析(三)
- Log4net源码分析(三)
- Log4net源码分析(三)
- Logcat源码分析(三)
- hdu-3729 二分图匹配匈牙利算法
- Linux进程间通信之POSIX消息队列
- 走迷宫
- 标准IO
- 没有文件扩展”js”的脚本引擎解决方法
- 12muduo_base库源码分析(三)
- 找零钱问题 (ChangeNote.cpp)
- Leetcode 368. Largest Divisible Subset
- BZOJ1798 洛谷P2023 [AHOI2009]维护序列
- effective javascript第一章
- 网络流模板
- maven依赖配置注意,特别重要
- jquery的ajax同步和异步
- Hdu 5241 Friends【规律+高精度乘法】