chunk size

Chuck Swiger cswiger at
Thu Oct 19 22:02:53 UTC 2006

On Oct 19, 2006, at 9:23 AM, Leidecker at wrote:
> FreeBSD uses another malloc alternative where the data and the  
> informations are splitted into two lists. The informations on sizes  
> are stored in a page direcory list. Entries of that list point to  
> their corresponding page with the data. My question is now,  
> regarding on the usage of gdb: How can I find out, of what size a  
> chunk is? Or, where do I find the page direcory list?

The PHK malloc implementation in /usr/src/lib/libc/stdlib/malloc.c  
declares the page directory to be static:

   static struct   pginfo **page_dir;

...either change this to make the symbol public, or perhaps add a  
_write in malloc_init() to output the location this structure:

% cp /usr/src/lib/libc/stdlib/malloc.c /tmp/malloc.c

Edit as you please, perhaps:

--- /tmp/malloc.c~      Thu Oct 19 17:50:25 2006
+++ /tmp/malloc.c       Thu Oct 19 17:50:30 2006
@@ -212,7 +212,7 @@
static u_long last_index;
/* Pointer to page directory. Allocated "as if with" malloc */
-static struct  pginfo **page_dir;
+struct pginfo **page_dir;
/* How many slots in the page directory */
static unsigned        malloc_ninfo;

% gcc -g -O -Wall -I/usr/src/lib/libc/include -shared -o /tmp/ /tmp/malloc.c
% LD_PRELOAD=/tmp/ gdb /tmp/test
GNU gdb 6.1.1 [FreeBSD]
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and  
you are
welcome to change it and/or distribute copies of it under certain  
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for  
This GDB was configured as "i386-marcel-freebsd"...
(gdb) b malloc
Function "malloc" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (malloc) pending.
(gdb) run
Starting program: /tmp/test
Breakpoint 2 at 0x280793f6: file /tmp/malloc.c, line 1152.
Pending breakpoint "malloc" resolved

Breakpoint 2, malloc (size=10) at /tmp/malloc.c:1152
1152        return (pubrealloc(NULL, size, " in malloc():"));
(gdb) p page_dir
$1 = (struct pginfo **) 0x0
(gdb) n
1153    }
(gdb) p page_dir
$2 = (struct pginfo **) 0x2815d000

However, before you go this route, perhaps you ought to consider what  
problem you are actually trying to solve by doing this.  :-)

You could always build and utilize the Linux malloc implementation,  
or jemalloc from -CURRENT, or even one of the debugging-friendly  
mallocs such as Doug Lea's in /usr/ports/devel/libdlmalloc instead...


More information about the freebsd-questions mailing list