CC ++ Memory Question Terrier - Purify (5)

zhaozj2021-02-08  193

Ten, set WatchPoint

You can set some WatchPoint for programs you want to monitor, so that you will debug the program or easier to find out the problem. As I said earlier, Purify can find your memory leak, but it cannot find memory leaks, you can track a memory by setting WatchPoint to find access to the memory during program execution.

Purify's WatchPoint can generate the following message:

l Watch (read by WatchPoint)

l WPW (written by WatchPoint)

l WPM (allocated by WatchPoint)

l WPF (released by WatchPoint)

l WPN (arrived to the SCOPE of WatchPoint's memory)

l WPX (SCOPE that leaving WatchPoint's memory)

Once you set a WatchPoint, Purify will automatically report the above information to tell you the access situation of memory. It is convenient for your debugging.

WatchPoint is usually used when you track a memory when you debug a procedure, you can also use it to track some system-level global variables such as errno. Once errno is written, it will immediately report a WPW message. After expanding, you can see the stack of functions, and which system call has occurred after calling. This use is convenient for us to find some non-memory issues.

Everyone may have a feeling, that is, in the general debugger, such as the WatchPoint setting in GDB (for GDB, please refer to my article "with GDB Debugger"), then WatchPoint in the debugger What is the difference between purify? Here are some places where WatchPoint in GDB is:

1) WatchPoint in GDB is used in single step tracking.

2) WatchPoint in GDB can only leave Scope in Scope in its memory, WatchPoint will be deleted.

3) Set a 4-byte memory WatchPoint in GDB, which will make the running efficiency of the program dropped 1000 orders.

WatchPoint in Purify effectively overcomes these issues, which monitors all memory usage in the global scope, and its speed is much larger than a series of debuggers such as GDB.

There are two ways to set up PURIFY's WatchPoint, one is using WatchPoint API functions in the program, one is to use (such as: gdb) directly in the debugger, let me introduce these two usage:

1. Use it in the program. Write down this program:

#include

Main ()

{

INT I;

Printf ("NOTE: Errno = 0x% x / n", errno);

Purify_watch (& Errno);

Errno = 0;

CLOSE (1000);

exit (0);

}

Compile with PURIFY:> Sudo Purify GCC -G -O Watch Watch.c After running, we can see the following screen:

We can see that PURIFY successfully monitors Errno variables. We can also see the monitored variables to change the value before and after changing.

2, used in GDB.

In GDB, we can simply use the GDB's print command to reach the Purify's WatchPoint purposes. This is the power of purify, which is called JIT (Just-in-Time) for this technology.

Example:

GDB) Print Purify_Watch (& my_STR)

(GDB) Print Purify_Watch_1 (& my_CHAR)

(GDB) Print Purify_WATCH_N (BUF, SIZEOF (BUF), "RW")

(DBX) Print Purify_WATCH_N (Write_Only_buf, 100, "W")

Let's take a look at the API function of Purify's WatchPoint, which is divided into three categories:

• Set the class int purify_watch (char * addr) to monitor the specified memory, char * represents single-byte units. INT purify_watch_ (char * addr) = 1, 2, 4, 8 in the is a number, which can be 1, 2, 4, 8, monitoring single bytes, double bytes, Four bytes, eight bytes. The function name is: purify_watch_1 (), purify_watch_4 (), purify_watch_8. INT purify_watch_n (char * addr, unsigned int size, char * type) (type = "r", "W" or "rw") Monitors the memory of a specific length, TYPE value is "R", "W" "rw" It means that the monitoring memory is written or written.

• Query class int purify_watch_info (). Print the situation where the currently set WatchPoint (usually used in the debugger of the GDB class). It is a bit like a gdb info watch command.

• Delete class int purify_watch_remove (int Watchno) Delete the specified WatchPoint, whose WatchNo is the return value of the function to set the WatchPoint. INT purify_watch_remove_all () Deletes all WatchPoint.

Eleven, using the parameters of PURIFY

Purify has a lot of parameters, and I don't have much specific parameters, please refer to your manual. Here, I simply tell the rules and methods of using their parameters.

The rules for the parameters of Purify are as follows:

1, must start with the character, that is, the minus number.

2. There is no space in both ends of the equal sign (=).

3, ignore the case of parameter names and variables.

4. The connectors in its parameters can be minus, underscore, or simply. Such as: -leaks-at-exit, -leaks_at_exit and -leaksatexit are one thing.

5. In the parameter, if you want to refer to multiple paths, you can separate it with a colon or space. Use the quotation when using spaces. Such as:% purify -user-path = '/ usr / home / program / usr / home / program1'% purify -user-path = / usr / home / program: / usr / home / Program1

6. Separate with a comma when writing multiple mail users. Don't go back to space. For example:% purify -mail-to-user = chris, PAT, KAM7, you can use wildcard or escape strings. Such as: program * and -log-file =. /% V.plog

The type of PURIFY parameters has three - Boolean, strings, and integers, such as:

-LEAKS-AT-EXIT = YES Boolean

-log-file =. / Pureout string type

-Chain-length = 10 integer type

There are three methods for setting parameters:

1. In the graphics window, set it in the dialog box by clicking the "Options -> Runtime" menu.

2, through two environment variables - PurifyOptions or PureOptions, such as:% set = new $ purifyoptions "-log-file = new $ purifyoptions" under SH or KSH: $ purifyOptions = "- log-file = new $ PurifyOptions "; Export / PurifyOptions

3, in the command line of the Link program. Such as: $ purify -cache-dir = $ home / pcache -always-use-cache-dir $ cc ...

Twelve, using purify API functions

There are many functions of Purify, and I don't know here, and the specific details, please refer to the usage manual. I only talk about the use of Purify's API functions. In general, there are two ways we can use the PURIFY API function.

1. Call in our debugger, such as: gdb) Print Purify_Describe (AddR) (DBX) Call Purify_WHAT_COLORS (BUF, SIZEOF (BUF)) (XDB) P Purify_Describe (AddR) Note: For purify_stop_here, we can use. (GDB) BREAK PURIFY_STOP_HERE (DBX) Stop in purify_stop_here (xdb) b purify_stop_here

2. Call in your own program. To call the PURIFY API function in the program, we need the following two steps: 1) Plus header files: #include 2) Put the lib file in the path that can be searched. Mainly a dynamic link library file libpurify_stubs.so and a static link library file libpurify_stubs.a

Thirteen, conclude

Purify is a very powerful tool, but unfortunately it can only be used in a few platforms. It seems that there are many features in our Windows, I use the SUN Solaris version of the Solaris version, very good, because our program is going to run on all UNIX, with a C cross platform, so use Solaris to do tester. I haven't used the PURIFY of other platforms, I don't know if I'm doing it under Solaris, I hope to have experienced peers. Anyway, I want to believe that IBM's Rational Department will do it better and better.

For this article, I originally placed this article in September or October. However, there is no way, I am too busy before, I am busy a bunch of paste, as long as I drag it to the present, now it is better, but my brain is not easy, I found that my head is stupid during writing. So what is wrong with something, especially if I use five input methods, so there is something wrong with the wrong word, it will make you can't understand, please forgive me. Ok, let me say, so tired. Or leave my contact information, welcome to discuss communication with me. I am currently designed and managed under UNIX, so it is more familiar to software development under UNIX, of course, not only technology, implementation, software design, system analysis, project management, I also have a slight experience . Welcome everyone to find me. (MSN is: haoel@hotmail.com (common) QQ is: 753640 (basically no need, because unsafe))

<- Previous Page

(Full text)

(All rights reserved, please indicate the author and the source when reproduced)

转载请注明原文地址:https://www.9cbs.com/read-1134.html

New Post(0)