BALL 1.5.0
debug.h
Go to the documentation of this file.
1// -*- Mode: C++; tab-width: 2; -*-
2// vi: set ts=2:
3//
4
5#ifndef BALL_COMMON_DEBUG_H
6#define BALL_COMMON_DEBUG_H
7
8#ifndef BALL_CONFIG_CONFIG_H
9# include <BALL/CONFIG/config.h>
10#endif
11
12#ifdef BALL_NO_INLINE_FUNCTIONS
13# define BALL_INLINE
14#else
15# define BALL_INLINE inline
16#endif
17
18#include <string>
19#include <cstring>
20
21#ifdef BALL_DEBUG
22
23# define BALL_PRECONDITION_EXCEPTION(condition, message)\
24 if (!(condition))\
25 {\
26 Exception::Precondition e(__FILE__, __LINE__, #condition);\
27 if (strcmp(message, "") != 0)\
28 {\
29 ::std::string tmp(e.getMessage());\
30 tmp += ::std::string(message);\
31 e.setMessage(tmp);\
32 }\
33 throw e;\
34 }\
35
36# define BALL_POSTCONDITION_EXCEPTION(condition, message)\
37 if (!(condition))\
38 {\
39 Exception::Postcondition e(__FILE__, __LINE__, #condition);\
40 if (strcmp(message, "") != 0)\
41 {\
42 std::string tmp(e.getMessage());\
43 tmp += std::string(message);\
44 e.setMessage(tmp);\
45 }\
46 throw e;\
47 }\
48
49# if defined(BALL_COMPILER_GXX) || defined(BALL_COMPILER_LLVM)
50# include <execinfo.h>
51# include <cxxabi.h>
52
53# define BALL_PRINT_BACKTRACE()\
54 {\
55 void *bt_addresses[100];\
56 char **bt;\
57 \
58 int bt_size = backtrace(bt_addresses, 100);\
59 bt = backtrace_symbols(bt_addresses, bt_size);\
60 \
61 char* demangled=0;\
62 size_t num_demangled;\
63 int status;\
64 std::vector<String> split;\
65 \
66 for(int i = 0; i < bt_size; i++)\
67 {\
68 String mangled(bt[i]);\
69 \
70 Log.warn() << i << ": " << bt[i] << " ";\
71 mangled = mangled.getField(1, "()");\
72 mangled = mangled.getField(0, "+");\
73 \
74 char* out = abi::__cxa_demangle(mangled.c_str(), demangled, &num_demangled, &status);\
75 if (status == 0) Log.warn() << "demangled: " << String(out);\
76 if (num_demangled > 0) free(demangled);\
77 demangled = 0;\
78 Log.warn() << std::endl;\
79 }\
80 free(bt);\
81 }\
82
83# else
84# define BALL_PRINT_BACKTRACE()
85# endif
86
87#else
88
89# define BALL_PRECONDITION_EXCEPTION(condition, message)
90# define BALL_POSTCONDITION_EXCEPTION(condition, message)
91# define BALL_PRINT_BACKTRACE()
92
93#endif // BALL_DEBUG
94
95#endif // BALL_COMMON_DEBUG_H