Stimfit @PACKAGE_VERSION@
Loading...
Searching...
No Matches
axodebug.h
Go to the documentation of this file.
1//******************************************************************************
2//
3// Copyright (c) 1993-1997 Axon Instruments.
4// All rights reserved.
5//
6//******************************************************************************
7// HEADER: AXODEBUG.H
8// PURPOSE: Contains utility macros and defines for Axon development debugging.
9// AUTHOR: BHI Oct 1993
10//
11// Debugging functions in WINDOWS.H
12// BOLL WINAPI IsWindow(HWND hWnd);
13// BOOL WINAPI IsGDIObject(HGDIOBJ hobj);
14// BOOL WINAPI IsBadReadPtr(const void *lp, UINT cb);
15// BOOL WINAPI IsBadWritePtr(void *lp, UINT cb);
16// BOOL WINAPI IsBadHugeReadPtr(const void _huge* lp, DWORD cb);
17// BOOL WINAPI IsBadHugeWritePtr(void _huge* lp, DWORD cb);
18// BOOL WINAPI IsBadCodePtr(FARPROC lpfn);
19// BOOL WINAPI IsBadStringPtr(const void *lpsz, UINT cchMax);
20// void WINAPI FatalExit(int nCode); // (nCode = -1 -> stack overflow)
21// void WINAPI FatalAppExit(UINT, LPCSTR);
22// void WINAPI DebugBreak(void);
23// void WINAPI OutputDebugString(LPCSTR);
24
25
26#ifndef __AXODEBUG_H__
27#define __AXODEBUG_H__
28
29#include <assert.h>
30
31#if defined(__WIN32__) && !defined(__MINGW32__)
32 #include <crtdbg.h>
33#endif
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39// Setup the debug reporting callback.
41
42// Protected call to DebugBreak() that only breaks if a debugger is running.
44
45// Prints a printf formatted string to the debug context.
46#ifdef __MINGW32__
47int AXODBG_printf(char *lpsz, ... );
48#else
49int cdecl AXODBG_printf(char *lpsz, ... );
50#endif
51
52// Set a prefix string used for all subsequent calls to AXODBG_printf()
53void AXODBG_SetTracePrefix(char const * szNewTracePrefix);
54
55// You can set the prefix to anything, but usually you will want to set it to include the EXE and/or DLL name.
56
57// This function sets the prefix to "{App=foo} {Mod=bar} "
58// Call this function once from within your DllMain(), WinMain() or CWinApp::InitInstance()
60
61
62// Prints a textual description of a WIN32 system error to the debug context.
63int AXODBG_ShowSystemError(DWORD dwSystemError);
64UINT AXODBG_GetSystemErrorText(DWORD dwSystemError, LPSTR pszBuf, UINT uMaxLen);
65
66// Returns TRUE if the given pointer is aligned on a multiple of the passed data size.
67BOOL AXODBG_IsAligned( void *pv, UINT uDataSize );
68
69// Returns TRUE if the given pointer is not on the stack (rough check).
70// Typically this function should be used in the constructor of large objects to check
71// that they are not being allocated on the stack.
72// e.g.
73// CSomeObject::CSomeObject()
74// {
75// MEMBERASSERT();
76// ASSERT_NOTONSTACK(this);
77// }
79
80// Assertion processing.
81void AXODBG_assert(LPCSTR psExp, LPCSTR psFile, int nLine);
82void AXODBG_ErrorMsg(LPCSTR psFile, int nLine, LPCSTR pszFormat, ...);
83void AXODBG_SystemErrorMsg(DWORD dwSystemError, LPCSTR psFile, int nLine);
84
85// Define our own ASSERT macro that is only compiled into a _DEBUG build.
86// This gives us more control over where the error output gets displayed
87// than the default runtime version.
88
89#if !defined(ASSERT)
90#ifdef _STFDEBUG
91/* #define ASSERT(exp) (void)( (exp) || (AXODBG_assert(#exp, __FILE__, __LINE__), 0) )*/
92 #define ASSERT(exp) assert(exp)
93#else
94 // #define ASSERT(exp) ((void)0)
95 #define ASSERT(exp) assert(exp)
96#endif
97#endif // ASSERT
98
99//
100// The ERRORMSG macros are like a combination of an ASSERT and a TRACE macro.
101// They are typically useful for marking a code path that should never get executed
102// (e.g. default clause of switch statement) with a debug time message and the
103// option to break to the debugger.
104//
105
106#if !defined(ERRORMSG)
107#ifdef _STFDEBUG
108 #define ERRORMSG(msg) AXODBG_ErrorMsg(__FILE__, __LINE__, msg)
109 #define ERRORMSG1(msg,a) AXODBG_ErrorMsg(__FILE__, __LINE__, msg, a)
110 #define ERRORMSG2(msg,a,b) AXODBG_ErrorMsg(__FILE__, __LINE__, msg, a, b)
111 #define ERRORMSG3(msg,a,b,c) AXODBG_ErrorMsg(__FILE__, __LINE__, msg, a, b, c)
112 #define ERRORMSG4(msg,a,b,c,d) AXODBG_ErrorMsg(__FILE__, __LINE__, msg, a, b, c, d)
113 #define ERRORMSG5(msg,a,b,c,d,e) AXODBG_ErrorMsg(__FILE__, __LINE__, msg, a, b, c, d, e)
114#else
115 #define ERRORMSG(msg) ((void)0)
116 #define ERRORMSG1(msg,a) ((void)0)
117 #define ERRORMSG2(msg,a,b) ((void)0)
118 #define ERRORMSG3(msg,a,b,c) ((void)0)
119 #define ERRORMSG4(msg,a,b,c,d) ((void)0)
120 #define ERRORMSG5(msg,a,b,c,d,e) ((void)0)
121#endif
122#endif // ASSERT
123
124#if defined(_WINDOWS) && !defined(__MINGW32__)
125 #define HWNDASSERT(hWnd) ASSERT(IsWindow(hWnd))
126 #define IsBadPtr(p) IsBadWritePtr((void *)(p), sizeof(*(p)))
127 #define IsBadArray(p,n) IsBadWritePtr((void *)(p), sizeof(*(p))*(n))
128 #define RPTRASSERT(p) ASSERT(!IsBadReadPtr((const void *)(p), sizeof(*(p))))
129 #define FNPTRASSERT(p) ASSERT(!IsBadCodePtr((FARPROC)(p)))
130 #define LPSZASSERT(p) ASSERT(!IsBadStringPtr(p, (UINT)(-1)))
131#else
132 #define IsBadPtr(p) (p==NULL)
133 #define IsBadArray(p,n) IsBadPtr(p)
134 #define RPTRASSERT(p) ASSERT(!IsBadPtr(p))
135 #define FNPTRASSERT(p) ASSERT(!IsBadPtr(p))
136 #define LPSZASSERT(p) ASSERT(!IsBadPtr(p))
137#endif
138
139#define WPTRASSERT(p) ASSERT(!IsBadPtr(p))
140#define MEMBERASSERT() WPTRASSERT(this)
141#define ARRAYASSERT(p,n) ASSERT(!IsBadArray(p, n))
142
143//==================================================================================================================
144// MACRO:
145// RARRAYASSERT(p,n)
146// WARRAYASSERT(p,n)
147// PURPOSE:
148// Validate an array by checking the caller has read access to the array.
149// PARAMETERS:
150// p The array
151// n The number of elements in the array (NOT the size in bytes)
152// USAGE:
153// int array[10];
154// RARRAYASSERT(array, 10);
155#if defined(_WINDOWS) && !defined(__MINGW32__)
156 #define RARRAYASSERT(p,n) ASSERT( ((p) != NULL) && !IsBadReadPtr( (const void *)(p), sizeof(*(p))*(n) ) )
157 #define WARRAYASSERT(p,n) ASSERT( ((p) != NULL) && !IsBadWritePtr( (void *)(p), sizeof(*(p))*(n) ) )
158#else
159 #define RARRAYASSERT(p,n) ASSERT(!IsBadPtr(p))
160 #define WARRAYASSERT(p,n) ASSERT(!IsBadPtr(p))
161#endif
162
163
164//==================================================================================================================
165// VERIFY is an ASSERT macro that always evaluates the given expression, but
166// only actually ASSERT's in a DEBUG build.
167#if !defined(VERIFY)
168#ifdef _STFDEBUG
169 #define VERIFY(exp) ASSERT(exp)
170#else
171 #define VERIFY(exp) (exp)
172#endif
173#endif
174
175//==================================================================================================================
176// The DEBUG_ONLY macro only expands out the expression in a DEBUG build.
177#if !defined(DEBUG_ONLY)
178#ifdef _STFDEBUG
179 #define DEBUG_ONLY(exp) (exp)
180#else
181 #define DEBUG_ONLY(exp) ((void)0)
182#endif
183#endif
184
185//==================================================================================================================
186// Trace functions that are only expanded for debug builds and when the AXODBG_TRACE flag is set.
187
188#if !defined(TRACE)
189#if (defined(_STFDEBUG) || defined(AXODBG_TRACE))
190 #define TRACE(s) AXODBG_printf(s)
191 #define DLLTRACE(s) AXODBG_printf(s)
192 #define TRACE1(s, a) AXODBG_printf(s, a)
193 #define TRACE2(s, a, b) AXODBG_printf(s, a, b)
194 #define TRACE3(s, a, b, c) AXODBG_printf(s, a, b, c)
195 #define TRACE4(s, a, b, c, d) AXODBG_printf(s, a, b, c, d)
196 #define TRACE5(s, a, b, c, d, e) AXODBG_printf(s, a, b, c, d, e)
197#else
198 #define TRACE(s) ((void)0)
199 #define DLLTRACE(s) ((void)0)
200 #define TRACE1(s, a) ((void)0)
201 #define TRACE2(s, a, b) ((void)0)
202 #define TRACE3(s, a, b, c) ((void)0)
203 #define TRACE4(s, a, b, c, d) ((void)0)
204 #define TRACE5(s, a, b, c, d, e) ((void)0)
205#endif
206#endif
207
208#define TRACE_INT(e) TRACE1(#e "=%d\n", e)
209#define TRACE_FLOAT(e) TRACE1(#e "=%g\n", e)
210#define TRACE_STRING(e) TRACE1(#e "=%s\n", e)
211
212//==================================================================================================================
213// SHOW_SYSTEM_ERROR will only dump a text description of a system error in a DEBUG build.
214//
215#ifdef _STFDEBUG
216 #define SHOW_SYSTEM_ERROR(dwError) AXODBG_ShowSystemError(dwError)
217#else
218 #define SHOW_SYSTEM_ERROR(dwError) ((void)0)
219#endif
220
221//==================================================================================================================
222// VERIFY_SYSTEM_CALL will only dump a text description of a system error in a DEBUG build.
223//
224#ifdef _STFDEBUG
225 #define VERIFY_SYSTEM_CALL(exp) (exp || (AXODBG_SystemErrorMsg(0, __FILE__, __LINE__), 0))
226#else
227 #define VERIFY_SYSTEM_CALL(exp) (exp)
228#endif
229
230//==================================================================================================================
231// ASSERT_ISALIGNED will throw an assertion error if the pointer is not aligned on a boundary of its data type.
232//
233#ifdef _STFDEBUG
234 #define ASSERT_ISALIGNED(p) ASSERT(AXODBG_IsAligned(p, sizeof(*p)))
235 #define ASSERT_NOTONSTACK(p) ASSERT(AXODBG_NotOnStack(p))
236#else
237 #define ASSERT_ISALIGNED(p) ((void)0)
238 #define ASSERT_NOTONSTACK(p) ((void)0)
239#endif
240
241
242//==================================================================================================================
243// The following macros set and clear, respectively, given bits
244// of the C runtime library debug flag, as specified by a bitmask.
245//
246// Valid flags include: (default value)
247// _CRTDBG_ALLOC_MEM_DF (on)
248// ON: Enable debug heap allocations and use of memory block type identifiers, such as _CLIENT_BLOCK.
249// OFF: Add new allocations to heap's linked list, but set block type to _IGNORE_BLOCK.
250// _CRTDBG_CHECK_ALWAYS_DF (off)
251// ON: Call _CrtCheckMemory at every allocation and deallocation request.
252// OFF: _CrtCheckMemory must be called explicitly.
253// _CRTDBG_CHECK_CRT_DF (off)
254// ON: Include _CRT_BLOCK types in leak detection and memory state difference operations.
255// OFF: Memory used internally by the run-time library is ignored by these operations.
256// _CRTDBG_DELAY_FREE_MEM_DF (off)
257// ON: Keep freed memory blocks in the heap's linked list, assign them the _FREE_BLOCK type, and fill them with the byte value 0xDD.
258// OFF: Do not keep freed blocks in the heap's linked list.
259// _CRTDBG_LEAK_CHECK_DF (off)
260// ON: Perform automatic leak checking at program exit via a call to _CrtDumpMemoryLeaks and generate an error report if the application failed to free all the memory it allocated.
261// OFF: Do not automatically perform leak checking at program exit.
262//
263#ifdef _STFDEBUG
264 #define SET_CRT_DEBUG_FIELD(a) \
265 _CrtSetDbgFlag((a) | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
266 #define CLEAR_CRT_DEBUG_FIELD(a) \
267 _CrtSetDbgFlag(~(a) & _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
268#else
269 #define SET_CRT_DEBUG_FIELD(a) ((void)0)
270 #define CLEAR_CRT_DEBUG_FIELD(a) ((void)0)
271#endif
272
273
274//==================================================================================================================
275// HEAPASSERT checks the integrity of the heap managed by the run-time library.
276//
277#ifdef _STFDEBUG
278 #define HEAPASSERT() ASSERT(_CrtCheckMemory())
279#else
280 #define HEAPASSERT() ((void)0)
281#endif
282
283
284//==================================================================================================================
285// Flags supported by AXODBG_SetDebugFlag.
286#define AXODBG_REPORT_FLAG 0x80000000 // If this flag is set the current flags are returned.
287#define AXODBG_BREAK_ON_ASSERT 0x00000001 // If set, assertion failures will immediately break to the debugger.
288
289// Function to set/get debugging flags modeled after _CrtSetDbgFlag().
291
292#ifdef _STFDEBUG
293 #define AXODBG_SET_DEBUG_FIELD(a) \
294 AXODBG_SetDebugFlag((a) | AXODBG_SetDebugFlag(AXODBG_REPORT_FLAG))
295 #define AXODBG_CLEAR_DEBUG_FIELD(a) \
296 AXODBG_SetDebugFlag(~(a) & AXODBG_SetDebugFlag(AXODBG_REPORT_FLAG))
297#else
298 #define AXODBG_SET_DEBUG_FIELD(a) ((void)0)
299 #define AXODBG_CLEAR_DEBUG_FIELD(a) ((void)0)
300#endif
301
302//==================================================================================================================
303// Debug time only call to AXODBG_DebugBreak()
304#ifdef _STFDEBUG
305 #define AXODBG_DEBUGBREAK() AXODBG_DebugBreak()
306#else
307 #define AXODBG_DEBUGBREAK() ((BOOL)FALSE)
308#endif
309
310#ifdef __cplusplus
311}
312#endif
313
314#ifdef __cplusplus
315
316//==================================================================================================================
317// Compile time assert for static expressions
318// (e.g. STATIC_ASSERT(sizeof(MyStruct)==256); )
319//
320#define STATIC_ASSERT(expr) C_ASSERT(expr)
321
322#endif
323
324#endif /* __AXODEBUG_H__ */
void AXODBG_Initialize(void)
BOOL AXODBG_DebugBreak(void)
void AXODBG_SystemErrorMsg(DWORD dwSystemError, LPCSTR psFile, int nLine)
BOOL AXODBG_IsAligned(void *pv, UINT uDataSize)
DWORD AXODBG_SetDebugFlag(DWORD dwFlags)
void AXODBG_SetTracePrefixFromModuleHandle(HMODULE mod_handle)
void AXODBG_SetTracePrefix(char const *szNewTracePrefix)
void AXODBG_ErrorMsg(LPCSTR psFile, int nLine, LPCSTR pszFormat,...)
void AXODBG_assert(LPCSTR psExp, LPCSTR psFile, int nLine)
int cdecl AXODBG_printf(char *lpsz,...)
BOOL AXODBG_NotOnStack(void *pv)
int AXODBG_ShowSystemError(DWORD dwSystemError)
UINT AXODBG_GetSystemErrorText(DWORD dwSystemError, LPSTR pszBuf, UINT uMaxLen)
int BOOL
Definition unix.h:49
unsigned int UINT
Definition unix.h:47
#define cdecl
Definition unix.h:197
CHAR * LPSTR
Definition unix.h:93
unsigned ABFLONG DWORD
Definition unix.h:45
const CHAR * LPCSTR
Definition unix.h:94
HINSTANCE HMODULE
Definition unix.h:138