📄 dmalloc.texi
字号:
This is one of the newer sections of the library implying that it isincomplete. If you have any questions or issues that you'd like to seehandled here, please let me know.The dmalloc library replaces the heap library calls normally found inyour system libraries with its own versions. When you make a call tomalloc (for example), you are calling dmalloc's version of the memoryallocation function. When you allocate memory with these functions, thedmalloc library keeps track of a number of pieces of debugginginformation about your pointer including: where it was allocated,exactly how much memory was requested, when the call was made, etc..This information can then be verified when the pointer is freed orreallocated and the details can be logged on any errors.Whenever you reallocate or free a memory address, the dmalloc libraryalways performs a number of checks on the pointer to make sure that itis valid and has not been corrupted. You can configure the library toperform additional checks such as detected fence-post writing. Thelibrary can also be configured to overwrite memory with non-zeros (onlyif calloc is not called) when it is allocated and erase the memory whenthe pointers are freed.@cindex check-heapIn addition to per-pointer checks, you can configure the library toperform complete heap checks. These complete checks verify all internalheap structures and include walking all of the known allocated pointersto verify each one in turn. You need this level of checking to findrandom pointers in your program which got corrupted but that won't befreed for a while. To turn on these checks, you will need to enable the'check-heap' debug token. @xref{Debug Tokens}. By default this willcause the heap to be fully checked each and every time dmalloc is calledwhether it is a malloc, free, realloc, or another dmalloc overloadedfunction.Performing a full heap check can take a good bit of CPU and it may bethat you will want to run it sporadically. This can be accomplished ina couple different ways including the '-i' interval argument to thedmalloc utility. @xref{Dmalloc Program}. This will cause the check tobe run every N-th time. For instance, 'dmalloc -i 3' will cause theheap to be checked before every 3rd call to a memory function. Valuesof 100 or even 1000 for high memory usage programs are more useful thansmaller ones.@cindex LOG_ITERATION_COUNT@cindex iteration count@cindex memory transaction countYou can also cause the program to start doing detailed heap checkingafter a certain point. For instance, with 'dmalloc -s 1000' option, youcan tell the dmalloc library to enable the heap checks after the 1000thmemory call. Examine the dmalloc log file produced and use theiteration count if you have @code{LOG_ITERATION_COUNT} enabled in your@file{settings.h} file.The start option can also have the format @samp{file:line}. Forinstance, if it is set to @samp{dmalloc_t.c:126}, dmalloc will startchecking the heap after it sees a dmalloc call from the@file{dmalloc_t.c} file, line number 126. If you use@samp{dmalloc_t.c:0}, with a 0 line number, then dmalloc will startchecking the heap after it sees a call from anywhere in the@file{dmalloc_t.c} file.@c ----------------------------------------------------------------------------@node Programming, Dmalloc Program, Overview, Top@chapter How to Program with the Library@cindex programming@menu* Allocation Macros:: Macros providing file and line information.* Return Address:: Getting caller address information.* Argument Checking:: Checking of function arguments.* Extensions:: Additional non-standard routines.* Error Codes:: Description of the internal error numbers.* Disabling the Library:: How to disable the library.* Using With C++:: Using the library with C++.* Using With a Debugger:: Using a debugger with the library.* Using With Threads:: Using the library with a thread package.* Using With Cygwin:: Using the library with Cygwin environment.* Debugging A Server:: Debugging memory in a server or cgi-bin process.@end menu@c --------------------------------@node Allocation Macros, Return Address, Programming, Programming@section Macros Providing File and Line Information@cindex allocation macros@cindex macros, allocation@cindex dmalloc.h fileBy including @file{dmalloc.h} in your C files, your calls to malloc,calloc, realloc, recalloc, memalign, valloc, strdup, and free arereplaced with calls to _dmalloc_malloc, _dmalloc_realloc, and_dmalloc_free with various flags. Additionally the library replacescalls to xmalloc, xcalloc, xrealloc, xrecalloc, xmemalign, xvalloc,xstrdup, and xfree with associated calls.These macros use the c-preprocessor @code{__FILE__} and @code{__LINE__}macros which get replaced at compilation time with the current file andline-number of the source code in question. The routines use thisinformation to produce verbose reports on memory problems.@examplenot freed: '0x38410' (22 bytes) from 'dmalloc_t.c:92'@end exampleThis line from a log file shows that memory was not freed from file@file{dmalloc_t.c} line 92. @xref{Memory Leaks}.@cindex recalloc@cindex memalign@cindex valloc@cindex strdupYou may notice some non standard memory allocation functions in theabove list. Recalloc is a routine like realloc that reallocatespreviously allocated memory to a new size. If the new memory size islarger than the old, recalloc initializes the new space to all zeros.This may or may not be supported natively by your operating system.Memalign is like malloc but should insure that the returned pointer isaligned to a certain number of specified bytes. Currently, the memalignfunction is not supported by the library. It defaults to returningpossibly non-aligned memory for alignment values less than a block-size.Valloc is like malloc but insures that the returned pointer will bealigned to a page boundary. This may or may not be supported nativelyby your operating system but is fully supported by the library. Strdupis a string duplicating routine which takes in a null terminated stringpointer and returns an allocated copy of the string that will need to bepassed to free later to deallocate.The X versions of the standard memory functions (xmalloc, xfree, etc.)will print out an error message to standard error and will stop if thelibrary is unable to allocate any additional memory. It is useful touse these routines instead of checking everywhere in your program forallocation routines returning NULL pointers.@emph{WARNING}: If you are including the @file{dmalloc.h} file in yoursources, it is recommended that it be at the end of your include filelist because dmalloc uses macros and may try to change declarations ofthe malloc functions if they come after it.@c --------------------------------@node Return Address, Argument Checking, Allocation Macros, Programming@section Getting Caller Address Information@cindex caller's address@cindex return-address@cindex return.h file@cindex raEven though the allocation macros can provide file/line information forsome of your code, there are still modules which either you can'tinclude @file{dmalloc.h} (such as library routines) or you just don'twant to. You can still get information about the routines that calldmalloc function from the return-address information. To accomplishthis, you must be using this library on one of the supportedarchitecture/compilers. @xref{Portability}.@cindex assembly hacksThe library attempts to use some assembly hacks to get thereturn-address or the address of the line that called the dmallocfunction. If you have unfreed memory that does not have associated fileand line information, you might see the following non-freed memorymessages.@examplenot freed: '0x38410' (22 bytes) from 'ra=0xdd2c'not freed: '0x38600' (10232 bytes) from 'ra=0x10234d'not freed: '0x38220' (137 bytes) from 'ra=0x82cc'@end example@cindex ra_info.plWith the help of a debugger, these return-addresses (or ra) can then beidentified. I've provided a @file{ra_info.pl} perl script in the@file{contrib/} directory with the dmalloc sources which seems to workwell with gdb. You can also use manual methods for gdb to find thereturn-address location. @xref{Translate Return Addresses}.@c --------------------------------@node Argument Checking, Extensions, Return Address, Programming@section Checking of Function Arguments@cindex argument checking@cindex checking arguments@cindex DMALLOC_FUNC_CHECK flagOne potential problem with the library and its multitude of checks anddiagnoses is that they only get performed when a dmalloc function iscalled. One solution this is to include @file{dmalloc.h} and compileyour source code with the @code{DMALLOC_FUNC_CHECK} flag defined andenable the @code{check-funcs} token. @xref{Debug Tokens}.@examplecc -DDMALLOC_FUNC_CHECK file.c@end example@emph{NOTE}: Once you have compiled your source with DMALLOC_FUNC_CHECKenabled, you will have to recompile with it off to disconnect thelibrary. @xref{Disabling the Library}.@emph{WARNING}: You should be sure to have @file{dmalloc.h} included atthe end of your include file list because dmalloc uses macros and maytry to change declarations of the checked functions if they come afterit.When this is defined dmalloc will override a number of functions andwill insert a routine which knows how to check its own arguments andthen call the real function. Dmalloc can check such functions as@code{bcopy}, @code{index}, @code{strcat}, and @code{strcasecmp}. Forthe full list see the end of @file{dmalloc.h}.When you call @code{strlen}, for instance, dmalloc will make sure thestring argument's fence-post areas have not been overwritten, its fileand line number locations are good, etc. With @code{bcopy}, dmallocwill make sure that the destination string has enough space to store thenumber of bytes specified.For all of the arguments checked, if the pointer is not in the heap thenit is ignored since dmalloc does not know anything about it.@c --------------------------------@node Extensions, Error Codes, Argument Checking, Programming@section Additional Non-standard Routines@cindex extensionsThe library has a number of variables that are not a standard part ofmost malloc libraries:@table @code@c --------------------------------@cindex dmalloc_errno number@cindex internal error number@cindex error number@item int dmalloc_errnoThis variable stores the internal dmalloc library error number like errnodoes for the system calls. It can be passed to @code{dmalloc_strerror()}(see below) to get a string version of the error. It will have a valueof zero if the library has not detected any problems.@c --------------------------------@cindex dmalloc_logpath variable@cindex logfile name@item char * dmalloc_logpathThis variable can be used to set the dmalloc log filename. The envvariable DMALLOC_LOGFILE overrides this variable.@c --------------------------------@end tableAdditionally the library provides a number of non-standard mallocroutines:@c --------------------------------@cindex dmalloc_shutdown function@cindex shutdown the library@deftypefunvoid dmalloc_shutdown ( void )This function shuts the library down and logs the final statistics andinformation especially the non-freed memory pointers. The library hascode to support auto-shutdown if your system has the @code{on_exit()}call, @code{atexit()} call, or compiler destructor support (see@file{conf.h}). If you do not have these, then @code{dmalloc_shutdown}should be called right before @code{exit()} or as the last function in@code{main()}.@examplemain()@{ @dots{} dmalloc_shutdown(); exit(0);@}@end example@end deftypefun@c --------------------------------@cindex dmalloc_verify function@cindex verify pointers@cindex verify the heap@deftypefunint dmalloc_verify ( char * @var{pnt} )This function verifies individual memory pointers that are suspect ofmemory problems. To check the entire heap pass in a NULL or 0 pointer.The routine returns DMALLOC_VERIFY_ERROR or DMALLOC_VERIFY_NOERROR.@emph{NOTE}: @samp{dmalloc_verify()} can only check the heap with thefunctions that have been enabled. For example, if fence-post checkingis not enabled, @samp{dmalloc_verify()} cannot check the fence-postareas in the heap.@end deftypefun@c --------------------------------@cindex dmalloc_debug function@cindex override debug settings@cindex set debug functionality flags@deftypefununsigned int dmalloc_debug ( const unsigned int @var{flags} )This routine sets the debug functionality flags and returns theprevious flag value. It is helpful in server or cgi-bin programswhere environmental variables cannot be used. @xref{Debugging AServer}. For instance, if debugging should never be enabled for aprogram, a call to @code{dmalloc_debug(0)} as the first call in@code{main()} will disable all the memory debugging from that pointon.@emph{NOTE}: you cannot add or remove certain flags such as signalhandlers since they are setup at initialization time only.@emph{NOTE}: you can also use @code{dmalloc_debug_setup} below.@end deftypefun@c --------------------------------@cindex dmalloc_debug_current function@cindex current debug value@deftypefununsigned int dmalloc_debug_current ( void )This routine returns the current debug functionality value value. Thisallows you to save a copy of the debug dmalloc settings to be changedand then restored later.@end deftypefun@c --------------------------------@cindex dmalloc_debug_setup@cindex override debug settings@cindex set debug functionality flags@cindex setup debug flags@cindex string of debug tokens@deftypefunvoid dmalloc_debug_setup ( const char * @var{options_str} )This routine sets the global debugging functionality as an optionstring. Normally this would be passed in in the DMALLOC_OPTIONSenvironmental variable. This is here to override the env or forcircumstances where modifying the environment is not possible or doesnot apply such as servers or cgi-bin programs. @xref{Debugging AServer}.Some examples:@example/* debug tokens high, threaded lock-on at 20, log to dmalloc.%p (pid) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -