Introduction to Dr Memory
Dr Memory is an open source free memory detection tool that detects memory-related programming errors, such as uninitialized access, illegal memory access, and memory leaks. Not only can it work under Linux, but it can also work on Microsoft's Windows operating system. However, when this article was written, Drmemory only supported 32-bit programs, which is a huge flaw, but believe that as development progresses, Drmemory will launch a version that supports 64-bit programs.
Dr Memory is similar to Valgrind, and can directly check the compiled executable. Users do not have to overwrite the source code of the program being checked, nor do they need to relink the Third-party library files, which is very convenient to use.
Dr Memory is built on the Dynamorio dynamic binary piling platform. Dynamic monitoring program operation, and memory access related to the implementation of the code to dynamically modify, record its behavior, and the use of advanced algorithms for error checking.
According to the Drmemory developer's paper on CGO 2011, Practical Memory Checking with Dr memory,drmemory has less impact on the normal execution of programs, which is the more advanced of the same tools. Its performance and Valgrind comparisons are shown in Figure 1 (the picture originates from the Drmemory homepage):
Figure 1. and Valgrind Performance Comparison
Valgrind has a large impact on the normal operation of the program, in general, if the full memory testing, the program can run 50 to 300 times times slower. and Drmemory has certain advantages in this respect.
Ease of use and performance are the main benefits of drmemory, and drmemory can be used to debug Windows programs, so it is widely considered a valgrind alternative on Windows. In the Linux platform, Drmemory can also often be a choice other than Valgrind.
Drmemory the memory leak monitoring using a relatively unique algorithm, a large number of reduced "false positive", that is, false errors. If you still can't find memory errors in your program after using tools such as Valgrind, try Drmemory.
On Linux, the current version of Drmemory is not yet able to debug 64-bit programs, which is a relatively large disadvantage.
Installation of Drmemory
On Linux, installing Dr Memory is very simple, simply unzip the download package, such as:
TAR–XZVF drmemory-linux-1.4.6-2.tar.gz
To use Drmemory, make sure that the following software is installed correctly:
Perl, Objdump, Addr2line.
In any current Linux distribution, these software should all be installed, so basically you just need to download the Drmemory tar package and then unzip it to use it.
Drmemory on Windows provides an executable installation package that can be installed by simply clicking the next step.
Hello Drmemory, First impressions
The use of drmemory is simple, it can be said that it is a fool-type. When running a program normally, we typed the command in the shell and then enter. In order to check with drmemory, just add the drmemory.pl before the normal command, such as the program Checker T, then:
drmemory.pl./t
In the computer field, Helloworld is always the first program. Let's write a hellodrmemory and have a simple touch with drmemory.
Listing 1,hello Drmem Example program
1:int Main ()
2: {
3:char *ptr;
4:int i;
5:for (i=0;i<100;i++)
6: {
7:ptr= (char*) malloc (i);
8:if (i%2) free (PTR);
9:}
10:return 0;
11:}
Obviously, there are 50 memory leaks, all in the same line of code (lines 8). Let's check it with Dr Memory.
-bash-3.2$ gcc-m32-o T t1.c
-G-bash-3.2$ drmemory.pl-bash-3.2$.//
~~dr.m~~ ~~dr.m~~
ERRORS found:< c5/>~~dr.m~~ 0 Unique, 0 Total unaddressable access (es)
~~dr.m~~ 0 Unique, 0 Total uninitialized Access (es)
~~dr.m~~ 0 Unique, 0 Total Invalid heap argument (s)
~~dr.m~~ 0 unique, 0 Total Warning (s)
~~dr.m~~ 1 Unique, total , 0 byte (s) of leak (s)
~~dr.m~~ 0 Unique, 0 Total, 0 byte (s) of possible leak (s)
~~dr.m~~ ERRORS ignored:
~~dr.m~~ 0 still-reachable Allocation (s)
~~dr.m~~ Details:/home/.../logs/drmemory-t.15086.000/results.txt
There will be a summary of the errors shown on the screen, looking at line 5th below ERRORS FOUND: "Total leaks". It's not bad. According to the prompts, more details are written to a result text file. Open and view the file to find out where the program is having memory errors. It's so convenient. But is the result file easy to read? Let's explain in detail how to read the result file produced by Drmemory.
Drmemory report read details memory illegal access
Drmemory believes that any reading and writing to unallocated memory areas is illegal. In Linux, an application can allocate memory in several ways:
Call Mmap (or Mremap)
Call malloc allocate memory on the heap
allocating memory on the stack using Alloca
Illegal access is an access to an area of memory that is allocated in the above three methods. Common problems include buffer overflow, array bounds, read-write memory, stack overflow, and so on. Let's test the following problem program.