carl  24.04
Computer ARithmetic Library
debug.h
Go to the documentation of this file.
1 /**
2  * @file debug.h
3  * @author Gereon Kremer <gereon.kremer@cs.rwth-aachen.de>
4  * This file contains convenience routines for debugging, e.g. to print more complex data structures like STL container or print stack traces.
5  */
6 
7 #pragma once
8 
9 #include <csignal>
10 #include <forward_list>
11 #include <iostream>
12 #include <list>
13 #include <map>
14 #include <set>
15 #include <sstream>
16 #include <typeinfo>
17 #include <unordered_map>
18 #include <vector>
19 
20 #include "../meta/platform.h"
21 
22 #ifndef __VS
23 #include <unistd.h>
24 #endif
25 
26 namespace carl {
27 
28 /**
29  * Uses GDB to print a stack trace.
30  */
31 void printStacktrace();
32 
33 std::string demangle(const char* name);
34 
35 std::string callingFunction();
36 
37 template<typename T>
38 std::string typeString() {
39  const char* name = typeid(T).name();
40  return demangle(name);
41 }
42 
43 #ifndef NDEBUG
44 /**
45  * Stores a textual representation of the last assertion that was registered via REGISTER_ASSERT.
46  */
47 extern std::string last_assertion_string;
48 /**
49  * Stores an integer representation of the last assertion that was registered via REGISTER_ASSERT.
50  */
51 extern int last_assertion_code;
52 /**
53  * Registers an upcoming assertion with the SIGABRT signal handler.
54  *
55  * If the program is compiled in debug mode, a signal handler is installed automatically that catches SIGABRT that is send when an assertion fails.
56  * The signal handler uses the data in last_assertion_string and last_assertion_code to generate an additional message and a custom exit code whenever an assertion is thrown.
57  * As for last_assertion_code the line number of the usage of this macro is used.
58  *
59  * If REGISTER_ASSERT was not called before SIGABRT is catched, the exit code is 23.
60  *
61  * This macro is intended to be used to identify a single assertion from the exit code in automated testing, for example using the delta debugger.
62  * The usage will usually look like this:
63  * @code{.cpp}
64  * REGISTER_ASSERT; assert( ... );
65  * @endcode
66  */
67 #ifdef __VS
68 #define __func__ __FUNCTION__
69 #endif
70 
71 #define REGISTER_ASSERT {\
72  std::stringstream ss; \
73  ss << __FILE__ << ":" << __LINE__ << " in " << __func__ << "()"; \
74  carl::last_assertion_string = ss.str(); \
75  carl::last_assertion_code = __LINE__; \
76  }
77 #define UNREGISTER_ASSERT {\
78  carl::last_assertion_string = ""; \
79  carl::last_assertion_code = 23; \
80  }
81 #define REGISTERED_ASSERT(condition) REGISTER_ASSERT; assert(condition); UNREGISTER_ASSERT
82 
83 #else
84 #define REGISTER_ASSERT
85 #define UNREGISTER_ASSERT
86 #define REGISTERED_ASSERT(condition)
87 #endif
88 
89 }
carl is the main namespace for the library.
std::string callingFunction()
Definition: debug.cpp:27
std::string demangle(const char *name)
Definition: debug.cpp:19
int last_assertion_code
Stores an integer representation of the last assertion that was registered via REGISTER_ASSERT.
Definition: debug.cpp:36
std::string typeString()
Definition: debug.h:38
void printStacktrace()
Uses GDB to print a stack trace.
Definition: debug.cpp:23
std::string last_assertion_string
Stores a textual representation of the last assertion that was registered via REGISTER_ASSERT.
Definition: debug.cpp:35