📄 xmemaspc1.ah
字号:
/*****************************************************************************/
/*
XMEMASPC1.AH - Extended C/C++ Dynamic Memory Control And Debug Library
Copyright (C) Juergen Mueller (J.M.) 2005-2008
All rights reserved.
You are expressly prohibited from selling this software in any form
or removing this notice.
THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, THE
IMPLIED WARRANTIES OF MERCHANTIBILITY, FITNESS FOR A PARTICULAR
PURPOSE, OR NON-INFRINGEMENT. THE AUTHOR SHALL NOT BE LIABLE FOR
ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. THE ENTIRE RISK
AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM AND DOCUMENTATION
IS WITH YOU.
Permission to modify the code and to distribute modified code is granted,
provided the above notices are retained, and a notice that the code was
modified is included with the above copyright notice.
written by: Juergen Mueller, D-70806 Kornwestheim, GERMANY
FILE : XMEMASPC1.AH
REVISION : 02-Jan-2008
11:23:57
*/
/*****************************************************************************/
/* XMEM example with AspectC++ */
/* "pure" version, direct use of C++ aspects without preprocessing code for XMEM */
/*****************************************************************************/
/* XMEM function prototypes */
/*****************************************************************************/
void *xmem_store_pointer(void *, size_t, char *, const char *, const char *, unsigned long);
void xmem_delete_pointer(void *, char *, const char *, const char *, unsigned long);
void *xmem_find_ptr(void *);
/*****************************************************************************/
/* XMEM aspects */
/*****************************************************************************/
aspect XMEM_malloc
{
advice call ("% malloc(...)") : around()
{
printf("\nXMEM aspect: before call to %s", JoinPoint::signature());
tjp->proceed(); /**** do the real malloc() call ****/
if ((void *)(*(unsigned long *)tjp->result()) != NULL)
{ /* malloc() succeeded, store pointer for XMEM tracing */
xmem_store_pointer((void *)(*(unsigned long *)tjp->result()),
(*(size_t *)tjp->arg(0)),
"malloc",
(tjp->args() == 4) ? ((const char *)(*(unsigned long *)tjp->arg(1))) : __FUNCTION__,
(tjp->args() == 4) ? ((const char *)(*(unsigned long *)tjp->arg(2))) : __FILE__,
(tjp->args() == 4) ? (*(unsigned long *)tjp->arg(3)) : __LINE__
);
}
printf("\nXMEM aspect: after return from %s", JoinPoint::signature());
}
};
aspect XMEM_calloc
{
advice call ("% calloc(...)") : around()
{
printf("\nXMEM aspect: before call to %s", JoinPoint::signature());
/* PROBLEM: there is no check whether (count * size) exceeds MAX(size_t) -> overflow may */
/* result in incorrect internal size calculation and probable overwriting of memory area */
tjp->proceed(); /**** do the real calloc() call ****/
if ((void *)(*(unsigned long *)tjp->result()) != NULL)
{ /* calloc() succeeded, store pointer for XMEM tracing */
xmem_store_pointer((void *)(*(unsigned long *)tjp->result()),
(*(unsigned long *)tjp->arg(0)) * (*(unsigned long *)tjp->arg(1)),
"calloc",
(tjp->args() == 5) ? ((const char *)(*(unsigned long *)tjp->arg(2))) : __FUNCTION__,
(tjp->args() == 5) ? ((const char *)(*(unsigned long *)tjp->arg(3))) : __FILE__,
(tjp->args() == 5) ? (*(unsigned long *)tjp->arg(4)) : __LINE__
);
}
printf("\nXMEM aspect: after return from %s", JoinPoint::signature());
}
};
aspect XMEM_realloc
{
advice call ("% realloc(...)") : around()
{
printf("\nXMEM aspect: before call to %s", JoinPoint::signature());
if ( ((void *)(*(unsigned long *)tjp->arg(0)) != NULL)
&& (xmem_find_ptr((void *)(*(unsigned long *)tjp->arg(0))) != NULL)
)
{ /* valid XMEM traced pointer, remove pointer from XMEM tracing */
xmem_delete_pointer((void *)(*(unsigned long *)tjp->arg(0)),
"realloc",
(tjp->args() == 5) ? ((const char *)(*(unsigned long *)tjp->arg(2))) : __FUNCTION__,
(tjp->args() == 5) ? ((const char *)(*(unsigned long *)tjp->arg(3))) : __FILE__,
(tjp->args() == 5) ? (*(unsigned long *)tjp->arg(4)) : __LINE__
);
}
tjp->proceed(); /**** do the real realloc() call ****/
if ((void *)(*(unsigned long *)tjp->result()) != NULL)
{ /* realloc() succeeded, store pointer for XMEM tracing */
xmem_store_pointer((void *)(*(unsigned long *)tjp->result()),
(*(unsigned long *)tjp->arg(1)),
"realloc",
(tjp->args() == 5) ? ((const char *)(*(unsigned long *)tjp->arg(2))) : __FUNCTION__,
(tjp->args() == 5) ? ((const char *)(*(unsigned long *)tjp->arg(3))) : __FILE__,
(tjp->args() == 5) ? (*(unsigned long *)tjp->arg(4)) : __LINE__
);
}
printf("\nXMEM aspect: after return from %s", JoinPoint::signature());
}
};
aspect XMEM_free
{
advice call ("% free(...)") : around()
{
printf("\nXMEM aspect: before call to %s", JoinPoint::signature());
if (xmem_find_ptr((void *)(*(unsigned long *)tjp->arg(0))) != NULL)
{ /* valid XMEM traced pointer, remove pointer from XMEM tracing */
xmem_delete_pointer((void *)(*(unsigned long *)tjp->arg(0)),
"free",
(tjp->args() == 4) ? ((const char *)(*(unsigned long *)tjp->arg(1))) : __FUNCTION__,
(tjp->args() == 4) ? ((const char *)(*(unsigned long *)tjp->arg(2))) : __FILE__,
(tjp->args() == 4) ? (*(unsigned long *)tjp->arg(3)) : __LINE__
);
tjp->proceed(); /**** do the real free() call ****/
}
printf("\nXMEM aspect: after return from %s", JoinPoint::signature());
}
};
aspect XMEM_strdup
{
advice call ("% strdup(...)" || "% _strdup(...)") : around()
{
printf("\nXMEM aspect: before call to %s", JoinPoint::signature());
tjp->proceed(); /**** do the real strdup() call ****/
if ((void *)(*(unsigned long *)tjp->result()) != NULL)
{ /* strdup() succeeded, store pointer for XMEM tracing */
xmem_store_pointer((void *)(*(unsigned long *)tjp->result()),
(unsigned long)(strlen((char *)(*(unsigned long *)tjp->arg(0))) + 1),
"strdup",
(tjp->args() == 4) ? ((const char *)(*(unsigned long *)tjp->arg(1))) : __FUNCTION__,
(tjp->args() == 4) ? ((const char *)(*(unsigned long *)tjp->arg(2))) : __FILE__,
(tjp->args() == 4) ? (*(unsigned long *)tjp->arg(3)) : __LINE__
);
}
printf("\nXMEM aspect: after return from %s", JoinPoint::signature());
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -