1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
[ From: Freddie M ]
Here is a howto for dmalloc using AIX 5L and xlc
For xlc compiler:
1. CC='xlc_r' ./configure --enable-threads
2. settings.h
ifndef LOCK_THREADS
#define LOCK_THREADS 1 <<<<<<< should be 1
#endif
3. malloc.c changes , diff changes also included
NOTE : all "malloc" entry points need to be changed to
have 2 "__" pre and post fix.
Example:
malloc __malloc__
free __free__
malloc.c changes:
#ifdef AIX
extern int __multi_threaded;
#include <malloc.h>
#endif
#if LOCK_THREADS
/*
* mutex lock the malloc library
*/
static void lock_thread(void)
{
/* we only lock if the lock-on counter has reached 0 */
#ifdef AIX
/* In AIX __mutli_threaded variable is 0 until a thread is created.
* It will never reset to 0 after that.
*/
if (__multi_threaded) {
#else
if (thread_lock_c == 0) {
#endif
#if HAVE_PTHREAD_MUTEX_LOCK
pthread_mutex_lock(&dmalloc_mutex);
#endif
}
}
/*
* mutex unlock the malloc library
*/
static void unlock_thread(void)
{
#ifdef AIX
/* In AIX __mutli_threaded variable is 0 until a thread is created.
* It will never reset to 0 after that.
*/
if (__multi_threaded) {
#if HAVE_PTHREAD_MUTEX_UNLOCK
pthread_mutex_unlock(&dmalloc_mutex);
#endif /* ifdef AIX */
}
...
#ifdef AIX
void __malloc_init__()
{
pthread_mutex_init(&dmalloc_mutex, THREAD_LOCK_INIT_VAL);
}
void __malloc_prefork_lock__()
{
pthread_mutex_lock(&dmalloc_mutex);
}
void __malloc_postfork_unlock__()
{
pthread_mutex_unlock(&dmalloc_mutex);
}
/* legacy function not needed */
int __mallopt__ (int x, int y)
{
}
/* legacy function not needed */
struct mallinfo __mallinfo__()
{
}
/* optional - not really needed */
void __malloc_once__()
{
}
#endif
---- here is the diff -----
*** ../1/dmalloc-4.8.2/malloc.c Mon Feb 26 13:31:16 2001
--- malloc.c Thu Sep 5 11:26:24 2002
***************
*** 61,66 ****
--- 61,71 ----
#include <signal.h>
#endif
+ #ifdef AIX
+ extern int __multi_threaded;
+ #include <malloc.h>
+ #endif
+
#define DMALLOC_DISABLE
#include "dmalloc.h"
***************
*** 163,169 ****
--- 168,181 ----
static void lock_thread(void)
{
/* we only lock if the lock-on counter has reached 0 */
+ #ifdef AIX
+ /* In AIX __mutli_threaded variable is 0 until a thread is
created.
+ * It will never reset to 0 after that.
+ */
+ if (__multi_threaded) {
+ #else
if (thread_lock_c == 0) {
+ #endif
#if HAVE_PTHREAD_MUTEX_LOCK
pthread_mutex_lock(&dmalloc_mutex);
#endif
***************
*** 175,180 ****
--- 187,202 ----
*/
static void unlock_thread(void)
{
+ #ifdef AIX
+ /* In AIX __mutli_threaded variable is 0 until a thread is
created.
+ * It will never reset to 0 after that.
+ */
+ if (__multi_threaded) {
+ #if HAVE_PTHREAD_MUTEX_UNLOCK
+ pthread_mutex_unlock(&dmalloc_mutex);
+ #endif
+ }
+ #else
/* if the lock-on counter has not reached 0 then count it down */
if (thread_lock_c > 0) {
thread_lock_c--;
***************
*** 205,210 ****
--- 227,233 ----
pthread_mutex_unlock(&dmalloc_mutex);
#endif
}
+ #endif
}
#endif
***************
*** 770,776 ****
* Returns 0L on error.
*/
#undef malloc
! DMALLOC_PNT malloc(DMALLOC_SIZE size)
{
char *file;
--- 793,799 ----
* Returns 0L on error.
*/
#undef malloc
! DMALLOC_PNT __malloc__(DMALLOC_SIZE size)
{
char *file;
***************
*** 785,791 ****
* Returns 0L on error.
*/
#undef calloc
! DMALLOC_PNT calloc(DMALLOC_SIZE num_elements, DMALLOC_SIZE size)
{
DMALLOC_SIZE len = num_elements * size;
char *file;
--- 808,814 ----
* Returns 0L on error.
*/
#undef calloc
! DMALLOC_PNT __calloc__(DMALLOC_SIZE num_elements, DMALLOC_SIZE
size)
{
DMALLOC_SIZE len = num_elements * size;
char *file;
***************
*** 804,810 ****
* Returns 0L on error.
*/
#undef realloc
! DMALLOC_PNT realloc(DMALLOC_PNT old_pnt, DMALLOC_SIZE new_size)
{
char *file;
--- 827,833 ----
* Returns 0L on error.
*/
#undef realloc
! DMALLOC_PNT __realloc__(DMALLOC_PNT old_pnt, DMALLOC_SIZE
new_size)
{
char *file;
***************
*** 901,907 ****
* is defined by your compiler.
*/
#undef free
! DMALLOC_FREE_RET free(DMALLOC_PNT pnt)
{
char *file;
int ret;
--- 924,930 ----
* is defined by your compiler.
*/
#undef free
! DMALLOC_FREE_RET __free__(DMALLOC_PNT pnt)
{
char *file;
int ret;
***************
*** 932,937 ****
--- 955,994 ----
return ret;
#endif
}
+
+ #ifdef AIX
+ void __malloc_init__()
+ {
+ pthread_mutex_init(&dmalloc_mutex, THREAD_LOCK_INIT_VAL);
+ }
+
+ void __malloc_prefork_lock__()
+ {
+ pthread_mutex_lock(&dmalloc_mutex);
+ }
+
+ void __malloc_postfork_unlock__()
+ {
+ pthread_mutex_unlock(&dmalloc_mutex);
+ }
+
+
+ /* legacy function not needed */
+ int __mallopt__ (int x, int y)
+ {
+ }
+
+ /* legacy function not needed */
+ struct mallinfo __mallinfo__()
+ {
+ }
+
+ /* optional - not really needed */
+ void __malloc_once__()
+ {
+ }
+
+ #endif
/******************************* utility calls
*******************************/
create the file mem.exp with the following entries
__malloc__
__free__
__realloc__
__calloc__
__mallopt__
__mallinfo__
__malloc_init__
__malloc_prefork_lock__
__malloc_postfork_unlock__
__malloc_once__
Building a shared library for 32 and 64 bit use.
Makefile - 32 bit .
1. add -DAIX to DEFS
2. change LIB_TH . This just actually renames the module
built by the ld command.
LIB_TH = mem32.o
3. Add to CCFLAGS
CCFLAGS = -g -D_LARGE_THREADS
4. change the $(LIB_TH) build stanza
$(LIB_TH) : $(OBJS) $(THREAD_OBJS)
ld -b32 -m -o $@ $? -bE:mem.exp -bM:SRE -lpthreads -lc
Makefile - 64 bit
1. add -DAIX to DEFS
2. change LIB_TH . This just actually renames the module
built by the ld command.
LIB_TH = mem64.o
3. Add to CCFLAGS
CCFLAGS = -g -q64 -D_LARGE_THREADS
4. change the $(LIB_TH) build stanza
$(LIB_TH) : $(OBJS) $(THREAD_OBJS)
ld -b64 -m -o $@ $? -bE:mem.exp -bM:SRE -lpthreads -lc
5. If you care about making a library archive
Add -X64 to the ar command
$(LIBRARY) : $(OBJS) $(NORMAL_OBJS)
ar -X64 cr $@ $?
ranlib $@
Save mem32.o and mem64.o somewhere safe so that a make clean does not
clean them out.
The last step is to make an AIX shared lirary.
ar -X32_64 -r libmymem.a mem32.o mem64.o
On the AIX machine
export MALLOCTYPE=user:libmymem.a
export LIBPATH=<path to where libmymem.a lives>
Define your dmalloc env
Test the library out
Other notes:
In return.h , there is a macro called #define GET_RET_ADDR(file)
file = DMALLOC_DEFAULT_FILE
for AIX when the calling address is not know, it will return an
"unknown" in the log for
unfreed memory
For AIX you can do the following:
typedef struct _frame
{
struct _frame *fr_next; /* frame list */
int fr_unused1;
long fr_lr; /* link register */
}
_frame_t;
/* _frame_t *get_stkp(void); */
#pragma mc_func get_stkp \
{ \
"7c230b78" /* mr 3,1 */ \
}
#pragma reg_killed_by get_stkp gr3
#define CALLER (((_frame_t *)get_stkp())->fr_next->fr_lr)
#define CALLER3 (
\
( ((_frame_t *)get_stkp())->fr_next == NULL )
\
? DMALLOC_DEFAULT_FILE :
\
( ((_frame_t *)get_stkp())->fr_next->fr_next ==
NULL ) \
? DMALLOC_DEFAULT_FILE : ((_frame_t
*)get_stkp())->fr_next->fr_next->fr_next->fr_lr \
)
#define CALLER2 (
\
( ((_frame_t *)get_stkp())->fr_next == NULL )
\
? DMALLOC_DEFAULT_FILE :
\
( ((_frame_t *)get_stkp())->fr_next->fr_next ==
NULL ) \
? DMALLOC_DEFAULT_FILE : ((_frame_t
*)get_stkp())->fr_next->fr_next->fr_lr \
)
You will want to define CALLER2 because if you define CALLER1 you will
just get "malloc" as the return address. You really want to get the
caller that called malloc instead of __malloc__
#define GET_RET_ADDR(file) file = (char *) CALLER2
or
#define GET_RET_ADDR(file) file = (char *) CALLER1
Other notes (con't)
1. If you are debugging a system command, say telnet,
this command may do an exec and not copy over the
LIBPATH where the damlloc library resides.
This results in the following error:
USER DEFINED MALLOC ERROR:
Unable to load user supplied object: "libmymem.a(mem32.o)", load()
errno == 2
The solution is to place the library in /usr/lib
chmod 555
chown bin.bin
|