xref: /titanic_44/usr/src/uts/common/io/bnxe/577xx/drivers/common/include/debug.h (revision d14abf155341d55053c76eeec58b787a456b753b)
1 
2 #ifndef _DEBUG_H_
3 #define _DEBUG_H_
4 /* TBA - complete debug.h file content */
5 
6 #ifdef __LINUX
7 #include <linux/types.h>
8 #include <linux/kernel.h>
9 #elif defined(USER_LINUX)
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <sys/types.h>
14 #elif defined(__SunOS)
15 #include <sys/types.h>
16 #include <sys/cmn_err.h>
17 #include <sys/ddi.h>
18 #include <sys/sunddi.h>
19 #undef u /* see solaris/src/bnxe.h for explanation */
20 #endif
21 #include "bcmtype.h"
22 
23 u8_t win_debug_enter_fatal_state( const IN u8_t b_dbg );
24 
25 #define DBG_ERR_STR "(!)"
26 #define DBG_WRN_STR "(?)"
27 
28 // convert __FILE__ to wchar_t - __WFILE__
29 #define WIDEN2(x) L ## x
30 #define WIDEN(x) WIDEN2(x)
31 #define __WFILE__ WIDEN(__FILE__)
32 
33 // This is a substitution for __FILE__ in order to get rid of entire file path
34 #if !(defined _VBD_CMD_)
35 #define __FILE_STRIPPED__  strrchr(__FILE__, '\\')   ?   strrchr(__FILE__, '\\')   + 1 : __FILE__
36 #define __WFILE_STRIPPED__ wcsrchr(__WFILE__, L'\\') ?   wcsrchr(__WFILE__, L'\\') + 1 : __WFILE__
37 
38 #else // VBD_CMD
39 
40 #define __FILE_STRIPPED__   __FILE__
41 #define __WFILE_STRIPPED__  __WFILE__
42 
43 // Currently only VBD_CMD support it but maybe other USER_MODE like WineDiag should support it as well
44 
45 void printf_color( unsigned long color, const char *format, ... );
46 
47 #define printf_ex printf_color
48 
49 #endif // !(_VBD_CMD_)
50 
51 #define MAX_STR_DBG_LOGGER_NAME 100
52 
53 // logger callback prototype
54 typedef u8_t (*debug_logger_cb_t)( void *context, long msg_code, long gui_code, u8_t b_raise_gui, u32_t string_cnt, u32_t data_cnt, ...) ;
55 
56 // logger functins prototype
57 u8_t debug_deregister_logger          ( IN const void* context ) ;
58 u8_t debug_register_logger            ( debug_logger_cb_t debug_logger_cb,
59                                         long              msg_code,
60                                         long              gui_code,
61                                         u8_t              b_raise_gui,
62                                         void*             context,
63                                         u32_t             times_to_log ) ;
64 u8_t debug_execute_loggers            ( unsigned short* wsz_file, unsigned long line, unsigned short* wsz_cond ) ;
65 void debug_register_logger_device_name( unsigned short* wstr_device_desc, unsigned long size ) ;
66 
67 
68 // max number of loggers
69 #define MAX_DEBUG_LOGGER_CNT 3
70 
71 typedef struct _dbg_logger_t
72 {
73     debug_logger_cb_t debug_logger_cb ; // callback
74     void*             context ;         // unique context
75     u32_t             msg_code ;        // msg_code
76     u32_t             gui_code ;        // gui_code
77     u8_t              b_raise_gui ;     // do raise gui message
78     u32_t             times_to_log ;    // number of times to log
79 } dbg_logger_t ;
80 
81 extern dbg_logger_t g_dbg_logger_arr[MAX_DEBUG_LOGGER_CNT] ;
82 
83 /* Debug Break Filters */
84 extern u32_t g_dbg_flags;
85 #define MEMORY_ALLOCATION_FAILURE       0x1
86 #define FW_SANITY_UPLOAD_CHECK          0x2
87 #define UNDER_TEST                      0x4
88 #define INVALID_MESSAGE_ID              0x8
89 #define ABORTIVE_DISCONNECT_DURING_IND  0x10
90 #define SRIOV_TEST                      0x20
91 
92 #define DBG_BREAK_ON(_cond)  (GET_FLAGS(g_dbg_flags, _cond) != 0)
93 
94 /* Storage defined in the module main c file */
95 extern u8_t     dbg_trace_level;
96 extern u32_t    dbg_code_path;
97 
98 /* code paths */
99 #define CP_INIT                 0x00000100    /* Initialization */
100 #define CP_NVM                  0x00000200    /* nvram          */
101 #define CP_ELINK                0x00000400    /* ELINK          */
102 #define CP_L2_SP                0x00001000    /* L2 Slow Path   */
103 #define CP_L2_SEND              0x00002000    /* L2 Transmit    */
104 #define CP_L2_RECV              0x00004000    /* L2 Receive     */
105 #define CP_L2_INTERRUPT         0x00008000    /* L2 Interrupt   */
106 #define CP_L2                   0x0000f000    /* L2 all         */
107 #define CP_L4_SP                0x00010000    /* L4 Slow Path   */
108 #define CP_L4_SEND              0x00020000    /* L4 Transmit    */
109 #define CP_L4_RECV              0x00040000    /* L4 Receive     */
110 #define CP_L4_INTERRUPT         0x00080000    /* L4 Interrupt   */
111 #define CP_L4                   0x000f0000    /* L4 all         */
112 #define CP_L5_SP                0x00100000    /* L5 Slow Path   */
113 #define CP_L5_SEND              0x00200000    /* L5 Transmit    */
114 #define CP_L5_RECV              0x00400000    /* L5 Receive     */
115 #define CP_L5                   0x00f00000    /* L5 all         */
116 #define CP_VF                   0x01000000    /* VF all         */
117 #define CP_EQ                   0x02000000    /* Event Queue    */
118 #define CP_STAT                 0x04000000    /* Statistics     */
119 #define CP_ER                   0x08000000    /* Event Queue    */
120 #define CP_OMGR                 0x10000000    /* OOO Manager    */
121 #define CP_ENCAP                0x20000000    /* Encapsulated packets  */
122 #define CP_DIAG                 0x40000000    /* Diagnostics    */
123 #define CP_MISC                 0x80000000    /* Miscellaneous  */
124 
125 
126 /* more code paths can be added
127  * bits that are still not defined can be privately used in each module */
128 #define CP_ALL                  0xffffff00
129 #define CP_MASK                 0xffffff00
130 
131 /* Message levels. */
132 typedef enum
133 {
134     LV_VERBOSE = 0x04,
135     LV_INFORM  = 0x03,
136     LV_WARN    = 0x02,
137     LV_FATAL   = 0x01
138 } msg_level_t;
139 
140 #define LV_MASK                 0xff
141 
142 /* Code path and messsage level combined.  These are the first argument
143  * of the DbgMessage macro. */
144 
145 #define VERBOSEi                (CP_INIT | LV_VERBOSE)
146 #define INFORMi                 (CP_INIT | LV_INFORM)
147 #define WARNi                   (CP_INIT | LV_WARN)
148 
149 #define VERBOSEnv               (CP_NVM | LV_VERBOSE)
150 #define INFORMnv                (CP_NVM | LV_INFORM)
151 #define WARNnv                  (CP_NVM | LV_WARN)
152 
153 #define VERBOSEl2sp             (CP_L2_SP | LV_VERBOSE)
154 #define INFORMl2sp              (CP_L2_SP | LV_INFORM)
155 #define WARNl2sp                (CP_L2_SP | LV_WARN)
156 
157 #define VERBOSEl2tx             (CP_L2_SEND | LV_VERBOSE)
158 #define INFORMl2tx              (CP_L2_SEND | LV_INFORM)
159 #define WARNl2tx                (CP_L2_SEND | LV_WARN)
160 
161 #define VERBOSEl2rx             (CP_L2_RECV | LV_VERBOSE)
162 #define INFORMl2rx              (CP_L2_RECV | LV_INFORM)
163 #define WARNl2rx                (CP_L2_RECV | LV_WARN)
164 
165 #define VERBOSEl2int            (CP_L2_INTERRUPT | LV_VERBOSE)
166 #define INFORMl2int             (CP_L2_INTERRUPT | LV_INFORM)
167 #define WARNl2int               (CP_L2_INTERRUPT | LV_WARN)
168 
169 #define VERBOSEl2               (CP_L2 | LV_VERBOSE)
170 #define INFORMl2                (CP_L2 | LV_INFORM)
171 #define WARNl2                  (CP_L2 | LV_WARN)
172 
173 #define VERBOSEl4sp             (CP_L4_SP | LV_VERBOSE)
174 #define INFORMl4sp              (CP_L4_SP | LV_INFORM)
175 #define WARNl4sp                (CP_L4_SP | LV_WARN)
176 
177 #define VERBOSEl4tx             (CP_L4_SEND | LV_VERBOSE)
178 #define INFORMl4tx              (CP_L4_SEND | LV_INFORM)
179 #define WARNl4tx                (CP_L4_SEND | LV_WARN)
180 
181 #define VERBOSEl4rx             (CP_L4_RECV | LV_VERBOSE)
182 #define INFORMl4rx              (CP_L4_RECV | LV_INFORM)
183 #define WARNl4rx                (CP_L4_RECV | LV_WARN)
184 
185 #define VERBOSEl4fp             (CP_L4_RECV | CP_L4_SEND | LV_VERBOSE)
186 #define INFORMl4fp              (CP_L4_RECV | CP_L4_SEND | LV_INFORM)
187 #define WARNl4fp                (CP_L4_RECV | CP_L4_SEND | LV_WARN)
188 
189 #define VERBOSEl4int            (CP_L4_INTERRUPT | LV_VERBOSE)
190 #define INFORMl4int             (CP_L4_INTERRUPT | LV_INFORM)
191 #define WARNl4int               (CP_L4_INTERRUPT | LV_WARN)
192 
193 #define VERBOSEl4               (CP_L4 | LV_VERBOSE)
194 #define INFORMl4                (CP_L4 | LV_INFORM)
195 #define WARNl4                  (CP_L4 | LV_WARN)
196 
197 #define VERBOSEl5sp             (CP_L5_SP | LV_VERBOSE)
198 #define INFORMl5sp              (CP_L5_SP | LV_INFORM)
199 #define WARNl5sp                (CP_L5_SP | LV_WARN)
200 
201 #define VERBOSEl5tx             (CP_L5_SEND | LV_VERBOSE)
202 #define INFORMl5tx              (CP_L5_SEND | LV_INFORM)
203 #define WARNl5tx                (CP_L5_SEND | LV_WARN)
204 
205 #define VERBOSEl5rx             (CP_L5_RECV | LV_VERBOSE)
206 #define INFORMl5rx              (CP_L5_RECV | LV_INFORM)
207 #define WARNl5rx                (CP_L5_RECV | LV_WARN)
208 
209 #define VERBOSEl5               (CP_L5 | LV_VERBOSE)
210 #define INFORMl5                (CP_L5 | LV_INFORM)
211 #define WARNl5                  (CP_L5 | LV_WARN)
212 
213 #define VERBOSEvf               (CP_VF | LV_VERBOSE)
214 #define INFORMvf                (CP_VF | LV_INFORM)
215 #define WARNvf                  (CP_VF | LV_WARN)
216 #define FATALvf                 (CP_VF | LV_FATAL)
217 
218 #define VERBOSEmi               (CP_MISC | LV_VERBOSE)
219 #define INFORMmi                (CP_MISC | LV_INFORM)
220 #define WARNmi                  (CP_MISC | LV_WARN)
221 
222 #define VERBOSEeq               (CP_EQ | LV_VERBOSE)
223 #define INFORMeq                (CP_EQ | LV_INFORM)
224 #define WARNeq                  (CP_EQ | LV_WARN)
225 
226 #define VERBOSOmgr              (CP_OMGR | LV_VERBOSE)
227 #define INFOROmgr               (CP_OMGR | LV_INFORM)
228 #define WAROmgr                 (CP_OMGR | LV_WARN)
229 
230 #define VERBOSEstat             (CP_STAT | LV_VERBOSE)
231 #define INFORMstat              (CP_STAT | LV_INFORM)
232 #define WARNstat                (CP_STAT | LV_WARN)
233 
234 /* Error Recovery */
235 #define VERBOSEer               (CP_ER | LV_VERBOSE)
236 #define INFORMer                (CP_ER | LV_INFORM)
237 #define WARNer                  (CP_ER | LV_WARN)
238 
239 /* Elink */
240 #define VERBOSEelink            (CP_ELINK | LV_VERBOSE)
241 #define INFORMelink             (CP_ELINK | LV_INFORM)
242 #define WARNelink               (CP_ELINK | LV_WARN)
243 
244 /* Encapsulated packets */
245 #define VERBOSEencap            (CP_ENCAP | LV_VERBOSE)
246 #define INFORMencap             (CP_ENCAP | LV_INFORM)
247 #define WARNencap               (CP_ENCAP | LV_WARN)
248 
249 #define FATAL                   (CP_ALL | LV_FATAL)
250 /* This is an existing debug macro in 2.6.27 Linux kernel */
251 #ifdef WARN
252 #undef WARN
253 #endif
254 
255 #if defined(_VBD_)
256 #define WARN                    (0)
257 #else
258 #define WARN                    (CP_ALL | LV_WARN)
259 #endif
260 #define INFORM                  (CP_ALL | LV_INFORM)
261 #define VERBOSE                 (CP_ALL | LV_VERBOSE)
262 
263 #if defined(DOS) || defined(__USER_MODE_DEBUG) || defined(UEFI)
264 #define DbgBreakIfAll(_cond) do {\
265     if(_cond) \
266     { \
267         printf("DEBUG BREAK! Condition failed: if("#_cond##")\n"); \
268         fflush(stdout); \
269 	log_message("DEBUG BREAK! Condition failed: if("#_cond##") at file %s (line %4d)\n", __FILE_STRIPPED__, __LINE__); \
270         assert(0); \
271     } \
272 } while(0)
273 
274 #define EXECUTE_LOGGERS(_s)        do {\
275         printf(_s);      \
276         fflush(stdout);  \
277 } while(0)
278 
279 #elif  defined(_VBD_CMD_)
280 #include <assert.h>
281 #include <string.h>
282 #define DbgBreakIfAll(_cond) do {\
283     if(_cond) \
284     { \
285         DbgMessage(NULL, FATAL, "DEBUG BREAK! Condition failed: if("#_cond##")\n"); \
286         assert(0); \
287     } \
288 } while(0)
289 
290 
291 #define EXECUTE_LOGGERS(_s)                  DbgMessage(NULL, FATAL, _s);
292 
293 #elif  defined(__LINUX) || defined(USER_LINUX)
294 
295 #define DbgBreakIfAll(_cond) do {\
296     if(_cond) \
297     { \
298         DbgMessage(NULL, FATAL, "DEBUG BREAK! Condition failed: if(%s)\n", #_cond); \
299         debug_break(); \
300     } \
301 } while(0)
302 
303 #define EXECUTE_LOGGERS(_s)                  DbgMessage(NULL, FATAL, _s);
304 
305 #elif defined(__SunOS)
306 
307 #if defined(__SunOS_MDB)
308 
309 #define DbgBreakIfAll(_c)      \
310     do {                       \
311         if (_c)                \
312         {                      \
313             /* nop in MDB */ ; \
314         }                      \
315     } while (0)
316 
317 #else /* !__SunOS_MDB */
318 
319 #define DbgBreakIfAll(_c)                                                     \
320     do {                                                                      \
321         if (_c)                                                               \
322         {                                                                     \
323             cmn_err(CE_PANIC, "<%d> %s(%4d): Condition Failed! - if ("#_c")", \
324                     ((FATAL) & LV_MASK),                                      \
325                     __FILE_STRIPPED__,                                        \
326                     __LINE__);                                                \
327         }                                                                     \
328     } while (0)
329 
330 #endif /* __SunOS_MDB */
331 
332 #define EXECUTE_LOGGERS(_s)
333 
334 #else // Windows
335 
336 unsigned char  __cdecl win_debug_log_assert( void*          drv_object,
337                                              long           msg_code,
338                                              long           gui_code,
339                                              unsigned char  b_raise_gui,
340                                              unsigned int   string_cnt,
341                                              unsigned int   data_cnt,
342                                              ...);
343 
344 #if DBG
345 
346 #define DbgBreakIfAll(_cond) DbgBreakIf(_cond)
347 
348 #else
349 
350 #include <ntddk.h>
351 #include <wchar.h>
352 
353 /*******************************************************************************
354  * Debug Print callback - printk (DbgPrint/vDbgPrintEx)
355  ******************************************************************************/
356 
357 typedef
358 ULONG
359 (__cdecl* PFN_DRIVER_TRACE)(
360     IN PCHAR String,
361     ...);
362 
363 extern PFN_DRIVER_TRACE            printk;
364 
365 #define DbgBreakIfAll(_cond) do {\
366     if(_cond) \
367     { \
368         debug_execute_loggers( __WFILE_STRIPPED__ , __LINE__, WIDEN(#_cond##) ); \
369         printk("eVBD-DRV: DEBUG BREAK! Condition failed: if("#_cond##")\n"); \
370         if ( win_debug_enter_fatal_state(FALSE) )\
371         {\
372            DbgBreakPoint();\
373         }\
374     } \
375 } while(0)
376 
377 #endif //DBG
378 
379 #define EXECUTE_LOGGERS(_s)                  debug_execute_loggers( __WFILE_STRIPPED__ , __LINE__, WIDEN(#_s##) )
380 
381 #endif
382 
383 
384 #if DBG
385 
386 /* These constants control the output of messages.
387  * Set your debug message output level and code path here. */
388 #ifndef DBG_MSG_CP
389 #define DBG_MSG_CP              CP_ALL      /* Where to output messages. */
390 #endif
391 
392 #ifndef DBG_MSG_LV
393 #ifdef _VBD_CMD_
394 #define DBG_MSG_LV              dbg_trace_level
395 #else
396 //change this to higher level than FATAL to open debug messages.
397 #define DBG_MSG_LV              LV_FATAL  /* Level of message output. */
398 #endif
399 #endif
400 
401 //STATIC is now not empty define
402 #ifndef STATIC
403 #define STATIC static
404 #endif
405 //#define DbgBreak(_c)
406 
407 /* This is the original implementation where both code path and debug level can only be defined
408    once at compile time */
409 #if 0
410 #define CODE_PATH(_m)           ((_m) & DBG_MSG_CP)
411 #define MSG_LEVEL(_m)           ((_m) & LV_MASK)
412 #define LOG_MSG(_m)             (CODE_PATH(_m) && \
413                                 MSG_LEVEL(_m) <= DBG_MSG_LV)
414 #endif
415 
416 #define CODE_PATH(_m)           ((_m) & dbg_code_path)
417 #define MSG_LEVEL(_m)           ((_m) & LV_MASK)
418 #define LOG_MSG(_m)             (CODE_PATH(_m) && \
419                                 MSG_LEVEL(_m) <= dbg_trace_level)
420 
421 
422 
423 void mm_print_bdf(int level, void* dev);
424 
425 /* per OS methods */
426 #if defined(UEFI)
427 #include <stdio.h>
428 #include <assert.h>
429 #include <string.h>
430 void console_cleanup(void);
431 void
432 console_init(
433     void
434     );
435 u32_t
436 console_print(
437     void * console_buffer_p
438     );
439 void __cdecl
440 debug_msgx(
441     unsigned long level,
442     char *msg,
443     ...);
444 
445 #define MessageHdr(_dev, _level) \
446     do { \
447         debug_msgx((_level), "TrLv<%d>, %s (%4d): ", ((_level) & LV_MASK), __FILE_STRIPPED__, __LINE__); \
448         mm_print_bdf(_level, (void*)(_dev)); \
449     } while (0)
450 
451 //void DbgMessage(void *dummy, int level, char *msg, ...);
452 #define DbgMessage
453 #define log_message
454 
455 #define debug_break() do { \
456     debug_msgx(FATAL, "DEBUG BREAK!\n"); \
457     console_print(NULL); \
458     fflush(stdout); \
459     assert(0); \
460 } while(0)
461 #define DbgBreak()            debug_break()
462 #define DbgBreakIf(_cond) do {\
463     if(_cond) \
464     { \
465         MessageHdr(NULL,LV_FATAL); \
466         debug_msgx(FATAL, "Condition failed: if("#_cond##")\n"); \
467         debug_break(); \
468     } \
469 } while(0)
470 #define DbgBreakMsg(_s) do { \
471     MessageHdr(NULL,LV_FATAL); \
472     debug_msgx(FATAL, "Debug Break Message: " _s); \
473     debug_break(); \
474 } while(0)
475 #define DbgBreakFastPath()        DbgBreak()
476 #define DbgBreakIfFastPath(_cond) DbgBreakIf(_cond)
477 #define DbgBreakMsgFastPath(_s)   DbgBreakMsg(_s)
478 #define dbg_out(_c, _m, _s, _d1) debug_msgx((WARN), "TrLv<%d>, %s (%4d): %p"##_s" %s\n", ((WARN) & LV_MASK), __FILE_STRIPPED__, __LINE__, _c, _d1 )
479 #elif defined(DOS)
480 #include <stdio.h>
481 #include <assert.h>
482 #include <string.h>
483 
484 void console_cleanup(void);
485 
486 void
487 console_init(
488     void
489     );
490 
491 u32_t
492 console_print(
493     void * console_buffer_p
494     );
495 
496 void __cdecl
497 debug_msgx(
498     unsigned long level,
499     char *msg,
500     ...);
501 
502 void __cdecl
503 log_msgx(
504     char *msg,
505     ...);
506 
507 // DOS
508 #define debug_message(l, fmt, ...) debug_msgx(l, fmt, ##__VA_ARGS__)
509 #define log_message(fmt, ...)	log_msgx(fmt, ##__VA_ARGS__)
510 
511 
512 
513 // TODO: need to relate to dbg_trace_module_name
514 /*
515 #define MessageHdr(_dev, _level) \
516     debug_message("%s, TrLv<%d>, %s (%d): %p ", dbg_trace_module_name, (_level), __FILE_STRIPPED__, __LINE__, _dev )
517 */
518 
519 // DOS
520 #define MessageHdr(_dev, _level) \
521         do { \
522         debug_message((_level), "TrLv<%d>, %s (%4d): ", ((_level) & LV_MASK), __FILE_STRIPPED__, __LINE__); \
523         mm_print_bdf(_level, (void*)(_dev)); \
524     } while (0)
525 
526 #define DbgMessage(_c, _m, _s, ...)                                            	\
527 do { 										\
528         MessageHdr((_c), (_m));                                                 \
529         debug_message((_m), _s, ##__VA_ARGS__); \
530 } while(0)
531 
532 // DOS
533 #define debug_break() do { \
534     debug_message(FATAL, "DEBUG BREAK at file %s (line %4d)!\n", __FILE_STRIPPED__, __LINE__); \
535     log_message("DEBUG BREAK at file %s (line %4d)!\n", __FILE_STRIPPED__, __LINE__); \
536     console_print(NULL); \
537     fflush(stdout); \
538     assert(0); \
539 } while(0)
540 
541 // DOS
542 #define DbgBreak()            debug_break()
543 // DOS
544 #define DbgBreakIf(_cond) do {\
545     if(_cond) \
546     { \
547         MessageHdr(NULL,LV_FATAL); \
548         debug_message(FATAL, "Condition failed: if("#_cond##")\n"); \
549         debug_break(); \
550     } \
551 } while(0)
552 // DOS
553 #define DbgBreakMsg(_s) do { \
554     MessageHdr(NULL,LV_FATAL); \
555     debug_message(FATAL, "Debug Break Message: " _s); \
556     debug_break(); \
557 } while(0)
558 
559 // DOS
560 #define DbgBreakFastPath()        DbgBreak()
561 #define DbgBreakIfFastPath(_cond) DbgBreakIf(_cond)
562 #define DbgBreakMsgFastPath(_s)   DbgBreakMsg(_s)
563 
564 
565 // DOS
566 #define dbg_out(_c, _m, _s, _d1) debug_msgx((WARN), "TrLv<%d>, %s (%4d): %p"##_s" %s\n", ((WARN) & LV_MASK), __FILE_STRIPPED__, __LINE__, _c, _d1 )
567 
568 #elif defined(__USER_MODE_DEBUG)
569 #include <stdio.h>
570 #include <assert.h>
571 #include <string.h>
572 void __cdecl
573 log_msgx(
574     char *msg,
575     ...);
576 
577 #define debug_message(fmt, ...) printf(fmt, ##__VA_ARGS__); fflush(stdout);
578 #define log_message(fmt, ...)	log_msgx(fmt, ##__VA_ARGS__)
579 
580 // TODO: need to relate to dbg_trace_module_name
581 /*
582 #define MessageHdr(_dev, _level) \
583     debug_message("%s, TrLv<%d>, %s (%d): %p ", dbg_trace_module_name, (_level), __FILE_STRIPPED__, __LINE__, _dev )
584 */
585 // __USER_MODE_DEBUG
586 #define MessageHdr(_dev, _level) \
587     debug_message("TrLv<%d>, %-12s (%4d): %p ", ((_level) & LV_MASK), __FILE_STRIPPED__, __LINE__, _dev )
588 
589 #define DbgMessage(_c, _m, _s, ...)                                            		\
590     if(LOG_MSG(_m))                                                   			\
591     {                                                               			\
592         MessageHdr((_c), (_m));                                                 	\
593         debug_message(_s, ##__VA_ARGS__);                                                 	\
594     }
595 
596 // __USER_MODE_DEBUG
597 #define debug_break() do { \
598     debug_message("DEBUG BREAK at file %s (line %4d)!\n", __FILE_STRIPPED__, __LINE__); \
599     log_message("DEBUG BREAK at file %s (line %4d)!\n", __FILE_STRIPPED__, __LINE__); \
600     fflush(stdout); \
601     assert(0); \
602 } while(0)
603 
604 // __USER_MODE_DEBUG
605 #define DbgBreak()            debug_break()
606 
607 #define DbgBreakIf(_cond) do {\
608     if(_cond) \
609     { \
610         MessageHdr(NULL,LV_FATAL); \
611         debug_message("Condition failed: if("#_cond##")\n"); \
612         debug_break(); \
613     } \
614 } while(0)
615 
616 // __USER_MODE_DEBUG
617 #define DbgBreakMsg(_s) do { \
618     MessageHdr(NULL,LV_FATAL); \
619     debug_message("Debug Break Message: " _s); \
620     debug_break(); \
621 } while(0)
622 
623 // __USER_MODE_DEBUG
624 #define DbgBreakFastPath()        DbgBreak()
625 #define DbgBreakIfFastPath(_cond) DbgBreakIf(_cond)
626 #define DbgBreakMsgFastPath(_s)   DbgBreakMsg(_s)
627 
628 //#define dbg_out(_c, _m, _s, _d1) DbgMessage((_c), (_m), (_s), (_d1))
629 #define dbg_out(_c, _m, _s, _d1) printf(_s, _d1)
630 
631 #elif defined(__LINUX) || defined(USER_LINUX)
632 
633 #define __cdecl
634 
635 #define DbgBreak            debug_break
636 
637 #undef __FILE_STRIPPED__
638 #ifdef __LINUX
639 char *os_if_strrchr(char *a, int   n);
640 
641 #define __FILE_STRIPPED__  os_if_strrchr(__FILE__, '/')   ?   os_if_strrchr(__FILE__, '/')   + 1 : __FILE__
642 #else
643 #define __FILE_STRIPPED__  strrchr(__FILE__, '/')   ?   strrchr(__FILE__, '/')   + 1 : __FILE__
644 #endif
645 
646 /*******************************************************************************
647  * Debug break and output routines.
648  ******************************************************************************/
649 void __cdecl
650 debug_msgx(
651     unsigned long level,
652     char *msg,
653     ...);
654 
655 #ifdef USER_LINUX
656 
657 void __cdecl
658 log_msgx(
659     char *msg,
660     ...);
661 
662 #define MessageHdr(_dev, _level) \
663     printf("TrLv<%d>, %s (%4d): %p ", ((_level) & LV_MASK), __FILE_STRIPPED__, __LINE__, _dev )
664 
665 #define log_message(fmt, ...)	log_msgx(fmt, ##__VA_ARGS__)
666 
667 #define debug_break() do { \
668     MessageHdr(NULL, LV_FATAL); \
669     debug_msgx(FATAL, "DEBUG BREAK at file %s (line %4d)!\n", __FILE_STRIPPED__, __LINE__); \
670     log_message("DEBUG BREAK at file %s (line %4d)!\n", __FILE_STRIPPED__, __LINE__); \
671     fflush(stdout); \
672     exit(1); \
673 } while(0)
674 
675 #else /* KERNEL */
676 
677 #define MessageHdr(_dev, _level) \
678     do { \
679         printk(KERN_CRIT "TrLv<%d>, %s (%4d): ", ((_level) & LV_MASK), __FILE_STRIPPED__, __LINE__); \
680         mm_print_bdf(_level, (void*)(_dev)); \
681     } while (0)
682 
683 void debug_break(void);
684 #endif
685 
686 #ifdef USER_LINUX
687 #define DbgMessageXX(_c, _m, _s...)                    \
688     if(LOG_MSG(_m))                                    \
689     {                                                  \
690     MessageHdr(_c, _m);                \
691         debug_msgx(_m, ##_s);   \
692     }
693 #else  /* __LINUX */
694 #define DbgMessageXX(_c, _m, _s...)                    \
695     if(unlikely(LOG_MSG(_m)))                                    \
696     {                                                  \
697     MessageHdr(_c, _m);                \
698         debug_msgx(_m, ##_s);   \
699     }
700 #endif
701 
702 #define DbgMessage  DbgMessageXX
703 
704 // LINUX
705 #ifdef USER_LINUX
706 #define DbgBreakIf(_c) \
707     if(_c) \
708     { \
709         DbgMessage(NULL, FATAL, "if("#_c")\n"); \
710         DbgBreak(); \
711     }
712 #else /* __LINUX */
713 #define DbgBreakIf(_c) \
714     if(unlikely(_c)) \
715     { \
716         DbgMessage(NULL, FATAL, "if("#_c")\n"); \
717         DbgBreak(); \
718     }
719 #endif
720 
721 // LINUX
722 #define DbgBreakMsg(_m)     do {DbgMessage(NULL, FATAL, _m); DbgBreak();} while (0)
723 
724 // LINUX
725 #define DbgBreakFastPath()        DbgBreak()
726 #define DbgBreakIfFastPath(_cond) DbgBreakIf(_cond)
727 #define DbgBreakMsgFastPath(_s)   DbgBreakMsg(_s)
728 
729 #define dbg_out(_c, _m, _s, _d1) debug_msgx(_m, "TrLv<%d>, %s (%4d): %p"_s" %s\n", ((WARN) & LV_MASK), __FILE_STRIPPED__, __LINE__, _c, _d1 )
730 
731 #elif defined(__SunOS)
732 
733 #if defined(__SunOS_MDB)
734 #define DbgMessage(_c, _m, _s, ...)
735 #define DbgBreak()
736 #define DbgBreakIf(_cond)
737 #define DbgBreakMsg(_s)
738 #define DbgBreakFastPath()
739 #define DbgBreakIfFastPath(_cond)
740 #define DbgBreakMsgFastPath(_s)
741 #define dbg_out(_c, _m, _s, _d1) cmn_err(CE_NOTE, _s, _d1)
742 #else
743 /* under //servers/main/nx2/577xx/drivers/solaris/src */
744 #include "bnxe_debug.h"
745 #endif
746 
747 #elif defined(__WINDOWS)
748 
749 #else
750 
751 #if defined(_VBD_)
752 #include <SAL.h>
753 #endif
754 
755 #ifndef _In_
756 #define _In_
757 #endif
758 #ifndef _Out_
759 #define _Out_
760 #endif
761 
762 /*******************************************************************************
763  * Debug break and output routines.
764  ******************************************************************************/
765 
766 void
767 debug_break(
768     void *ctx);
769 
770 void __cdecl
771 debug_msg(
772     void *ctx,
773     unsigned long level,
774     _In_ char *file,
775     unsigned long line,
776     _In_ char *msg,
777     ...);
778 
779 void __cdecl
780 debug_msgx(
781     void *ctx,
782     unsigned long level,
783     _In_ char *msg,
784     ...);
785 
786 void um_dbg_log_msg(void* ctx, char *file, char *function, unsigned long line, const char* format_msg, unsigned int args_num, ...);
787 
788 #define VA_NUM_ARGS_SIZE_PREFIX__VA_NUM_ARGS_SIZE_POSTFIX ,,,,,,,,,,0
789 #define VA_NUM_ARGS(...) VA_NUM_ARGS_IMP_((VA_NUM_ARGS_SIZE_PREFIX_ ## __VA_ARGS__ ## _VA_NUM_ARGS_SIZE_POSTFIX,10,9,8,7,6,5,4,3,2,1,0))
790 #define VA_NUM_ARGS_IMP_(__args) VA_NUM_ARGS_IMP __args
791 #define VA_NUM_ARGS_IMP(__p0,__p1,__p2,__p3,__p4,__p5,__p6,__p7,__p8,__p9,__n,...) __n
792 
793 // WINDDK
794 #define DbgMessage(_c, _m, _s, ...)                                                                                \
795     if(LOG_MSG(_m))                                                                                                \
796     {                                                                                                              \
797         debug_msg(_c, _m, __FILE__, __LINE__, _s, ##__VA_ARGS__);                                                  \
798         um_dbg_log_msg(_c, __FILE__, __FUNCTION__, __LINE__, _s, VA_NUM_ARGS(##__VA_ARGS__), ##__VA_ARGS__);       \
799     }
800 
801 // WINDDK
802 
803 #define DbgBreakIf(_c) \
804     if(_c) \
805     { \
806         debug_msg(NULL, FATAL, __FILE__, __LINE__, "if("#_c##")\n"); \
807         debug_execute_loggers( __WFILE_STRIPPED__ , __LINE__, WIDEN(#_c##) );\
808         win_debug_enter_fatal_state(TRUE);\
809         __debugbreak(); \
810     }
811 
812 // WINDDK
813 #define DbgBreakMsg(_m)     debug_msg(NULL, FATAL, __FILE__, __LINE__, _m); \
814                             debug_execute_loggers( __WFILE_STRIPPED__ , __LINE__, WIDEN(#_m##) );\
815                             __debugbreak()
816 // WINDDK
817 #define DbgBreak()          debug_execute_loggers( __WFILE_STRIPPED__ , __LINE__, L"DbgBreak" );\
818                             __debugbreak()
819 // WINDDK (debug)
820 #define DbgBreakFastPath()          DbgBreak()
821 #define DbgBreakIfFastPath(_cond)   DbgBreakIf(_cond)
822 #define DbgBreakMsgFastPath(_s)     DbgBreakMsg(_s)
823 
824 // WINDDK
825 #define dbg_out(_c, _m, _s, _d1) debug_msg(_c, _m, __FILE__, __LINE__, _s, _d1)
826 
827 #endif //OS architectures
828 
829 // Error Macros (Currently supports Windows DDK & DOS):
830 // in debug builds - outputs a debug message and enter condition in case TRUE
831 // in release builds - enters condition in case TRUE (like debug but without the debug print)
832 
833 // Macro for checking parameter for NULL value
834 //    Usage Example:
835 //       if( CHECK_NULL( ptr ))
836 //       {
837 //          return FALSE ;
838 //       }
839 #define CHK_NULL(p) ((p==NULL)    ? (dbg_out(NULL, WARN, DBG_ERR_STR" %s is NULL\n",#p), TRUE): FALSE )
840 
841 // Macros that returns the value of the expression and outputs a debug string in debug versions
842 //    Usage Example:
843 //       if( ERR_IF( val < 0 ))
844 //       {
845 //          return FALSE ;
846 //       }
847 #define ERR_IF(cond)(((cond)==TRUE) ? (dbg_out(NULL, WARN, DBG_ERR_STR" ErrIf failed %s\n",#cond), TRUE): FALSE )
848 #define WRN_IF(cond)(((cond)==TRUE) ? (dbg_out(NULL, WARN, DBG_WRN_STR" WrnIf failed %s\n",#cond), TRUE): FALSE )
849 
850 #else // !DBG
851 #define STATIC static
852 
853 #define DbgMessage(_c, _m, ...)
854 
855 
856 #if ! (defined(WIN_DIAG) || defined(__LINUX) || defined(USER_LINUX) || defined(__SunOS))
857 // WINDDK DbgBreak (retail) and logging an event
858 #define DbgBreak()                       debug_execute_loggers( __WFILE_STRIPPED__ , __LINE__, L"DbgBreak" )
859 #define DbgBreakIf(_cond)     if(_cond){ debug_execute_loggers( __WFILE_STRIPPED__ , __LINE__, WIDEN(#_cond##) ); }
860 #define DbgBreakMsg(_s)                  debug_execute_loggers( __WFILE_STRIPPED__ , __LINE__, WIDEN(#_s##) )
861 
862 // WINDDK DbgBreak (retail) without logging an event
863 #define DbgBreakNoLog()
864 #define DbgBreakIfNoLog(_cond)
865 #define DbgBreakMsgNoLog(_s)
866 
867 // WINDDK DbgBreak FastPath (retail)
868 #define DbgBreakFastPath()         DbgBreakNoLog()
869 #define DbgBreakIfFastPath(_cond)  DbgBreakIfNoLog(_cond)
870 #define DbgBreakMsgFastPath(_s)    DbgBreakMsgNoLog(_s)
871 
872 #else // WIN_DIAG and Linux and Solaris
873 #define DbgBreak()
874 #define DbgBreakIf(_cond)
875 #define DbgBreakMsg(_s)
876 #define DbgBreakFastPath()
877 #define DbgBreakIfFastPath(_cond)
878 #define DbgBreakMsgFastPath(_s)
879 #endif // !WIN_DIAG
880 
881 #define CHK_NULL(p) (p==NULL)
882 #define ERR_IF(cond)(cond==TRUE)
883 #define WRN_IF(cond)(cond==TRUE)
884 
885 #endif // !DBG
886 
887 
888 #endif /* _DEBUG_H_ */
889