1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright by The HDF Group.                                               *
3  * Copyright by the Board of Trustees of the University of Illinois.         *
4  * All rights reserved.                                                      *
5  *                                                                           *
6  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
7  * terms governing use, modification, and redistribution, is contained in    *
8  * the COPYING file, which can be found at the root of the source code       *
9  * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
10  * If you do not have access to either file, you may request a copy from     *
11  * help@hdfgroup.org.                                                        *
12  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13 
14 /* Programmer:  Quincey Koziol <koziol@ncsa.uiuc.edu>
15  *              Wednesday, July 9, 2003
16  *
17  * Purpose:	Local Heap object debugging functions.
18  */
19 #define H5HL_PACKAGE		/* Suppress error about including H5HLpkg */
20 
21 
22 #include "H5private.h"		/* Generic Functions			*/
23 #include "H5Eprivate.h"		/* Error handling		        */
24 #include "H5HLpkg.h"		/* Local heaps				*/
25 #include "H5Iprivate.h"		/* ID Functions		                */
26 #include "H5MMprivate.h"	/* Memory management			*/
27 
28 
29 /*-------------------------------------------------------------------------
30  * Function:	H5HL_debug
31  *
32  * Purpose:	Prints debugging information about a heap.
33  *
34  * Return:	Non-negative on success/Negative on failure
35  *
36  * Programmer:	Robb Matzke
37  *		matzke@llnl.gov
38  *		Aug  1 1997
39  *
40  * Modifications:
41  *		Robb Matzke, 1999-07-28
42  *		The ADDR argument is passed by value.
43  *
44  *              John Mainzer, 6/17/05
45  *              Modified the function to use the new dirtied parameter of
46  *              of H5AC_unprotect() instead of modifying the is_dirty
47  *              field of the cache info.
48  *
49  *-------------------------------------------------------------------------
50  */
51 herr_t
H5HL_debug(H5F_t * f,hid_t dxpl_id,haddr_t addr,FILE * stream,int indent,int fwidth)52 H5HL_debug(H5F_t *f, hid_t dxpl_id, haddr_t addr, FILE * stream, int indent, int fwidth)
53 {
54     H5HL_t		*h = NULL;
55     int			free_block;
56     H5HL_free_t		*freelist;
57     uint8_t		*marker = NULL;
58     size_t		amount_free = 0;
59     herr_t              ret_value = SUCCEED;       /* Return value */
60 
61     FUNC_ENTER_NOAPI(FAIL)
62 
63     /* check arguments */
64     HDassert(f);
65     HDassert(H5F_addr_defined(addr));
66     HDassert(stream);
67     HDassert(indent >= 0);
68     HDassert(fwidth >= 0);
69 
70     if(NULL == (h = (H5HL_t *)H5HL_protect(f, dxpl_id, addr, H5AC_READ)))
71         HGOTO_ERROR(H5E_HEAP, H5E_CANTLOAD, FAIL, "unable to load heap")
72 
73     HDfprintf(stream, "%*sLocal Heap...\n", indent, "");
74     HDfprintf(stream, "%*s%-*s %lu\n", indent, "", fwidth,
75 	    "Header size (in bytes):",
76 	    (unsigned long)h->prfx_size);
77     HDfprintf(stream, "%*s%-*s %a\n", indent, "", fwidth,
78 	      "Address of heap data:",
79 	      h->dblk_addr);
80     HDfprintf(stream, "%*s%-*s %Zu\n", indent, "", fwidth,
81 	    "Data bytes allocated for heap:",
82             h->dblk_size);
83 
84     /*
85      * Traverse the free list and check that all free blocks fall within
86      * the heap and that no two free blocks point to the same region of
87      * the heap.  */
88     if(NULL == (marker = (uint8_t *)H5MM_calloc(h->dblk_size)))
89 	HGOTO_ERROR(H5E_HEAP, H5E_CANTALLOC, FAIL, "memory allocation failed")
90 
91     HDfprintf(stream, "%*sFree Blocks (offset, size):\n", indent, "");
92     for(free_block = 0, freelist = h->freelist; freelist; freelist = freelist->next, free_block++) {
93         char temp_str[32];
94 
95         HDsnprintf(temp_str, sizeof(temp_str), "Block #%d:", free_block);
96 	HDfprintf(stream, "%*s%-*s %8Zu, %8Zu\n", indent+3, "", MAX(0,fwidth-9),
97 		temp_str,
98 		freelist->offset, freelist->size);
99 	if((freelist->offset + freelist->size) > h->dblk_size)
100 	    HDfprintf(stream, "***THAT FREE BLOCK IS OUT OF BOUNDS!\n");
101 	else {
102             int	overlap = 0;
103             size_t i;
104 
105 	    for(i = 0; i < freelist->size; i++) {
106 		if(marker[freelist->offset + i])
107 		    overlap++;
108 		marker[freelist->offset + i] = 1;
109 	    } /* end for */
110 	    if(overlap)
111 		HDfprintf(stream, "***THAT FREE BLOCK OVERLAPPED A PREVIOUS ONE!\n");
112 	    else
113 		amount_free += freelist->size;
114 	} /* end for */
115     } /* end for */
116 
117     if(h->dblk_size)
118 	HDfprintf(stream, "%*s%-*s %.2f%%\n", indent, "", fwidth,
119 		"Percent of heap used:",
120 		((double)100.0f * (double)(h->dblk_size - amount_free) / (double)h->dblk_size));
121 
122     /*
123      * Print the data in a VMS-style octal dump.
124      */
125     H5_buffer_dump(stream, indent, h->dblk_image, marker, (size_t)0, h->dblk_size);
126 
127 done:
128     if(h && H5HL_unprotect(h) < 0)
129 	HDONE_ERROR(H5E_OHDR, H5E_PROTECT, FAIL, "unable to release object header")
130     H5MM_xfree(marker);
131 
132     FUNC_LEAVE_NOAPI(ret_value)
133 } /* end H5HL_debug() */
134 
135