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