1cd139dc7SShuo Chen// excerpts from http://code.google.com/p/muduo/
2cd139dc7SShuo Chen//
3cd139dc7SShuo Chen// Use of this source code is governed by a BSD-style license
4cd139dc7SShuo Chen// that can be found in the License file.
5cd139dc7SShuo Chen//
6cd139dc7SShuo Chen// Author: Shuo Chen (giantchen at gmail dot com)
7cd139dc7SShuo Chen
8cd139dc7SShuo Chen#include "Exception.h"
9cd139dc7SShuo Chen
10cd139dc7SShuo Chen#include <cxxabi.h>
11cd139dc7SShuo Chen#include <execinfo.h>
12cd139dc7SShuo Chen#include <stdlib.h>
13cd139dc7SShuo Chen
14cd139dc7SShuo Chenusing namespace muduo;
15cd139dc7SShuo Chen
16cd139dc7SShuo ChenException::Exception(const char* what)
17cd139dc7SShuo Chen  : message_(what)
18cd139dc7SShuo Chen{
19cd139dc7SShuo Chen  const int len = 200;
20cd139dc7SShuo Chen  void* buffer[len];
21cd139dc7SShuo Chen  int nptrs = ::backtrace(buffer, len);
22cd139dc7SShuo Chen  char** strings = ::backtrace_symbols(buffer, nptrs);
23cd139dc7SShuo Chen  if (strings)
24cd139dc7SShuo Chen  {
25cd139dc7SShuo Chen    for (int i = 0; i < nptrs; ++i)
26cd139dc7SShuo Chen    {
27cd139dc7SShuo Chen      // TODO demangle funcion name with abi::__cxa_demangle
28cd139dc7SShuo Chen      stack_.append(strings[i]);
29cd139dc7SShuo Chen      stack_.push_back('\n');
30cd139dc7SShuo Chen    }
31cd139dc7SShuo Chen    free(strings);
32cd139dc7SShuo Chen  }
33cd139dc7SShuo Chen}
34cd139dc7SShuo Chen
35cd139dc7SShuo ChenException::~Exception() throw ()
36cd139dc7SShuo Chen{
37cd139dc7SShuo Chen}
38cd139dc7SShuo Chen
39cd139dc7SShuo Chenconst char* Exception::what() const throw()
40cd139dc7SShuo Chen{
41cd139dc7SShuo Chen  return message_.c_str();
42cd139dc7SShuo Chen}
43cd139dc7SShuo Chen
44cd139dc7SShuo Chenconst char* Exception::stackTrace() const throw()
45cd139dc7SShuo Chen{
46cd139dc7SShuo Chen  return stack_.c_str();
47cd139dc7SShuo Chen}
48cd139dc7SShuo Chen
49