Debugging Dynamic Memory Usage Errors Using HP WDB v6.3 (5900-2181, August 2012)
Example 4 Simulating out-of-memory conditions after <N> allocations
Sample Program
bash-2.05b$ cat null-check.c
1 #include <stdio.h>
2 #include <malloc.h>
3
4 int cnt;
5
6 void break_here()
7 {
8 }
9
10 void test_null_check()
11 {
12 int i;
13 char *a;
14
15 cnt = 0;
16 for(i = 0; i <= 10; i++) {
17 a = malloc(100);
18 if (a == NULL)
19 printf("Out of memory scenario simulated\n");
20 }
21 }
22
23 int main()
24 {
25 test_null_check();
26 exit (0);
27 }
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 null
Reading symbols from null...done.
(gdb) set heap-check null-check 6
(gdb) b main
Breakpoint 1 at 0x2c64: file null-check.c, line 25 from null.
(gdb) b 19
Breakpoint 2 at 0x2c10: file null-check.c, line 19 from null.
(gdb) r
Starting program: null
Breakpoint 1, main () at null-check.c:25
25 test_null_check();
(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.
24