| [229e3c] | 1 | /*
 | 
|---|
 | 2 |  * Assert.cpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: Mar 18, 2010
 | 
|---|
 | 5 |  *      Author: crueger
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | #include "Helpers/Assert.hpp"
 | 
|---|
 | 9 | #include <iostream>
 | 
|---|
 | 10 | 
 | 
|---|
 | 11 | using namespace std;
 | 
|---|
 | 12 | 
 | 
|---|
| [5be0eb] | 13 | namespace Assert{
 | 
|---|
 | 14 |   AssertionFailure::AssertionFailure(std::string _condition,
 | 
|---|
 | 15 |                                      std::string _file,
 | 
|---|
 | 16 |                                      int _line,
 | 
|---|
 | 17 |                                      std::string _message) :
 | 
|---|
 | 18 |     condition(_condition),
 | 
|---|
 | 19 |     file(_file),
 | 
|---|
 | 20 |     line(_line),
 | 
|---|
 | 21 |     message(_message)
 | 
|---|
 | 22 |   {}
 | 
|---|
 | 23 | 
 | 
|---|
 | 24 |   std::string AssertionFailure::getFile(){
 | 
|---|
 | 25 |     return file;
 | 
|---|
 | 26 |   }
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 |   int AssertionFailure::getLine(){
 | 
|---|
 | 29 |     return line;
 | 
|---|
 | 30 |   }
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 |   std::string AssertionFailure::getMessage(){
 | 
|---|
 | 33 |     return message;
 | 
|---|
 | 34 |   }
 | 
|---|
 | 35 | 
 | 
|---|
 | 36 |   std::ostream& AssertionFailure::operator<<(std::ostream& out){
 | 
|---|
 | 37 |     out << "Assertion \"" << condition << "\" failed in file " << file << " at line " << line << endl;
 | 
|---|
 | 38 |     out << "Assertion Message: " << message << std::endl;
 | 
|---|
 | 39 |     return out;
 | 
|---|
 | 40 |   }
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 |   const char  ActionKeys[]  = {'\0','a','t','i'};
 | 
|---|
 | 43 |   const char* ActionNames[] = {"Ask","Abort","Throw","Ignore"};
 | 
|---|
 | 44 | }
 | 
|---|
 | 45 | 
 | 
|---|
| [2c8934] | 46 | #ifndef NDEBUG
 | 
|---|
 | 47 | 
 | 
|---|
| [90aeb9] | 48 | #ifdef __GNUC__
 | 
|---|
 | 49 | #include <cstdlib>
 | 
|---|
 | 50 | #include <execinfo.h>
 | 
|---|
 | 51 | #include <cxxabi.h>
 | 
|---|
 | 52 | #endif
 | 
|---|
 | 53 | 
 | 
|---|
| [839e85] | 54 | Assert::Action Assert::_my_assert::defaultAction = Ask;
 | 
|---|
 | 55 | std::vector<Assert::hook_t> Assert::_my_assert::hooks;
 | 
|---|
| [229e3c] | 56 | 
 | 
|---|
| [839e85] | 57 | std::map<std::string,bool> Assert::_wrapper::ignores;
 | 
|---|
 | 58 | const char* Assert::_wrapper::message_ptr = "source pointer did not point to object of desired type";
 | 
|---|
 | 59 | const char* Assert::_wrapper::message_ref = "source reference did not contain object of desired type";
 | 
|---|
| [13d5a9] | 60 | 
 | 
|---|
| [506d2f] | 61 | bool Assert::_my_assert::check(const char* condition,
 | 
|---|
 | 62 |                                const char* message,
 | 
|---|
 | 63 |                                const char* filename,
 | 
|---|
 | 64 |                                const int line,
 | 
|---|
 | 65 |                                bool& ignore)
 | 
|---|
| [229e3c] | 66 | {
 | 
|---|
| [506d2f] | 67 |   cout << "Assertion \"" << condition << "\" failed in file " << filename << " at line " << line << endl;
 | 
|---|
 | 68 |   cout << "Assertion Message: " << message << std::endl;
 | 
|---|
 | 69 |   while(true){
 | 
|---|
 | 70 |     char choice;
 | 
|---|
 | 71 |     if(defaultAction==Assert::Ask) {
 | 
|---|
| [90aeb9] | 72 | #ifdef __GNUC__
 | 
|---|
| [506d2f] | 73 |       cout << "Please choose: (a)bort, (t)hrow execption, show (b)actrace, (i)gnore, al(w)ays ignore" << endl;
 | 
|---|
| [90aeb9] | 74 | #else
 | 
|---|
| [506d2f] | 75 |       cout << "Please choose: (a)bort, (t)hrow execption, (i)gnore, al(w)ays ignore" << endl;
 | 
|---|
| [90aeb9] | 76 | #endif /* __GNUC__ */
 | 
|---|
| [506d2f] | 77 |       cin >> choice;
 | 
|---|
 | 78 |     }
 | 
|---|
 | 79 |     else{
 | 
|---|
 | 80 |       choice = ActionKeys[defaultAction];
 | 
|---|
 | 81 |     }
 | 
|---|
 | 82 |     switch(choice){
 | 
|---|
 | 83 |       case 'a':
 | 
|---|
 | 84 |         return true;
 | 
|---|
 | 85 |         break;
 | 
|---|
 | 86 |       case 't':
 | 
|---|
 | 87 |         throw AssertionFailure(condition,filename,line,message);
 | 
|---|
 | 88 |         break;
 | 
|---|
| [90aeb9] | 89 | #ifdef __GNUC__
 | 
|---|
| [506d2f] | 90 |       case 'b':
 | 
|---|
 | 91 |         Assert::_my_assert::backtrace(filename,line);
 | 
|---|
 | 92 |        break;
 | 
|---|
| [90aeb9] | 93 | #endif /* __GNUC__ */
 | 
|---|
| [506d2f] | 94 |       case 'w':
 | 
|---|
 | 95 |         ignore = true;
 | 
|---|
 | 96 |         // fallthrough
 | 
|---|
 | 97 |       case 'i':
 | 
|---|
 | 98 |         return false;
 | 
|---|
 | 99 |         break;
 | 
|---|
| [229e3c] | 100 |     }
 | 
|---|
 | 101 |   }
 | 
|---|
 | 102 |   return false;
 | 
|---|
 | 103 | }
 | 
|---|
| [5be0eb] | 104 | 
 | 
|---|
| [90aeb9] | 105 | #ifdef __GNUC__
 | 
|---|
 | 106 | void Assert::_my_assert::backtrace(const char *file, int line){
 | 
|---|
 | 107 |   const size_t max_depth = 100;
 | 
|---|
 | 108 |   void* stack_addrs[max_depth];
 | 
|---|
 | 109 |   size_t stack_depth;
 | 
|---|
 | 110 |   char **stack_strings=0;
 | 
|---|
 | 111 |   const char *func_name=0;
 | 
|---|
 | 112 |   size_t sz = 64;
 | 
|---|
 | 113 | 
 | 
|---|
 | 114 |   // get the backtrace
 | 
|---|
 | 115 |   stack_depth   = ::backtrace(stack_addrs,max_depth);
 | 
|---|
 | 116 |   stack_strings = backtrace_symbols(stack_addrs, stack_depth);
 | 
|---|
 | 117 |   // used later for demangling
 | 
|---|
 | 118 |   // reserved here, so we can free it unconditionally
 | 
|---|
 | 119 |   char *dm_function = static_cast<char*>(malloc(sz));
 | 
|---|
 | 120 |   if(!dm_function){
 | 
|---|
 | 121 |     // malloc failed... we are out of luck
 | 
|---|
 | 122 |     cout << "cannot provide stack trace due to exhausted memory" << endl;
 | 
|---|
 | 123 |     return;
 | 
|---|
 | 124 |   }
 | 
|---|
 | 125 | 
 | 
|---|
 | 126 |   cout << "Backtrace from  " << file << "@" << line << ":" << endl;
 | 
|---|
 | 127 | 
 | 
|---|
 | 128 |   // i=2 because we don't want this function, nor the assertion handler
 | 
|---|
 | 129 |   for(unsigned int i=2;i<stack_depth-2;++i){
 | 
|---|
 | 130 |     // find the mangled function name
 | 
|---|
 | 131 |     char *begin = stack_strings[i];
 | 
|---|
 | 132 |     // function name starts with a (
 | 
|---|
 | 133 |     while(*begin && *begin!='(') ++begin;
 | 
|---|
 | 134 |     char *end=begin;
 | 
|---|
 | 135 |     while(*end && *end!='+') ++end;
 | 
|---|
 | 136 | 
 | 
|---|
 | 137 |     // see if we found our function name
 | 
|---|
 | 138 |     if(*begin && *end){
 | 
|---|
 | 139 |       *begin++ = 0;
 | 
|---|
 | 140 |       *end = 0;
 | 
|---|
 | 141 |       // use the C++ demangler
 | 
|---|
 | 142 | 
 | 
|---|
 | 143 |       int status;
 | 
|---|
 | 144 |       char *func_ret = abi::__cxa_demangle(begin, dm_function, &sz, &status);
 | 
|---|
 | 145 |       if(func_ret){
 | 
|---|
 | 146 |         // abi might have realloced...
 | 
|---|
 | 147 |         dm_function = func_ret;
 | 
|---|
 | 148 |         func_name = dm_function;
 | 
|---|
 | 149 |       }
 | 
|---|
 | 150 |       else{
 | 
|---|
 | 151 |         // demangling failed... get the function name without demangling
 | 
|---|
 | 152 |         func_name = begin;
 | 
|---|
 | 153 |       }
 | 
|---|
 | 154 |     }
 | 
|---|
 | 155 |     else{
 | 
|---|
 | 156 |       // function name not found... get the whole line
 | 
|---|
 | 157 |       func_name = stack_strings[i];
 | 
|---|
 | 158 |     }
 | 
|---|
 | 159 |     cout << func_name << endl;
 | 
|---|
 | 160 |   }
 | 
|---|
 | 161 |   free(dm_function);
 | 
|---|
 | 162 |   free(stack_strings); // malloc()ed by backtrace_symbols
 | 
|---|
 | 163 | }
 | 
|---|
 | 164 | #endif /* __GNUC__ */
 | 
|---|
 | 165 | 
 | 
|---|
| [839e85] | 166 | void Assert::_my_assert::doHooks(){
 | 
|---|
| [5be0eb] | 167 |   for(vector<hook_t>::reverse_iterator iter = hooks.rbegin(); iter!=hooks.rend(); ++iter ){
 | 
|---|
 | 168 |     (*iter)();
 | 
|---|
 | 169 |   }
 | 
|---|
 | 170 | }
 | 
|---|
 | 171 | 
 | 
|---|
| [839e85] | 172 | void Assert::_my_assert::addHook(hook_t hook){
 | 
|---|
| [5be0eb] | 173 |   hooks.push_back(hook);
 | 
|---|
 | 174 | }
 | 
|---|
 | 175 | 
 | 
|---|
| [839e85] | 176 | void Assert::_my_assert::removeHook(Assert::hook_t hook){
 | 
|---|
| [5be0eb] | 177 |   for(vector<hook_t>::iterator iter = hooks.begin(); iter!=hooks.end();){
 | 
|---|
 | 178 |     if((*iter)==hook){
 | 
|---|
 | 179 |       iter = hooks.erase(iter);
 | 
|---|
 | 180 |     }
 | 
|---|
 | 181 |     else{
 | 
|---|
 | 182 |       ++iter;
 | 
|---|
 | 183 |     }
 | 
|---|
 | 184 |   }
 | 
|---|
 | 185 | }
 | 
|---|
 | 186 | 
 | 
|---|
| [839e85] | 187 | void Assert::_my_assert::setDefault(Assert::Action action){
 | 
|---|
| [5be0eb] | 188 |   defaultAction = action;
 | 
|---|
 | 189 | }
 | 
|---|
| [839e85] | 190 | Assert::Action Assert::_my_assert::getDefault(){
 | 
|---|
| [5be0eb] | 191 |   return defaultAction;
 | 
|---|
 | 192 | }
 | 
|---|
| [839e85] | 193 | std::string Assert::_my_assert::printDefault(){
 | 
|---|
| [5be0eb] | 194 |   return ActionNames[defaultAction];
 | 
|---|
 | 195 | }
 | 
|---|
| [2c8934] | 196 | 
 | 
|---|
 | 197 | #endif
 | 
|---|
 | 198 | 
 | 
|---|