Debugging Dynamic Memory Usage Errors Using HP WDB
Example 6 Simulating out-of-memory conditions after a random number of allocations
Sample Program
bash-2.05b$ cat null-random.c
1 #include <stdio.h>
2 #include <malloc.h>
3
4 int main()
5 {
6 int i;
7 char *a;
8 for (i=0; i <= 500; i++) {
9 a = malloc(100);
10 if (a == NULL)
11 {
12 printf("Out of memory simulated\n");
13 }
14 }
15 exit (0);
16 }
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 null-random
Reading symbols from null-random...done.
(gdb) set heap-check on
(gdb) b main
Breakpoint 1 at 0x2be4: file null-random.c, line 8 from null-random.
(gdb) b 12
Breakpoint 2 at 0x2c10: file null-random.c, line 12 from null-random.
(gdb) r
Starting program: null-random
Breakpoint 1, main () at null-random.c:8
8 for (i=0; i <= 500; i++) {
(gdb) set heap-check null-check random
(gdb) catch nomem
Catchpoint 3 (nomem)
(gdb) c
Continuing.
warning: Malloc is returning simulated 0x00000000 value
0x70e78e8c in __rtc_nomem_event+0x4 () from /opt/langtools/lib/librtc.sl
(gdb) c
Continuing.
Breakpoint 2, main () at null-random.c:12
12 printf("Out of memory simulated\n");
(gdb) p i
$1 = 55
(gdb) c
Continuing.
Out of memory simulated
warning: Malloc is returning simulated 0x00000000 value
0x70e78e8c in __rtc_nomem_event+0x4 () from /opt/langtools/lib/librtc.sl
(gdb) c
Continuing.
Memory-Debugging Features of WDB 35