BALL  1.4.1
debug.h
Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 
00005 #ifndef BALL_COMMON_DEBUG_H
00006 #define BALL_COMMON_DEBUG_H
00007 
00008 #ifndef BALL_CONFIG_CONFIG_H
00009 # include <BALL/CONFIG/config.h>
00010 #endif
00011 
00012 #ifdef BALL_NO_INLINE_FUNCTIONS
00013 # define BALL_INLINE
00014 #else
00015 # define BALL_INLINE inline
00016 #endif
00017 
00018 #include <string>
00019 #include <cstring>
00020 
00021 #ifdef BALL_DEBUG
00022 
00023 # define BALL_PRECONDITION_EXCEPTION(condition, message)\
00024   if (!(condition))\
00025   {\
00026     Exception::Precondition e(__FILE__, __LINE__, #condition);\
00027     if (strcmp(message, "") != 0)\
00028     {\
00029       ::std::string tmp(e.getMessage());\
00030       tmp += ::std::string(message);\
00031       e.setMessage(tmp);\
00032     }\
00033     throw e;\
00034   }\
00035 
00036 # define BALL_POSTCONDITION_EXCEPTION(condition, message)\
00037   if (!(condition))\
00038   {\
00039     Exception::Postcondition e(__FILE__, __LINE__, #condition);\
00040     if (strcmp(message, "") != 0)\
00041     {\
00042       std::string tmp(e.getMessage());\
00043       tmp += std::string(message);\
00044       e.setMessage(tmp);\
00045     }\
00046     throw e;\
00047   }\
00048 
00049 # ifdef BALL_COMPILER_GXX
00050 # include <execinfo.h>
00051 # include <cxxabi.h>
00052 
00053 # define BALL_PRINT_BACKTRACE()\
00054   {\
00055     void *bt_addresses[100];\
00056     char **bt;\
00057     \
00058     int bt_size = backtrace(bt_addresses, 100);\
00059     bt = backtrace_symbols(bt_addresses, bt_size);\
00060     \
00061     char* demangled=0;\
00062     size_t num_demangled;\
00063     int status;\
00064     std::vector<String> split;\
00065     \
00066     for(int i = 0; i < bt_size; i++)\
00067     {\
00068       String mangled(bt[i]);\
00069       \
00070       Log.warn() << i << ": " << bt[i] << " ";\
00071       mangled = mangled.getField(1, "()");\
00072       mangled = mangled.getField(0, "+");\
00073       \
00074       char* out = abi::__cxa_demangle(mangled.c_str(), demangled, &num_demangled, &status);\
00075       if (status == 0) Log.warn() << "demangled: " << String(out);\
00076       if (num_demangled > 0) free(demangled);\
00077       demangled = 0;\
00078       Log.warn() << std::endl;\
00079     }\
00080     free(bt);\
00081   }\
00082 
00083 # else
00084 # define BALL_PRINT_BACKTRACE()
00085 # endif
00086 
00087 #else
00088 
00089 # define BALL_PRECONDITION_EXCEPTION(condition, message)
00090 # define BALL_POSTCONDITION_EXCEPTION(condition, message)
00091 # define BALL_PRINT_BACKTRACE()
00092 
00093 #endif // BALL_DEBUG
00094 
00095 #endif // BALL_COMMON_DEBUG_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines