Introduction to testing, debugging, and performance monitoring commands in Linux

Source: Internet
Author: User
For ~ 6 these commands seem simple to be used separately, but they can exert their real power only when combined with redirection and regular expressions to form more complex functions or even automated scripts. Cmp and diff can be used for version control and compare the differences between two version files. They can also be used to compare program output. 1. cmp byte comparison

For ~ 6 these commands seem simple to be used separately, but they can exert their real power only when combined with redirection and regular expressions to form more complex functions or even automated scripts.

Cmp and diff can be used for version control and compare the differences between two version files. They can also be used to compare program output.

1. cmp

Compare two files by byte and output them. If there is no option, the first difference is output.

Common options:

-L outputs all different bytes and displays the number of bytes (decimal) and each different byte (octal ). For example, if file1.txtin the 3th node is Octal to 151,flie2.txt is 12, it is displayed as follows:

3 151 12

2. diff

Compare two files one by one. if a directory is specified, compare the files with the same name under the Directory.

The output is: number of lines in file 1 + format prompt + number of lines in file 2, where the format prompt is a, c, d, corresponding to add, change, delete.

The exit value of diff has a specific meaning. 0 indicates no difference, 1 indicates finding different places, and 2 indicates an error.

Common options:

-Y -- side-by-side is output in two columns. Use | to mark different rows of the two, <标注后者比前者少一行,> The former is less than the latter. You can also add the following options:
-W num -- width = NUM the output can print a maximum of NUM (130 by default) columns.
-- Left-column: only the left column of the total row is output.

-C-c num -- context [= NUM]: output the context of NUM (default: 3) row replication. The full text of both files is displayed.

-U-u num -- uniied [= NUM]: output the context of NUM (default 3) row merging. The same parts of the two files are only displayed once.

-RuN generates a patch file in the format of diff-ruN file1 file2> patch. dat. then, you can use patch file1 patch. dat to update the content of file1 to be consistent with that of file2.

Sort, sum, and wc can also process files and outputs as needed. For Linux, which can redirect stdin to a file, the output processing is the same as the file processing.

3. sort

Output rows in alphabetical order. This command can process multiple files at the same time.

Common options:

-U remove duplicate rows

-R descending output

-N is sorted by number (so that 2 won't be ranked after 10)

-T is separated by a separator. you can use-k to specify the number of columns.

4. sum

Calculate the file checksum and the number of blocks occupied. The size of the checksum algorithm and block can be set according to the options.

5. wc

Number of lines, words, and bytes of the output file. If there is more than one file, the total number of lines will be output.

Common options:

-C: number of output bytes

-M: number of output characters

-L number of output rows

-W: number of output words (word)

6. grep

Search for and display the content that matches the regular expression in the text. Here we will not mention how powerful the regular expression is. its rules can be written into a blog article separately, and many people have already done so. The following are some usage inspirations. after reading this article, we will feel that the original "search" needs are so diverse. The example is out of "program design practices":

Which files use the Regexp class? % Grep Regexp *. java

In which file is the implementation of this class? % Grep 'class. * Regexp '*. java

How many empty lines are in the file? % Grep '.' *. c ++ | wc

Statistics and determination of Running time: time and gprof

7. time

Usage: $ time executable file/Command

Displays the total time, user time, and system time of the program running.

For a program with short execution time, the running time may be displayed as 0.

Because it is not collected by the program execution itself. if the time command is stopped or switched out of memory, the displayed time may be greater than the actual time.

8. gprof

Measure the running time and number of calls of each function when the program is running. This is very helpful for code optimization. through this statistics, you can find functions with a large time-consuming ratio and a large number of calls and perform alignment optimization to improve program performance. For functions with less execution and less time consumption, even if there is a possibility of optimization, you do not have to worry about the optimization scheme, because the optimization of it does not significantly improve the performance.
If you need gprof, you need to add the-pg option during gcc. a gmon. out file is generated.

Code error: gcc-Wall option, lint
9. gcc-Wall

The reason for listing this gcc option is that its role is easily ignored. The difference between investors and gamblers is that investors take advantage of every opportunity, no matter how small it is, while gamblers rely only on luck. The author suggests applying this sentence to programming, selecting all the optional warning options for the compilation program, and regard it as a program investment with no risk and high repayment. Do not ask "should I use this warning facility ?" "Why not use this warning facility ?" Enable all warning options unless there is an excellent reason for not doing so.

10. lint

At first, lint was used to check unportable code in C language, but now it is not just a portability check program, you can also check out the features in the code that are portable and fully compliant with the syntax but are likely to be incorrect.

Lint has many versions. in Linux, splint can be used for installation through apt-get.

Integrated debugging: gdb

11. gdb

Gdb is a comprehensive debugging tool that provides functions such as breakpoint, variable monitoring and setting, signal, function stack viewing, and single-step execution, there are many related blog posts for reference. I have also made a record: gdb debugging command. Others: strace

12. strace

Strace can trace the system calls generated by a process, including parameters, return values, and execution time.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.