CC ++ Memory Question Terrier - Purify (3)

zhaozj2021-02-08  316

V. File descriptor

In the above memory problem table, for most memory issues, I believe that for those who are familiar with C / C , it is not strange. There are some contents about WatchPoint and file descriptors, which may make you look a relatively blurry. For WatchPoint, I will talk later. In this section, I will say how the problem is generated by an example, and therefore introduce some of the features of Purify.

First look at the following program:

#include

Main ()

{

File * fp;

Int Num;

fp = fopen ("./ Test.txt", "R");

IF (fp == NULL) {

PERROR ("Error:");

EXIT (-1);

}

FSCANF (FP, "% D", & NUM);

IF (Num <0) {

Printf ("Error: The Num Should Be Greater Than 0! / N");

EXIT (-1);

}

Fclose (fp);

}

Built a test.txt file in the current directory and set its contents to -20. Compile and run the program with Purify:

> Purify GCC -G -O Testfd Testfd.c

PURIFY 2003.06.00 Solaris 2 (32-BIT) Copyright (C) 1992-2002 Rational Software Corp. All Rights Reserved.

Instrumenting: ccqqf6py.o linking

> ./ Testfd

The following screen appears:

In the figure, we can see that the Purify report has a FIU error. It means that we do not close the file descriptor when you quit. There are also a safe file descriptor information, which is the FIU of the three standard file descriptors of 0, 1, 2, which is normal, so there is no small triangle symbol in front of it.

With this example, we can see that Purify can not only find memory error, but also find errors for file descriptors.

If you don't want Purify to display FIU information, you can set up PURIFDS-INUSE-AT-EXIT = NO option, such as:

> Purify -fds-inuse-at-exit gcc -g -o testfd testfd.c

Or use the Purify's API function purify_clear_fds_inuse to block the display, you can call the PURIFY API function in your program. I will tell you later about the details of the PURIFY API function.

6. Controlling the output of PURIFY

1, generate ASCII text files

By default, Purify will display a graphical window to report information. Of course, if your environment is limited, you don't want Purify to appear graphical interfaces, just generate text files to report, you can have a Purify parameter, you can easily do this.

When compiled, you only need to make a simple set of Purify's compilation parameters to do, such as:

> purify -windows = no gcc -g -o hello hello.c

Purify will write its report information to a standard error device, in text mode, Purify does not report that the same error occurs, but only the reported information. We can use two ways to make Purify information to the text file.

The first is the redirection function of the operating system, such as:

Under CSH:% a.out.pure> & a.out.Messages

Under SH and KSH: $ a.out.pure 2> a.out.Messages

The second is to specify the log file parameters of the Purify, such as:

-log-file = .plog

Below, it is a PURIFY generated ascii text file:

> ./hello

**** Purify Instrument Hello (PID 25698 AT WED DEC 10

22:29:33

2003)

* Purify 2003.06.00 Solaris 2 (32-bit) Copyright (C) 1992-2002 Rational Software Corp. All Rights Reserved.

* For Contact Information Type: "Purify-Help"

* Options settings: -follow-child-processes = YES -PURIFY -WINDOWS = NO /

-purify-home = / usr / rate / releases / purify.sol.2003.06.00 /

-gcc3_path = / usr / local / bin / GCC /

-Cache-Dir = / usr / rate / releases / purify.sol.2003.06.00 / cache /

-demangle_program = / usr / local / bin / C filt

* License successfully checked out.

* Command-line: ./hello

**** Purify Instrument Hello (PID 25698) ****

ABR: ARRAY BOUNDS READ:

* This is occring while in:

Strlen [RTLIB.O]

_DOPRNT [libc.so.1]

Printf [libc.so.1]

Main [hello.c: 11]

_Start [crt1.o]

* Reading 13 bytes from 0x8EA08 in The Heap (1 Byte At 0x8EA14 ILLEGAL).

* Address 0x8ea08 is at the beginning of a malloc'd block of 12 bytes.

* This Block Was Allocated from:

Malloc [rtlib.o]

Main [hello.c: 8]

_Start [crt1.o]

Hello, World

**** Purify Instrument Hello (PID 25698) ****

Current File Descriptors in Use: 5

FIU: File Descriptor 0:

FIU: File Descriptor 1:

FIU: File Descriptor 2:

FIU: File Descriptor 26: FIU: File Description 27:

**** Purify Instrument Hello (PID 25698) ****

Purify: Searching for All Memory Leaks ...

Memory Leaked: 12 bytes (100%); potentially leaked: 0 Bytes (0%)

MLK: 12 bytes Leaked at 0x8EA08

* This Memory Was Allocated from:

Malloc [rtlib.o]

Main [hello.c: 8]

_Start [crt1.o]

Purify Heap Analysis (Combining Suppressed and Unsuppressed Blocks)

Blocks Bytes

Leaked 1 12

Potentially Leaked 0 0

IN-USE 0 0

----------------------------------------

Total Allocated 1 12

**** Purify Instrument Hello (PID 25698) ****

* Program EXITED with STATUS CODE 13.

* 1 Access Error, 1 Total Occurrence.

* 12 bytes leaked.

* 0 Bytes potentially leaked.

* Basic Memory USAGE (include purify overhead):

351348 CODE

101724 DATA / BSS

8192 HEAP (PEAK USE)

1272 Stack

* Shared Library Memory Usage (Including Purify Overhead):

992 Libpure_Solaris2_init.so.1 (Shared Code)

280 Libpure_Solaris2_init.so.1 (Private Data)

1079516 libc.so.1_pure_p3_c0_111202132_58_32_1158500s (Shared Code)

31404 libc.so.1_pure_p3_c0_111202132_58_32_1158500s (Private Data)

2324 libdl.so.1_pure_p3_c0_111202132_58_32_4624s (Shared Code)

4 libdl.so.1_pure_p3_c0_111202132_58_32_4624s (Private Data)

14048 Libinternal_stubs.so.1 (Shared Code)

940 libinternal_stubs.so.1 (Private Data)

2, generate purify own files

By viewing the ASCII text file, we found that it is not easy to view, especially when the error is much more, and uses no source code in the file, it is worse than the graphical interface. But in order to transfer the PURIFY's report information to others, the file and graphics interface are both, we can use the Purify own file called the Purify View file. We can open this file using the PURIFY graphical interface to view it under the graphical window. We can have two ways to get this file. One is to create it in the menu of the PURIFY graphics interface. The second method is to set the Purify View file using the PURIFY -View-File = .pv parameter.

When you open this file, you either select the "Open" menu in the Purify menu, or use this command:

% Purify -View .pv

3, automatically send mail

Use the -mail-to-user parameter using the PURIFY to easily let Purify send a report message. Such as:

% purify -mail-to-user = chris gcc ...

% purify -mail-to-user = chris, Pat GCC ...

% purify -mail-to-user = devgrp gcc ...

By default, as long as you set this parameter, Purify does not open the graphical interface window, if you want to turn on the graphic window, then you use the -Windows = YES parameter.

4, output your own information

If you want to output your own information in Purify, you can use the Purify's API function in your program:

l Purify_Printf (const char * fmt, ...) Using this function can output your own information in the PURIFY graphical interface, file file.

l Purify_logfile_printf (const char * fmt, ...) Using this function You can output your own information in the Purify's ASCII text file.

l purify_printf_with_call_chain (const char * fmt, ...) Using this function can print out the function calling the stack while the PURIFY output. This function is very similar to Purify_Printf.

Note that the above three functions and the Printf function in the standard C are almost the same, but these functions do not support all% formats in the Printf function, which only support:% D,% U,% N, % S,% C,% E,% F, and% g of these formats, and the% E% F% G and does not support its precision definition.

<- Previous Next->

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

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

New Post(0)