aboutsummaryrefslogtreecommitdiffstats
path: root/longbow/src/LongBow/traps.h
blob: e6c68d6541cf473d7f03c60064781d49ff656f77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file traps.h
 * @ingroup runtime
 * @brief Runtime and Test Traps
 *
 */
#ifndef LongBow_traps_h
#define LongBow_traps_h

/**
 * @def trapUnrecoverableState
 * @brief Lay a trap to report an unrecoverable state in program execution.
 *
 * @param ... A printf(3) format string of explanitory text and values
 */
#define trapUnrecoverableState(...) \
    longBowTrap(&LongBowTrapUnrecoverableState, "Unrecoverable State: " __VA_ARGS__)

/**
 * @def trapNotImplemented
 * @brief Lay a trap to report and abort an unimplemented capability.
 *
 * @param ... A printf(3) format string of explanitory text and values
 */
#define trapNotImplemented(...) \
    longBowTrap(&LongBowTrapNotImplemented, "Feature not implemented: " __VA_ARGS__)

/**
 * @def trapIllegalValue
 * @brief Trap an illegal value.
 *
 * To used for capturing parameter validation, for example.
 *
 * @param [in] argumentName A name that is displayed in the report message.
 * @param [in] ... A printf format string and associated parameters.
 */
#define trapIllegalValue(argumentName, ...) \
    longBowTrap(&LongBowTrapIllegalValue, "Illegal value for '" #argumentName "': " __VA_ARGS__)

/**
 * @def trapIllegalValueIf
 * @brief Trap an illegal value if a condition is met.
 *
 * To used for the case where the value is valid
 * (eg. it is the correct type) but is an illegal value to be used in the context in which it was attempted to be applied.
 *
 * @param [in] condition A logical expression that if true, induces this trap.
 * @param [in] ... A printf format string and associated parameters.
 */
#define trapIllegalValueIf(condition, ...) \
    longBowTrapIf(&LongBowTrapIllegalValue, condition, "Illegal value: " __VA_ARGS__)

/**
 * @def trapInvalidValueIf
 * @brief Trap an invalid value if a condition is met.
 *
 * Used for capturing parameter validation.
 * For example, a composite value (C struct) has an internally invalid state for example.
 *
 * @param [in] condition A logical expression that if true, induces this trap.
 * @param [in] ... A printf format string and associated parameters.
 */
#define trapInvalidValueIf(condition, ...) \
    longBowTrapIf(&LongBowTrapInvalidValue, condition, "Invalid value: " __VA_ARGS__)

/**
 * @def trapOutOfBounds
 * @brief Trap an out-of-bounds condition on an index.
 *
 * @param [in] index The index that is out of bounds.
 * @param [in] ... A printf format string and associated parameters.
 */
#define trapOutOfBounds(index, ...) \
    longBowTrap(&LongBowTrapOutOfBounds, "Element out of bounds, " #index ": " __VA_ARGS__)

/**
 * @def trapOutOfBoundsIf
 * @brief Trap an out-of-bounds condition for a specific value.
 *
 * @param [in] condition A logical expression that if true, induces this trap.
 * @param [in] ... A printf format string and associated parameters.
 */
#define trapOutOfBoundsIf(condition, ...) \
    longBowTrapIf(&LongBowTrapOutOfBounds, condition, "Out of bounds: " __VA_ARGS__)

/**
 * @def trapOutOfMemory
 * @brief Signal that no more memory could be allocated.
 *
 * @param ... A printf format string and accompanying parameters.
 */
#define trapOutOfMemory(...) \
    longBowTrap(&LongBowTrapOutOfMemoryEvent, "Out of memory. " __VA_ARGS__)

/**
 * @def trapOutOfMemoryIf
 * @brief  Signal that no more memory could be allocated.
 *
 * @param [in] condition A logical expression that if true, induces this trap.
 * @param [in] ... A printf format string and associated parameters.
 */
#define trapOutOfMemoryIf(condition, ...) \
    longBowTrapIf(&LongBowTrapOutOfMemoryEvent, condition, "Out of memory. " __VA_ARGS__)

/**
 * @def trapUnexpectedState
 * @brief Signal that an unexpected or inconsistent state was encountered.
 *
 * @param ... A printf format string and accompanying parameters.
 */
#define trapUnexpectedState(...) \
    longBowTrap(&LongBowTrapUnexpectedStateEvent, "Unexpected state. " __VA_ARGS__)

/**
 * @def trapUnexpectedStateIf
 * @brief If the given condition is true, signal that an unexpected state was encountered .
 *
 * @param [in] condition A logical expression that if true, induces this trap.
 * @param [in] ... A printf format string and associated parameters.
 */
#define trapUnexpectedStateIf(condition, ...) \
    longBowTrapIf(&LongBowTrapUnexpectedStateEvent, condition, "Unexpected state: " __VA_ARGS__)

/**
 * @def trapCoreDump
 * @brief Send a SIGTRAP to the current process.
 */
#define trapCoreDump() \
    kill(0, SIGTRAP);

/**
 * @def trapCannotObtainLock
 * @brief Signal that a lock could not be obtained.
 *
 * @param ... A printf format string and accompanying parameters.
 */
#define trapCannotObtainLock(...) \
    longBowTrap(&LongBowTrapCannotObtainLockEvent, "Cannot obtain lock " __VA_ARGS__)

/**
 * @def trapCannotObtainLock
 * @brief Signal that a lock could not be obtained.
 *
 * @param ... A printf format string and accompanying parameters.
 */
#define trapCannotObtainLockIf(condition, ...) \
    longBowTrapIf(&LongBowTrapCannotObtainLockEvent, condition, "Cannot obtain lock " __VA_ARGS__)

#endif