Say "工欲善其事, must first have its device", write the program in C language, the most afraid of encountering a memory leak, memory out of bounds access, the heart that nasty ah ...
If on the I368-linlux, the Valgrind tool is preferred, but on the Arm-linux platform, how, Dmalloc is a good choice. Of course, IBM's purify is another level of player, not to mention.
1. Download dmalloc:www.dmalloc.com to the official website
2. Cross-compiling Dmalloc
2.1 Configure
Because the test program is executed on the PC during the configure process, and the cross-compiled test program does not run on the PC, resulting in configure, I do not add the--host=arm-linux option
./configure--prefix=/opt/crosstool/arm-linux/gcc-3.4.4-glibc-2.3.5/arm-linux/arm-linux/--enable-cxx-- Enable-threads
Where opt/crosstool/arm-linux/gcc-3.4.4-glibc-2.3.5/arm-linux/arm-linux/is the directory of my cross-compilation environment
2.2
Modify environment variables, rescue
Export path=/opt/crosstool/arm-linux/gcc-3.4.4-glibc-2.3.5/arm-linux/arm-linux/bin/: $PATH
The cross-compiler tool is under/opt/crosstool/arm-linux/gcc-3.4.4-glibc-2.3.5/arm-linux/arm-linux/bin/, and the name is GCC, g++ and so on.
At this point, the which GCC command will see the cross-compilation tool GCC
2.3
Make Threadscxx
Make install
In this way, the Dmalloc version of the support thread and C + + is compiled.
3. Environment configuration
The cross-compiled dmalloc is placed on the embedded board and executed./dmalloc-b-L Logfile-i
You will see that there are two lines of output:
Dmalloc_options=debug=0x4e48503,inter=100,log=logfile
Export Dmalloc_options
Execute these two lines as shell scripts, that is, set environment variables
4. Write code Test Dmalloc
Simple program:
#include <signal.h>
#include <unistd.h>
#include <cstdlib>
int main ()
{
Char *p = (char *) malloc (10);
for (int i = 0; i < one; ++i)
P[i] = ' a ';
return 0;
}
The above program has obvious memory out-of-bounds access.
Compile it with the following command:
$ arm-linux-g++ Try.cpp-ddmalloc_func_check-ldmalloc
Generate A.out
Execute on the board./a.out
There will be logfile, the contents are as follows:
948436802:1: Dmalloc version ' 5.5.2 ' from ' http://dmalloc.com/'
948436802:1: Flags = 0x4e48503, logfile ' logfile '
948436802:1: Interval = +, addr = 0, seen # = 0, limit = 0
948436802:1: Starting time = 948436802
948436802:1: Process pid = 5091
948436802:1: Error details:checking user pointer
948436802:1: Pointer ' 0x40016fe8 ' from ' unknown ' prev access ' try.cpp:11 '
948436802:1: Dump of proper fence-top bytes: ' i\336\312\372 '
948436802:1: Dump of ' 0x40016fe8 '-6: ' \300\300\033\253\300\300aaaaaaaaaaa\336\312\372 '
948436802:1: Next pointer ' 0x40017000 ' (size 0) may has run under from ' unknown '
948436802:1: ERROR: _dmalloc_chunk_heap_check:failed over picket-fence magic-number check (Err 27)
Haha, OK
As you can see from the above output, there is an out-of-bounds access when accessing the memory allocated on line 11th of Try.cpp.
5. Dmalloc also supports C + +, but is imperfect and prints no error line numbers
In C + +, to use the-LDMALLOCTHCXX option, can not be used-ldmalloc
In fact, no line number is OK, with the GDB tool and print out the address can also know the line number.
Dmalloc Arm-linux Platform Use