Debugging Dynamic Memory Usage Errors Using HP WDB
Scrambling a Heap Block
The set heap-check scramble <on/off> command enables you to scramble a heap block
and overwrite it with a specific pattern ("0xfeedface") when it is allocated or de-allocated.
If the application continues to use (read) a freed block (incorrect memory usage), the application
fails to find the expected data in the block. (This means that the data in the block is different from
the initial data that was written in the block.)
This increases the chances of the application to crash or result in unpredictable program behavior
sooner with the unexpected data that is read from the block. Additionally, you can detect this
condition with assertion checks in the code to validate the read data during the further run of
the application.
This command does not detect the corruption. It is only a minimal aid to detect corruption.
Example 14 (page 54) illustrates the scramble feature in WDB.
Example 14 Scrambling a memory block on de-allocation
Sample Program
$ cat scramble.c
1 #include <stdio.h>
2 #include <malloc.h>
3
4 int
5 main ()
6 {
7 char **tp;
8 tp = malloc (100);
9 printf ("Batch RTC test over, *tp=%p.\n", *tp);
10 fflush(stdout);
11 free(tp);
12 exit (0);
13 }
Sample Debugging Session
$ gdb scramble
HP gdb 5.5 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.8 (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 scramble on
(gdb) b main
Breakpoint 1 at 0x295c: file scramble.c, line 8 from scramble.
(gdb) r
Starting program: scramble
Breakpoint 1, main () at scramble.c:8
8 tp = malloc (100);
(gdb) n
9 printf ("Batch RTC test over, *tp=%p.\n", *tp);
(gdb) p *tp
$1 = 0xfeedface <Error reading address 0xfeedface: Bad address>
54