Debian Codegraph @

contrib/aix_5l_notes

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