5 * DEBUG: section 45 Callback Data Registry
6 * ORIGINAL AUTHOR: Duane Wessels
7 * Modified by Moez Mahfoudh (08/12/2000)
8 * History added by Robert Collins (2002-10-25)
10 * SQUID Web Proxy Cache http://www.squid-cache.org/
11 * ----------------------------------------------------------
13 * Squid is the result of efforts by numerous individuals from
14 * the Internet community; see the CONTRIBUTORS file for full
15 * details. Many organizations have provided support for Squid's
16 * development; see the SPONSORS file for full details. Squid is
17 * Copyrighted (C) 2001 by the Regents of the University of
18 * California; see the COPYRIGHT file for full details. Squid
19 * incorporates software developed and/or copyrighted by other
20 * sources; see the CREDITS file for full details.
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
39 \defgroup CBDATAInternal Callback Data Allocator Internals
42 * These routines manage a set of registered callback data pointers.
43 * One of the easiest ways to make Squid coredump is to issue a
44 * callback to for some data structure which has previously been
45 * freed. With these routines, we register (add) callback data
46 * pointers, lock them just before registering the callback function,
47 * validate them before issuing the callback, and then free them
53 #include "mgr/Registration.h"
61 #define HASHED_CBDATA 1
64 static int cbdataCount
= 0;
66 dlink_list cbdataEntries
;
75 CBDataCall (char const *callLabel
, char const *aFile
, int aLine
) : label(callLabel
), file(aFile
), line(aLine
) {}
84 /// \ingroup CBDATAInternal
85 #define OFFSET_OF(TYPE, MEMBER) ((size_t) &(((TYPE) *)0)->(MEMBER))
87 /// \ingroup CBDATAInternal
90 /** \todo examine making cbdata templated on this - so we get type
91 * safe access to data - RBC 20030902 */
94 hash_link hash
; // Must be first
99 void dump(StoreEntry
*)const;
103 void *operator new(size_t size
, void *where
);
104 void operator delete(void *where
, void *where2
);
106 MEMPROXY_CLASS(cndata
);
115 void addHistory(char const *label
, char const *aFile
, int aLine
) {
116 if (calls
.size() > 1000)
119 calls
.push_back(new CBDataCall(label
, aFile
, aLine
));
125 Stack
<CBDataCall
*> calls
;
128 /* cookie used while debugging */
130 void check(int aLine
) const {assert(cookie
== ((long)this ^ Cookie
));}
131 static const long Cookie
;
134 size_t dataSize() const { return sizeof(data
);}
135 static long MakeOffset();
136 static const long Offset
;
137 /* MUST be the last per-instance member */
143 const long cbdata::Cookie((long)0xDEADBEEF);
145 const long cbdata::Offset(MakeOffset());
148 cbdata::operator new(size_t size
, void *where
)
150 // assert (size == sizeof(cbdata));
155 * Only ever invoked when placement new throws
156 * an exception. Used to prevent an incorrect
160 cbdata::operator delete(void *where
, void *where2
)
168 cbdata
*zero
= (cbdata
*)0L;
169 void **dataOffset
= &zero
->data
;
170 return (long)dataOffset
;
173 MEMPROXY_CLASS_INLINE(cbdata
);
176 static OBJH cbdataDump
;
178 static OBJH cbdataDumpHistory
;
181 /// \ingroup CBDATAInternal
186 *cbdata_index
= NULL
;
188 /// \ingroup CBDATAInternal
189 int cbdata_types
= 0;
192 static hash_table
*cbdata_htable
= NULL
;
195 cbdata_cmp(const void *p1
, const void *p2
)
197 return (char *) p1
- (char *) p2
;
201 cbdata_hash(const void *p
, unsigned int mod
)
203 return ((unsigned long) p
>> 8) % mod
;
213 while ((aCall
= calls
.pop()))
218 FREE
*free_func
= cbdata_index
[type
].free_func
;
231 cbdataInternalInitType(cbdata_type type
, const char *name
, int size
, FREE
* free_func
)
234 assert (type
== cbdata_types
+ 1);
236 cbdata_index
= (CBDataIndex
*)xrealloc(cbdata_index
, (type
+ 1) * sizeof(*cbdata_index
));
237 memset(&cbdata_index
[type
], 0, sizeof(*cbdata_index
));
240 label
= (char *)xmalloc(strlen(name
) + 20);
242 snprintf(label
, strlen(name
) + 20, "cbdata %s (%d)", name
, (int) type
);
245 assert((size_t)cbdata::Offset
== (sizeof(cbdata
) - ((cbdata
*)NULL
)->dataSize()));
246 size
+= cbdata::Offset
;
249 cbdata_index
[type
].pool
= memPoolCreate(label
, size
);
251 cbdata_index
[type
].free_func
= free_func
;
255 cbdata_htable
= hash_create(cbdata_cmp
, 1 << 12, cbdata_hash
);
260 cbdataInternalAddType(cbdata_type type
, const char *name
, int size
, FREE
* free_func
)
265 type
= (cbdata_type
)(cbdata_types
+ 1);
267 cbdataInternalInitType(type
, name
, size
, free_func
);
273 cbdataRegisterWithCacheManager(void)
275 Mgr::RegisterAction("cbdata",
276 "Callback Data Registry Contents",
280 Mgr::RegisterAction("cbdatahistory",
281 "Detailed call history for all current cbdata contents",
282 cbdataDumpHistory
, 0, 1);
288 cbdataInternalAllocDbg(cbdata_type type
, const char *file
, int line
)
290 cbdataInternalAlloc(cbdata_type type
)
295 assert(type
> 0 && type
<= cbdata_types
);
296 /* placement new: the pool alloc gives us cbdata + user type memory space
297 * and we init it with cbdata at the start of it
301 p
= cbdata_index
[type
].pool
->alloc();
303 hash_join(cbdata_htable
, &c
->hash
);
305 c
= new (cbdata_index
[type
].pool
->alloc()) cbdata
;
306 p
= (void *)&c
->data
;
312 c
->cookie
= (long) c
^ cbdata::Cookie
;
318 c
->calls
= Stack
<CBDataCall
*> ();
319 c
->addHistory("Alloc", file
, line
);
320 dlinkAdd(c
, &c
->link
, &cbdataEntries
);
321 debugs(45, 3, "cbdataAlloc: " << p
<< " " << file
<< ":" << line
);
329 cbdataInternalFreeDbg(void *p
, const char *file
, int line
)
331 cbdataInternalFree(void *p
)
336 c
= (cbdata
*) hash_lookup(cbdata_htable
, p
);
338 c
= (cbdata
*) (((char *) p
) - cbdata::Offset
);
342 debugs(45, 3, "cbdataFree: " << p
<< " " << file
<< ":" << line
);
345 debugs(45, 9, "cbdataFree: " << p
);
353 c
->addHistory("Free", file
, line
);
357 debugs(45, 9, "cbdataFree: " << p
<< " has " << c
->locks
<< " locks, not freeing");
362 debugs(45, 9, "cbdataFree: Freeing " << p
);
365 dlinkDelete(&c
->link
, &cbdataEntries
);
368 /* This is ugly. But: operator delete doesn't get
369 * the type parameter, so we can't use that
370 * to free the memory.
371 * So, we free it ourselves.
372 * Note that this means a non-placement
373 * new would be a seriously bad idea.
374 * Lastly, if we where a templated class,
375 * we could use the normal delete operator
376 * and it would Just Work. RBC 20030902
378 cbdata_type theType
= c
->type
;
380 hash_remove_link(cbdata_htable
, &c
->hash
);
382 cbdata_index
[theType
].pool
->freeOne((void *)p
);
384 c
->cbdata::~cbdata();
385 cbdata_index
[theType
].pool
->freeOne(c
);
392 cbdataInternalLockDbg(const void *p
, const char *file
, int line
)
394 cbdataInternalLock(const void *p
)
403 c
= (cbdata
*) hash_lookup(cbdata_htable
, p
);
405 c
= (cbdata
*) (((char *) p
) - cbdata::Offset
);
410 debugs(45, 3, "cbdataLock: " << p
<< "=" << (c
? c
->locks
+ 1 : -1) << " " << file
<< ":" << line
);
412 c
->addHistory("Reference", file
, line
);
416 debugs(45, 9, "cbdataLock: " << p
<< "=" << (c
? c
->locks
+ 1 : -1));
422 assert(c
->locks
< INT_MAX
);
429 cbdataInternalUnlockDbg(const void *p
, const char *file
, int line
)
431 cbdataInternalUnlock(const void *p
)
440 c
= (cbdata
*) hash_lookup(cbdata_htable
, p
);
442 c
= (cbdata
*) (((char *) p
) - cbdata::Offset
);
447 debugs(45, 3, "cbdataUnlock: " << p
<< "=" << (c
? c
->locks
- 1 : -1) << " " << file
<< ":" << line
);
449 c
->addHistory("Dereference", file
, line
);
453 debugs(45, 9, "cbdataUnlock: " << p
<< "=" << (c
? c
->locks
- 1 : -1));
461 assert(c
->locks
> 0);
465 if (c
->valid
|| c
->locks
)
470 debugs(45, 9, "cbdataUnlock: Freeing " << p
);
474 dlinkDelete(&c
->link
, &cbdataEntries
);
478 /* This is ugly. But: operator delete doesn't get
479 * the type parameter, so we can't use that
480 * to free the memory.
481 * So, we free it ourselves.
482 * Note that this means a non-placement
483 * new would be a seriously bad idea.
484 * Lastly, if we where a templated class,
485 * we could use the normal delete operator
486 * and it would Just Work. RBC 20030902
488 cbdata_type theType
= c
->type
;
490 hash_remove_link(cbdata_htable
, &c
->hash
);
492 cbdata_index
[theType
].pool
->freeOne((void *)p
);
494 c
->cbdata::~cbdata();
495 cbdata_index
[theType
].pool
->freeOne(c
);
500 cbdataReferenceValid(const void *p
)
505 return 1; /* A NULL pointer cannot become invalid */
507 debugs(45, 9, "cbdataReferenceValid: " << p
);
510 c
= (cbdata
*) hash_lookup(cbdata_htable
, p
);
512 c
= (cbdata
*) (((char *) p
) - cbdata::Offset
);
517 assert(c
->locks
> 0);
524 cbdataInternalReferenceDoneValidDbg(void **pp
, void **tp
, const char *file
, int line
)
526 cbdataInternalReferenceDoneValid(void **pp
, void **tp
)
529 void *p
= (void *) *pp
;
530 int valid
= cbdataReferenceValid(p
);
534 cbdataInternalUnlockDbg(p
, file
, line
);
537 cbdataInternalUnlock(p
);
551 cbdata::dump(StoreEntry
*sentry
) const
554 void *p
= (void *)hash
.key
;
556 void *p
= (void *)&data
;
558 storeAppendPrintf(sentry
, "%c%p\t%d\t%d\t%20s:%-5d\n", valid
? ' ' :
559 '!', p
, type
, locks
, file
, line
);
562 struct CBDataDumper
: public unary_function
<cbdata
, void> {
563 CBDataDumper(StoreEntry
*anEntry
):where(anEntry
) {}
565 void operator()(cbdata
const &x
) {
575 cbdataDump(StoreEntry
* sentry
)
577 storeAppendPrintf(sentry
, "%d cbdata entries\n", cbdataCount
);
580 storeAppendPrintf(sentry
, "Pointer\tType\tLocks\tAllocated by\n");
581 CBDataDumper
dumper(sentry
);
582 for_each (cbdataEntries
, dumper
);
583 storeAppendPrintf(sentry
, "\n");
584 storeAppendPrintf(sentry
, "types\tsize\tallocated\ttotal\n");
586 for (int i
= 1; i
< cbdata_types
; i
++) {
587 MemAllocator
*pool
= cbdata_index
[i
].pool
;
591 int obj_size
= pool
->objectSize();
593 int obj_size
= pool
->objectSize() - cbdata::Offset
;
595 storeAppendPrintf(sentry
, "%s\t%d\t%ld\t%ld\n", pool
->objectType() + 7, obj_size
, (long int)pool
->getMeter().inuse
.level
, (long int)obj_size
* pool
->getMeter().inuse
.level
);
600 storeAppendPrintf(sentry
, "detailed allocation information only available when compiled with --enable-debug-cbdata\n");
604 storeAppendPrintf(sentry
, "\nsee also \"Memory utilization\" for detailed per type statistics\n");
607 CBDATA_CLASS_INIT(generic_cbdata
);
611 struct CBDataCallDumper
: public unary_function
<CBDataCall
, void> {
612 CBDataCallDumper (StoreEntry
*anEntry
):where(anEntry
) {}
614 void operator()(CBDataCall
const &x
) {
615 storeAppendPrintf(where
, "%s\t%s\t%d\n", x
.label
, x
.file
, x
.line
);
621 struct CBDataHistoryDumper
: public CBDataDumper
{
622 CBDataHistoryDumper(StoreEntry
*anEntry
):CBDataDumper(anEntry
),where(anEntry
), callDumper(anEntry
) {}
624 void operator()(cbdata
const &x
) {
625 CBDataDumper::operator()(x
);
626 storeAppendPrintf(where
, "\n");
627 storeAppendPrintf(where
, "Action\tFile\tLine\n");
628 for_each (x
.calls
,callDumper
);
629 storeAppendPrintf(where
, "\n");
633 CBDataCallDumper callDumper
;
637 cbdataDumpHistory(StoreEntry
*sentry
)
639 storeAppendPrintf(sentry
, "%d cbdata entries\n", cbdataCount
);
640 storeAppendPrintf(sentry
, "Pointer\tType\tLocks\tAllocated by\n");
641 CBDataHistoryDumper
dumper(sentry
);
642 for_each (cbdataEntries
, dumper
);