The testHash program is a multiple-part example showing you how to use Purify to manage memory access errors and memory leaks. (For the introductory Hello World example, click )
The testHash program is located in the <purifyhome>/example directory. The original uncorrected source code is in the hash.c file. The corrected source code is in hash.c_afterpure.
To build the program, copy the example directory to your home directory, then run make.
% cp -r `purify -printhomedir`/example
~/example
% cd ~/example
% make
Note:
Don't worry if your output, debugging information, or memory addresses are not identical to those shown here. This kind of data is compiler and system dependent. Source code line numbers may also differ.
The testHash program implements a hash table and includes "rigorous" testing routines.
Run the testHash program:
% testHash
Testing makeHashTable.
Testing putHash - adding from 0 to 100.
Testing getHash - getting from 0 to 100.
Testing remHash - removing from 0 to 50.
Testing remHash - removing from 0 to 50.
Testing getHash - getting from 0 to 50.
Testing getHash - getting from 50 to 100.
Testing putHash - adding from 0 to 50.
Testing putHash - adding from 50 to 100.
Testing delHashTable.
%
The testHash program shows hash.c as passing this test suite. However, Purify will show that it contains a number of major errors. If these routines are included in a larger program, the errors can appear as crashes in seemingly unrelated code.
Run the instrumented version of testHash:
% testHash.pure
You can see that Purify detects many memory access errors.
The easiest way to track down multiple errors in a program is to run the instrumented program under a debugger and set a breakpoint on the purify_stop_here function. Each time Purify detects a new error, it generates a message and hits the breakpoint in the debugger. This helps identify the error at its origin.
Alternatively, with just-in-time debugging, Purify can automatically attach a debugger to your program when it detects an error.
Unless otherwise noted, the debugging examples in this section use the dbx debugger. The banner information that appears in several of the messages is left out of these examples.
You might notice minor differences between these examples and the implementation of dbx on IRIX.
% dbx testHash.pure
Reading symbolic information...
Read 2588 symbols
(dbx) stop in purify_stop_here
(2) stop in purify_stop_here
(dbx) run
Running: testHash.pure
Testing makeHashTable.
Testing putHash - adding from 0 to 100.
stopped in purify_stop_here at 0x2dd4
purify_stop_here:nop
Current function is putHash
135 entry && strcmp (entry->key,
key);
(dbx)
On HP-UX, use the wrapper script for xdb, located in <producthome>/purify_xdb. This configures the debugger for Purify'd programs. (Purify provides similar scripts for dde and softdebug.) You should also add the line:
z 18 sir
to your ~/.xdbrc file. See the README file in the purifyhome directory for more details.
% purify_xdb testHash.pure
200: * value should be there or not.
201: */
202: int main()
203: {
204: hashtable* ht;
205: char* testTable[TABLE_SIZE];
206:
> 207: fillTestTable(testTable);
208:
209: ht = testMakeHashTable();
210: testPutHash(ht, testTable, 0, 100, FALSE);
211: testGetHash(ht, testTable, 0, 100, TRUE);
212: testRemHash(ht, testTable, 0, 50, TRUE);
213: testRemHash(ht, testTable, 0, 50, FALSE);
214: testGetHash(ht, testTable, 0, 50, FALSE);
File: testHash.c Procedure: main Line: 207
Copyright Hewlett-Packard Co. 1985,1987-1992. All Rights Reserved.
<<<< XDB Version A.09.01 HP-UX >>>>
No core file
Procedures: 15
Files: 3
>Sig Stop Ignore Report Name
18 No Yes No death of child
>b purify_stop_here
Overall breakpoints state: ACTIVE
Added:
1: count: 1 Active purify_stop_here: 1:
>r
Starting process 24385: "testHash.pure"
Wait...loading shared-library map tables. Done.
Testing makeHashTable.
Testing putHash - adding from 0 to 100.
breakpoint at 0x00029a88
purify_stop_here.c: purify_stop_here: 1:
>d
You're now ready to find and correct memory access errors. To proceed, click