VirtualBox

source: vbox/trunk/include/iprt/assert.h@ 3810

Last change on this file since 3810 was 3630, checked in by vboxsync, 17 years ago

iprt_hdr_h -> _iprt_hdr_h

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 37.0 KB
Line 
1/** @file
2 * innotek Portable Runtime - Assertions.
3 */
4
5/*
6 * Copyright (C) 2006-2007 innotek GmbH
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License as published by the Free Software Foundation,
12 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
13 * distribution. VirtualBox OSE is distributed in the hope that it will
14 * be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * If you received this file as part of a commercial VirtualBox
17 * distribution, then only the terms of your commercial VirtualBox
18 * license agreement apply instead of the previous paragraph.
19 */
20
21#ifndef ___iprt_assert_h
22#define ___iprt_assert_h
23
24#include <iprt/cdefs.h>
25#include <iprt/types.h>
26
27/** @defgroup grp_rt_assert Assert - Assertions
28 * @ingroup grp_rt
29 * @{
30 */
31
32
33/** @def AssertBreakpoint()
34 * Assertion Breakpoint.
35 *
36 * @remark In the gnu world we add a nop instruction after the int3 to
37 * force gdb to remain at the int3 source line.
38 * @remark The L4 kernel will try make sense of the breakpoint, thus the jmp.
39 */
40#ifdef RT_STRICT
41# ifdef __GNUC__
42# ifndef __L4ENV__
43# define AssertBreakpoint() do { if (RTAssertDoBreakpoint()) { __asm__ __volatile__ ("int3\n\tnop"); } } while (0)
44# else
45# define AssertBreakpoint() do { if (RTAssertDoBreakpoint()) { __asm__ __volatile__ ("int3; jmp 1f; 1:"); } } while (0)
46# endif
47# elif defined(_MSC_VER)
48# define AssertBreakpoint() do { if (RTAssertDoBreakpoint()) { __debugbreak(); } } while (0)
49# else
50# error "Unknown compiler"
51# endif
52#else
53# define AssertBreakpoint() do { } while (0)
54#endif
55
56/**
57 * RTASSERTTYPE is the type the AssertCompile() macro redefines.
58 * It has no other function and shouldn't be used.
59 * Visual C++ uses this.
60 */
61typedef int RTASSERTTYPE[1];
62
63/**
64 * RTASSERTVAR is the type the AssertCompile() macro redefines.
65 * It has no other function and shouldn't be used.
66 * GCC uses this.
67 */
68#ifdef __GNUC__
69__BEGIN_DECLS
70#endif
71extern int RTASSERTVAR[1];
72#ifdef __GNUC__
73__END_DECLS
74#endif
75
76/** @def AssertCompile
77 * Asserts that a compile-time expression is true. If it's not break the build.
78 * @param expr Expression which should be true.
79 */
80#ifdef __GNUC__
81# define AssertCompile(expr) extern int RTASSERTVAR[1] __attribute__((unused)), RTASSERTVAR[(expr) ? 1 : 0] __attribute__((unused))
82#else
83# define AssertCompile(expr) typedef int RTASSERTTYPE[(expr) ? 1 : 0]
84#endif
85
86/** @def AssertCompileSize
87 * Asserts a size at compile.
88 * @param type The type.
89 * @param size The expected type size.
90 */
91#define AssertCompileSize(type, size) \
92 AssertCompile(sizeof(type) == (size))
93
94/** @def AssertCompileSizeAlignment
95 * Asserts a size alignment at compile.
96 * @param type The type.
97 * @param align The size alignment to assert.
98 */
99#define AssertCompileSizeAlignment(type, align) \
100 AssertCompile(!(sizeof(type) & ((align) - 1)))
101
102/** @def AssertCompileMemberAlignment
103 * Asserts a member offset alignment at compile.
104 * @param type The type.
105 * @param member The member.
106 * @param align The member offset alignment to assert.
107 */
108#if defined(__GNUC__) && defined(__cplusplus)
109# if __GNUC__ >= 4
110# define AssertCompileMemberAlignment(type, member, align) \
111 AssertCompile(!(__builtin_offsetof(type, member) & ((align) - 1)))
112# else
113# define AssertCompileMemberAlignment(type, member, align) \
114 AssertCompile(!(RT_OFFSETOF(type, member) & ((align) - 1)))
115# endif
116#else
117# define AssertCompileMemberAlignment(type, member, align) \
118 AssertCompile(!(RT_OFFSETOF(type, member) & ((align) - 1)))
119#endif
120
121
122/** @def AssertCompileMemberSize
123 * Asserts a member offset alignment at compile.
124 * @param type The type.
125 * @param member The member.
126 * @param size The member size to assert.
127 */
128#define AssertCompileMemberSize(type, member, size) \
129 AssertCompile(RT_SIZEOFMEMB(type, member) == (size))
130
131/** @def AssertCompileMemberSizeAlignment
132 * Asserts a member size alignment at compile.
133 * @param type The type.
134 * @param member The member.
135 * @param align The member size alignment to assert.
136 */
137#define AssertCompileMemberSizeAlignment(type, member, align) \
138 AssertCompile(!(RT_SIZEOFMEMB(type, member) & ((align) - 1)))
139
140
141/** @def Assert
142 * Assert that an expression is true. If it's not hit breakpoint.
143 * @param expr Expression which should be true.
144 */
145#ifdef RT_STRICT
146# define Assert(expr) \
147 do { \
148 if (RT_UNLIKELY(!(expr))) \
149 { \
150 AssertMsg1(#expr, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
151 AssertBreakpoint(); \
152 } \
153 } while (0)
154#else
155# define Assert(expr) do { } while (0)
156#endif
157
158
159/** @def AssertReturn
160 * Assert that an expression is true and returns if it isn't.
161 * In RT_STRICT mode it will hit a breakpoint before returning.
162 *
163 * @param expr Expression which should be true.
164 * @param rc What is to be presented to return.
165 */
166#ifdef RT_STRICT
167# define AssertReturn(expr, rc) \
168 do { \
169 if (RT_UNLIKELY(!(expr))) \
170 { \
171 AssertMsg1(#expr, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
172 AssertBreakpoint(); \
173 return (rc); \
174 } \
175 } while (0)
176#else
177# define AssertReturn(expr, rc) \
178 do { \
179 if (RT_UNLIKELY(!(expr))) \
180 return (rc); \
181 } while (0)
182#endif
183
184/** @def AssertReturnVoid
185 * Assert that an expression is true and returns if it isn't.
186 * In RT_STRICT mode it will hit a breakpoint before returning.
187 *
188 * @param expr Expression which should be true.
189 */
190#ifdef RT_STRICT
191# define AssertReturnVoid(expr) \
192 do { \
193 if (RT_UNLIKELY(!(expr))) \
194 { \
195 AssertMsg1(#expr, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
196 AssertBreakpoint(); \
197 return; \
198 } \
199 } while (0)
200#else
201# define AssertReturnVoid(expr) \
202 do { \
203 if (RT_UNLIKELY(!(expr))) \
204 return; \
205 } while (0)
206#endif
207
208
209/** @def AssertBreak
210 * Assert that an expression is true and breaks if it isn't.
211 * In RT_STRICT mode it will hit a breakpoint before doing break.
212 *
213 * @param expr Expression which should be true.
214 * @param stmt Statement to execute before break in case of a failed assertion.
215 */
216#ifdef RT_STRICT
217# define AssertBreak(expr, stmt) \
218 if (RT_UNLIKELY(!(expr))) { \
219 AssertMsg1(#expr, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
220 AssertBreakpoint(); \
221 stmt; \
222 break; \
223 } else do {} while (0)
224#else
225# define AssertBreak(expr, stmt) \
226 if (RT_UNLIKELY(!(expr))) { \
227 stmt; \
228 break; \
229 } else do {} while (0)
230#endif
231
232
233/** @def AssertMsg
234 * Assert that an expression is true. If it's not print message and hit breakpoint.
235 * @param expr Expression which should be true.
236 * @param a printf argument list (in parenthesis).
237 */
238#ifdef RT_STRICT
239# define AssertMsg(expr, a) \
240 do { \
241 if (RT_UNLIKELY(!(expr))) \
242 { \
243 AssertMsg1(#expr, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
244 AssertMsg2 a; \
245 AssertBreakpoint(); \
246 } \
247 } while (0)
248#else
249# define AssertMsg(expr, a) do { } while (0)
250#endif
251
252/** @def AssertMsgReturn
253 * Assert that an expression is true and returns if it isn't.
254 * In RT_STRICT mode it will hit a breakpoint before returning.
255 *
256 * @param expr Expression which should be true.
257 * @param a printf argument list (in parenthesis).
258 * @param rc What is to be presented to return.
259 */
260#ifdef RT_STRICT
261# define AssertMsgReturn(expr, a, rc) \
262 do { \
263 if (RT_UNLIKELY(!(expr))) \
264 { \
265 AssertMsg1(#expr, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
266 AssertMsg2 a; \
267 AssertBreakpoint(); \
268 return (rc); \
269 } \
270 } while (0)
271#else
272# define AssertMsgReturn(expr, a, rc) \
273 do { \
274 if (RT_UNLIKELY(!(expr))) \
275 return (rc); \
276 } while (0)
277#endif
278
279/** @def AssertMsgReturnVoid
280 * Assert that an expression is true and returns if it isn't.
281 * In RT_STRICT mode it will hit a breakpoint before returning.
282 *
283 * @param expr Expression which should be true.
284 * @param a printf argument list (in parenthesis).
285 */
286#ifdef RT_STRICT
287# define AssertMsgReturnVoid(expr, a) \
288 do { \
289 if (RT_UNLIKELY(!(expr))) \
290 { \
291 AssertMsg1(#expr, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
292 AssertMsg2 a; \
293 AssertBreakpoint(); \
294 return; \
295 } \
296 } while (0)
297#else
298# define AssertMsgReturnVoid(expr, a) \
299 do { \
300 if (RT_UNLIKELY(!(expr))) \
301 return; \
302 } while (0)
303#endif
304
305
306/** @def AssertMsgBreak
307 * Assert that an expression is true and breaks if it isn't.
308 * In RT_STRICT mode it will hit a breakpoint before doing break.
309 *
310 * @param expr Expression which should be true.
311 * @param a printf argument list (in parenthesis).
312 * @param stmt Statement to execute before break in case of a failed assertion.
313 */
314#ifdef RT_STRICT
315# define AssertMsgBreak(expr, a, stmt) \
316 if (RT_UNLIKELY(!(expr))) { \
317 AssertMsg1(#expr, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
318 AssertMsg2 a; \
319 AssertBreakpoint(); \
320 stmt; \
321 break; \
322 } else do {} while (0)
323#else
324# define AssertMsgBreak(expr, a, stmt) \
325 if (RT_UNLIKELY(!(expr))) { \
326 stmt; \
327 break; \
328 } else do {} while (0)
329#endif
330
331
332/** @def AssertFailed
333 * An assertion failed hit breakpoint.
334 */
335#ifdef RT_STRICT
336# define AssertFailed() \
337 do { \
338 AssertMsg1((const char *)0, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
339 AssertBreakpoint(); \
340 } while (0)
341#else
342# define AssertFailed() do { } while (0)
343#endif
344
345/** @def AssertFailedReturn
346 * An assertion failed, hit breakpoint (RT_STRICT mode only) and return.
347 *
348 * @param rc The rc to return.
349 */
350#ifdef RT_STRICT
351# define AssertFailedReturn(rc) \
352 do { \
353 AssertMsg1((const char *)0, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
354 AssertBreakpoint(); \
355 return (rc); \
356 } while (0)
357#else
358# define AssertFailedReturn(rc) \
359 do { \
360 return (rc); \
361 } while (0)
362#endif
363
364/** @def AssertFailedReturnVoid
365 * An assertion failed, hit breakpoint (RT_STRICT mode only) and return.
366 */
367#ifdef RT_STRICT
368# define AssertFailedReturnVoid() \
369 do { \
370 AssertMsg1((const char *)0, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
371 AssertBreakpoint(); \
372 return; \
373 } while (0)
374#else
375# define AssertFailedReturnVoid() \
376 do { \
377 return; \
378 } while (0)
379#endif
380
381
382/** @def AssertFailedBreak
383 * An assertion failed, hit breakpoint (RT_STRICT mode only), execute
384 * the given statement and break.
385 *
386 * @param stmt Statement to execute before break.
387 */
388#ifdef RT_STRICT
389# define AssertFailedBreak(stmt) \
390 if (1) { \
391 AssertMsg1((const char *)0, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
392 AssertBreakpoint(); \
393 stmt; \
394 break; \
395 } else do {} while (0)
396#else
397# define AssertFailedBreak(stmt) \
398 if (1) { \
399 stmt; \
400 break; \
401 } else do {} while (0)
402#endif
403
404
405/** @def AssertMsgFailed
406 * An assertion failed print a message and a hit breakpoint.
407 *
408 * @param a printf argument list (in parenthesis).
409 */
410#ifdef RT_STRICT
411# define AssertMsgFailed(a) \
412 do { \
413 AssertMsg1((const char *)0, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
414 AssertMsg2 a; \
415 AssertBreakpoint(); \
416 } while (0)
417#else
418# define AssertMsgFailed(a) do { } while (0)
419#endif
420
421/** @def AssertMsgFailedReturn
422 * An assertion failed, hit breakpoint with message (RT_STRICT mode only) and return.
423 *
424 * @param a printf argument list (in parenthesis).
425 * @param rc What is to be presented to return.
426 */
427#ifdef RT_STRICT
428# define AssertMsgFailedReturn(a, rc) \
429 do { \
430 AssertMsg1((const char *)0, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
431 AssertMsg2 a; \
432 AssertBreakpoint(); \
433 return (rc); \
434 } while (0)
435#else
436# define AssertMsgFailedReturn(a, rc) \
437 do { \
438 return (rc); \
439 } while (0)
440#endif
441
442/** @def AssertMsgFailedReturnVoid
443 * An assertion failed, hit breakpoint with message (RT_STRICT mode only) and return.
444 *
445 * @param a printf argument list (in parenthesis).
446 */
447#ifdef RT_STRICT
448# define AssertMsgFailedReturnVoid(a) \
449 do { \
450 AssertMsg1((const char *)0, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
451 AssertMsg2 a; \
452 AssertBreakpoint(); \
453 return; \
454 } while (0)
455#else
456# define AssertMsgFailedReturnVoid(a) \
457 do { \
458 return; \
459 } while (0)
460#endif
461
462
463/** @def AssertMsgFailedBreak
464 * An assertion failed, hit breakpoint (RT_STRICT mode only), execute
465 * the given statement and break.
466 *
467 * @param a printf argument list (in parenthesis).
468 * @param stmt Statement to execute before break.
469 */
470#ifdef RT_STRICT
471# define AssertMsgFailedBreak(a, stmt) \
472 if (1) { \
473 AssertMsg1((const char *)0, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
474 AssertMsg2 a; \
475 AssertBreakpoint(); \
476 stmt; \
477 break; \
478 } else do {} while (0)
479#else
480# define AssertMsgFailedBreak(a, stmt) \
481 if (1) { \
482 stmt; \
483 break; \
484 } else do {} while (0)
485#endif
486
487
488/** @def AssertReleaseBreakpoint()
489 * Assertion Breakpoint.
490 *
491 * @remark In the gnu world we add a nop instruction after the int3 to
492 * force gdb to remain at the int3 source line.
493 * @remark The L4 kernel will try make sense of the breakpoint, thus the jmp.
494 */
495#ifdef __GNUC__
496# ifndef __L4ENV__
497# define AssertReleaseBreakpoint() do { __asm__ __volatile__ ("int3\n\tnop"); } while (0)
498# else
499# define AssertReleaseBreakpoint() do { __asm__ __volatile__ ("int3; jmp 1f; 1:"); } while (0)
500# endif
501#elif defined(_MSC_VER)
502# define AssertReleaseBreakpoint() __debugbreak()
503#else
504# error "Unknown compiler"
505#endif
506
507
508/** @def AssertRelease
509 * Assert that an expression is true. If it's not hit a breakpoint.
510 *
511 * @param expr Expression which should be true.
512 */
513#define AssertRelease(expr) \
514 do { \
515 if (RT_UNLIKELY(!(expr))) \
516 { \
517 AssertMsg1(#expr, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
518 AssertReleaseBreakpoint(); \
519 } \
520 } while (0)
521
522/** @def AssertReleaseReturn
523 * Assert that an expression is true, hit a breakpoing and return if it isn't.
524 *
525 * @param expr Expression which should be true.
526 * @param rc What is to be presented to return.
527 */
528#define AssertReleaseReturn(expr, rc) \
529 do { \
530 if (RT_UNLIKELY(!(expr))) \
531 { \
532 AssertMsg1(#expr, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
533 AssertReleaseBreakpoint(); \
534 return (rc); \
535 } \
536 } while (0)
537
538/** @def AssertReleaseReturnVoid
539 * Assert that an expression is true, hit a breakpoing and return if it isn't.
540 *
541 * @param expr Expression which should be true.
542 */
543#define AssertReleaseReturnVoid(expr) \
544 do { \
545 if (RT_UNLIKELY(!(expr))) \
546 { \
547 AssertMsg1(#expr, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
548 AssertReleaseBreakpoint(); \
549 return; \
550 } \
551 } while (0)
552
553
554/** @def AssertReleaseBreak
555 * Assert that an expression is true, hit a breakpoing and break if it isn't.
556 *
557 * @param expr Expression which should be true.
558 * @param stmt Statement to execute before break in case of a failed assertion.
559 */
560#define AssertReleaseBreak(expr, stmt) \
561 if (RT_UNLIKELY(!(expr))) { \
562 AssertMsg1(#expr, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
563 AssertReleaseBreakpoint(); \
564 stmt; \
565 break; \
566 } else do {} while (0)
567
568
569
570/** @def AssertReleaseMsg
571 * Assert that an expression is true, print the message and hit a breakpoint if it isn't.
572 *
573 * @param expr Expression which should be true.
574 * @param a printf argument list (in parenthesis).
575 */
576#define AssertReleaseMsg(expr, a) \
577 do { \
578 if (RT_UNLIKELY(!(expr))) \
579 { \
580 AssertMsg1(#expr, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
581 AssertMsg2 a; \
582 AssertReleaseBreakpoint(); \
583 } \
584 } while (0)
585
586/** @def AssertReleaseMsgReturn
587 * Assert that an expression is true, print the message and hit a breakpoint and return if it isn't.
588 *
589 * @param expr Expression which should be true.
590 * @param a printf argument list (in parenthesis).
591 * @param rc What is to be presented to return.
592 */
593#define AssertReleaseMsgReturn(expr, a, rc) \
594 do { \
595 if (RT_UNLIKELY(!(expr))) \
596 { \
597 AssertMsg1(#expr, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
598 AssertMsg2 a; \
599 AssertReleaseBreakpoint(); \
600 return (rc); \
601 } \
602 } while (0)
603
604/** @def AssertReleaseMsgReturn
605 * Assert that an expression is true, print the message and hit a breakpoint and return if it isn't.
606 *
607 * @param expr Expression which should be true.
608 * @param a printf argument list (in parenthesis).
609 */
610#define AssertReleaseMsgReturnVoid(expr, a) \
611 do { \
612 if (RT_UNLIKELY(!(expr))) \
613 { \
614 AssertMsg1(#expr, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
615 AssertMsg2 a; \
616 AssertReleaseBreakpoint(); \
617 return; \
618 } \
619 } while (0)
620
621
622/** @def AssertReleaseMsgBreak
623 * Assert that an expression is true, print the message and hit a breakpoing and break if it isn't.
624 *
625 * @param expr Expression which should be true.
626 * @param a printf argument list (in parenthesis).
627 * @param stmt Statement to execute before break in case of a failed assertion.
628 */
629#define AssertReleaseMsgBreak(expr, a, stmt) \
630 if (RT_UNLIKELY(!(expr))) { \
631 AssertMsg1(#expr, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
632 AssertMsg2 a; \
633 AssertReleaseBreakpoint(); \
634 stmt; \
635 break; \
636 } else do {} while (0)
637
638
639/** @def AssertReleaseFailed
640 * An assertion failed, hit a breakpoint.
641 */
642#define AssertReleaseFailed() \
643 do { \
644 AssertMsg1((const char *)0, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
645 AssertReleaseBreakpoint(); \
646 } while (0)
647
648/** @def AssertReleaseFailedReturn
649 * An assertion failed, hit a breakpoint and return.
650 *
651 * @param rc What is to be presented to return.
652 */
653#define AssertReleaseFailedReturn(rc) \
654 do { \
655 AssertMsg1((const char *)0, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
656 AssertReleaseBreakpoint(); \
657 return (rc); \
658 } while (0)
659
660/** @def AssertReleaseFailedReturn
661 * An assertion failed, hit a breakpoint and return.
662 */
663#define AssertReleaseFailedReturnVoid() \
664 do { \
665 AssertMsg1((const char *)0, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
666 AssertReleaseBreakpoint(); \
667 return; \
668 } while (0)
669
670
671/** @def AssertReleaseFailedBreak
672 * An assertion failed, hit a breakpoint and break.
673 *
674 * @param stmt Statement to execute before break.
675 */
676#define AssertReleaseFailedBreak(stmt) \
677 if (1) { \
678 AssertMsg1((const char *)0, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
679 AssertReleaseBreakpoint(); \
680 stmt; \
681 break; \
682 } else do {} while (0)
683
684
685/** @def AssertReleaseMsgFailed
686 * An assertion failed, print a message and hit a breakpoint.
687 *
688 * @param a printf argument list (in parenthesis).
689 */
690#define AssertReleaseMsgFailed(a) \
691 do { \
692 AssertMsg1((const char *)0, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
693 AssertMsg2 a; \
694 AssertReleaseBreakpoint(); \
695 } while (0)
696
697/** @def AssertReleaseMsgFailedReturn
698 * An assertion failed, print a message, hit a breakpoint and return.
699 *
700 * @param a printf argument list (in parenthesis).
701 * @param rc What is to be presented to return.
702 */
703#define AssertReleaseMsgFailedReturn(a, rc) \
704 do { \
705 AssertMsg1((const char *)0, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
706 AssertMsg2 a; \
707 AssertReleaseBreakpoint(); \
708 return (rc); \
709 } while (0)
710
711/** @def AssertReleaseMsgFailedReturnVoid
712 * An assertion failed, print a message, hit a breakpoint and return.
713 *
714 * @param a printf argument list (in parenthesis).
715 */
716#define AssertReleaseMsgFailedReturnVoid(a) \
717 do { \
718 AssertMsg1((const char *)0, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
719 AssertMsg2 a; \
720 AssertReleaseBreakpoint(); \
721 return; \
722 } while (0)
723
724
725/** @def AssertReleaseMsgFailedBreak
726 * An assertion failed, print a message, hit a breakpoint and break.
727 *
728 * @param a printf argument list (in parenthesis).
729 * @param stmt Statement to execute before break.
730 */
731#define AssertReleaseMsgFailedBreak(a, stmt) \
732 if (1) { \
733 AssertMsg1((const char *)0, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
734 AssertMsg2 a; \
735 AssertReleaseBreakpoint(); \
736 stmt; \
737 break; \
738 } else do {} while (0)
739
740
741
742/** @def AssertFatal
743 * Assert that an expression is true. If it's not hit a breakpoint (for ever).
744 *
745 * @param expr Expression which should be true.
746 */
747#define AssertFatal(expr) \
748 do { \
749 if (RT_UNLIKELY(!(expr))) \
750 for (;;) \
751 { \
752 AssertMsg1(#expr, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
753 AssertReleaseBreakpoint(); \
754 } \
755 } while (0)
756
757/** @def AssertFatalMsg
758 * Assert that an expression is true, print the message and hit a breakpoint (for ever) if it isn't.
759 *
760 * @param expr Expression which should be true.
761 * @param a printf argument list (in parenthesis).
762 */
763#define AssertFatalMsg(expr, a) \
764 do { \
765 if (RT_UNLIKELY(!(expr))) \
766 for (;;) \
767 { \
768 AssertMsg1(#expr, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
769 AssertMsg2 a; \
770 AssertReleaseBreakpoint(); \
771 } \
772 } while (0)
773
774/** @def AssertFatalFailed
775 * An assertion failed, hit a breakpoint (for ever).
776 */
777#define AssertFatalFailed() \
778 do { \
779 for (;;) \
780 { \
781 AssertMsg1((const char *)0, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
782 AssertReleaseBreakpoint(); \
783 } \
784 } while (0)
785
786/** @def AssertFatalMsgFailed
787 * An assertion failed, print a message and hit a breakpoint (for ever).
788 *
789 * @param a printf argument list (in parenthesis).
790 */
791#define AssertFatalMsgFailed(a) \
792 do { \
793 for (;;) \
794 { \
795 AssertMsg1((const char *)0, __LINE__, __FILE__, __PRETTY_FUNCTION__); \
796 AssertMsg2 a; \
797 AssertReleaseBreakpoint(); \
798 } \
799 } while (0)
800
801
802/** @def AssertRC
803 * Asserts a iprt status code successful.
804 *
805 * On failure it will print info about the rc and hit a breakpoint.
806 *
807 * @param rc iprt status code.
808 * @remark rc is references multiple times. In release mode is NOREF()'ed.
809 */
810#define AssertRC(rc) AssertMsgRC(rc, ("%Vra\n", (rc)))
811
812/** @def AssertRCReturn
813 * Asserts a iprt status code successful, bitch (RT_STRICT mode only) and return if it isn't.
814 *
815 * @param rc iprt status code.
816 * @param rcRet What is to be presented to return.
817 * @remark rc is references multiple times. In release mode is NOREF()'ed.
818 */
819#define AssertRCReturn(rc, rcRet) AssertMsgRCReturn(rc, ("%Vra\n", (rc)), rcRet)
820
821/** @def AssertRCReturnVoid
822 * Asserts a iprt status code successful, bitch (RT_STRICT mode only) and return if it isn't.
823 *
824 * @param rc iprt status code.
825 * @remark rc is references multiple times. In release mode is NOREF()'ed.
826 */
827#define AssertRCReturnVoid(rc) AssertMsgRCReturnVoid(rc, ("%Vra\n", (rc)))
828
829/** @def AssertRCBreak
830 * Asserts a iprt status code successful, bitch (RT_STRICT mode only) and break if it isn't.
831 *
832 * @param rc iprt status code.
833 * @param stmt Statement to execute before break in case of a failed assertion.
834 * @remark rc is references multiple times. In release mode is NOREF()'ed.
835 */
836#define AssertRCBreak(rc, stmt) AssertMsgRCBreak(rc, ("%Vra\n", (rc)), stmt)
837
838/** @def AssertMsgRC
839 * Asserts a iprt status code successful.
840 *
841 * It prints a custom message and hits a breakpoint on FAILURE.
842 *
843 * @param rc iprt status code.
844 * @param msg printf argument list (in parenthesis).
845 * @remark rc is references multiple times. In release mode is NOREF()'ed.
846 */
847#define AssertMsgRC(rc, msg) \
848 do { AssertMsg(RT_SUCCESS(rc), msg); NOREF(rc); } while (0)
849
850/** @def AssertMsgRCReturn
851 * Asserts a iprt status code successful and if it's not return the specified status code.
852 *
853 * If RT_STRICT is defined the message will be printed and a breakpoint hit before it returns
854 *
855 * @param rc iprt status code.
856 * @param msg printf argument list (in parenthesis).
857 * @param rcRet What is to be presented to return.
858 * @remark rc is references multiple times. In release mode is NOREF()'ed.
859 */
860#define AssertMsgRCReturn(rc, msg, rcRet) \
861 do { AssertMsgReturn(RT_SUCCESS(rc), msg, rcRet); NOREF(rc); } while (0)
862
863/** @def AssertMsgRCReturnVoid
864 * Asserts a iprt status code successful and if it's not return.
865 *
866 * If RT_STRICT is defined the message will be printed and a breakpoint hit before it returns
867 *
868 * @param rc iprt status code.
869 * @param msg printf argument list (in parenthesis).
870 * @remark rc is references multiple times. In release mode is NOREF()'ed.
871 */
872#define AssertMsgRCReturnVoid(rc, msg) \
873 do { AssertMsgReturnVoid(RT_SUCCESS(rc), msg); NOREF(rc); } while (0)
874
875/** @def AssertMsgRCBreak
876 * Asserts a iprt status code successful and break if it's not.
877 *
878 * If RT_STRICT is defined the message will be printed and a breakpoint hit before it returns
879 *
880 * @param rc iprt status code.
881 * @param msg printf argument list (in parenthesis).
882 * @param stmt Statement to execute before break in case of a failed assertion.
883 * @remark rc is references multiple times. In release mode is NOREF()'ed.
884 */
885#define AssertMsgRCBreak(rc, msg, stmt) \
886 do { AssertMsgBreak(RT_SUCCESS(rc), msg, stmt); NOREF(rc); } while (0)
887
888/** @def AssertRCSuccess
889 * Asserts an iprt status code equals VINF_SUCCESS.
890 *
891 * On failure it will print info about the rc and hit a breakpoint.
892 *
893 * @param rc iprt status code.
894 * @remark rc is references multiple times. In release mode is NOREF()'ed.
895 */
896#define AssertRCSuccess(rc) AssertMsg((rc) == VINF_SUCCESS, ("%Vra\n", (rc)))
897
898/** @def AssertRCSuccessReturn
899 * Asserts that an iprt status code equals VINF_SUCCESS, bitch (RT_STRICT mode only) and return if it isn't.
900 *
901 * @param rc iprt status code.
902 * @param rcRet What is to be presented to return.
903 * @remark rc is references multiple times. In release mode is NOREF()'ed.
904 */
905#define AssertRCSuccessReturn(rc, rcRet) AssertMsgReturn((rc) == VINF_SUCCESS, ("%Vra\n", (rc)), rcRet)
906
907/** @def AssertRCSuccessReturnVoid
908 * Asserts that an iprt status code equals VINF_SUCCESS, bitch (RT_STRICT mode only) and return if it isn't.
909 *
910 * @param rc iprt status code.
911 * @remark rc is references multiple times. In release mode is NOREF()'ed.
912 */
913#define AssertRCSuccessReturnVoid(rc) AssertMsgReturnVoid((rc) == VINF_SUCCESS, ("%Vra\n", (rc)))
914
915/** @def AssertRCSuccessBreak
916 * Asserts that an iprt status code equals VINF_SUCCESS, bitch (RT_STRICT mode only) and break if it isn't.
917 *
918 * @param rc iprt status code.
919 * @param stmt Statement to execute before break in case of a failed assertion.
920 * @remark rc is references multiple times. In release mode is NOREF()'ed.
921 */
922#define AssertRCSuccessBreak(rc, stmt) AssertMsgBreak((rc) == VINF_SUCCESS, ("%Vra\n", (rc)), stmt)
923
924
925/** @def AssertReleaseRC
926 * Asserts a iprt status code successful.
927 *
928 * On failure information about the error will be printed and a breakpoint hit.
929 *
930 * @param rc iprt status code.
931 * @remark rc is references multiple times.
932 */
933#define AssertReleaseRC(rc) AssertReleaseMsgRC(rc, ("%Vra\n", (rc)))
934
935/** @def AssertReleaseRCReturn
936 * Asserts a iprt status code successful, returning if it isn't.
937 *
938 * On failure information about the error will be printed, a breakpoint hit
939 * and finally returning from the function if the breakpoint is somehow ignored.
940 *
941 * @param rc iprt status code.
942 * @param rcRet What is to be presented to return.
943 * @remark rc is references multiple times.
944 */
945#define AssertReleaseRCReturn(rc, rcRet) AssertReleaseMsgRCReturn(rc, ("%Vra\n", (rc)), rcRet)
946
947/** @def AssertReleaseRCReturnVoid
948 * Asserts a iprt status code successful, returning if it isn't.
949 *
950 * On failure information about the error will be printed, a breakpoint hit
951 * and finally returning from the function if the breakpoint is somehow ignored.
952 *
953 * @param rc iprt status code.
954 * @remark rc is references multiple times.
955 */
956#define AssertReleaseRCReturnVoid(rc) AssertReleaseMsgRCReturnVoid(rc, ("%Vra\n", (rc)))
957
958/** @def AssertReleaseRCBreak
959 * Asserts a iprt status code successful, break if it isn't.
960 *
961 * On failure information about the error will be printed, a breakpoint hit
962 * and finally the break statement will be issued if the breakpoint is somehow ignored.
963 *
964 * @param rc iprt status code.
965 * @param stmt Statement to execute before break in case of a failed assertion.
966 * @remark rc is references multiple times.
967 */
968#define AssertReleaseRCBreak(rc, stmt) AssertReleaseMsgRCBreak(rc, ("%Vra\n", (rc)), stmt)
969
970/** @def AssertReleaseMsgRC
971 * Asserts a iprt status code successful.
972 *
973 * On failure a custom message is printed and a breakpoint is hit.
974 *
975 * @param rc iprt status code.
976 * @param msg printf argument list (in parenthesis).
977 * @remark rc is references multiple times.
978 */
979#define AssertReleaseMsgRC(rc, msg) AssertReleaseMsg(RT_SUCCESS(rc), msg)
980
981/** @def AssertReleaseMsgRCReturn
982 * Asserts a iprt status code successful.
983 *
984 * On failure a custom message is printed, a breakpoint is hit, and finally
985 * returning from the function if the breakpoint is showhow ignored.
986 *
987 * @param rc iprt status code.
988 * @param msg printf argument list (in parenthesis).
989 * @param rcRet What is to be presented to return.
990 * @remark rc is references multiple times.
991 */
992#define AssertReleaseMsgRCReturn(rc, msg, rcRet) AssertReleaseMsgReturn(RT_SUCCESS(rc), msg, rcRet)
993
994/** @def AssertReleaseMsgRCReturnVoid
995 * Asserts a iprt status code successful.
996 *
997 * On failure a custom message is printed, a breakpoint is hit, and finally
998 * returning from the function if the breakpoint is showhow ignored.
999 *
1000 * @param rc iprt status code.
1001 * @param msg printf argument list (in parenthesis).
1002 * @remark rc is references multiple times.
1003 */
1004#define AssertReleaseMsgRCReturnVoid(rc, msg) AssertReleaseMsgReturnVoid(RT_SUCCESS(rc), msg)
1005
1006/** @def AssertReleaseMsgRCBreak
1007 * Asserts a iprt status code successful.
1008 *
1009 * On failure a custom message is printed, a breakpoint is hit, and finally
1010 * the brean statement is issued if the breakpoint is showhow ignored.
1011 *
1012 * @param rc iprt status code.
1013 * @param msg printf argument list (in parenthesis).
1014 * @param stmt Statement to execute before break in case of a failed assertion.
1015 * @remark rc is references multiple times.
1016 */
1017#define AssertReleaseMsgRCBreak(rc, msg, stmt) AssertReleaseMsgBreak(RT_SUCCESS(rc), msg, stmt)
1018
1019/** @def AssertReleaseRCSuccess
1020 * Asserts that an iprt status code equals VINF_SUCCESS.
1021 *
1022 * On failure information about the error will be printed and a breakpoint hit.
1023 *
1024 * @param rc iprt status code.
1025 * @remark rc is references multiple times.
1026 */
1027#define AssertReleaseRCSuccess(rc) AssertReleaseMsg((rc) == VINF_SUCCESS, ("%Vra\n", (rc)))
1028
1029/** @def AssertReleaseRCSuccessReturn
1030 * Asserts that an iprt status code equals VINF_SUCCESS.
1031 *
1032 * On failure information about the error will be printed, a breakpoint hit
1033 * and finally returning from the function if the breakpoint is somehow ignored.
1034 *
1035 * @param rc iprt status code.
1036 * @param rcRet What is to be presented to return.
1037 * @remark rc is references multiple times.
1038 */
1039#define AssertReleaseRCSuccessReturn(rc, rcRet) AssertReleaseMsgReturn((rc) == VINF_SUCCESS, ("%Vra\n", (rc)), rcRet)
1040
1041/** @def AssertReleaseRCSuccessReturnVoid
1042 * Asserts that an iprt status code equals VINF_SUCCESS.
1043 *
1044 * On failure information about the error will be printed, a breakpoint hit
1045 * and finally returning from the function if the breakpoint is somehow ignored.
1046 *
1047 * @param rc iprt status code.
1048 * @remark rc is references multiple times.
1049 */
1050#define AssertReleaseRCSuccessReturnVoid(rc) AssertReleaseMsgReturnVoid((rc) == VINF_SUCCESS, ("%Vra\n", (rc)))
1051
1052/** @def AssertReleaseRCSuccessBreak
1053 * Asserts that an iprt status code equals VINF_SUCCESS.
1054 *
1055 * On failure information about the error will be printed, a breakpoint hit
1056 * and finally the break statement will be issued if the breakpoint is somehow ignored.
1057 *
1058 * @param rc iprt status code.
1059 * @param stmt Statement to execute before break in case of a failed assertion.
1060 * @remark rc is references multiple times.
1061 */
1062#define AssertReleaseRCSuccessBreak(rc, stmt) AssertReleaseMsgBreak((rc) == VINF_SUCCESS, ("%Vra\n", (rc)), stmt)
1063
1064
1065/** @def AssertFatalRC
1066 * Asserts a iprt status code successful.
1067 *
1068 * On failure information about the error will be printed and a breakpoint hit.
1069 *
1070 * @param rc iprt status code.
1071 * @remark rc is references multiple times.
1072 */
1073#define AssertFatalRC(rc) AssertFatalMsgRC(rc, ("%Vra\n", (rc)))
1074
1075/** @def AssertReleaseMsgRC
1076 * Asserts a iprt status code successful.
1077 *
1078 * On failure a custom message is printed and a breakpoint is hit.
1079 *
1080 * @param rc iprt status code.
1081 * @param msg printf argument list (in parenthesis).
1082 * @remark rc is references multiple times.
1083 */
1084#define AssertFatalMsgRC(rc, msg) AssertFatalMsg(RT_SUCCESS(rc), msg)
1085
1086/** @def AssertFatalRCSuccess
1087 * Asserts that an iprt status code equals VINF_SUCCESS.
1088 *
1089 * On failure information about the error will be printed and a breakpoint hit.
1090 *
1091 * @param rc iprt status code.
1092 * @remark rc is references multiple times.
1093 */
1094#define AssertFatalRCSuccess(rc) AssertFatalMsg((rc) == VINF_SUCCESS, ("%Vra\n", (rc)))
1095
1096
1097/** @def AssertPtr
1098 * Asserts that a pointer is valid.
1099 *
1100 * @param pv The pointer.
1101 */
1102#define AssertPtr(pv) AssertMsg(VALID_PTR(pv), ("%p\n", (pv)))
1103
1104/** @def AssertPtrReturn
1105 * Asserts that a pointer is valid.
1106 *
1107 * @param pv The pointer.
1108 * @param rcRet What is to be presented to return.
1109 */
1110#define AssertPtrReturn(pv, rcRet) AssertMsgReturn(VALID_PTR(pv), ("%p\n", (pv)), rcRet)
1111
1112/** @def AssertPtrReturnVoid
1113 * Asserts that a pointer is valid.
1114 *
1115 * @param pv The pointer.
1116 */
1117#define AssertPtrReturnVoid(pv) AssertMsgReturnVoid(VALID_PTR(pv), ("%p\n", (pv)))
1118
1119/** @def AssertPtrBreak
1120 * Asserts that a pointer is valid.
1121 *
1122 * @param pv The pointer.
1123 * @param stmt Statement to execute before break in case of a failed assertion.
1124 */
1125#define AssertPtrBreak(pv, stmt) AssertMsgBreak(VALID_PTR(pv), ("%p\n", (pv)), stmt)
1126
1127/** @def AssertPtrNull
1128 * Asserts that a pointer is valid or NULL.
1129 *
1130 * @param pv The pointer.
1131 */
1132#define AssertPtrNull(pv) AssertMsg(VALID_PTR(pv) || (pv) == NULL, ("%p\n", (pv)))
1133
1134/** @def AssertPtrNullReturn
1135 * Asserts that a pointer is valid or NULL.
1136 *
1137 * @param pv The pointer.
1138 * @param rcRet What is to be presented to return.
1139 */
1140#define AssertPtrNullReturn(pv, rcRet) AssertMsgReturn(VALID_PTR(pv) || (pv) == NULL, ("%p\n", (pv)), rcRet)
1141
1142/** @def AssertPtrNullReturnVoid
1143 * Asserts that a pointer is valid or NULL.
1144 *
1145 * @param pv The pointer.
1146 */
1147#define AssertPtrNullReturnVoid(pv) AssertMsgReturnVoid(VALID_PTR(pv) || (pv) == NULL, ("%p\n", (pv)))
1148
1149/** @def AssertPtrNullBreak
1150 * Asserts that a pointer is valid or NULL.
1151 *
1152 * @param pv The pointer.
1153 * @param stmt Statement to execute before break in case of a failed assertion.
1154 */
1155#define AssertPtrNullBreak(pv, stmt) AssertMsgBreak(VALID_PTR(pv) || (pv) == NULL, ("%p\n", (pv)), stmt)
1156
1157
1158__BEGIN_DECLS
1159
1160/**
1161 * The 1st part of an assert message.
1162 *
1163 * @param pszExpr Expression. Can be NULL.
1164 * @param uLine Location line number.
1165 * @param pszFile Location file name.
1166 * @param pszFunction Location function name.
1167 * @remark This API exists in HC Ring-3 and GC.
1168 */
1169RTDECL(void) AssertMsg1(const char *pszExpr, unsigned uLine, const char *pszFile, const char *pszFunction);
1170
1171/**
1172 * The 2nd (optional) part of an assert message.
1173 * @param pszFormat Printf like format string.
1174 * @param ... Arguments to that string.
1175 * @remark This API exists in HC Ring-3 and GC.
1176 */
1177RTDECL(void) AssertMsg2(const char *pszFormat, ...);
1178
1179/**
1180 * Overridable function that decides whether assertions executes the breakpoint or not.
1181 *
1182 * The generic implementation will return true.
1183 *
1184 * @returns true if the breakpoint should be hit, false if it should be ignored.
1185 * @remark The RTDECL() makes this a bit difficult to override on windows. Sorry.
1186 */
1187RTDECL(bool) RTAssertDoBreakpoint(void);
1188
1189
1190/** The last assert message, 1st part. */
1191extern RTDATADECL(char) g_szRTAssertMsg1[1024];
1192/** The last assert message, 2nd part. */
1193extern RTDATADECL(char) g_szRTAssertMsg2[2048];
1194
1195__END_DECLS
1196
1197/** @} */
1198
1199#endif
1200
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette