Debugging Dynamic Memory Usage Errors Using HP WDB

Example 9 Monitoring program heap growth
Sample Program
bash-2.05b$ cat heap-size.c
1 #include<stdio.h>
2
3 int main()
4 {
5 char * cp;
6 printf("Start of the program\n");
7 cp = (char *)malloc(1024 *1024*10);
8 free (cp);
9 cp = (char *)malloc(1024 *1024*8);
10 free (cp);
11 exit(0);
12 }
Sample Debugging Session
bash-2.05b$ /opt/langtools/bin/gdb
HP gdb 5.5.0 for PA-RISC 1.1 or 2.0 (narrow), HP-UX 11.00
and target hppa1.1-hp-hpux11.00.
Copyright 1986 - 2001 Free Software Foundation, Inc.
Hewlett-Packard Wildebeest 5.5.0 (based on GDB) is covered
by the GNU General Public License. Type "show copying" to
see the conditions to change it and/or distribute copies.
Type "show warranty" for warranty/support.
(gdb) file heap-size
Reading symbols from heap-size...done.
(gdb) set heap-check on
(gdb) set heap-check heap-size 8000000
(gdb) b main
Breakpoint 1 at 0x2c04: file heap-size.c, line 6 from heap-size.
(gdb) b 11
Breakpoint 2 at 0x2c60: file heap-size.c, line 11 from heap-size.
(gdb) r
Starting program: heap-size
Breakpoint 1, main () at heap-size.c:6
6 printf("Start of the program\n");
(gdb) c
Continuing.
Start of the program
warning: Attempt to grow the heap at 0x4042c3e0
0x70e78d7c in __rtc_event+0 () from /opt/langtools/lib/librtc.sl
(gdb) bt
#0 0x70e78d7c in __rtc_event+0 () from /opt/langtools/lib/librtc.sl
#1 0x70e7aff4 in malloc_padded+0xa8 () from /opt/langtools/lib/librtc.sl
#2 0x70e7b634 in malloc+0xd4 () from /opt/langtools/lib/librtc.sl
#3 0x2c28 in main () at heap-size.c:7
#4 0x70ee3478 in _start+0xc0 () from /usr/lib/libc.2
(gdb) c
Continuing.
Breakpoint 2, main () at heap-size.c:11
11 exit(0);
42