📄 mpatrol.c
字号:
/* * mpatrol * A library for controlling and tracing dynamic memory allocations. * Copyright (C) 1997-2002 Graeme S. Roy <graeme.roy@analog.com> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307, USA. *//* * A tool for setting various mpatrol library options when running a program * that has been linked with the mpatrol library. */#include "getopt.h"#include "version.h"#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <errno.h>#if TARGET == TARGET_UNIX#include <sys/param.h>#include <sys/types.h>#include <sys/wait.h>#include <unistd.h>#elif TARGET == TARGET_WINDOWS#include <process.h>#endif /* TARGET */#if MP_IDENT_SUPPORT#ident "$Id: mpatrol.c,v 1.45 2002/01/08 20:13:59 graeme Exp $"#else /* MP_IDENT_SUPPORT */static MP_CONST MP_VOLATILE char *mpatrol_id = "$Id: mpatrol.c,v 1.45 2002/01/08 20:13:59 graeme Exp $";#endif /* MP_IDENT_SUPPORT */#define PROGVERSION "2.7" /* the current version of this program *//* The flags used to parse the command line options. */typedef enum options_flags{ OF_ALLOCSTOP = 'A', OF_ALLOCBYTE = 'a', OF_PAGEALLOCUPPER = 'B', OF_PAGEALLOCLOWER = 'b', OF_CHECKALL = 'C', OF_CHECK = 'c', OF_DEFALIGN = 'D', OF_DYNAMIC = 'd', OF_SHOWENV = 'E', OF_EDIT = 'e', OF_FREESTOP = 'F', OF_FREEBYTE = 'f', OF_SAFESIGNALS = 'G', OF_USEDEBUG = 'g', OF_HTML = 'H', OF_HELP = 'h', OF_READENV = 'I', OF_LIST = 'i', OF_THREADS = 'j', OF_LOGALL = 'L', OF_LOGFILE = 'l', OF_ALLOWOFLOW = 'M', OF_USEMMAP = 'm', OF_NOPROTECT = 'N', OF_NOFREE = 'n', OF_OFLOWSIZE = 'O', OF_OFLOWBYTE = 'o', OF_PROFFILE = 'P', OF_PROF = 'p', OF_FAILSEED = 'Q', OF_FAILFREQ = 'q', OF_REALLOCSTOP = 'R', OF_PROGFILE = 'r', OF_SHOWALL = 'S', OF_AUTOSAVE = 's', OF_TRACEFILE = 'T', OF_TRACE = 't', OF_UNFREEDABORT = 'U', OF_LIMIT = 'u', OF_VERSION = 'V', OF_PRESERVE = 'v', OF_OFLOWWATCH = 'w', OF_CHECKALLOCS = SHORTOPT_MAX + 1, OF_CHECKFORK, OF_CHECKFREES, OF_CHECKMEMORY, OF_CHECKREALLOCS, OF_LARGEBOUND, OF_LEAKTABLE, OF_LOGALLOCS, OF_LOGFREES, OF_LOGMEMORY, OF_LOGREALLOCS, OF_MEDIUMBOUND, OF_SHOWFREE, OF_SHOWFREED, OF_SHOWMAP, OF_SHOWSYMBOLS, OF_SHOWUNFREED, OF_SMALLBOUND}options_flags;/* The buffer used to build up the environment variable containing options * for the mpatrol library. */static char options[1024];/* The current length of the options buffer. */static size_t optlen;/* The filename used to invoke this tool. */static char *progname;/* The following string options correspond to their uppercase equivalents when * setting the environment variable containing mpatrol library options. */static char *allocstop, *reallocstop, *freestop;static char *allocbyte, *freebyte;static char *oflowbyte, *oflowsize;static char *defalign, *limit;static char *failfreq, *failseed, *unfreedabort;static char *logfile, *proffile;static char *tracefile, *progfile;static char *autosave, *check;static char *nofree, *pagealloc;static char *smallbound, *mediumbound, *largebound;/* The following boolean options correspond to their uppercase equivalents when * setting the environment variable containing mpatrol library options. */static int checkallocs, checkreallocs;static int checkfrees, checkmemory;static int showmap, showsymbols;static int showfree, showfreed;static int showunfreed, leaktable;static int logallocs, logreallocs;static int logfrees, logmemory;static int allowoflow, prof, trace;static int safesignals, noprotect;static int checkfork, preserve;static int oflowwatch, usemmap;static int usedebug, editlist, html;/* The table describing all recognised options. */static option options_table[] ={ {"alloc-byte", OF_ALLOCBYTE, "unsigned integer", "\tSpecifies an 8-bit byte pattern with which to prefill newly-allocated\n" "\tmemory.\n"}, {"alloc-stop", OF_ALLOCSTOP, "unsigned integer", "\tSpecifies an allocation index at which to stop the program when it is\n" "\tbeing allocated.\n"}, {"allow-oflow", OF_ALLOWOFLOW, NULL, "\tSpecifies that a warning rather than an error should be produced if\n" "\tany memory operation function overflows the boundaries of a memory\n" "\tallocation, and that the operation should still be performed.\n"}, {"auto-save", OF_AUTOSAVE, "unsigned integer", "\tSpecifies the frequency at which to periodically write the profiling\n" "\tdata to the profiling output file.\n"}, {"check", OF_CHECK, "unsigned range", "\tSpecifies a range of allocation indices at which to check the\n" "\tintegrity of free memory and overflow buffers.\n"}, {"check-all", OF_CHECKALL, NULL, "\tEquivalent to the --check-allocs, --check-reallocs, --check-frees and\n" "\t--check-memory options specified together.\n"}, {"check-allocs", OF_CHECKALLOCS, NULL, "\tChecks that no attempt is made to allocate a block of memory of size\n" "\tzero.\n"}, {"check-fork", OF_CHECKFORK, NULL, "\tChecks at every call to see if the process has been forked in case\n" "\tnew log, profiling and tracing output files need to be started.\n"}, {"check-frees", OF_CHECKFREES, NULL, "\tChecks that no attempt is made to deallocate a NULL pointer.\n"}, {"check-memory", OF_CHECKMEMORY, NULL, "\tChecks that no attempt is made to perform a zero-length memory\n" "\toperation on a NULL pointer.\n"}, {"check-reallocs", OF_CHECKREALLOCS, NULL, "\tChecks that no attempt is made to reallocate a NULL pointer or resize\n" "\tan existing block of memory to size zero.\n"}, {"def-align", OF_DEFALIGN, "unsigned integer", "\tSpecifies the default alignment for general-purpose memory\n" "\tallocations, which must be a power of two.\n"}, {"dynamic", OF_DYNAMIC, NULL, "\tSpecifies that programs which were not linked with the mpatrol\n" "\tlibrary should also be traced, but only if they were dynamically\n" "\tlinked.\n"}, {"edit", OF_EDIT, NULL, "\tSpecifies that a text editor should be invoked to edit any relevant\n" "\tsource files that are associated with any warnings or errors when\n" "\tthey occur.\n"}, {"fail-freq", OF_FAILFREQ, "unsigned integer", "\tSpecifies the frequency at which all memory allocations will randomly\n" "\tfail.\n"}, {"fail-seed", OF_FAILSEED, "unsigned integer", "\tSpecifies the random number seed which will be used when determining\n" "\twhich memory allocations will randomly fail.\n"}, {"free-byte", OF_FREEBYTE, "unsigned integer", "\tSpecifies an 8-bit byte pattern with which to prefill newly-freed\n" "\tmemory.\n"}, {"free-stop", OF_FREESTOP, "unsigned integer", "\tSpecifies an allocation index at which to stop the program when it is\n" "\tbeing freed.\n"}, {"help", OF_HELP, NULL, "\tDisplays this quick-reference option summary.\n"}, {"html", OF_HTML, NULL, "\tSpecifies that the log file should be formatted in HTML.\n"}, {"large-bound", OF_LARGEBOUND, "unsigned integer", "\tSpecifies the limit in bytes up to which memory allocations should be\n" "\tclassified as large allocations for profiling purposes.\n"}, {"leak-table", OF_LEAKTABLE, NULL, "\tSpecifies that the leak table should be automatically used and a leak\n" "\ttable summary should be displayed at the end of program execution.\n"}, {"limit", OF_LIMIT, "unsigned integer", "\tSpecifies the limit in bytes at which all memory allocations should\n" "\tfail if the total allocated memory should increase beyond this.\n"}, {"list", OF_LIST, NULL, "\tSpecifies that a context listing should be shown for any relevant\n" "\tsource files that are associated with any warnings or errors when\n" "\tthey occur.\n"}, {"log-all", OF_LOGALL, NULL, "\tEquivalent to the --log-allocs, --log-reallocs, --log-frees and\n" "\t--log-memory options specified together.\n"}, {"log-allocs", OF_LOGALLOCS, NULL, "\tSpecifies that all memory allocations are to be logged and sent to\n" "\tthe log file.\n"}, {"log-file", OF_LOGFILE, "string", "\tSpecifies an alternative file in which to place all diagnostics from\n" "\tthe mpatrol library.\n"}, {"log-frees", OF_LOGFREES, NULL, "\tSpecifies that all memory deallocations are to be logged and sent to\n" "\tthe log file.\n"}, {"log-memory", OF_LOGMEMORY, NULL, "\tSpecifies that all memory operations are to be logged and sent to the\n" "\tlog file.\n"}, {"log-reallocs", OF_LOGREALLOCS, NULL, "\tSpecifies that all memory reallocations are to be logged and sent to\n" "\tthe log file.\n"}, {"medium-bound", OF_MEDIUMBOUND, "unsigned integer", "\tSpecifies the limit in bytes up to which memory allocations should be\n" "\tclassified as medium allocations for profiling purposes.\n"}, {"no-free", OF_NOFREE, "unsigned integer", "\tSpecifies that a number of recently-freed memory allocations should\n" "\tbe prevented from being returned to the free memory pool.\n"}, {"no-protect", OF_NOPROTECT, NULL, "\tSpecifies that the mpatrol library's internal data structures should\n" "\tnot be made read-only after every memory allocation, reallocation or\n" "\tdeallocation.\n"}, {"oflow-byte", OF_OFLOWBYTE, "unsigned integer", "\tSpecifies an 8-bit byte pattern with which to fill the overflow\n" "\tbuffers of all memory allocations.\n"}, {"oflow-size", OF_OFLOWSIZE, "unsigned integer", "\tSpecifies the size in bytes to use for all overflow buffers, which\n" "\tmust be a power of two.\n"}, {"oflow-watch", OF_OFLOWWATCH, NULL, "\tSpecifies that watch point areas should be used for overflow buffers\n" "\trather than filling with the overflow byte.\n"}, {"page-alloc-lower", OF_PAGEALLOCLOWER, NULL, "\tSpecifies that each individual memory allocation should occupy at\n" "\tleast one page of virtual memory and should be placed at the lowest\n" "\tpoint within these pages.\n"}, {"page-alloc-upper", OF_PAGEALLOCUPPER, NULL, "\tSpecifies that each individual memory allocation should occupy at\n" "\tleast one page of virtual memory and should be placed at the highest\n" "\tpoint within these pages.\n"}, {"preserve", OF_PRESERVE, NULL, "\tSpecifies that any reallocated or freed memory allocations should\n" "\tpreserve their original contents.\n"}, {"prof", OF_PROF, NULL, "\tSpecifies that all memory allocations are to be profiled and sent to\n" "\tthe profiling output file.\n"}, {"prof-file", OF_PROFFILE, "string", "\tSpecifies an alternative file in which to place all memory allocation\n" "\tprofiling information from the mpatrol library.\n"}, {"prog-file", OF_PROGFILE, "string", "\tSpecifies an alternative filename with which to locate the executable\n" "\tfile containing the program's symbols.\n"}, {"read-env", OF_READENV, NULL, "\tReads and passes through the contents of the " MP_OPTIONS "\n" "\tenvironment variable.\n"}, {"realloc-stop", OF_REALLOCSTOP, "unsigned integer", "\tSpecifies an allocation index at which to stop the program when a\n" "\tmemory allocation is being reallocated.\n"}, {"safe-signals", OF_SAFESIGNALS, NULL, "\tInstructs the library to save and replace certain signal handlers\n" "\tduring the execution of library code and to restore them\n" "\tafterwards.\n"}, {"show-all", OF_SHOWALL, NULL, "\tEquivalent to the --show-free, --show-freed, --show-unfreed,\n" "\t--show-map and --show-symbols options specified together.\n"}, {"show-env", OF_SHOWENV, NULL, "\tDisplays the contents of the " MP_OPTIONS " environment variable.\n"}, {"show-free", OF_SHOWFREE, NULL, "\tSpecifies that a summary of all of the free memory blocks should be\n" "\tdisplayed at the end of program execution.\n"}, {"show-freed", OF_SHOWFREED, NULL, "\tSpecifies that a summary of all of the freed memory allocations\n" "\tshould be displayed at the end of program execution.\n"}, {"show-map", OF_SHOWMAP, NULL, "\tSpecifies that a memory map of the entire heap should be displayed at\n" "\tthe end of program execution.\n"}, {"show-symbols", OF_SHOWSYMBOLS, NULL, "\tSpecifies that a summary of all of the function symbols read from\n" "\tthe program's executable file should be displayed at the end of\n" "\tprogram execution.\n"}, {"show-unfreed", OF_SHOWUNFREED, NULL, "\tSpecifies that a summary of all of the unfreed memory allocations\n" "\tshould be displayed at the end of program execution.\n"}, {"small-bound", OF_SMALLBOUND, "unsigned integer",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -