This chapter is a complete reference to the Memory Advisor API, as defined by the memadv.h header file.
Introduction
Memory Advisor API Variables and Macros
Configuration Option Names
The Memory Advisor Operating Mode
The MA_CHK_READ and MA_CHK_WRITE Constants
The _ma_errno Variable
Thread States
The Memory Advisor Error File Descriptor
The Memory Advisor Historical IDs
Overriding Memory Advisor's Internal Allocator
Integrating a User-Defined Memory Allocator
Miscellaneous Variables
Commonly Used Functions
Functions for Memory Leak Reports
Functions for Other Reports
Functions for Controlling Memory Advisor
Functions for Memory Advisor Watchpoints
Functions for Checking Data Access
Functions for Getting Stack Trace Information
Macros for Implementing Discretionary Debugging
Functions for Integrating User-Defined Memory Allocators
Function Descriptions
MaAllocChainCheck()
MaAllocChainList()
MaAllocFreeFifoFlush()
MACL()
MADBG()
MADBG_CODE()
MADBG_LOC()
MaLeakBase()
MaLeakClearAll()
MaLeakPtrNoLeakSearch()
MaLeakPtrNotALeak()
MaLeakShowAll()
MaLeakShowNew()
MaOptionGet()
MaOptionSet()
MaOptionShowAll()
MaOutputHandler()
MaOutputInfoMsg()
MaOutputString()
MaProcAbort()
MaProcExit()
MaProcFuncList()
MaProcInitLibrary()
MaProcReadSyms()
MaPtrCheckData()
MaPtrCheckString()
MaPtrCheckStringCh()
MaPtrCheckStringLen()
MaPtrDataSize()
MaPtrInfo()
MaPtrSetMode()
MaReportAddressMap()
MaReportAllocations()
MaReportFdLeaks()
MaStackGet()
MaStackGetCaller()
MaStackGetCurrent()
MaStackGetEntry()
MaStackGetLevel()
MaStackWriteToFd()
MaStackWriteToFile()
MaStop()
MaThreadWhoHasLock()
MaUsrAllocArenaDelete()
MaUsrAllocArenaExtend()
MaUsrAllocArenaNew()
MaUsrAllocSegFifoAdd()
MaUsrAllocSegFree()
MaUsrAllocSegReg()
MaUsrAllocSegRegister()
MaUsrAllocSegRelease()
MaWatchAdd()
MaWatchCheckAll()
MaWatchnRead()
MaWatchnReadWrite()
MaWatchnWrite()
MaWatchRemove()
MaWatchRemoveAll()
MaWatchShowAll()
The functions in the API provide you with tools to access dynamic memory and stack context information. Using these functions, you can set Memory Advisor configuration options during program execution so that you can enable, modify, and disable error-checking for portions of your program. These functions also let you report memory and stack information at any point in your program, not just at Memory Advisor default points.
To use Memory Advisor functions, variables, and macros:
Introduction
Memory Advisor API Variables and Macros
Configuration Option Names
To use the MaOptionSet() and MaOptionGet() functions to retrieve or set the value of configuration options from your program, you must supply a string naming the option. The following constants, one for each option, are a convenience and will help isolate your code from changes to option names. The names of the constants are the same as the names of the options:
MA_BOUNDARY_POST MA_BOUNDARY_PRE ... MA_WARN_ZERO_BYTE MA_WATCHPOINT_SAVE
We have provided the following macros for your ease in determining the Memory Advisor operating mode. We recommend you use these macros rather than interrogating the _ma_operating_mode variable directly. Each of the following macros returns a boolean value indicating whether Memory Advisor is operating in the named mode:
if( MA_MODE_CHECK(MA_IS_MADBG_ONLY) == TRUE )
The Memory Advisor Historical IDs
Memory Advisor tracks the number of times certain events occur during execution of a program. It keeps an array of counters (unsigned long) that you can access for statistics about your program. The counters reside in the _ma_historical_ids[] array, and the following is a list of indices into the array:
| MA_ID_MALLOC | malloc() counter |
| MA_ID_REALLOC | realloc() counter |
| MA_ID_CALLOC | calloc() counter |
| MA_ID_XTMALLOC | XtMalloc() counter |
| MA_ID_XTREALLOC | XtRealloc() counter |
| MA_ID_XTCALLOC | XtCalloc() counter |
| MA_ID_ALIGNED | memalign() counter |
| MA_ID_NEW | C++ new operator counter |
| MA_ID_GETOPT | MaOptionGet() counter |
| MA_ID_SBRK | sbrk() counter |
| MA_ID_STRDUP | strdup() counter |
| MA_ID_HIST | total allocation counter |
| MA_ID_INTERCEPT | total intercept counter |
| MA_ID_USRALLOC | user-defined memory allocator counter |
| MA_ID_FREE | free() counter |
| MA_ID_CFREE | cfree() counter |
| MA_ID_XTFREE | XtFree() counter |
| MA_ID_REALLOC_FREE | realloc() free counter|
| MA_ID_XTREALLOC_FREE | XtRealloc() free counter |
| MA_ID_DELETE | C++ delete operator counter |
| MA_ID_USRFREE | user free counter |
printf("Number of total allocations:\t%u\n",
_ma_historical_ids[MA_ID_HIST]);
void *(*_ma_GetDataFunc)(int, long, void *);
The following are the access modes to which Memory Advisor sets memory. Although Memory Advisor generally handles setting access modes itself, you can set memory directly by calling MaPtrSetMode(), supplying one of the following modes:
Additionally, MaUsrAllocSegFifoAdd() takes as a parameter a function pointer of type UAFreeFunc_t whose profile is:
void (*UAFreeFunc_t)(void *, void *);
This section describes some common scenarios in which you might want to use Memory Advisor API functions. In each case, we provide a list of relevant functions. For full details, see the discussion of that particular function.
Commonly Used Functions
Functions for Memory Leak Reports
This function prints a report of all leaks since your program began: Functions for Other Reports
This function reports your program's memory allocations:
Functions for Integrating User-Defined Memory Allocators
These functions let you integrate your own allocator (referred to as a user-defined memory allocator) with Memory Advisor. The functions operate on two distinct data types: arenas and memory segments.
This function creates a new arena:
MaAllocChainCheck()
Description
The MaAllocChainCheck() function verifies that the allocation chain is intact and that your program has not corrupted it.
Syntax
The syntax for this function is:
if (MaAllocChainCheck(1)){
do_cleanup();
exit(1);
}
int fd;
fd = open("memadvise.log", O_CREAT | O_TRUNC);
MaAllocChainList(fd,1);
****************** MemAdvise: Dump of Malloc Chain ******************
Data Which Data
Address Location Call Length
-------- --------------------------------------------- ------ ------
0x12D8D8 malloc() 2nd 1024
calcfunc() [calc.c:318]
timesheet() [calc.c:16]
main() [calc.c:216]
0x12E004 malloc() 3rd 24
sum() [math.c:831]
timesheet() [calc.c:16]
main() [calc.c:216]
MaAllocFreeFifoFlush();