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