|
3.8 Using Dmalloc With a DebuggerHere are a number of possible scenarios for using the dmalloc library to track down problems with your program. You should first enable a logfile filename and turn on a set of debug features. You can use dmalloc -l logfile low to accomplish this. If you are interested in having the error messages printed to your terminal as well, enable the `print-messages' token by typing dmalloc -p print-messages afterwards. See section Dmalloc Utility Program. Now you can enter your debugger (I use the excellent GNU debugger
gdb), and put a break-point in If you are using GDB, I would recommend adding the contents of
`dmalloc.gdb' in the `contrib' subdirectory to your
`.gdbinit' file in your home directory. This enables the
If you are using shared libraries, you may want to execute the following commands initially to load in dmalloc and other library symbols:
3.8.1 Diagnosing General Problems with a DebuggerIf your program stops at the To get more information about the problem, first print via the debugger the dmalloc_errno variable to get the library's internal error code. You can suspend your debugger and run dmalloc -e value-returned-from-print to get an English translation of the error. A number of the error messages are designed to indicate specific problems with the library administrative structures and may not be user-friendly. If the problem was due to the arguments or system allocations then the
source of the problem has been found. However, if some code did
something wrong, you may have some more work to do to locate the actual
problem. The You may also want to put calls to
3.8.2 Tracking Down Non-Freed MemorySo you've run your program, examined the log-file and discovered (to your horror) some un-freed memory. Memory leaks can become large problems since even the smallest and most insignificant leak can starve the program given the right circumstances.
Above you will see a sample of some non-freed memory messages from the logfile. In the first line the `0x45008' is the pointer that was not freed, the `12 bytes' is the size of the unfreed block, and the `ra=0x1f8f4' or return-address shows where the allocation originated from. See section Getting Caller Address Information. The systems which cannot provide return-address information show `unknown' instead, as in the 2nd line in the sample above. The `argv.c:1077' information from the 3rd line shows the file and line number which allocated the memory which was not freed. This information comes from the calls from C files which included `dmalloc.h'. See section Macros Providing File and Line Information. At the bottom of the sample it totals the memory for you and breaks it down to known memory (those calls which supplied the file/line information) and unknown (the rest). Often, you may allocate memory in via However, there is a way to track down unfreed memory in this
circumstance. You need to compile the library with
The How can a pointer be "seen" 3 times? Let say you So to find out who is allocating this particular 12 bytes the 3rd time,
try dmalloc -a 0x45008:3. The library will stop the program the
third time it sees the `0x45008' address. You then enter a
debugger and put a break point at To not bother with the
3.8.3 Diagnosing Fence-Post Overwritten MemoryFor a definition of fence-posts please see the "Features" section. See section General Features of the Library. To detect fence-post overruns, you need to enable the `check-fence' token. See section Description of the Debugging Tokens. This pads your allocations with some extra bytes at the front and the end and watches the space to make sure that they don't get overwritten. NOTE: The library cannot detect if this space gets read, only written. If you have encountered a fence-post memory error, the logfile should be able to tell you the offending address.
The above sample shows that the pointer `0x1d008' has had its lower fence-post area overwritten. This means that the code wrote below the bottom of the address or above the address right below this one. In the sample, the string that did it was `WOW!'. The library first shows you what the proper fence-post information should look like, and then shows what the pointer's bad information was. If it cannot print the character, it will display the value as `\ddd' where ddd are three octal digits. By enabling the
This document was generated by Gray Watson on May, 16 2007 using texi2html 1.76.
This work is licensed by Gray Watson
under the Creative Commons
Attribution-Share Alike 3.0 License. |