📄 dmalloc.texi
字号:
@examplealias dmalloc 'eval `\dmalloc -C \!*`'@end example@cindex rc shellIf you are using rc shell, you should add the following to your@file{.rcrc} file (notice the @kbd{-R} option for rc-shell output):@examplefn dmalloc @{eval `@{/usr/local/bin/dmalloc $*@}@}@end exampleBy the way, if you are looking for a shell, I heartily recommend tryingout zsh at URL @uref{http://www.zsh.org/}. It is a bourne shellwritten from scratch with much the same features as tcsh without the cshcrap.@emph{NOTE}: 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} and see any output with DMALLOC_OPTIONS in it then the aliasdid not work.@item Although not necessary, you may want to include @file{dmalloc.h}in your C files and recompile. This will allow the library to reportthe file/line numbers of calls that generate problems. @xref{AllocationMacros}. It should be inserted at the @emph{bottom} of your includefiles as to not conflict with wother includes. You may want to ifdef itas well and compile with @kbd{cc -DDMALLOC @dots{}}:@example/* other includes above ^^^ */#ifdef DMALLOC#include "dmalloc.h"#endif@end example@item Link the dmalloc library into your program. The dmalloc libraryshould probably be placed at or near the end of the library list.@item Enable the debugging features by typing @kbd{dmalloc -l logfile-i 100 low} (for example). This will:@itemize @bullet@item set the malloc log path to @file{logfile} (@kbd{-l logfile})@item have the library check itself every 100 iterations (@kbd{-i 100})@item enable a number of debug features (@kbd{low}). You canalso try @kbd{runtime} for minimal checking or @kbd{medium} or@kbd{high} for more extensive heap verification.@end itemize@kbd{dmalloc --usage} will provide verbose usage info for the dmallocprogram. @xref{Dmalloc Program}.You may also want to install the @file{dmallocrc} file in your homedirectory as @file{.dmallocrc}. This allows you to add your owncombination of debug tokens. @xref{RC File}.@item 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. @xref{Troubleshooting}.@end enumerate@c --------------------------------@node Allocation Basics, Features, Getting Started, Overview@section Basic Description of Terms and Functions@cindex allocation basics@cindex basic allocation information@menu* Basic Definitions:: General memory terms and concepts.* Malloc Functions:: Functionality supported by all malloc libs.@end menu@c --------------------------------@node Basic Definitions, Malloc Functions, Allocation Basics, Allocation Basics@subsection General Memory Terms and Concepts@cindex basic definitions@cindex memory definitionsAny program can be divided into 2 logical parts: text and data. Text isthe actual program code in machine-readable format and data is theinformation that the text operates on when it is executing. The data,in turn, can be divided into 3 logical parts according to where it isstored: @dfn{static}, @dfn{stack}, and @dfn{heap}.@cindex static memoryStatic data is the information whose storage space is compiled into theprogram.@example/* global variables are allocated as static data */int numbers[10];main()@{ @dots{}@}@end example@cindex stack memoryStack data is data allocated at runtime to hold information used insideof functions. This data is managed by the system in the space calledstack space.@examplevoid foo()@{ /* this local variable is stored on the stack */ float total; @dots{}@}main()@{ foo();@}@end example@cindex heap memoryHeap data is also allocated at runtime and provides a programmer withdynamic memory capabilities.@examplemain()@{ /* the address is stored on the stack */ char * string; @dots{} /* * Allocate a string of 10 bytes on the heap. Store the * address in string which is on the stack. */ string = (char *)malloc(10); @dots{} /* de-allocate the heap memory now that we're done with it */ (void)free(string); @dots{}@}@end exampleIt is the heap data that is managed by this library.Although the above is an example of how to use the malloc and freecommands, it is not a good example of why using the heap for runtimestorage is useful.Consider this: You write a program that reads a file into memory,processes it, and displays results. You would like to handle files witharbitrary size (from 10 bytes to 1.2 megabytes and more). One problem,however, is that the entire file must be in memory at one time to do thecalculations. You don't want to have to allocate 1.2 megabytes when youmight only be reading in a 10 byte file because it is wasteful of systemresources. Also, you are worried that your program might have to handlefiles of more than 1.2 megabytes.A solution: first check out the file's size and then, using theheap-allocation routines, get enough storage to read the entire fileinto memory. The program will only be using the system resourcesnecessary for the job and you will be guaranteed that your program canhandle any sized file.@c --------------------------------@node Malloc Functions,, Basic Definitions, Allocation Basics@subsection Functionality Supported by All Malloc Libraries@cindex malloc functionsAll malloc libraries support 4 basic memory allocation commands. Theseinclude @dfn{malloc}, @dfn{calloc}, @dfn{realloc}, and @dfn{free}. Formore information about their capabilities, check your system's manualpages -- in unix, do a @code{man 3 malloc}.@deftypefunvoid *malloc ( unsigned int @var{size} )@cindex mallocUsage: @code{pnt = (type *)malloc(size)}The malloc routine is the basic memory allocation routine. It allocatesan area of @code{size} bytes. It will return a pointer to the spacerequested.@end deftypefun@deftypefunvoid *calloc ( unsigned int @var{number}, unsigned int@var{size} )@cindex calloc@cindex Allocation of zeros@cindex zeros, allocation ofUsage: @code{pnt = (type *)calloc(number, size)}The calloc routine allocates a certain @code{number} of items, each of@code{size} bytes, and returns a pointer to the space. It isappropriate to pass in a @code{sizeof(type)} value as the size argument.Also, calloc nulls the space that it returns, assuring that the memoryis all zeros.@end deftypefun@deftypefunvoid *realloc ( void *@var{old_pnt}, unsigned int@var{new_size} )@cindex reallocUsage: @code{new_pnt = (type *)realloc(old_pnt, new_size)}The realloc function expands or shrinks the memory allocation in@code{old_pnt} to @code{new_size} number of bytes. Realloc copies asmuch of the information from @code{old_pnt} as it can into the@code{new_pnt} space it returns, up to @code{new_size} bytes. If thereis a problem allocating this memory, 0L will be returned.If the @code{old_pnt} is 0L then realloc will do the equivalent of a@code{malloc(new_size)}. If @code{new_size} is 0 and @code{old_pnt} isnot 0L, then it will do the equivalent of @code{free(old_pnt)} and willreturn 0L.@end deftypefun@deftypefunvoid free ( void *@var{pnt} )@cindex freeUsage: @code{free(pnt)}The free routine releases allocation in @code{pnt} which was returned bymalloc, calloc, or realloc back to the heap. This allows other parts ofthe program to re-use memory that is not needed anymore. It guaranteesthat the process does not grow too big and swallow a large portion ofthe system resources.@end deftypefun@emph{WARNING}: there is a quite common myth that all of the space thatis returned by malloc libraries has already been cleared. @emph{Only}the @code{calloc} routine will zero the memory space it returns.@c --------------------------------@node Features, How It Works, Allocation Basics, Overview@section General Features of the Library@cindex featuresThe debugging features that are available in this debug malloc librarycan be divided into a couple basic classifications:@table @asis@item file and line number information@cindex file/line numbers@cindex cppOne of the nice things about a good debugger is its ability to providethe file and line number of an offending piece of code. This libraryattempts to give this functionality with the help of @dfn{cpp}, the Cpreprocessor. @xref{Allocation Macros}.@item return-address information@cindex return-addressTo debug calls to the library from external sources (i.e. those filesthat could not use the allocation macros), some facilities have beenprovided to supply the caller's address. This address, with the help ofa debugger, can help you locate the source of a problem. @xref{ReturnAddress}.@item fence-post (i.e. bounds) checking@cindex fence-post checking@cindex bounds checking@cindex checking bounds@dfn{Fence-post} memory is the area immediately above or below memoryallocations. It is all too easy to write code that accesses above orbelow an allocation -- especially when dealing with arrays or strings.The library can write special values in the areas around everyallocation so it will notice when these areas have been overwritten.@xref{Fence-Post Overruns}.@emph{NOTE}: The library cannot notice when the program @emph{reads}from these areas, only when it writes values. Also, fence-post checkingwill increase the amount of memory the program allocates.@item heap-constancy verification@cindex constancy verificationThe administration of the library is reasonably complex. If any of theheap-maintenance information is corrupted, the program will either crashor give unpredictable results.By enabling heap-consistency checking, the library will run through itsadministrative structures to make sure all is in order. This will meanthat problems will be caught faster and diagnosed better.The drawback of this is, of course, that the library often takes quite along time to do this. It is suitable to enable this only duringdevelopment and debugging sessions.@emph{NOTE}: the heap checking routines cannot guarantee that the testswill not cause a segmentation-fault if the heap administrationstructures are properly (or improperly if you will) overwritten. Inother words, the tests will verify that everything is okay but may notinform the user of problems in a graceful manner.@item logging statistics@cindex logging statistics@cindex statistics@cindex memory leaks@cindex leaking memoryOne of the reasons why the debug malloc library was initially developedwas to track programs' memory usage -- specifically to locate@dfn{memory leaks} which are places where allocated memory is nevergetting freed. @xref{Memory Leaks}.The library has a number of logging capabilities that can track un-freedmemory pointers as well as runtime memory usage, memory transactions,administrative actions, and final statistics.@item examining freed memory@cindex freed memory@cindex freed memoryAnother common problem happens when a program frees a memory pointer butgoes on to use it again by mistake. This can lead to mysterious crashesand unexplained problems.To combat this, the library can write special values into a block ofmemory after it has been freed. This serves two purposes: it will makesure that the program will get garbage data if it trying to access thearea again, and it will allow the library to verify the area later forsigns of overwriting.@end tableIf any of the above debugging features detect an error, the library willtry to recover. If logging is enabled then an error will be logged withas much information as possible.The error messages that the library displays are designed to give themost information for developers. If the error message is notunderstood, then it is most likely just trying to indicate that a partof the heap has been corrupted.@cindex dump core@cindex core dumpThe library can be configured to quit immediately when an error isdetected and to dump a core file or memory-image. This can be examinedwith a debugger to determine the source of the problem. The library caneither stop after dumping core or continue running.@cindex system memory problems@cindex memory problems in system functions@emph{NOTE}: do not be surprised if the library catches problems withyour system's routines. It took me hours to finally come to theconclusion that the localtime call, included in SunOS release 4.1,overwrites one of its fence-post markers.@c --------------------------------@node How It Works,, Features, Overview@section How the Library Checks Your Program
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -