📄 memcheck.c
字号:
/****************************************************************************** * Memory Checker * * Copyright (C) 2002 Hal Duston * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program 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 General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * * ******************************************************************************/#ifdef HAVE_CONFIG_H# include <config.h>#endif#include <ctype.h>#ifdef HAVE_ERRNO_H# include <errno.h>#endif#ifndef HAVE_DECL_ERRNOextern int errno;#endif#ifdef HAVE_LIMITS_H# include <limits.h>#endif#ifdef HAVE_STDARG_H# include <stdarg.h>#else# ifdef HAVE_VARARGS_H# include <varargs.h># endif#endif#include <stdio.h>#ifdef HAVE_SYS_TYPES_H# include <sys/types.h>#endif#if STDC_HEADERS# include <stdlib.h># include <stddef.h>#else# if HAVE_STDLIB_H# include <stdlib.h># endif#endif#if HAVE_STRING_H# if !STDC_HEADERS && HAVE_MEMORY_H# include <memory.h># endif# include <string.h>#else# if HAVE_STRINGS_H# include <strings.h># endif#endif#ifdef HAVE_UNISTD_H# include <unistd.h>#endif#ifdef HAVE_SIGNAL_H# include <signal.h>#endif#ifndef HAVE_MEMCPY# ifdef HAVE_BCOPY# define memcpy(d,s,n) bcopy((s),(d),(n))# endif#endif#ifndef HAVE_MEMMOVE# ifdef HAVE_BCOPY# define memmove(d,s,n) bcopy((s),(d),(n))# endif#endif#ifdef HAVE_FCNTL_H# include <fcntl.h>#endif#ifdef HAVE_MALLOC_H# include <malloc.h>#endif/* This mess was copied from the autoconf acspecific.m4, *//* which in turn was copied from the GNU getpagesize.h. */#ifndef HAVE_GETPAGESIZE# ifdef _SC_PAGESIZE# define getpagesize() sysconf(_SC_PAGESIZE)# else /* no _SC_PAGESIZE */# ifdef HAVE_SYS_PARAM_H# include <sys/param.h># ifdef EXEC_PAGESIZE# define getpagesize() EXEC_PAGESIZE# else /* no EXEC_PAGESIZE */# ifdef NBPG# define getpagesize() NBPG * CLSIZE# ifndef CLSIZE# define CLSIZE 1# endif /* no CLSIZE */# else /* no NBPG */# ifdef NBPC# define getpagesize() NBPC# else /* no NBPC */# ifdef PAGESIZE# define getpagesize() PAGESIZE# endif /* no PAGESIZE */# endif /* no NBPC */# endif /* no NBPG */# endif /* no EXEC_PAGESIZE */# else /* no HAVE_SYS_PARAM_H */# define getpagesize() 8192 /* punt totally */# endif /* no HAVE_SYS_PARAM_H */# endif /* no _SC_PAGESIZE */#elseextern int getpagesize(void);#endif /* no HAVE_GETPAGESIZE */#ifdef HAVE_SYS_MMAN_H# include <sys/mman.h>#endif#ifdef HAVE_SYS_REG_H# include <sys/reg.h>#endif#ifdef HAVE_SYS_PARAM_H# include <sys/param.h>#endif#ifdef HAVE_SYS_USER_H# include <sys/user.h>#endif#if defined(__x86_64__) && defined(__linux__)# include "x86_64-ucontext.h"#else# if (defined(__arm) || defined(__arm__)) && defined(__linux__)# include "arm-ucontext.h"# else# ifdef HAVE_UCONTEXT_H# include <ucontext.h># endif# endif#endif#ifdef HAVE_EXECINFO_H# include <execinfo.h>#endif#include "memcheck.h"#ifndef MAP_FAILED# define MAP_FAILED (void *)(-1L)#endif#ifndef MAP_ANONYMOUS# ifdef MAP_ANON# define MAP_ANONYMOUS MAP_ANON# endif#endif#ifndef SA_SIGINFO# define SA_SIGINFO 0#endif#ifdef __GNUC__# define memcheck_return_address(arg1,arg2) \ ((void)(arg1), (void)(arg2), __builtin_return_address(0))#else# if STACK_ORDER == -1# define memcheck_return_address(arg1,arg2) \ ((void)(arg2), memcheck_get_return_address(&(arg1)))# elif STACK_ORDER == 1# define memcheck_return_address(arg1,arg2) \ ((void)(arg1), memcheck_get_return_address(&(arg2)))# endif# if defined(_AIX)# define STACK_OFFSET 4# elif defined(__hppa) || defined(__hppa__)# define STACK_OFFSET 3# elif defined(__alpha)# define STACK_OFFSET 2# else# define STACK_OFFSET 1# endif#endif#define meminfo_from_ptr(ptr) \ ((struct meminfo *)((((long)(ptr)) & -memcheck_pagesize) \ - memcheck_pagesize))#define MAX_REALLOC_CALL ((memcheck_pagesize \ - sizeof(struct meminfo)) \ / sizeof(struct caller) + 1)#ifdef HAVE_CADDR_T# define mprotect(ptr,size,prot) (mprotect)((caddr_t)ptr,size,prot)# define munmap(ptr,size) (munmap)((caddr_t)ptr,size)#endif#ifdef HAVE_UCONTEXT_T# ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_CR2# define ucontext_data(uc) ((void *)((uc)->uc_mcontext.cr2))# else# ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_SC_TRAPARG_A0# define ucontext_data(uc) ((void *)((uc)->uc_mcontext.sc_traparg_a0))# else# ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_MC_ERR# define ucontext_data(uc) ((void *)((uc)->uc_mcontext.mc_err))# else# ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_FAULT_ADDRESS# define ucontext_data(uc) ((void *)((uc)->uc_mcontext.fault_address))# else# ifdef HAVE_UCONTEXT_T_UC_MCONTEXT__ES_DAR# define ucontext_data(uc) ((void *)((uc)->uc_mcontext->es.dar))# endif# endif# endif# endif# endif# ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_SC_IP# define ucontext_text(uc) ((void *)((uc)->uc_mcontext.sc_ip))# else# ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_SC_PC# define ucontext_text(uc) ((void *)((uc)->uc_mcontext.sc_pc))# else# ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_RIP# define ucontext_text(uc) ((void *)((uc)->uc_mcontext.rip))# else# ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_EIP# define ucontext_text(uc) ((void *)((uc)->uc_mcontext.eip))# else# ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_MC_RIP# define ucontext_text(uc) ((void *)((uc)->uc_mcontext.mc_rip))# else# ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_MC_EIP# define ucontext_text(uc) ((void *)((uc)->uc_mcontext.mc_eip))# else# ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_ARM_PC# define ucontext_text(uc) ((void *)((uc)->uc_mcontext.arm_pc))# else# ifdef GetSSReg# define ucontext_text(uc) ((void *)GetSSReg(&(uc)->uc_mcontext, ss_pcoq_head))# else# ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_GR8# define ucontext_text(uc) ((void *)((uc)->uc_mcontext.gr8))# else# ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_PSW# define ucontext_text(uc) ((void *)((uc)->uc_mcontext.psw.addr))# else# ifdef HAVE_UCONTEXT_T_UC_MCONTEXT__SS_SRR0# define ucontext_text(uc) ((void *)((uc)->uc_mcontext->ss.srr0))# else# ifndef R_PC# ifdef REG_EIP# define R_PC REG_EIP# else# ifdef EIP# define R_PC EIP# else# ifdef REG_RIP# define R_PC REG_RIP# else# ifdef REG_PC# define R_PC REG_PC# endif# endif# endif# endif# endif# ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_GREGS# ifdef R_TRAPARG_A0# define ucontext_data(uc) ((void *)((uc)->uc_mcontext.gregs[R_TRAPARG_A0]))# endif# ifdef R_PC# define ucontext_text(uc) ((void *)((uc)->uc_mcontext.gregs[R_PC]))# endif# else# ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_MC_REGS# ifdef R_TRAPARG_A0# define ucontext_data(uc) ((void *)((uc)->uc_mcontext.mc_regs[R_TRAPARG_A0]))# endif# ifdef R_PC# define ucontext_text(uc) ((void *)((uc)->uc_mcontext.mc_regs[R_PC]))# endif# else# ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_SC_REGS# ifdef R_TRAPARG_A0# define ucontext_data(uc) ((void *)((uc)->uc_mcontext.sc_regs[R_TRAPARG_A0]))# endif# ifdef R_PC# define ucontext_text(uc) ((void *)((uc)->uc_mcontext.sc_regs[R_PC]))# endif# else# ifdef HAVE_UCONTEXT_T_UC_MCONTEXT_SC# define ucontext_text(uc) (sigcontext_text((uc)->uc_mcontext.sc))# define ucontext_data(uc) (sigcontext_data((uc)->uc_mcontext.sc))# endif# endif# endif# endif# endif# endif# endif# endif# endif# endif# endif# endif# endif# endif# endif#endif#if defined(HAVE_SIGCONTEXT_T) || defined(HAVE_STRUCT_SIGCONTEXT)# if defined(HAVE_SIGCONTEXT_T_CR2) || defined(HAVE_STRUCT_SIGCONTEXT_CR2)# define sigcontext_data(sc) ((void *)((sc)->cr2))# else# if defined(HAVE_SIGCONTEXT_T_FAULT_ADDRESS) || defined(HAVE_STRUCT_SIGCONTEXT_FAULT_ADDRESS)# define sigcontext_data(sc) (((void *)((sc)->fault_address)))# endif# endif# if defined(HAVE_SIGCONTEXT_T_SC_PC) || defined(HAVE_STRUCT_SIGCONTEXT_SC_PC)# define sigcontext_text(sc) ((void *)((sc)->sc_pc))# else# if defined(HAVE_SIGCONTEXT_T_SC_IR) || defined(HAVE_STRUCT_SIGCONTEXT_SC_IR)# define sigcontext_text(sc) ((void *)((sc)->sc_ir))# else# if defined(HAVE_SIGCONTEXT_T_SC_JMPBUF) || defined(HAVE_STRUCT_SIGCONTEXT_SC_JMPBUF)# define sigcontext_data(sc) ((void *)((sc)->sc_jmpbuf.jmp_context.o_vaddr))# define sigcontext_text(sc) ((void *)((sc)->sc_jmpbuf.jmp_context.iar))# else# if defined(HAVE_SIGCONTEXT_T_SC_RIP) || defined(HAVE_STRUCT_SIGCONTEXT_SC_RIP)# define sigcontext_text(sc) ((void *)((sc)->sc_rip))# else# if defined(HAVE_SIGCONTEXT_T_SC_EIP) || defined(HAVE_STRUCT_SIGCONTEXT_SC_EIP)# define sigcontext_text(sc) ((void *)((sc)->sc_eip))# else# if defined(HAVE_SIGCONTEXT_T_RIP) || defined(HAVE_STRUCT_SIGCONTEXT_RIP)# define sigcontext_text(sc) ((void *)((sc)->rip))# else# if defined(HAVE_SIGCONTEXT_T_EIP) || defined(HAVE_STRUCT_SIGCONTEXT_EIP)# define sigcontext_text(sc) ((void *)((sc)->eip))# else# if defined(HAVE_SIGCONTEXT_T_ARM_PC) || defined(HAVE_STRUCT_SIGCONTEXT_ARM_PC)# define sigcontext_text(sc) ((void *)((sc)->arm_pc))# else# if defined(HAVE_SIGCONTEXT_T_SREGS) || defined(HAVE_STRUCT_SIGCONTEXT_SREGS)# define sigcontext_text(sc) ((void *)((sc)->sregs->regs.psw.addr))# else# if defined(HAVE_SIGCONTEXT_T_SC_TRAPARG_A0) || defined(HAVE_STRUCT_SIGCONTEXT_SC_TRAPARG_A0)# define sigcontext_text(cs) ((void *)((sc)->sc_traparg_a0))# endif# endif# endif# endif# endif# endif# endif# endif# endif# endif#endif#if defined(ucontext_data)# define context_data(context) (ucontext_data((ucontext_t *)(context)))#else# if defined(sigcontext_data)# define context_data(context) (sigcontext_data((sigcontext_t *)(context)))# else# define context_data(context) ((void *)(((void)(context)), NULL))# endif#endif#if defined(ucontext_text)# define context_text(context) (ucontext_text((ucontext_t *)(context)))#else# if defined(sigcontext_text)# define context_text(context) (sigcontext_text((sigcontext_t *)(context)))# else# define context_text(context) ((void *)(((void)(context)), NULL))# endif#endif#if defined(LONG_BIT) && LONG_BIT == 64# define MEMCHECK_SENTRY 0x5aa55aa55aa55aa5#else# define MEMCHECK_SENTRY 0x5aa55aa5#endif#define MEMCHECK_INFO 3#define MEMCHECK_WARN 2#define MEMCHECK_ERROR 1#define MEMCHECK_NONE 0#define strdup_function 1#define malloc_function 2#define calloc_function 3#define realloc_function 4#define free_function 5#define cfree_function 6#define valloc_function 7#define memalign_function 8#define posix_memalign_function 9#define alloc_type 1#define free_type 2#ifdef HAVE_SIGACTION# ifdef HAVE_SIGINFO_Ttypedef siginfo_t *sa_action_type_arg2;# elsetypedef int sa_action_type_arg2;# endif# if defined(HAVE_SIGACTION_T_SA_SIGACTION) \ || defined(HAVE_STRUCT_SIGACTION_SA_SIGACTION)typedef RETSIGTYPE (*sa_function)(int signum, sa_action_type_arg2 arg2, void *context);# elif defined(HAVE_SIGACTION_T_SA_HANDLER) \ || defined(HAVE_STRUCT_SIGACTION_SA_HANDLER)typedef RETSIGTYPE (*sa_function)(int signum);# endif#endif#define BACKTRACE_SIZE 32struct backtrace{ int size; void *array[BACKTRACE_SIZE];};struct caller{#ifdef HAVE_BACKTRACE struct backtrace trace;#endif const void *addr; const char *file; int line; int func;};struct memcheck_function{ const char *name; int type;};struct meminfo;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -