Debugging Dynamic Memory Usage Errors Using HP WDB (766161-001, March 2014)

Example 7 Monitoring a specific address
Sample Program
bash-2.05b$ cat watch-addr.c
1 #include<stdio.h>
2
3 void enable_watch(char *cp)
4 {
5
6 }
7
8 int main()
9 {
10 char *cp = (char*)malloc(100);
11 enable_watch(cp);
12 free(cp);
13 exit(0);
14 }
Sample Debugging Session
$ /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 watch-addr
Reading symbols from watch-addr...done.
(gdb) set heap-check on
(gdb) b main
Breakpoint 1 at 0x2bf0: file watch-addr.c, line 10 from watch-addr.
(gdb) b 13
Breakpoint 2 at 0x2c24: file watch-addr.c, line 13 from watch-addr.
(gdb) r
Starting program: watch-addr
Breakpoint 1, main () at watch-addr.c:10
10 char *cp = (char*)malloc(100);
(gdb) n
11 enable_watch(cp);
(gdb) p/x cp
$1 = 0x4042a3b8
(gdb) set heap-check watch 0x4042a3b8
(gdb) c
Continuing.
warning: Watch address 0x4042a3b8 deallocated
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 0x70e7ada0 in rtc_record_free+0xb8 () from /opt/langtools/lib/librtc.sl
#2 0x70e7b9a0 in free+0xc8 () from /opt/langtools/lib/librtc.sl
#3 0x2c24 in main () at watch-addr.c:12
#4 0x70ee3478 in _start+0xc0 () from /usr/lib/libc.2
(gdb) c
Continuing.
Breakpoint 2, main () at watch-addr.c:13
13 exit(0);
(gdb) q
The program is running. Exit anyway? (y or n) y
30