• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef FILE_NAME
52 #define FILE_NAME __FILE__
53 #endif
54 
55 #define OT_ASSERT(cond)                            \
56     do                                             \
57     {                                              \
58         if (!(cond))                               \
59         {                                          \
60             otPlatAssertFail(FILE_NAME, __LINE__); \
61             while (1)                              \
62             {                                      \
63             }                                      \
64         }                                          \
65     } while (0)
66 
67 #elif defined(__APPLE__) || defined(__linux__)
68 
69 #include <assert.h>
70 
71 #define OT_ASSERT(cond) assert(cond)
72 
73 #else // OPENTHREAD_CONFIG_PLATFORM_ASSERT_MANAGEMENT
74 
75 #define OT_ASSERT(cond) \
76     do                  \
77     {                   \
78         if (!(cond))    \
79         {               \
80             while (1)   \
81             {           \
82             }           \
83         }               \
84     } while (0)
85 
86 #endif // OPENTHREAD_CONFIG_PLATFORM_ASSERT_MANAGEMENT
87 
88 #else // OPENTHREAD_CONFIG_ASSERT_ENABLE
89 
90 #define OT_ASSERT(cond)
91 
92 #endif // OPENTHREAD_CONFIG_ASSERT_ENABLE
93 
94 /**
95  * Checks a given status (which is expected to be successful) against zero (0) which indicates success,
96  * and `OT_ASSERT()` if it is not.
97  *
98  * @param[in]  aStatus     A scalar status to be evaluated against zero (0).
99  */
100 #define SuccessOrAssert(aStatus) \
101     do                           \
102     {                            \
103         if ((aStatus) != 0)      \
104         {                        \
105             OT_ASSERT(false);    \
106         }                        \
107     } while (false)
108 
109 /**
110  * @def AssertPointerIsNotNull
111  *
112  * Asserts that a given pointer (API input parameter) is not `nullptr`. This macro checks the pointer only
113  * when `OPENTHREAD_CONFIG_ASSERT_CHECK_API_POINTER_PARAM_FOR_NULL` is enabled. Otherwise it is an empty macro.
114  *
115  * @param[in]  aPointer   The pointer variable (API input parameter) to check.
116  */
117 #if OPENTHREAD_CONFIG_ASSERT_CHECK_API_POINTER_PARAM_FOR_NULL
118 #define AssertPointerIsNotNull(aPointer) OT_ASSERT((aPointer) != nullptr)
119 #else
120 #define AssertPointerIsNotNull(aPointer)
121 #endif
122 
123 #endif // DEBUG_HPP_
124