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 #include <config.h>
00022 #include <drizzled/util/backtrace.h>
00023 
00024 #include <string.h>
00025 #include <stdlib.h>
00026 #include <iostream>
00027 
00028 #ifdef __GNUC__
00029 #ifdef HAVE_BACKTRACE
00030 #include <execinfo.h>
00031 #include <cxxabi.h>
00032 #endif // HAVE_BACKTRACE
00033 #endif // __GNUC__
00034 
00035 namespace drizzled
00036 {
00037 namespace util
00038 {
00039 
00040 void custom_backtrace(const char *file, int line, const char *func)
00041 {
00042   std::cerr << std::endl << "call_backtrace() began at " << file << ":" << line << " for " << func << "()" << std::endl;
00043 #ifdef __GNUC__
00044 #ifdef HAVE_BACKTRACE
00045   void *array[50];
00046   size_t size;
00047   char **strings;
00048 
00049   size= backtrace(array, 50);
00050   strings= backtrace_symbols(array, size);
00051 
00052   std::cerr << "Number of stack frames obtained: " << size <<  std::endl;
00053 
00054   for (size_t x= 1; x < size; x++) 
00055   {
00056     size_t sz= 200;
00057     char *function= (char *)malloc(sz);
00058     char *begin= 0;
00059     char *end= 0;
00060 
00061     for (char *j = strings[x]; *j; ++j)
00062     {
00063       if (*j == '(') {
00064         begin = j;
00065       }
00066       else if (*j == '+') {
00067         end = j;
00068       }
00069     }
00070     if (begin && end)
00071     {
00072       begin++;
00073       *end= '\0';
00074 
00075       int status;
00076       char *ret = abi::__cxa_demangle(begin, function, &sz, &status);
00077       if (ret) 
00078       {
00079         function= ret;
00080       }
00081       else
00082       {
00083         strncpy(function, begin, sz);
00084         strncat(function, "()", sz);
00085         function[sz-1] = '\0';
00086       }
00087       std::cerr << function << std::endl;
00088     }
00089     else
00090     {
00091       std::cerr << strings[x] << std::endl;
00092     }
00093     free(function);
00094   }
00095 
00096 
00097   free (strings);
00098 #endif // HAVE_BACKTRACE
00099 #endif // __GNUC__
00100 }
00101 
00102 } /* namespace util */
00103 } /* namespace drizzled */