📄 mpatrol.texi
字号:
internal functions.@itemAll diagnostics and logging are sent to a file in the current directory, butthis can be overridden, including forcing the log file to be the standard outputor standard error file streams. An environment variable specifying a defaultdirectory in which to place log files can also be set.@itemOptions exist to log details of every memory allocation, reallocation ordeallocation when they occur. A function exists to log the details of anymemory allocation to the mpatrol log file.@itemOptions exist to halt the program at a specific memory allocation, reallocationor deallocation when running the program within a debugger. These options haveno effect when running the program without a debugger.@itemAn option exists to enable memory allocation profiling, which forces a summaryof all memory allocation statistics to be written to a specified file for lateruse by a profiling command. The profiling file can also be written at aspecified frequency. An environment variable specifying a default directory inwhich to place profiling output files can also be set.@itemA profiling command is provided which reads a profiling output file produced bythe mpatrol library and displays a set of tables based on the accumulated data.The profiling information includes summaries of all of the memory allocationslisted by size and the function that allocated them and a list of memory leakswith the call stack of the allocating function. It also includes a graph of allmemory allocations listed in tabular form, and an optional graph specificationfile for later processing by the @command{dot} graph visualisation package.@itemAn option exists to enable memory allocation tracing, which forces certaindetails for every memory allocation event to be written to a specified file forlater use by a tracing command. The tracing file is written in a conciseencoded form so as to keep the size of the file down. An environment variablespecifying a default directory in which to place tracing output files can alsobe set.@cindex trace-driven simulation@cindex simulation, trace-driven@itemA tracing command is provided which reads a tracing output file produced by thempatrol library and displays the memory allocation events in tabular orgraphical form. It also displays any relevant statistics that could becalculated, and has options to write out the trace in HATF format or write outa trace-driven memory allocation simulation program as C source code.@itemOn UNIX platforms, the @code{mmap()} function can optionally be used to allocateuser memory instead of the @code{sbrk()} function, but only if the systemsupports it. If @code{mmap()} is supported then internal mpatrol library memoryis normally allocated with this function in order to segregate it from usermemory but this behaviour can be swapped around.@itemOn non-UNIX platforms where the mpatrol library overrides @code{malloc()}without requiring the inclusion of @file{mpatrol.h}, versions of the UNIXfunctions @code{brk()} and @code{sbrk()} are provided for compatibility withcertain libraries. These should @emph{not} be called by user code as they haveonly limited functionality.@itemAll newly-allocated memory that is not allocated by the @code{calloc()} or@code{recalloc()} functions will be pre-filled with a non-zero value in order tocatch out programs that wrongly assume that all newly-allocated memory iszeroed. This value can be modified at run-time.@itemCan automatically check to see if there have been any illegal writes to byteslocated just before and after every memory allocation through the use ofoverflow buffers. The size of such overflow buffers and the value to pre-fillthem with can be modified at run-time. The checks will be performed beforeevery memory allocation call to ensure that nothing has overwritten theoverflow buffers, but a function is also provided to perform additional checksunder the programmer's control and an option exists to specify a range andfrequency in which checks will be performed.@itemOn systems that support them, watch point areas can be used instead of overflowbuffers so that every read and write to memory is checked to ensure that it isnot within an overflow buffer.@itemSupports the @option{-fcheck-memory-usage} option of @command{gcc} to check allheap memory accesses in programs that were compiled with that option. Currentlythis only supports checking that memory accesses do not overflow heapallocations or access free memory, rather than keeping records of individualmemory accesses that GNU Checker does.@itemCan automatically check to see if there have been any illegal writes to freememory blocks. The value to pre-fill free memory blocks with can be modifiedat run-time. The check will be performed before every memory allocation callto ensure that nothing has overwritten the free memory block, but a function isalso provided to perform additional checks under the programmer's control andan option exists to specify a range in which checks will be performed.@itemOn systems that support memory protection, every memory allocation canoptionally be allocated at least one page of memory. That way, any free memoryblocks can be made read and write protected so that nothing can access freememory on the heap. An option is provided to specify whether all memoryallocations should be allocated at the start or at the end of such pages, andthe bytes left over within the pages become overflow buffers.@itemAll freed memory allocations can optionally be prevented from being returned tothe free memory pool. This is useful for detecting if use is being made offreed memory just after a memory allocation has been freed. The contents ofthe memory allocation can either be preserved or can be pre-filled with a valuein order to detect illegal writes to the freed memory allocation. In addition,only a specified number of recently-freed memory allocations can be preventedfrom being returned to the free memory pool. Any older freed memory allocationswill then eventually be reused.@itemThe @code{alloca()}, @code{strdupa()} and @code{strndupa()} functions areimplemented so that the temporary stack-based allocations that they wouldnormally make are now temporary heap-based allocations that can be traced bympatrol. Such allocations will be implicitly freed when the function thatallocated them returns, but a function also exists to explicitly free them aswell.@itemCalls to memory operation functions (such as @code{memset()} or @code{memcpy()})have their arguments checked to ensure that they do not pass null pointers orattempt to read or write memory straddling the boundary of a previouslyallocated memory block, although an option exists to turn such an error into awarning so that the operation can still be performed. Tracing from all suchfunctions can also optionally be written to the log file.@itemThe internal data structures used by the library are kept separate from therest of the memory allocations. On systems that support memory protection, allof these internal data structures will be write-protected in order to preventcorruption by the calling program. This feature can be overridden at run-timeas it can slow the program down.@cindex signals@itemCertain signals can be saved and restored on entry to each library function and@code{errno} is set to @code{ENOMEM} if memory cannot be allocated, except forthe ANSI C++ operators which throw the @code{std::bad_alloc} exception instead.@itemOn systems that support memory protection, the library attempts to detect anyillegal memory accesses and display as much information as it can obtain aboutthe address in question and where the illegal memory access occurred.@itemA call stack traceback from any function performing a memory allocation isstored if the library supports this feature on the system it is being run on.This information can then be displayed when information about a specific memoryallocation is required. Many different call stack traceback implementations areprovided for different platforms. A function is also provided to write thecurrent call stack to the mpatrol log file.@itemSymbol table details from executable files and shared libraries areautomatically read on systems that support this feature in order to make thecall stack tracebacks more meaningful. An option also exists to display acomplete list of the symbols that were read by the library at programtermination. A function is also provided to return symbolic information aboutany code address.@itemCompiler-generated line number tables from any debugging sections that existin executable files and shared libraries can also be used by the mpatrol libraryin order to provide more meaningful information in call stack tracebacks. Anexternal command is also provided to make use of a debugger to get suchinformation if one is available.@itemIf the library is unable to automatically determine a program's executablefilename to read symbols from then an option exists to specify the full path tothe program's executable file.@itemOptions are provided to edit and list a source file at a specific line numberwhen a warning or error occurs due to that source line. An external commandwhich provides this functionality outwith the mpatrol library is included, andfunctions are provided to do this from within user code.@itemAn option exists to change the default alignment used for general-purpose memoryallocations.@itemContains support for a user-defined limit to available memory which can beuseful for stress-testing a program in simulated low memory conditions.@itemContains a feature to randomly fail a specific frequency of memory allocationswhich can be useful for stress-testing error recovery code in a program.@itemAn option exists to display a complete memory map of the heap at programtermination. A function to do this is also available to call at any pointduring program execution.@itemA function is provided to take a snapshot of the heap at the current point inexecution. The value returned by this function can then be used to pinpointthe differences in heap allocation details between that point and a later pointin the program.@itemFunctions are provided to iterate across all of the current heap allocationsand call a user-defined callback function for each one they find.@itemA leak table is provided, which records a flat profile of memory allocationbehaviour between two points in a program and is keyed by source file location.Memory allocation events can either be recorded in the leak table automaticallyvia a run-time option or the leak table can be manipulated through severalfunctions.@itemFunctions are provided to write user-defined information directly to the mpatrollog file, as well as hexadecimal memory dumps of any memory location.@itemOptions exist to display all freed and unfreed memory allocations at programtermination in order to detect memory leaks, as well as all free memory blocks.A separate program is also provided for locating memory leaks in unfinished logfiles.@itemAn option exists to abort the program with a failure condition if there are morethan a specified number of unfreed memory allocations at program termination.This could be useful for batch testing in order to check that all tests free upmost of their allocated memory.@itemMemory allocations can be @emph{marked} to indicate to the mpatrol library thatthey should remain allocated for the lifetime of the program and should not befreed or be listed as a memory leak.@itemFunctions always report if their arguments are illegal in order to pinpoint anyerrors, and options exist to perform rigorous checking of arguments whenallocating, reallocating and freeing memory. In addition, checking is performedto ensure that memory allocated by @code{operator new[]} is not freed with@code{free()} for example.@itemThe type of function performing a memory allocation is always stored along withthe allocation, as well as the file and line number it was called from. Ifcompiled with @command{gcc}, the function name will also be stored and thethread identifier will be stored if using the thread-safe library.@itemThe library uses a header file to redefine the memory allocation functions asmacros in order to obtain more information about where they were called from.This is not strictly required on UNIX and Windows platforms (and AmigaOS whenusing @command{gcc}), since the library automatically redefines the defaultsystem memory allocation functions. All redefinitions in the header can alsobe disabled by defining the @code{NDEBUG} preprocessor macro, which alsodisables the effect of calling any mpatrol library function.@itemA command is supplied to run a program that was linked with the mpatrol librarywith any specified options on the command line. On some UNIX platforms, anoption also exists to override the default memory allocation routines for anydynamically-linked program that was not previously linked with the mpatrollibrary.@itemThe mpatrol library can be built to liaise with Parasoft Inuse, a commercialgraphical memory usage tool that can display the current memory map of a runningprocess. Inuse is supplied with Parasoft Insure++.@itemComes with a library of tools that are built on top of the mpatrol library andcan be used to extend it for specific applications.@itemAn automake macro is provided to ease the integration of mpatrol into a new orexisting project.@itemA small tool is provided to read a dictionary file and display all of the wordsthat can be represented in hexadecimal form. Such hexadecimal constants can beused to initialise variables in user programs in order to aid debugging.@item
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -