Calling Purify watchpoint functions

You can set watchpoints by calling one of Purify's watchpoint functions, either from your debugger or from the program itself. Each watchpoint function takes the address of the beginning of the memory segment to watch and defines a watchpoint over a certain number of bytes in that segment.

Depending on the function, Purify watchpoints trap when any of the specified bytes are read, written, allocated, or freed. For example, the default watchpoint function

purify_watch(char *addr)

watches 4 bytes of memory, trapping when any of the 4 bytes are written, allocated, or freed. It does not trap when the bytes are read.

Purify provides other functions to handle the common cases of watching writes (and optionally reads) over 1, 2, 4, and 8 bytes. For maximum flexibility, the function

purify_watch_n(char *addr, unsigned int size, char *type)

watches a segment of size bytes starting at addr, trapping whenever any of those bytes are allocated or freed and, depending on the type argument, trapping when any of those bytes are read ("r"), written ("w"), or either read or written ("rw").

Purify assigns a number to each watchpoint so you can easily identify it. To print the list of current watchpoints, call the function purify_watch_info. To remove a specific watchpoint, call purify_watch_remove with the appropriate watchpoint number. Call purify_watch_remove_all to remove all the watchpoints.

Purify saves watchpoints in a file called ./<programname>.watchpoints. For more information, read Saving watchpoints.

Stopping at watchpoints in a debugger

To stop at a watchpoint in a debugger, place a breakpoint on purify_stop_here. Purify stops at this point for both memory access errors and for watchpoints.

A watchpoint example

In this example, a watchpoint is set in the program using the function purify_watch on line 8. This watchpoint monitors all writes to errno, a system variable that records error codes for system calls.

On line 10, errno is initialized to 0. On line 11, the system call close is invoked using an invalid file descriptor 1000. A watchpoint message is triggered when errno is set to 0, and again when the close call sets that variable.

The watchpoint message is triggered when close is invoked:

The function on the top of the stack in this message is cerror instead of close. This is because all system calls that set errno tail-call the function cerror, which sets errno and then returns to the original caller of the system function.