source: svn/trunk/newcon3bcm2_21bu/magnum/basemodules/mem/bmem_debug.c @ 25

Last change on this file since 25 was 2, checked in by phkim, 11 years ago

1.phkim

  1. revision copy newcon3sk r27
  • Property svn:executable set to *
File size: 9.8 KB
Line 
1/***************************************************************************
2 *     Copyright (c) 2001-2012, Broadcom Corporation
3 *     All Rights Reserved
4 *     Confidential Property of Broadcom Corporation
5 *
6 *  THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE
7 *  AGREEMENT  BETWEEN THE USER AND BROADCOM.  YOU HAVE NO RIGHT TO USE OR
8 *  EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT.
9 *
10 * $brcm_Workfile: bmem_debug.c $
11 * $brcm_Revision: Hydra_Software_Devel/25 $
12 * $brcm_Date: 2/8/12 1:56p $
13 *
14 * Module Description:
15 *
16 * Revision History:
17 *
18 * $brcm_Log: /magnum/basemodules/mem/bmem_debug.c $
19 *
20 * Hydra_Software_Devel/25   2/8/12 1:56p albertl
21 * SW7425-2345: Changed total allocation and number of allocation tracking
22 * to work in eFastest.
23 *
24 * Hydra_Software_Devel/24   3/28/11 5:12p albertl
25 * SW7425-247: Incorporated BDBG_OBJECT handle validation.
26 *
27 * Hydra_Software_Devel/23   11/4/10 7:56p albertl
28 * SW7420-1155: Fixed pEnd to really be last available byte and addrEnd to
29 * be last byte in chunk rather than byte after that.
30 *
31 * Hydra_Software_Devel/22   7/26/10 3:02p erickson
32 * SW3548-3013: BMEM_Dbg_DumpHeap should report BMEM_Handle pointer value
33 * and specified, not derived, overall size
34 *
35 * Hydra_Software_Devel/21   3/26/10 6:29p albertl
36 * SW7405-3979: Changed filename field from string to pointer and removed
37 * slow copy operation.
38 *
39 * Hydra_Software_Devel/20   2/24/10 4:14p erickson
40 * SW3548-2721: don't print uninitialized file and line in
41 * BMEM_Dbg_DumpHeap. make BMEM_Dbg_DumpBlock private because its params
42 * are internal.
43 *
44 * Hydra_Software_Devel/19   2/18/10 6:31p albertl
45 * SW3548-2721: Changed debug module name to BMEM_DBG.  Added BKNI_Printf
46 * messages in csv format.
47 *
48 * Hydra_Software_Devel/18   5/26/09 4:18p albertl
49 * PR55389: Replaced uintptr_t with uint32_t.
50 *
51 * Hydra_Software_Devel/17   4/14/08 4:52p albertl
52 * PR36283:  Fixed debug message to properly calculate block start and end
53 * addresses for both system and local bookkeeping.
54 *
55 * Hydra_Software_Devel/16   12/6/07 3:23p tdo
56 * PR37550: Fix coverity issues
57 *
58 * Hydra_Software_Devel/15   5/14/07 1:19p albertl
59 * PR30621:  Fixed heaps being added to allocation list but not being
60 * removed when freed in some safety configurations.
61 *
62 * Hydra_Software_Devel/14   1/16/07 2:41p erickson
63 * PR25037: improve BMEM validate and debug messages
64 *
65 * Hydra_Software_Devel/13   4/5/06 2:43p albertl
66 * PR20659:  Changed BDBG_WRN to BDBG_MSG.
67 *
68 * Hydra_Software_Devel/12   12/20/04 9:21p marcusk
69 * PR13562: Fixed typo in debug statement.
70 *
71 * Hydra_Software_Devel/11   12/20/04 8:08p marcusk
72 * PR13562: Fixed bug in BMEM_Dbg_DumpBlock() that was not taking into
73 * account the front scrap size when determining the addresses of the
74 * buffer.
75 *
76 * Hydra_Software_Devel/10   12/14/04 4:31p marcusk
77 * PR13562: Updated to output the heap size, allocated size, and free size
78 * when a memory allocation fails.
79 *
80 * Hydra_Software_Devel/9   10/7/04 5:37p pntruong
81 * PR12728: Fixed -pdantic build warnings and errors.
82 *
83 * Hydra_Software_Devel/8   4/1/04 12:56p vsilyaev
84 * PR 10201: Added memory monitor
85 *
86 * Hydra_Software_Devel/7   2/25/04 2:52p hongtaoz
87 * PR9855: removed redundent functions BMEM_Report and BMEM_ReportVerbose;
88 * being aware of that BMEM_Dbg_DumpBlock and BMEM_Dbg_DumpHeap suffice
89 * heap debug requirement; avoid using floating point in BMEM_Dbg_Map;
90 *
91 * Hydra_Software_Devel/6   12/31/03 12:02p jasonh
92 * PR 8940: Changed return type of destroyheap to void. Removed use of
93 * DEBUG macro and printf.
94 *
95 * Hydra_Software_Devel/5   10/2/03 1:02p pntruong
96 * Removed -ansi warnings and build erros.
97 *
98 * Hydra_Software_Devel/4   9/5/03 1:57p jasonh
99 * Changed macro to use standard version.
100 *
101 * Hydra_Software_Devel/3   9/2/03 1:34p vadim
102 * Some magnum updates.
103 *
104 * Hydra_Software_Devel/2   3/20/03 3:51p erickson
105 * renamed all MEM_ to BMEM_
106 *
107 * Hydra_Software_Devel/1   3/20/03 3:24p erickson
108 * initial bmem work, taken from SetTop/memorylib
109 *
110 ***************************************************************************/
111#include "bstd.h"
112
113#include "bmem.h"
114#include "bmem_config.h"
115#include "bmem_priv.h"
116#include "bmem_debug.h"
117#include "bkni.h"
118
119BDBG_MODULE(BMEM_DBG);
120
121#define BMEM_MAP_SIZE       (80*40)
122#define BMEM_MAP_ADDR(addr) ((addr) * BMEM_MAP_SIZE / (pheap->pEnd - pheap->pStart + 1))
123/**********************************************************************func*
124 * BMEM_Dbg_Map
125 *
126 */
127void BMEM_Dbg_Map
128(
129        BMEM_Handle pheap
130)
131{
132        int               i;
133        BMEM_P_BlockInfo *pbi;
134        char             *ach;
135
136        BDBG_OBJECT_ASSERT(pheap, BMEM_Heap);
137
138        /* Allocate memory for ach */
139        ach = (char *)(BKNI_Malloc((BMEM_MAP_SIZE+1)*sizeof(char)));
140        if(!ach)
141        {
142                return;
143        }
144
145        for(i = 0; i < BMEM_MAP_SIZE; ++i)
146        {
147                ach[i] = '#';
148        }
149
150        for(pbi = pheap->pAllocTop; pbi != NULL; pbi = pbi->pnext)
151        {
152                uint32_t addrStart = BMEM_P_GetAddress( pheap, pbi ) - pbi->ulFrontScrap -
153                        (uint32_t) pheap->pStart;
154                uint32_t addrFront = BMEM_P_GetAddress(pheap, pbi) - (uint32_t)pheap->pStart;
155                uint32_t addrBack = addrStart+pbi->ulSize-pbi->ulBackScrap;
156                uint32_t addrEnd = addrStart+pbi->ulSize;
157                uint32_t ul;
158
159                addrStart = (uint32_t)BMEM_MAP_ADDR(addrStart);
160                addrFront = (uint32_t)BMEM_MAP_ADDR(addrFront);
161                addrBack = (uint32_t)BMEM_MAP_ADDR(addrBack);
162                addrEnd = (uint32_t)BMEM_MAP_ADDR(addrEnd);
163                for(ul = addrStart; ul < addrFront; ++ul)
164                {
165                        ach[ul] = ',';
166                }
167                for(ul = addrFront+1; ul < addrBack; ++ul)
168                {
169                        ach[ul]='=';
170                }
171                for(ul = addrBack; ul<addrEnd; ++ul)
172                {
173                        ach[ul]='.';
174                }
175                if(ach[addrFront] == '#')
176                {
177                        ach[addrFront] = 'a';
178                }
179                else
180                {
181                        ach[addrFront]++;
182                }
183        }
184
185        for(pbi = pheap->pFreeTop; pbi != NULL; pbi = pbi->pnext)
186        {
187                uint32_t addrStart = BMEM_P_GetAddress( pheap, pbi ) - pbi->ulFrontScrap -
188                        (uint32_t) pheap->pStart;
189                uint32_t addrEnd = addrStart + pbi->ulSize;
190                uint32_t ul;
191
192                addrStart = (uint32_t)BMEM_MAP_ADDR(addrStart);
193                addrEnd = (uint32_t)BMEM_MAP_ADDR(addrEnd);
194                for(ul = addrStart+1; ul < addrEnd; ++ul)
195                {
196                        ach[ul] = '-';
197                }
198
199
200                if(ach[addrStart] == '#')
201                {
202                        ach[addrStart] = 'a';
203                }
204                else
205                {
206                        ach[addrStart]++;
207                }
208        }
209
210        ach[BMEM_MAP_SIZE] = '\0';
211        BDBG_MSG(("%s", ach));
212        BKNI_Free((char *)ach);
213}
214
215/**********************************************************************func*
216 * BMEM_Dbg_DumpBlock
217 *
218 */
219static void BMEM_Dbg_DumpBlock(BMEM_Handle pheap, BMEM_P_BlockInfo *pbi, bool freeBlock)
220{
221        const char *filename;
222        unsigned linenum;
223
224        BDBG_OBJECT_ASSERT(pheap, BMEM_Heap);
225        if (freeBlock) {
226                /* free block has possibly uninitialized filename/linenum */
227                filename = NULL;
228                linenum = 0;
229        }
230        else {
231                /* eliminate the path which is usually unnecessary and very long */
232                filename = pbi->pchFilename;
233            while (*filename) filename++;
234                while (*filename != '/' && filename > pbi->pchFilename) filename--;
235                if (*filename == '/') filename++;
236
237            linenum = pbi->ulLine;
238        }
239
240        /* This format is directly importable to Excel where you can sort by address.
241        It is not DBG output. It should be defaulted off because it will always print. */
242        {
243        uint32_t ulFrontBookKeepingSize = (pheap->eBookKeeping == BMEM_BOOKKEEPING_LOCAL) ?
244                                          BMEM_FRONT_BOOKKEEPING_SIZE_LOCAL : BMEM_FRONT_BOOKKEEPING_SIZE_SYSTEM;
245        uint8_t *addr = (uint8_t *)(BMEM_P_GetAddress( pheap, pbi ) + (uint32_t)ulFrontBookKeepingSize);
246        BKNI_Printf("%p,%#x,%#x,%#x,%lu,%s,%d\n",
247                pheap,
248                pheap->ulOffset,
249                addr,
250                addr + pbi->ulSize,
251                (unsigned long int)pbi->ulSize,
252                filename,
253                linenum);
254        }
255
256#ifdef BMEM_TRACK_FILE_AND_LINE
257        if(pheap->pSafetyConfigInfo->bTrackFileAndLine)
258        {
259                BDBG_MSG(("(%s:%d)", filename, pbi->ulLine));
260        }
261#endif
262        /* The front scrap area comes before the pbi data structure */
263        BDBG_MSG(("%p[%#x..%#x..%#x]: [%lu][%lu][%lu]  [%p]",
264                pbi,    /* base address of the pbi */
265                pheap->ulOffset + ((uint32_t)BMEM_P_GetAddress(pheap, pbi) - pbi->ulFrontScrap - (uint32_t)pheap->pvHeap), /* start of memory allocation block */
266                pbi->ulSize, /* size */
267                pheap->ulOffset + ((uint32_t)BMEM_P_GetAddress(pheap, pbi) - pbi->ulFrontScrap - (uint32_t)pheap->pvHeap) + pbi->ulSize, /* end of memory allocation block */
268                (unsigned long int)pbi->ulFrontScrap,
269                (unsigned long int)pbi->ulSize,
270                (unsigned long int)pbi->ulBackScrap,
271                pbi->pnext));
272}
273
274/**********************************************************************func*
275 * BMEM_Dbg_DumpHeap
276 *
277 */
278void BMEM_Dbg_DumpHeap(BMEM_Handle pheap)
279{
280        BMEM_P_BlockInfo *pbi;
281        unsigned long ulFree = 0;
282
283        BDBG_OBJECT_ASSERT(pheap, BMEM_Heap);
284        if (!pheap->pAllocTop) {
285                BKNI_Printf("You need to set BMEM_Heap_Settings.eSafetyConfig = BMEM_SafetyConfig_eSafe to track allocations.\n");
286        }
287        else {
288                BKNI_Printf("Allocated:\n");
289                BKNI_Printf("heap,offset,addr,endaddr,totalsize,filename,line\n");
290                BDBG_MSG(("pbi[start..size..end]: [fscrap][totalsize][endscrap] [nextpbi]"));
291                for(pbi = pheap->pAllocTop; pbi != NULL; pbi = pbi->pnext)
292                {
293                        BMEM_Dbg_DumpBlock(pheap, pbi, false);
294                }
295        }
296
297        BKNI_Printf(("Free:\n"));
298        BKNI_Printf("heap,offset,addr,endaddr,totalsize,filename,line\n");
299        BDBG_MSG(("pbi[start..size..end]: [fscrap][totalsize][endscrap] [nextpbi]"));
300        for(pbi = pheap->pFreeTop; pbi != NULL; pbi = pbi->pnext)
301        {
302                ulFree += pbi->ulSize;
303                BMEM_Dbg_DumpBlock(pheap, pbi, true);
304        }
305
306        BKNI_Printf("Heap %p Summary:\n", pheap);
307        BKNI_Printf("Total Size: %ld, Allocated: %ld, Free: %ld\n", pheap->zSize, pheap->ulTotalAllocated, ulFree);
308}
309
310/**********************************************************************func*
311 * BMEM_Dbg_GetErrorCount
312 *
313 */
314uint32_t BMEM_Dbg_GetErrorCount(BMEM_Handle pheap)
315{
316    BDBG_OBJECT_ASSERT(pheap, BMEM_Heap);
317        return pheap->ulNumErrors;
318}
319
320/**********************************************************************func*
321 * BMEM_Dbg_ClearErrorCount
322 *
323 */
324void BMEM_Dbg_ClearErrorCount(BMEM_Handle pheap)
325{
326    BDBG_OBJECT_ASSERT(pheap, BMEM_Heap);
327        pheap->ulNumErrors = 0;
328}
329
330/* End of File */
Note: See TracBrowser for help on using the repository browser.