📄 dmalloc.html
字号:
<HTML><HEAD><!-- Created by texi2html 1.56k from ./dmalloc.texi on 19 October 2004 --><TITLE>Dmalloc Tutorial</TITLE></HEAD><BODY><H1>Debug Malloc Library</H1><H2>Version 5.4.2</H2><H2>October 2004</H2><ADDRESS>Gray Watson</ADDRESS><P><P><HR><P><H1>Table of Contents</H1><UL><LI><A NAME="TOC1" HREF="dmalloc.html#SEC1">1. Library Copying and Licensing Conditions</A><LI><A NAME="TOC2" HREF="dmalloc.html#SEC2">2. Description of Features and How to Get Started</A><UL><LI><A NAME="TOC3" HREF="dmalloc.html#SEC3">2.1 How to Install the Library</A><LI><A NAME="TOC4" HREF="dmalloc.html#SEC4">2.2 Getting Started with the Library</A><LI><A NAME="TOC5" HREF="dmalloc.html#SEC5">2.3 Basic Description of Terms and Functions</A><UL><LI><A NAME="TOC6" HREF="dmalloc.html#SEC6">2.3.1 General Memory Terms and Concepts</A><LI><A NAME="TOC7" HREF="dmalloc.html#SEC7">2.3.2 Functionality Supported by All Malloc Libraries</A></UL><LI><A NAME="TOC8" HREF="dmalloc.html#SEC8">2.4 General Features of the Library</A><LI><A NAME="TOC9" HREF="dmalloc.html#SEC9">2.5 How the Library Checks Your Program</A></UL><LI><A NAME="TOC10" HREF="dmalloc.html#SEC10">3. How to Program with the Library</A><UL><LI><A NAME="TOC11" HREF="dmalloc.html#SEC11">3.1 Macros Providing File and Line Information</A><LI><A NAME="TOC12" HREF="dmalloc.html#SEC12">3.2 Getting Caller Address Information</A><LI><A NAME="TOC13" HREF="dmalloc.html#SEC13">3.3 Checking of Function Arguments</A><LI><A NAME="TOC14" HREF="dmalloc.html#SEC14">3.4 Additional Non-standard Routines</A><LI><A NAME="TOC15" HREF="dmalloc.html#SEC15">3.5 Description of the Internal Error Codes</A><LI><A NAME="TOC16" HREF="dmalloc.html#SEC16">3.6 How to Disable the library</A><LI><A NAME="TOC17" HREF="dmalloc.html#SEC17">3.7 Using the Library with C++</A><LI><A NAME="TOC18" HREF="dmalloc.html#SEC18">3.8 Using Dmalloc With a Debugger</A><UL><LI><A NAME="TOC19" HREF="dmalloc.html#SEC19">3.8.1 Diagnosing General Problems with a Debugger</A><LI><A NAME="TOC20" HREF="dmalloc.html#SEC20">3.8.2 Tracking Down Non-Freed Memory</A><LI><A NAME="TOC21" HREF="dmalloc.html#SEC21">3.8.3 Diagnosing Fence-Post Overwritten Memory</A><LI><A NAME="TOC22" HREF="dmalloc.html#SEC22">3.8.4 Translating Return Addresses into Code Locations</A></UL><LI><A NAME="TOC23" HREF="dmalloc.html#SEC23">3.9 Using the Library with a Thread Package</A><LI><A NAME="TOC24" HREF="dmalloc.html#SEC24">3.10 Using the library with Cygwin environment.</A><LI><A NAME="TOC25" HREF="dmalloc.html#SEC25">3.11 Debugging Memory in a Server or Cgi-Bin Process</A></UL><LI><A NAME="TOC26" HREF="dmalloc.html#SEC26">4. Dmalloc Utility Program</A><UL><LI><A NAME="TOC27" HREF="dmalloc.html#SEC27">4.1 Using a Shell Alias with the Utility</A><LI><A NAME="TOC28" HREF="dmalloc.html#SEC28">4.2 How to Use the Dmalloc Program</A><LI><A NAME="TOC29" HREF="dmalloc.html#SEC29">4.3 Environment Variable Name and Features</A><LI><A NAME="TOC30" HREF="dmalloc.html#SEC30">4.4 Description of the Debugging Tokens</A><LI><A NAME="TOC31" HREF="dmalloc.html#SEC31">4.5 Format of the Runtime Configuration File</A></UL><LI><A NAME="TOC32" HREF="dmalloc.html#SEC32">5. Information on the Source Code</A><UL><LI><A NAME="TOC33" HREF="dmalloc.html#SEC33">5.1 Definition of Terms and other Information</A><LI><A NAME="TOC34" HREF="dmalloc.html#SEC34">5.2 General Compatibility Concerns</A><LI><A NAME="TOC35" HREF="dmalloc.html#SEC35">5.3 Issues Important for Porting the Library</A></UL><LI><A NAME="TOC36" HREF="dmalloc.html#SEC36">6. Some Solutions to Common Problems</A><LI><A NAME="TOC37" HREF="dmalloc.html#SEC37">Index of Concepts</A></UL><P><HR><P><H1><A NAME="SEC1" HREF="dmalloc.html#TOC1">1. Library Copying and Licensing Conditions</A></H1><P><A NAME="IDX1"></A><A NAME="IDX2"></A><A NAME="IDX3"></A><A NAME="IDX4"></A><P>Copyright 1992 to 2004 by Gray Watson.<P>Permission to use, copy, modify, and distribute this software for anypurpose and without fee is hereby granted, provided that the abovecopyright notice and this permission notice appear in all copies, andthat the name of Gray Watson not be used in advertising or publicitypertaining to distribution of the document or software without specific,written prior permission.<P>Gray Watson makes no representations about the suitability of thesoftware described herein for any purpose. It is provided "as is"without express or implied warranty.<H1><A NAME="SEC2" HREF="dmalloc.html#TOC2">2. Description of Features and How to Get Started</A></H1><P><A NAME="IDX5"></A><H2><A NAME="SEC3" HREF="dmalloc.html#TOC3">2.1 How to Install the Library</A></H2><P><A NAME="IDX6"></A><A NAME="IDX7"></A><A NAME="IDX8"></A><A NAME="IDX9"></A><A NAME="IDX10"></A><P>To configure, compile, and install the library, follow these stepscarefully.<OL><LI>Make sure you have the latest version of the library availablefrom the home page at URL <A HREF="http://dmalloc.com/">http://dmalloc.com/</A>.<A NAME="IDX11"></A><LI>You may want to edit or at least review the settings in<TT>`settings.dist'</TT> to tune specific features of the library. The<TT>`configure'</TT> script will copy this file to <TT>`settings.h'</TT> whichis where you should be adding per-architecture settings.<A NAME="IDX12"></A><A NAME="IDX13"></A><A NAME="IDX14"></A><A NAME="IDX15"></A><A NAME="IDX16"></A><LI>Type <KBD>sh ./configure</KBD> to configure the library. You may wantto first examine the <TT>`config.help'</TT> file for some information aboutconfigure. You may want to use the <KBD>--disable-cxx</KBD> option if you donot want the Makefile to build the C++ version of dmalloc. You may wantto use the <KBD>--enable-threads</KBD> option to build the threaded versionof dmalloc. You may want to use the <KBD>--enable-shlib</KBD> option tobuild the shared versions of the dmalloc libraries. <KBD>sh ./configure--help</KBD> lists the available options to configure. Configure shouldgenerate the <TT>`Makefile'</TT> and configuration files automatically.<LI>You may want to examine the <TT>`Makefile'</TT> and <TT>`conf.h'</TT> filescreated by configure to make sure it did its job correctly.<A NAME="IDX17"></A><LI>You might want to tune the settings in <TT>`settings.h'</TT> file totune the library to the local architecture. This file contains relevantsettings if you are using pthreads or another thread library.See section <A HREF="dmalloc.html#SEC23">3.9 Using the Library with a Thread Package</A>. The <TT>`configure'</TT> script created thisfile from the <TT>`settings.dist'</TT> file. Any permanent changes to thesesettings should made to the <TT>`settings.dist'</TT> file. You then can run<TT>`config.status'</TT> to re-create the <TT>`settings.h'</TT> file.<A NAME="IDX18"></A><LI>The <CODE>DMALLOC_SIZE</CODE> variable gets auto-configured in<TT>`dmalloc.h.2'</TT> but it may not generate correct settings for allsystems. You may have to alter the definitions in this file to getthings to stop complaining when you go to compile about the sizearguments to malloc routines. Comments on this please.<LI>Typing <KBD>make</KBD> should be enough to build <TT>`libdmalloc.a'</TT>,and <TT>`dmalloc'</TT> program. If it does not work, please see if thereare any notes in the contrib directory about your system-type. If notand you figure your problem out, please send me some notes so futureusers can profit from your experiences.<EM>NOTE</EM>: You may experience some errors compiling some of thereturn.h assembly macros which attempt to determine the callers addressfor logging purposes. You may want to first try disabling any compileroptimization flags. If this doesn't work then you may need to disablethe <SAMP>`USE_RETURN_MACROS'</SAMP> variable in the <TT>`settings.h'</TT> file.<A NAME="IDX19"></A><EM>NOTE</EM>: The code is dependent on an ANSI-C compiler. If theconfigure script gives the <SAMP>`WARNING'</SAMP> that you do not have an ANSI-Ccompiler, you may still be able to add some sort of option to yourcompiler to make it ANSI. If there such is an option, please send it tothe author so it can be added to the configure script.<LI>If you use threads and did not add the <KBD>--enable-threads</KBD>argument to configure, typing <KBD>make threads</KBD> should be enough tobuild <TT>`libdmallocth.a'</TT> which is the threaded version of thelibrary. This may or may not work depending on the configurationscripts ability to detect your local thread functionality. Feel free tosend me mail with improvements.See the "Using With Threads" section for more information about theoperation of the library with your threaded program. See section <A HREF="dmalloc.html#SEC23">3.9 Using the Library with a Thread Package</A>.<LI>If you have a C++ compiler installed, the library should haveautomatically built <TT>`libdmallocxx.a'</TT> which is the C++ version ofthe library. If it was not done automatically, you can build it bytyping <KBD>make cxx</KBD>. You should link this library into your C++programs instead of <TT>`libdmalloc.a'</TT>. See the <TT>`dmallocc.cc'</TT> C++file which contains basic code to overload the <CODE>new</CODE>, <CODE>new[]</CODE>,<CODE>delete</CODE>, and <CODE>delete[]</CODE> C++ operators. My apologies on theminimal C++ support. I am still living in a mostly C world. Any helpimproving this interface without sacrificing portability would beappreciated.<A NAME="IDX20"></A><A NAME="IDX21"></A><LI>Typing <KBD>make light</KBD> should build and run the <TT>`dmalloc_t'</TT> testprogram through a set of light trials. By default this will execute<TT>`dmalloc_t'</TT> 5 times -- each time will execute 10,000 mallocoperations in a very random manner. Anal folks can type <KBD>makeheavy</KBD> to up the ante. Use <KBD>dmalloc_t --usage</KBD> for the list of all<TT>`dmalloc_t'</TT> options.<LI>Typing <KBD>make install</KBD> should install the <TT>`libdmalloc.a'</TT>library in <TT>`/usr/local/lib'</TT>, the <TT>`dmalloc.h'</TT> include file in<TT>`/usr/local/include'</TT>, and the <TT>`dmalloc'</TT> utility in<TT>`/usr/local/bin'</TT>. You may also want to type <KBD>make installth</KBD>to install the thread library into place and/or <KBD>make installcc</KBD> toinstall the C++ library into place.You may have specified a <SAMP>`--prefix=PATH'</SAMP> option to configure inwhich case <SAMP>`/usr/local'</SAMP> will have been replaced with <SAMP>`PATH'</SAMP>.</OL><P>See the "Getting Started" section to get up and running with thelibrary. See section <A HREF="dmalloc.html#SEC4">2.2 Getting Started with the Library</A>.<H2><A NAME="SEC4" HREF="dmalloc.html#TOC4">2.2 Getting Started with the Library</A></H2><P><A NAME="IDX22"></A><A NAME="IDX23"></A><A NAME="IDX24"></A><A NAME="IDX25"></A><A NAME="IDX26"></A><A NAME="IDX27"></A><P>This section should give you a quick idea on how to get going.Basically, you need to do the following things to make use of thelibrary:<OL><LI>Make sure you have the latest version of the library availablefrom the home page at URL <A HREF="http://dmalloc.com/">http://dmalloc.com/</A>.<LI>Follow the installation instructions on how to configure,make, and install the library (i.e. type: <KBD>make install</KBD>).See section <A HREF="dmalloc.html#SEC3">2.1 How to Install the Library</A>.<A NAME="IDX28"></A><A NAME="IDX29"></A><A NAME="IDX30"></A><A NAME="IDX31"></A><A NAME="IDX32"></A><LI>You need to make sure that the library configuration andbuild process above was able to locate one of the <CODE>on_exit</CODE>function, <CODE>atexit</CODE> function, or had compiler destructor support.If one of these functions or support is available then the dmalloclibrary should be able to automatically shut itself down when theprogram exits. This causes the memory statistics and unfreedinformation to be dumped to the log file. However, if your system hasnone of the above, then you will need to call <CODE>dmalloc_shutdown</CODE>yourself before your program exits.<A NAME="IDX33"></A><A NAME="IDX34"></A><A NAME="IDX35"></A><A NAME="IDX36"></A><LI>Add an alias for dmalloc to your shell's rc file if supported.The idea is to have the shell capture the dmalloc program's output andadjust the environment. Bash, ksh, and zsh users should add thefollowing to their <TT>`.bashrc'</TT>, <TT>`.profile'</TT>, or <TT>`.zshrc'</TT>file respectively (notice the <KBD>-b</KBD> option for bourne shell output):<PRE>function dmalloc { eval `command dmalloc -b $*`; }</PRE>If your shell does not support the <CODE>command</CODE> function then try:<PRE>function dmalloc { eval `\dmalloc -b $*`; }</PRE>or<PRE>function dmalloc { eval `/usr/local/bin/dmalloc -b $*`; }</PRE><A NAME="IDX37"></A><A NAME="IDX38"></A>If you are <EM>still</EM> using csh or tcsh, you should add the followingto your <TT>`.cshrc'</TT> file (notice the <KBD>-C</KBD> option for c-shelloutput):<PRE>alias dmalloc 'eval `\dmalloc -C \!*`'</PRE><A NAME="IDX39"></A>If you are using rc shell, you should add the following to your<TT>`.rcrc'</TT> file (notice the <KBD>-R</KBD> option for rc-shell output):<PRE>fn dmalloc {eval `{/usr/local/bin/dmalloc $*}}</PRE>By the way, if you are looking for a shell, I heartily recommend tryingout zsh at URL <A HREF="http://www.zsh.org/">http://www.zsh.org/</A>. It is a bourne shellwritten from scratch with much the same features as tcsh without the cshcrap.<EM>NOTE</EM>: After you add the alias to the file you need to log out andlog back in to have it take effect, or you can execute the aboveappropriate command on the command line. If you enter <KBD>dmallocruntime</KBD> and see any output with DMALLOC_OPTIONS in it then the aliasdid not work.<LI>Although not necessary, you may want to include <TT>`dmalloc.h'</TT>in your C files and recompile. This will allow the library to reportthe file/line numbers of calls that generate problems. See section <A HREF="dmalloc.html#SEC11">3.1 Macros Providing File and Line Information</A>. It should be inserted at the <EM>bottom</EM> of your includefiles as to not conflict with wother includes. You may want to ifdef itas well and compile with <KBD>cc -DDMALLOC ...</KBD>:<PRE>/* other includes above ^^^ */#ifdef DMALLOC#include "dmalloc.h"#endif</PRE><LI>Link the dmalloc library into your program. The dmalloc libraryshould probably be placed at or near the end of the library list.<LI>Enable the debugging features by typing <KBD>dmalloc -l logfile-i 100 low</KBD> (for example). This will:<UL><LI>set the malloc log path to <TT>`logfile'</TT> (<KBD>-l logfile</KBD>)<LI>have the library check itself every 100 iterations (<KBD>-i 100</KBD>)<LI>enable a number of debug features (<KBD>low</KBD>). You canalso try <KBD>runtime</KBD> for minimal checking or <KBD>medium</KBD> or<KBD>high</KBD> for more extensive heap verification.</UL><KBD>dmalloc --usage</KBD> will provide verbose usage info for the dmallocprogram. See section <A HREF="dmalloc.html#SEC26">4. Dmalloc Utility Program</A>.You may also want to install the <TT>`dmallocrc'</TT> file in your homedirectory as <TT>`.dmallocrc'</TT>. This allows you to add your owncombination of debug tokens. See section <A HREF="dmalloc.html#SEC31">4.5 Format of the Runtime Configuration File</A>.<LI>Run your program, examine the logfile that should have been created bydmalloc_shutdown, and use its information to help debug your program.See the next section for help with this. See section <A HREF="dmalloc.html#SEC36">6. Some Solutions to Common Problems</A>.</OL><H2><A NAME="SEC5" HREF="dmalloc.html#TOC5">2.3 Basic Description of Terms and Functions</A></H2><P><A NAME="IDX40"></A><A NAME="IDX41"></A><H3><A NAME="SEC6" HREF="dmalloc.html#TOC6">2.3.1 General Memory Terms and Concepts</A></H3>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -