Repository navigation
Expand file tree
/
Copy pathDebug.h
More file actions
477 lines (407 loc) · 30.4 KB
/
Copy pathDebug.h
File metadata and controls
477 lines (407 loc) · 30.4 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/**
******************************************************************************
* @file Debug.h
* @author William Xu
* @version V1.0.0
* @date 05-May-2014
* @brief This header contains defines, macros, and functions to aid in
debugging the MICO project.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, MXCHIP Inc. SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2014 MXCHIP Inc.</center></h2>
******************************************************************************
*/
#ifndef __Debug_h__
#define __Debug_h__
#include "MicoRTOS.h"
#include "MicoDefaults.h"
#include "platform.h"
#include "platform_assert.h"
// ==== LOGGING ====
#define SHORT_FILE strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__
#define YesOrNo(x) (x ? "YES" : "NO")
#if DEBUG
#ifndef MICO_DISABLE_STDIO
#ifndef NO_MICO_RTOS
extern int mico_debug_enabled;
extern mico_mutex_t stdio_tx_mutex;
#define custom_log(N, M, ...) do {if (mico_debug_enabled==0)break;\
mico_rtos_lock_mutex( &stdio_tx_mutex );\
printf("[%d][%s: %s:%4d] " M "\r\n", mico_get_time(), N, SHORT_FILE, __LINE__, ##__VA_ARGS__);\
mico_rtos_unlock_mutex( &stdio_tx_mutex );}while(0==1)
#define debug_print_assert(A,B,C,D,E,F, ...) do {if (mico_debug_enabled==0)break;\
mico_rtos_lock_mutex( &stdio_tx_mutex );\
printf("[%d][MICO:%s:%s:%4d] **ASSERT** %s""\r\n", mico_get_time(), (D!=NULL) ? D : "", F, E, (C!=NULL) ? C : "", ##__VA_ARGS__);\
mico_rtos_unlock_mutex( &stdio_tx_mutex );}while(0==1)
#if TRACE
#define custom_log_trace(N) do {if (mico_debug_enabled==0)break;\
mico_rtos_lock_mutex( &stdio_tx_mutex );\
printf("[%s: [TRACE] %s] %s()\r\n", N, SHORT_FILE, __PRETTY_FUNCTION__);\
mico_rtos_unlock_mutex( &stdio_tx_mutex );}while(0==1)
#else // !TRACE
#define custom_log_trace(N)
#endif // TRACE
#else // NO_MICO_RTOS
#define custom_log(N, M, ...) do {printf("[%s: %s:%4d] " M "\r\n", N, SHORT_FILE, __LINE__, ##__VA_ARGS__);}while(0==1)
#define debug_print_assert(A,B,C,D,E,F, ...) do {printf("[MICO:%s:%s:%4d] **ASSERT** %s""\r\n", (D!=NULL) ? D : "", F, E, (C!=NULL) ? C : "", ##__VA_ARGS__);}while(0==1)
#if TRACE
#define custom_log_trace(N) do {printf("[%s: [TRACE] %s] %s()\r\n", N, SHORT_FILE, __PRETTY_FUNCTION__);}while(0==1)
#else // !TRACE
#define custom_log_trace(N)
#endif // TRACE
#endif
#else
#define custom_log(N, M, ...)
#define custom_log_trace(N)
#define debug_print_assert(A,B,C,D,E,F, ...)
#endif //MICO_DISABLE_STDIO
#else // DEBUG = 0
// IF !DEBUG, make the logs NO-OP
#define custom_log(N, M, ...)
#define custom_log_trace(N)
#define debug_print_assert(A,B,C,D,E,F, ...)
#endif // DEBUG
// ==== PLATFORM TIMEING FUNCTIONS ====
#if TIME_PLATFORM
#define function_timer_log(M, N, ...) fprintf(stderr, "[FUNCTION TIMER: " N "()] " M "\n", ##__VA_ARGS__)
#define TIMEPLATFORM( FUNC, FUNC_NAME ) \
do \
{ \
struct timespec startTime; \
clock_gettime(CLOCK_MONOTONIC, &startTime); \
{ FUNC; } \
struct timespec endTime; \
clock_gettime(CLOCK_MONOTONIC, &endTime); \
struct timespec timeDiff = TimeDifference( startTime, endTime ); \
function_timer_log("%lld us", \
FUNC_NAME, \
ElapsedTimeInMicroseconds( timeDiff )); \
} \
while( 1==0 )
#else
#define function_timer_log(M, N, ...)
#define TIMEPLATFORM( FUNC, FUNC_NAME ) \
do \
{ \
{ FUNC; } \
} \
while( 1==0 )
#endif
// ==== BRANCH PREDICTION & EXPRESSION EVALUATION ====
#if( !defined( unlikely ) )
//#define unlikely( EXPRESSSION ) __builtin_expect( !!(EXPRESSSION), 0 )
#define unlikely( EXPRESSSION ) !!(EXPRESSSION)
#endif
//---------------------------------------------------------------------------------------------------------------------------
/*! @defined check
@abstract Check that an expression is true (non-zero).
@discussion
If expression evalulates to false, this prints debugging information (actual expression string, file, line number,
function name, etc.) using the default debugging output method.
Code inside check() statements is not compiled into production builds.
*/
#if( !defined( check ) )
#define check( X ) \
do \
{ \
if( unlikely( !(X) ) ) \
{ \
debug_print_assert( 0, #X, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__ ); \
} \
\
} while( 1==0 )
#endif
//---------------------------------------------------------------------------------------------------------------------------
/*! @defined check_string
@abstract Check that an expression is true (non-zero) with an explanation.
@discussion
If expression evalulates to false, this prints debugging information (actual expression string, file, line number,
function name, etc.) using the default debugging output method.
Code inside check() statements is not compiled into production builds.
*/
#if( !defined( check_string ) )
#define check_string( X, STR ) \
do \
{ \
if( unlikely( !(X) ) ) \
{ \
debug_print_assert( 0, #X, STR, __FILE__, __LINE__, __PRETTY_FUNCTION__ ); \
MICO_ASSERTION_FAIL_ACTION(); \
} \
\
} while( 1==0 )
#endif
//---------------------------------------------------------------------------------------------------------------------------
/*! @defined require
@abstract Requires that an expression evaluate to true.
@discussion
If expression evalulates to false, this prints debugging information (actual expression string, file, line number,
function name, etc.) using the default debugging output method then jumps to a label.
*/
#if( !defined( require ) )
#define require( X, LABEL ) \
do \
{ \
if( unlikely( !(X) ) ) \
{ \
debug_print_assert( 0, #X, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__ ); \
goto LABEL; \
} \
\
} while( 1==0 )
#endif
//---------------------------------------------------------------------------------------------------------------------------
/*! @defined require_string
@abstract Requires that an expression evaluate to true with an explanation.
@discussion
If expression evalulates to false, this prints debugging information (actual expression string, file, line number,
function name, etc.) and a custom explanation string using the default debugging output method then jumps to a label.
*/
#if( !defined( require_string ) )
#define require_string( X, LABEL, STR ) \
do \
{ \
if( unlikely( !(X) ) ) \
{ \
debug_print_assert( 0, #X, STR, __FILE__, __LINE__, __PRETTY_FUNCTION__ ); \
goto LABEL; \
} \
\
} while( 1==0 )
#endif
//---------------------------------------------------------------------------------------------------------------------------
/*! @defined require_quiet
@abstract Requires that an expression evaluate to true.
@discussion
If expression evalulates to false, this jumps to a label. No debugging information is printed.
*/
#if( !defined( require_quiet ) )
#define require_quiet( X, LABEL ) \
do \
{ \
if( unlikely( !(X) ) ) \
{ \
goto LABEL; \
} \
\
} while( 1==0 )
#endif
//---------------------------------------------------------------------------------------------------------------------------
/*! @defined require_noerr
@abstract Require that an error code is noErr (0).
@discussion
If the error code is non-0, this prints debugging information (actual expression string, file, line number,
function name, etc.) using the default debugging output method then jumps to a label.
*/
#if( !defined( require_noerr ) )
#define require_noerr( ERR, LABEL ) \
do \
{ \
OSStatus localErr; \
\
localErr = (OSStatus)(ERR); \
if( unlikely( localErr != 0 ) ) \
{ \
debug_print_assert( localErr, NULL, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__ ); \
goto LABEL; \
} \
\
} while( 1==0 )
#endif
//---------------------------------------------------------------------------------------------------------------------------
/*! @defined require_noerr_string
@abstract Require that an error code is noErr (0).
@discussion
If the error code is non-0, this prints debugging information (actual expression string, file, line number,
function name, etc.), and a custom explanation string using the default debugging output method using the
default debugging output method then jumps to a label.
*/
#if( !defined( require_noerr_string ) )
#define require_noerr_string( ERR, LABEL, STR ) \
do \
{ \
OSStatus localErr; \
\
localErr = (OSStatus)(ERR); \
if( unlikely( localErr != 0 ) ) \
{ \
debug_print_assert( localErr, NULL, STR, __FILE__, __LINE__, __PRETTY_FUNCTION__ ); \
goto LABEL; \
} \
\
} while( 1==0 )
#endif
//---------------------------------------------------------------------------------------------------------------------------
/*! @defined require_noerr_action_string
@abstract Require that an error code is noErr (0).
@discussion
If the error code is non-0, this prints debugging information (actual expression string, file, line number,
function name, etc.), and a custom explanation string using the default debugging output method using the
default debugging output method then executes an action and jumps to a label.
*/
#if( !defined( require_noerr_action_string ) )
#define require_noerr_action_string( ERR, LABEL, ACTION, STR ) \
do \
{ \
OSStatus localErr; \
\
localErr = (OSStatus)(ERR); \
if( unlikely( localErr != 0 ) ) \
{ \
debug_print_assert( localErr, NULL, STR, __FILE__, __LINE__, __PRETTY_FUNCTION__ ); \
{ ACTION; } \
goto LABEL; \
} \
\
} while( 1==0 )
#endif
//---------------------------------------------------------------------------------------------------------------------------
/*! @defined require_noerr_quiet
@abstract Require that an error code is noErr (0).
@discussion
If the error code is non-0, this jumps to a label. No debugging information is printed.
*/
#if( !defined( require_noerr_quiet ) )
#define require_noerr_quiet( ERR, LABEL ) \
do \
{ \
if( unlikely( (ERR) != 0 ) ) \
{ \
goto LABEL; \
} \
\
} while( 1==0 )
#endif
//---------------------------------------------------------------------------------------------------------------------------
/*! @defined require_noerr_action
@abstract Require that an error code is noErr (0) with an action to execute otherwise.
@discussion
If the error code is non-0, this prints debugging information (actual expression string, file, line number,
function name, etc.) using the default debugging output method then executes an action and jumps to a label.
*/
#if( !defined( require_noerr_action ) )
#define require_noerr_action( ERR, LABEL, ACTION ) \
do \
{ \
OSStatus localErr; \
\
localErr = (OSStatus)(ERR); \
if( unlikely( localErr != 0 ) ) \
{ \
debug_print_assert( localErr, NULL, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__ ); \
{ ACTION; } \
goto LABEL; \
} \
\
} while( 1==0 )
#endif
//---------------------------------------------------------------------------------------------------------------------------
/*! @defined require_noerr_action_quiet
@abstract Require that an error code is noErr (0) with an action to execute otherwise.
@discussion
If the error code is non-0, this executes an action and jumps to a label. No debugging information is printed.
*/
#if( !defined( require_noerr_action_quiet ) )
#define require_noerr_action_quiet( ERR, LABEL, ACTION ) \
do \
{ \
if( unlikely( (ERR) != 0 ) ) \
{ \
{ ACTION; } \
goto LABEL; \
} \
\
} while( 1==0 )
#endif
//---------------------------------------------------------------------------------------------------------------------------
/*! @defined require_action
@abstract Requires that an expression evaluate to true with an action to execute otherwise.
@discussion
If expression evalulates to false, this prints debugging information (actual expression string, file, line number,
function name, etc.) using the default debugging output method then executes an action and jumps to a label.
*/
#if( !defined( require_action ) )
#define require_action( X, LABEL, ACTION ) \
do \
{ \
if( unlikely( !(X) ) ) \
{ \
debug_print_assert( 0, #X, NULL, __FILE__, __LINE__, __PRETTY_FUNCTION__ ); \
{ ACTION; } \
goto LABEL; \
} \
\
} while( 1==0 )
#endif
//---------------------------------------------------------------------------------------------------------------------------
/*! @defined require_action_string
@abstract Requires that an expression evaluate to true with an explanation and action to execute otherwise.
@discussion
If expression evalulates to false, this prints debugging information (actual expression string, file, line number,
function name, etc.) and a custom explanation string using the default debugging output method then executes an
action and jumps to a label.
*/
#if( !defined( require_action_string ) )
#define require_action_string( X, LABEL, ACTION, STR ) \
do \
{ \
if( unlikely( !(X) ) ) \
{ \
debug_print_assert( 0, #X, STR, __FILE__, __LINE__, __PRETTY_FUNCTION__ ); \
{ ACTION; } \
goto LABEL; \
} \
\
} while( 1==0 )
#endif
//---------------------------------------------------------------------------------------------------------------------------
/*! @defined require_action_quiet
@abstract Requires that an expression evaluate to true with an action to execute otherwise.
@discussion
If expression evalulates to false, this executes an action and jumps to a label. No debugging information is printed.
*/
#if( !defined( require_action_quiet ) )
#define require_action_quiet( X, LABEL, ACTION ) \
do \
{ \
if( unlikely( !(X) ) ) \
{ \
{ ACTION; } \
goto LABEL; \
} \
\
} while( 1==0 )
#endif
// ==== ERROR MAPPING ====
#define global_value_errno( VALUE ) ( errno ? errno : kUnknownErr )
#define map_global_value_errno( TEST, VALUE ) ( (TEST) ? 0 : global_value_errno(VALUE) )
#define map_global_noerr_errno( ERR ) ( !(ERR) ? 0 : global_value_errno(ERR) )
#define map_fd_creation_errno( FD ) ( IsValidFD( FD ) ? 0 : global_value_errno( FD ) )
#define map_noerr_errno( ERR ) map_global_noerr_errno( (ERR) )
#define socket_errno( SOCK ) ( errno ? errno : kUnknownErr )
#define socket_value_errno( SOCK, VALUE ) socket_errno( SOCK )
#define map_socket_value_errno( SOCK, TEST, VALUE ) ( (TEST) ? 0 : socket_value_errno( (SOCK), (VALUE) ) )
#define map_socket_noerr_errno( SOCK, ERR ) ( !(ERR) ? 0 : socket_errno( (SOCK) ) )
//---------------------------------------------------------------------------------------------------------------------------
/*! @defined check_ptr_overlap
@abstract Checks that two ptrs do not overlap.
*/
#define check_ptr_overlap( P1, P1_SIZE, P2, P2_SIZE ) \
do \
{ \
check( !( ( (uintptr_t)(P1) >= (uintptr_t)(P2) ) && \
( (uintptr_t)(P1) < ( ( (uintptr_t)(P2) ) + (P2_SIZE) ) ) ) ); \
check( !( ( (uintptr_t)(P2) >= (uintptr_t)(P1) ) && \
( (uintptr_t)(P2) < ( ( (uintptr_t)(P1) ) + (P1_SIZE) ) ) ) ); \
\
} while( 1==0 )
#define IsValidFD( X ) ( ( X ) >= 0 )
#endif // __Debug_h__