Various methods and tools for debugging parts of the library or your application. More...
Classes | |
class | concepts::Assertion |
Exception class for assertions. More... | |
class | concepts::Stacktrace |
Dumps a stack trace using gdb. More... | |
Macros | |
#define | conceptsAssert(cond, exc) { } |
Assert that a certain condition is fulfilled. More... | |
#define | conceptsAssert3(cond, exc, msg) { } |
Assert that a certain condition is fulfilled. More... | |
#define | conceptsThrowSimpleE(msg) |
Throws a equivalent to conceptsAssert(false, exc) More... | |
#define | DEBUGL(doit, msg) |
Detailed Description
Various methods and tools for debugging parts of the library or your application.
Debugging Concepts with a Debugger
This is quite simple: make sure you use the -g
flag of the compiler for compiling the library and your application and then use a debugger on the resulting binary. Examples for debuggers are:
- GDB: The GNU Debugger, no graphical frontend
- xxgdb, a graphical frontend to GDB
- DDD: The Data Display Debugger, a Motif/Lesstif frontend to GDB
- KDbg, a QT/KDE frontend to GDB
- UPS, Ups is a source level C,C++ and Fortran debugger that runs under X11. It runs native on some versions of UNIX® (e.g. BSD/OS, FreeBSD, Linux, SunOS & Solaris, etc.), on others it can be built as a front end to GDB.
I prefer DDD, which should be part of every Linux distribution and is installed on the ETH workstations.
Debugging Concepts with Output Statements and Assertions
Output Statements
There exists a header file to simplify the creation of debugging output statements: debug.hh. It contains one compiler macros which is important: DEBUGL. This macro is only evaluated if the DEBUG
symbol is defined.
In order to have fine grained debugging possibilities, you should use different symbols in a local debugging header file. For instance:
In our example, all statements with dgBDiffApplOperator_D
are executed. Such a statement could look like this:
The output of the statment above looks like
If you have more complex statements which you would like to hide for production use, you can do something like the following
Like before, these statements are only executed (and even only compiled) if \c DEBUG is defined and \c dgBDiffApplOperator_D is true (ie. non-zero). <h3>Assertions</h3> The class concepts::Assertion can be used to assert certain conditions. \code #include "basics/exceptions.hh"
// ...
const int* m = dynamic_cast<const int*>(n); conceptsAssert(m != 0, Assertion()); cout << *m << endl;
// ...
conceptsAssert3(a == b, Assertion(), "a = " << a << ", b = " << b);
If the assertion fails, an exception of type concepts::Assertion is thrown. If the macro \c conceptsAssert3 is used instead of \c conceptsAssert, the additional information given as the third argument is printed in case of an error. It can be caught by \code try {
// here, an assertion might fail } catch (Assertion& e) { std::cout << e << std::endl; } Instead of catching Assertion
it is also possible to catch ExceptionBase
(the base class of all exceptions in the library).
Stacktrace
In case an assertion fails, a stacktrace is printed by calling gdb in concepts::Stacktrace::print. This can be controlled by concepts::Stacktrace::doit:
inhibits printing stack traces.
Macro Definition Documentation
◆ conceptsAssert
#define conceptsAssert | ( | cond, | |
exc | |||
) | { } |
Assert that a certain condition is fulfilled.
Otherwise, issue an exception (if its not catched, the program is aborted). This is the main routine in the exception mechanism for debug mode error checking. See the Assertion class for more information.
- Parameters
-
cond Condition, if unfulfill an exception is thrown exc Exception to throw, if the condition is unfulfilled
- See also
- Assertion
- Examples
- hpFEM2d.cc, linearFEM1d-simple.cc, and linearFEM1d.cc.
Definition at line 395 of file exceptions.hh.
◆ conceptsAssert3
#define conceptsAssert3 | ( | cond, | |
exc, | |||
msg | |||
) | { } |
Assert that a certain condition is fulfilled.
Otherwise, issue an exception (if its not catched, the program is aborted). This is an extention to the conceptsAssert
macro. The additional feature is the third parameter msg:
conceptsAssert3(1 == 0, Assertion(), "add " << "a message" << " here");
gives
[file.cc, line 34] int main(int, char**) -- error Assertion() by violating condition <1 == 0>, add a message here
as error message.
- Parameters
-
cond Condition, if unfulfill an exception is thrown exc Exception to throw, if the condition is unfulfilled msg Additional message in stream notation
- See also
- Assertion
Definition at line 443 of file exceptions.hh.
◆ conceptsThrowSimpleE
#define conceptsThrowSimpleE | ( | msg | ) |
Throws a equivalent to conceptsAssert(false, exc)
Definition at line 412 of file exceptions.hh.
◆ DEBUGL
#define DEBUGL | ( | doit, | |
msg | |||
) |
Debug Line. Prints a debugging message with information about file, line and function where it is located. At the end, the line is wrapped. The message looks like
[file.cc, line 62] void function(double) -- this is the message
- Parameters
-
doit The line is only printed if true msg Text in a stream (can include variables etc.) to be printed
- Examples
- meshes.cc.