Drizzled Public API Documentation

backtrace.cc
00001 /* - mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2010 Brian Aker
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 
00022 #include <config.h>
00023 #include <drizzled/util/backtrace.h>
00024 
00025 #include <string.h>
00026 #include <stdlib.h>
00027 #include <iostream>
00028 
00029 #ifdef __GNUC__
00030 #ifdef HAVE_BACKTRACE
00031 #include <execinfo.h>
00032 #include <cxxabi.h>
00033 #endif // HAVE_BACKTRACE
00034 #endif // __GNUC__
00035 
00036 
00037 namespace drizzled
00038 {
00039 namespace util
00040 {
00041 
00042 void custom_backtrace(void)
00043 {
00044 #ifdef __GNUC__
00045 #ifdef HAVE_BACKTRACE
00046   void *array[50];
00047   size_t size;
00048   char **strings;
00049 
00050   size= backtrace(array, 50);
00051   strings= backtrace_symbols(array, size);
00052 
00053   std::cerr << "Number of stack frames obtained: " << size <<  std::endl;
00054 
00055   for (size_t x= 1; x < size; x++) 
00056   {
00057     size_t sz= 200;
00058     char *function= (char *)malloc(sz);
00059     char *begin= 0;
00060     char *end= 0;
00061 
00062     for (char *j = strings[x]; *j; ++j)
00063     {
00064       if (*j == '(') {
00065         begin = j;
00066       }
00067       else if (*j == '+') {
00068         end = j;
00069       }
00070     }
00071     if (begin && end)
00072     {
00073       begin++;
00074       *end= '\0';
00075 
00076       int status;
00077       char *ret = abi::__cxa_demangle(begin, function, &sz, &status);
00078       if (ret) 
00079       {
00080         function= ret;
00081       }
00082       else
00083       {
00084         strncpy(function, begin, sz);
00085         strncat(function, "()", sz);
00086         function[sz-1] = '\0';
00087       }
00088       std::cerr << function << std::endl;
00089     }
00090     else
00091     {
00092       std::cerr << strings[x] << std::endl;
00093     }
00094     free(function);
00095   }
00096 
00097 
00098   free (strings);
00099 #endif // HAVE_BACKTRACE
00100 #endif // __GNUC__
00101 }
00102 
00103 } /* namespace util */
00104 } /* namespace drizzled */