xref: /freebsd/sys/contrib/ncsw/inc/debug_ext.h (revision 640235e2c2ba32947f7c59d168437ffa1280f1e6)
1 /* Copyright (c) 2008-2011 Freescale Semiconductor, Inc.
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above copyright
9  *       notice, this list of conditions and the following disclaimer in the
10  *       documentation and/or other materials provided with the distribution.
11  *     * Neither the name of Freescale Semiconductor nor the
12  *       names of its contributors may be used to endorse or promote products
13  *       derived from this software without specific prior written permission.
14  *
15  *
16  * ALTERNATIVELY, this software may be distributed under the terms of the
17  * GNU General Public License ("GPL") as published by the Free Software
18  * Foundation, either version 2 of that License or (at your option) any
19  * later version.
20  *
21  * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
22  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /**************************************************************************//**
34  @File          debug_ext.h
35 
36  @Description   Debug mode definitions.
37 *//***************************************************************************/
38 
39 #ifndef __DEBUG_EXT_H
40 #define __DEBUG_EXT_H
41 
42 #include "std_ext.h"
43 #include "xx_ext.h"
44 #include "memcpy_ext.h"
45 #if (DEBUG_ERRORS > 0)
46 #include "sprint_ext.h"
47 #include "string_ext.h"
48 #endif /* DEBUG_ERRORS > 0 */
49 
50 
51 #if (DEBUG_ERRORS > 0)
52 
53 /* Internally used macros */
54 
55 #define DUMP_Print          XX_Print
56 #define DUMP_MAX_LEVELS     6
57 #define DUMP_MAX_STR        64
58 
59 
60 #define _CREATE_DUMP_SUBSTR(phrase) \
61     dumpTmpLevel = 0; dumpSubStr[0] = '\0'; \
62     sprintf(dumpTmpStr, "%s", #phrase); \
63     p_DumpToken = strtok(dumpTmpStr, (dumpIsArr[0] ? "[" : ".")); \
64     while (p_DumpToken != NULL) \
65     { \
66         strcat(dumpSubStr, p_DumpToken); \
67         if (dumpIsArr[dumpTmpLevel]) \
68         { \
69             strcat(dumpSubStr, dumpIdxStr[dumpTmpLevel]); \
70             p_DumpToken = strtok(NULL, "."); \
71         } \
72         if ((p_DumpToken = strtok(NULL, (dumpIsArr[++dumpTmpLevel] ? "[" : "."))) != 0) \
73             strcat(dumpSubStr, "."); \
74     }\
75 
76 
77 /**************************************************************************//**
78  @Group         gen_id  General Drivers Utilities
79 
80  @Description   External routines.
81 
82  @{
83 *//***************************************************************************/
84 
85 /**************************************************************************//**
86  @Group         dump_id  Memory and Registers Dump Mechanism
87 
88  @Description   Macros for dumping memory mapped structures.
89 
90  @{
91 *//***************************************************************************/
92 
93 /**************************************************************************//**
94  @Description   Declaration of dump mechanism variables.
95 
96                 This macro must be declared at the beginning of each routine
97                 which uses the dump mechanism macros, before the routine's code
98                 starts.
99 *//***************************************************************************/
100 #define DECLARE_DUMP \
101     char    dumpIdxStr[DUMP_MAX_LEVELS + 1][6] = { "", }; \
102     char    dumpSubStr[DUMP_MAX_STR] = ""; \
103     char    dumpTmpStr[DUMP_MAX_STR] = ""; \
104     char    *p_DumpToken = NULL; \
105     int     dumpArrIdx = 0, dumpArrSize = 0, dumpVarSize = 0, dumpLevel = 0, dumpTmpLevel = 0; \
106     uint8_t dumpIsArr[DUMP_MAX_LEVELS + 1] = { 0 }; \
107     /* Prevent warnings if not all used */ \
108     UNUSED(dumpIdxStr[0][0]); \
109     UNUSED(dumpSubStr[0]); \
110     UNUSED(dumpTmpStr[0]); \
111     UNUSED(p_DumpToken); \
112     UNUSED(dumpArrIdx); \
113     UNUSED(dumpArrSize); \
114     UNUSED(dumpVarSize); \
115     UNUSED(dumpLevel); \
116     UNUSED(dumpTmpLevel); \
117     UNUSED(dumpIsArr[0]);
118 
119 
120 /**************************************************************************//**
121  @Description   Prints a title for a subsequent dumped structure or memory.
122 
123                 The inputs for this macro are the structure/memory title and
124                 its base addresses.
125 *//***************************************************************************/
126 #define DUMP_TITLE(addr, msg)  \
127     DUMP_Print("\r\n"); DUMP_Print msg; \
128     DUMP_Print(" (0x%p)\r\n" \
129                "---------------------------------------------------------\r\n", \
130                (addr))
131 
132 /**************************************************************************//**
133  @Description   Prints a subtitle for a subsequent dumped sub-structure (optional).
134 
135                 The inputs for this macro are the sub-structure subtitle.
136                 A separating line with this subtitle will be printed.
137 *//***************************************************************************/
138 #define DUMP_SUBTITLE(subtitle)  \
139     DUMP_Print("----------- "); DUMP_Print subtitle; DUMP_Print("\r\n")
140 
141 
142 /**************************************************************************//**
143  @Description   Dumps a memory region in 4-bytes aligned format.
144 
145                 The inputs for this macro are the base addresses and size
146                 (in bytes) of the memory region.
147 *//***************************************************************************/
148 #define DUMP_MEMORY(addr, size)  \
149     MemDisp((uint8_t *)(addr), (int)(size))
150 
151 
152 /**************************************************************************//**
153  @Description   Declares a dump loop, for dumping a sub-structure array.
154 
155                 The inputs for this macro are:
156                 - idx: an index variable, for indexing the sub-structure items
157                        inside the loop. This variable must be declared separately
158                        in the beginning of the routine.
159                 - cnt: the number of times to repeat the loop. This number should
160                        equal the number of items in the sub-structures array.
161 
162                 Note, that the body of the loop must be written inside brackets.
163 *//***************************************************************************/
164 #define DUMP_SUBSTRUCT_ARRAY(idx, cnt) \
165     for (idx=0, dumpIsArr[dumpLevel++] = 1; \
166          (idx < cnt) && sprintf(dumpIdxStr[dumpLevel-1], "[%d]", idx); \
167          idx++, ((idx < cnt) || ((dumpIsArr[--dumpLevel] = 0) == 0)))
168 
169 
170 /**************************************************************************//**
171  @Description   Dumps a structure's member variable.
172 
173                 The input for this macro is the full reference for the member
174                 variable, where the structure is referenced using a pointer.
175 
176                 Note, that a members array must be dumped using DUMP_ARR macro,
177                 rather than using this macro.
178 
179                 If the member variable is part of a sub-structure hierarchy,
180                 the full hierarchy (including array indexing) must be specified.
181 
182                 Examples:   p_Struct->member
183                             p_Struct->sub.member
184                             p_Struct->sub[i].member
185 *//***************************************************************************/
186 #define DUMP_VAR(st, phrase) \
187     do { \
188         void *addr = (void *)&((st)->phrase); \
189         _CREATE_DUMP_SUBSTR(phrase); \
190         dumpVarSize = sizeof((st)->phrase); \
191         switch (dumpVarSize) \
192         { \
193             case 1:  DUMP_Print("0x%08X: 0x%02x%14s\t%s\r\n", \
194                                 addr, GET_UINT8(*(uint8_t*)addr), "", dumpSubStr); break; \
195             case 2:  DUMP_Print("0x%08X: 0x%04x%12s\t%s\r\n", \
196                                 addr, GET_UINT16(*(uint16_t*)addr), "", dumpSubStr); break; \
197             case 4:  DUMP_Print("0x%08X: 0x%08x%8s\t%s\r\n", \
198                                 addr, GET_UINT32(*(uint32_t*)addr), "", dumpSubStr); break; \
199             case 8:  DUMP_Print("0x%08X: 0x%016llx\t%s\r\n", \
200                                 addr, GET_UINT64(*(uint64_t*)addr), dumpSubStr); break; \
201             default: DUMP_Print("Bad size %d (" #st "->" #phrase ")\r\n", dumpVarSize); \
202         } \
203     } while (0)
204 
205 
206 /**************************************************************************//**
207  @Description   Dumps a structure's members array.
208 
209                 The input for this macro is the full reference for the members
210                 array, where the structure is referenced using a pointer.
211 
212                 If the members array is part of a sub-structure hierarchy,
213                 the full hierarchy (including array indexing) must be specified.
214 
215                 Examples:   p_Struct->array
216                             p_Struct->sub.array
217                             p_Struct->sub[i].array
218 *//***************************************************************************/
219 #define DUMP_ARR(st, phrase) \
220     do { \
221         _CREATE_DUMP_SUBSTR(phrase); \
222         dumpArrSize = ARRAY_SIZE((st)->phrase); \
223         dumpVarSize = sizeof((st)->phrase[0]); \
224         switch (dumpVarSize) \
225         { \
226             case 1: \
227                 for (dumpArrIdx=0; dumpArrIdx < dumpArrSize; dumpArrIdx++) { \
228                     DUMP_Print("0x%08X: 0x%02x%14s\t%s[%d]\r\n", \
229                                &((st)->phrase[dumpArrIdx]), GET_UINT8((st)->phrase[dumpArrIdx]), "", dumpSubStr, dumpArrIdx); \
230                 } break; \
231             case 2: \
232                 for (dumpArrIdx=0; dumpArrIdx < dumpArrSize; dumpArrIdx++) { \
233                     DUMP_Print("0x%08X: 0x%04x%12s\t%s[%d]\r\n", \
234                                &((st)->phrase[dumpArrIdx]), GET_UINT16((st)->phrase[dumpArrIdx]), "", dumpSubStr, dumpArrIdx); \
235                 } break; \
236             case 4: \
237                 for (dumpArrIdx=0; dumpArrIdx < dumpArrSize; dumpArrIdx++) { \
238                     DUMP_Print("0x%08X: 0x%08x%8s\t%s[%d]\r\n", \
239                                &((st)->phrase[dumpArrIdx]), GET_UINT32((st)->phrase[dumpArrIdx]), "", dumpSubStr, dumpArrIdx); \
240                 } break; \
241             case 8: \
242                 for (dumpArrIdx=0; dumpArrIdx < dumpArrSize; dumpArrIdx++) { \
243                     DUMP_Print("0x%08X: 0x%016llx\t%s[%d]\r\n", \
244                                &((st)->phrase[dumpArrIdx]), GET_UINT64((st)->phrase[dumpArrIdx]), dumpSubStr, dumpArrIdx); \
245                 } break; \
246             default: DUMP_Print("Bad size %d (" #st "->" #phrase "[0])\r\n", dumpVarSize); \
247         } \
248     } while (0)
249 
250 
251 #endif /* DEBUG_ERRORS > 0 */
252 
253 
254 /** @} */ /* end of dump_id group */
255 /** @} */ /* end of gen_id group */
256 
257 
258 #endif /* __DEBUG_EXT_H */
259 
260