Use mtrace in Linux for memory monitoring

Source: Internet
Author: User
Article Title: Use mtrace in Linux for memory monitoring. Linux is a technology channel of the IT lab in China. Including desktop applications, Linux system management, kernel research, some basic categories such as embedded systems and open-source systems may cause memory overflow and other problems when you write complex programs with many pointers. Debugging is also quite tiring. In fact, there is a tool in Linux that can be used for debugging. This is Mtrace. Mtrace is mainly able to detect some memory allocation and leakage failures. Next, let's take a look at its usage.
 
There are four basic steps to debug a program using Mtrace. Some auxiliary function functions in the gnu c function library are required.
 
1. Include the header file in the program to be tracked, and include a function call: mtrace () at the beginning of the main () function (). Mtrace () is called at the beginning of the main function. Therefore, mtrace can trace and analyze all memory allocation and release operations after the process.
 
2. Define an environment variable to indicate a file. This file is used to output log information. Example:
 
$ Export MALLOC_TRACE = mymemory. log
 
3. The program runs normally. The memory allocation and release operations in the program can be recorded.
 
4. Use mtrace to analyze log files. For example:
 
$ Mtrace testmem $ MALLOC_TRACE
 
Note that in many embedded environments, the export command is not provided and environment variables are not recorded. Is there no way at this time? Maybe the only way is to add the environment variable to the application. The Glibc library contains the putenv function, which is used to write environment variables. For more information, see the manual.
 
Int putenv (char * string) [Function]
 
After environment variables are set. Another problem is: if the program is self-running, that is, when it is not terminated, what should I do if I want to analyze the memory in a specific place?
 
Look at Glibc !, Call the muntrace function, and the log file is generated. Then you can use the mtrace command to analyze the log file.
 
The following is an example.
 
  
[Hwang @ langchao test] $ cat tew.trace. c
# Include <>
# Include
# Include
Int main ()
{
Char * hello;
Mtrace ();
Hello = (char *) malloc (20 );
Sprintf (hello ,"
Hello world! ");
Return 1;
}
[Hwang @ langchao test] $ export MALLOC_TRACE = mytrace. log
[Hwang @ langchao test] $ gcc tew.trace. c-o tew.trace
[Hwang @ langchao test] $./tew.trace
[Hwang @ langchao test] $ mtrace tew.trace mytrace. log
Memory not freed:
-----------------
Address Size Caller
0x08049860 0x14 at/usr/src/build/53700-i386/BUILD/glibc-2.2.4/csu/init. c: 0

 
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.