1 /*
2  *  Copyright (c) 2016, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  *   This file includes functions for debugging.
32  */
33 
34 #ifndef DEBUG_HPP_
35 #define DEBUG_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #include <ctype.h>
40 #include <stdio.h>
41 
42 #if OPENTHREAD_CONFIG_ASSERT_ENABLE
43 
44 #if OPENTHREAD_CONFIG_PLATFORM_ASSERT_MANAGEMENT
45 
46 #include "openthread/platform/misc.h"
47 
48 /**
49  * Allow the build system to provide a custom file name.
50  *
51  */
52 #ifndef FILE_NAME
53 #define FILE_NAME __FILE__
54 #endif
55 
56 #define OT_ASSERT(cond)                            \
57     do                                             \
58     {                                              \
59         if (!(cond))                               \
60         {                                          \
61             otPlatAssertFail(FILE_NAME, __LINE__); \
62             while (1)                              \
63             {                                      \
64             }                                      \
65         }                                          \
66     } while (0)
67 
68 #elif defined(__APPLE__) || defined(__linux__)
69 
70 #include <assert.h>
71 
72 #define OT_ASSERT(cond) assert(cond)
73 
74 #else // OPENTHREAD_CONFIG_PLATFORM_ASSERT_MANAGEMENT
75 
76 #define OT_ASSERT(cond) \
77     do                  \
78     {                   \
79         if (!(cond))    \
80         {               \
81             while (1)   \
82             {           \
83             }           \
84         }               \
85     } while (0)
86 
87 #endif // OPENTHREAD_CONFIG_PLATFORM_ASSERT_MANAGEMENT
88 
89 #else // OPENTHREAD_CONFIG_ASSERT_ENABLE
90 
91 #define OT_ASSERT(cond)
92 
93 #endif // OPENTHREAD_CONFIG_ASSERT_ENABLE
94 
95 /**
96  * Checks a given status (which is expected to be successful) against zero (0) which indicates success,
97  * and `OT_ASSERT()` if it is not.
98  *
99  * @param[in]  aStatus     A scalar status to be evaluated against zero (0).
100  *
101  */
102 #define SuccessOrAssert(aStatus) \
103     do                           \
104     {                            \
105         if ((aStatus) != 0)      \
106         {                        \
107             OT_ASSERT(false);    \
108         }                        \
109     } while (false)
110 
111 /**
112  * @def AssertPointerIsNotNull
113  *
114  * Asserts that a given pointer (API input parameter) is not `nullptr`. This macro checks the pointer only
115  * when `OPENTHREAD_CONFIG_ASSERT_CHECK_API_POINTER_PARAM_FOR_NULL` is enabled. Otherwise it is an empty macro.
116  *
117  * @param[in]  aPointer   The pointer variable (API input parameter) to check.
118  *
119  */
120 #if OPENTHREAD_CONFIG_ASSERT_CHECK_API_POINTER_PARAM_FOR_NULL
121 #define AssertPointerIsNotNull(aPointer) OT_ASSERT((aPointer) != nullptr)
122 #else
123 #define AssertPointerIsNotNull(aPointer)
124 #endif
125 
126 #endif // DEBUG_HPP_
127