Debugging Dynamic Memory Usage Errors Using HP WDB (5900-1474; WDB 6.2; January 2011)
Example 11 Monitoring heap-corruption caused by erroneous handling of string functions
Sample Program
bash-2.05b$ cat string.c
1 #include<stdio.h>
2
3 int main()
4 {
5 char *ptr, *ptr1;
6
7 ptr = (char*)malloc(10);
8 ptr1 = (char *)malloc(20);
9
10 strcpy(ptr, "Hello");
11 strcpy(ptr1, "Welcome to HP WDB");
12
13 memcpy(ptr+5,ptr1,10);
14 }
Sample Debugging Session
bash-2.05b$ /opt/langtools/bin/gdb string
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) set heap-check on
(gdb) set heap-check string on
(gdb) b main
Breakpoint 1 at 0x2bdc: file string.c, line 7 from string.
(gdb) r
Starting program: string
Breakpoint 1, main () at string.c:7
7 ptr = (char*)malloc(10);
(gdb) c
Continuing.
warning: memcpy corrupted (address = 0x4042a3dd size = 10)
#1 main() at string.c:7
#2 _start() from /usr/lib/libc.2
#3 _start() from /opt/langtools/lib/librtc.sl
#4 $START$() from
warning: Use command backtrace (bt) to see the current context.
Ignore top 4 frames belonging to leak detection library of gdb.
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 0x70e7cc44 in search_addr+0x64 () from /opt/langtools/lib/librtc.sl
#2 0x70e7cd88 in libc_mem_common+0x130 () from /opt/langtools/lib/librtc.sl
Memory-Debugging Features of WDB 47