34 #if defined(__GLIBC__) || defined(__APPLE__)
49 static bool output_demangled_name(
51 const std::string &stack_entry)
53 bool return_value=
false;
55 std::string working(stack_entry);
60 if(start!=std::string::npos &&
61 end!=std::string::npos &&
65 std::string mangled(working.substr(start+1, length));
67 int demangle_success=1;
68 std::unique_ptr<char, freert> demangled(
70 mangled.c_str(),
nullptr,
nullptr, &demangle_success));
72 if(demangle_success==0)
74 out << working.substr(0, start+1)
76 << working.substr(end);
91 #if defined(__GLIBC__) || defined(__APPLE__)
92 void * stack[50] = {};
94 std::size_t entries=backtrace(stack,
sizeof(stack) /
sizeof(
void *));
95 std::unique_ptr<char*, freert> description(
96 backtrace_symbols(stack, entries));
98 for(std::size_t i=0; i<entries; i++)
100 if(!output_demangled_name(out, description.get()[i]))
101 out << description.get()[i];
102 out <<
'\n' << std::flush;
105 #elif defined(_WIN32)
108 HANDLE process = GetCurrentProcess();
110 SymInitialize(process, NULL,
TRUE);
112 auto number_of_frames =
113 CaptureStackBackTrace(0,
sizeof(stack) /
sizeof(
void *), stack, NULL);
118 const auto max_name_len = 255;
119 auto symbol =
static_cast<SYMBOL_INFO *
>(
120 calloc(
sizeof SYMBOL_INFO + (max_name_len - 1) *
sizeof(TCHAR), 1));
121 symbol->MaxNameLen = max_name_len;
122 symbol->SizeOfStruct =
sizeof SYMBOL_INFO;
124 for(std::size_t i = 0; i < number_of_frames; i++)
126 SymFromAddr(process, (DWORD64)(stack[i]), 0, symbol);
127 out << std::setw(3) << i;
128 out <<
" 0x" << std::hex << std::setw(8) << symbol->Address;
129 out <<
' ' << symbol->Name;
130 out <<
'\n' << std::flush;
135 out <<
"Backtraces not supported\n" << std::flush;
143 std::ostringstream ostr;
151 std::cerr <<
"--- begin invariant violation report ---\n";
152 std::cerr << reason.
what() <<
'\n';
153 std::cerr <<
"--- end invariant violation report ---\n";
158 std::ostringstream out;
159 out <<
"Invariant check failed\n"
160 <<
"File: " <<
file <<
":" <<
line <<
" function: " <<
function <<
'\n'
162 <<
"Reason: " <<
reason <<
'\n'
163 <<
"Backtrace:" <<
'\n'
172 if(!s.empty() && s.back() !=
'\n')
A logic error, augmented with a distinguished field to hold a backtrace.
const std::string backtrace
const std::string condition
virtual std::string what() const noexcept
virtual std::string what() const noexcept
const std::string diagnostics
std::string get_backtrace()
Returns a backtrace.
void report_exception_to_stderr(const invariant_failedt &reason)
Dump exception report to stderr.
void print_backtrace(std::ostream &out)
Prints a back trace to 'out'.
bool cbmc_invariants_should_throw
Set this to true to cause invariants to throw a C++ exception rather than abort the program.
void * calloc(__CPROVER_size_t nmemb, __CPROVER_size_t size)