VirtualBox

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

Last change on this file since 1224 was 1208, checked in by vboxsync, 18 years ago

gcc-4.1.2 of Mandriva2007.1 complains about mixed C/C++ declarations of RTASSERTVAR[]. This workaround compiles fine here with gcc-3.4.6 and 4.1.2 of Debian.

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