dbg_mlc.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. /*
  2. * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
  3. * Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved.
  4. * Copyright (c) 1997 by Silicon Graphics. All rights reserved.
  5. * Copyright (c) 1999-2004 Hewlett-Packard Development Company, L.P.
  6. * Copyright (C) 2007 Free Software Foundation, Inc
  7. *
  8. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  9. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  10. *
  11. * Permission is hereby granted to use or copy this program
  12. * for any purpose, provided the above notices are retained on all copies.
  13. * Permission to modify the code and to distribute modified code is granted,
  14. * provided the above notices are retained, and a notice that the code was
  15. * modified is included with the above copyright notice.
  16. */
  17. #include "private/dbg_mlc.h"
  18. #ifndef MSWINCE
  19. # include <errno.h>
  20. #endif
  21. #include <string.h>
  22. #ifndef SHORT_DBG_HDRS
  23. /* Check whether object with base pointer p has debugging info. */
  24. /* p is assumed to point to a legitimate object in our part */
  25. /* of the heap. */
  26. /* This excludes the check as to whether the back pointer is */
  27. /* odd, which is added by the GC_HAS_DEBUG_INFO macro. */
  28. /* Note that if DBG_HDRS_ALL is set, uncollectible objects */
  29. /* on free lists may not have debug information set. Thus it's */
  30. /* not always safe to return TRUE (1), even if the client does */
  31. /* its part. Return -1 if the object with debug info has been */
  32. /* marked as deallocated. */
  33. GC_INNER int GC_has_other_debug_info(ptr_t p)
  34. {
  35. ptr_t body = (ptr_t)((oh *)p + 1);
  36. word sz = GC_size(p);
  37. if (HBLKPTR(p) != HBLKPTR((ptr_t)body)
  38. || sz < DEBUG_BYTES + EXTRA_BYTES) {
  39. return 0;
  40. }
  41. if (((oh *)p) -> oh_sf != (START_FLAG ^ (word)body)
  42. && ((word *)p)[BYTES_TO_WORDS(sz)-1] != (END_FLAG ^ (word)body)) {
  43. return 0;
  44. }
  45. if (((oh *)p)->oh_sz == sz) {
  46. /* Object may have had debug info, but has been deallocated */
  47. return -1;
  48. }
  49. return 1;
  50. }
  51. #endif /* !SHORT_DBG_HDRS */
  52. #ifdef LINT2
  53. long GC_random(void)
  54. {
  55. static unsigned seed = 1; /* not thread-safe */
  56. /* Linear congruential pseudo-random numbers generator. */
  57. seed = (seed * 1103515245U + 12345) & GC_RAND_MAX; /* overflow is ok */
  58. return (long)seed;
  59. }
  60. #endif
  61. #ifdef KEEP_BACK_PTRS
  62. #ifdef LINT2
  63. # define RANDOM() GC_random()
  64. #else
  65. # include <stdlib.h>
  66. # define GC_RAND_MAX RAND_MAX
  67. # if defined(__GLIBC__) || defined(SOLARIS) \
  68. || defined(HPUX) || defined(IRIX5) || defined(OSF1)
  69. # define RANDOM() random()
  70. # else
  71. # define RANDOM() (long)rand()
  72. # endif
  73. #endif /* !LINT2 */
  74. /* Store back pointer to source in dest, if that appears to be possible. */
  75. /* This is not completely safe, since we may mistakenly conclude that */
  76. /* dest has a debugging wrapper. But the error probability is very */
  77. /* small, and this shouldn't be used in production code. */
  78. /* We assume that dest is the real base pointer. Source will usually */
  79. /* be a pointer to the interior of an object. */
  80. GC_INNER void GC_store_back_pointer(ptr_t source, ptr_t dest)
  81. {
  82. if (GC_HAS_DEBUG_INFO(dest)) {
  83. # ifdef PARALLEL_MARK
  84. AO_store((volatile AO_t *)&((oh *)dest)->oh_back_ptr,
  85. (AO_t)HIDE_BACK_PTR(source));
  86. # else
  87. ((oh *)dest) -> oh_back_ptr = HIDE_BACK_PTR(source);
  88. # endif
  89. }
  90. }
  91. GC_INNER void GC_marked_for_finalization(ptr_t dest)
  92. {
  93. GC_store_back_pointer(MARKED_FOR_FINALIZATION, dest);
  94. }
  95. /* Store information about the object referencing dest in *base_p */
  96. /* and *offset_p. */
  97. /* source is root ==> *base_p = address, *offset_p = 0 */
  98. /* source is heap object ==> *base_p != 0, *offset_p = offset */
  99. /* Returns 1 on success, 0 if source couldn't be determined. */
  100. /* Dest can be any address within a heap object. */
  101. GC_API GC_ref_kind GC_CALL GC_get_back_ptr_info(void *dest, void **base_p,
  102. size_t *offset_p)
  103. {
  104. oh * hdr = (oh *)GC_base(dest);
  105. ptr_t bp;
  106. ptr_t bp_base;
  107. # ifdef LINT2
  108. /* Explicitly instruct the code analysis tool that */
  109. /* GC_get_back_ptr_info is not expected to be called with an */
  110. /* incorrect "dest" value. */
  111. if (!hdr) ABORT("Invalid GC_get_back_ptr_info argument");
  112. # endif
  113. if (!GC_HAS_DEBUG_INFO((ptr_t) hdr)) return GC_NO_SPACE;
  114. bp = (ptr_t)GC_REVEAL_POINTER(hdr -> oh_back_ptr);
  115. if (MARKED_FOR_FINALIZATION == bp) return GC_FINALIZER_REFD;
  116. if (MARKED_FROM_REGISTER == bp) return GC_REFD_FROM_REG;
  117. if (NOT_MARKED == bp) return GC_UNREFERENCED;
  118. # if ALIGNMENT == 1
  119. /* Heuristically try to fix off by 1 errors we introduced by */
  120. /* insisting on even addresses. */
  121. {
  122. ptr_t alternate_ptr = bp + 1;
  123. ptr_t target = *(ptr_t *)bp;
  124. ptr_t alternate_target = *(ptr_t *)alternate_ptr;
  125. if ((word)alternate_target >= (word)GC_least_plausible_heap_addr
  126. && (word)alternate_target <= (word)GC_greatest_plausible_heap_addr
  127. && ((word)target < (word)GC_least_plausible_heap_addr
  128. || (word)target > (word)GC_greatest_plausible_heap_addr)) {
  129. bp = alternate_ptr;
  130. }
  131. }
  132. # endif
  133. bp_base = (ptr_t)GC_base(bp);
  134. if (NULL == bp_base) {
  135. *base_p = bp;
  136. *offset_p = 0;
  137. return GC_REFD_FROM_ROOT;
  138. } else {
  139. if (GC_HAS_DEBUG_INFO(bp_base)) bp_base += sizeof(oh);
  140. *base_p = bp_base;
  141. *offset_p = bp - bp_base;
  142. return GC_REFD_FROM_HEAP;
  143. }
  144. }
  145. /* Generate a random heap address. */
  146. /* The resulting address is in the heap, but */
  147. /* not necessarily inside a valid object. */
  148. GC_API void * GC_CALL GC_generate_random_heap_address(void)
  149. {
  150. size_t i;
  151. word heap_offset = RANDOM();
  152. if (GC_heapsize > GC_RAND_MAX) {
  153. heap_offset *= GC_RAND_MAX;
  154. heap_offset += RANDOM();
  155. }
  156. heap_offset %= GC_heapsize;
  157. /* This doesn't yield a uniform distribution, especially if */
  158. /* e.g. RAND_MAX = 1.5* GC_heapsize. But for typical cases, */
  159. /* it's not too bad. */
  160. for (i = 0;; ++i) {
  161. size_t size;
  162. if (i >= GC_n_heap_sects)
  163. ABORT("GC_generate_random_heap_address: size inconsistency");
  164. size = GC_heap_sects[i].hs_bytes;
  165. if (heap_offset < size) {
  166. break;
  167. } else {
  168. heap_offset -= size;
  169. }
  170. }
  171. return GC_heap_sects[i].hs_start + heap_offset;
  172. }
  173. /* Generate a random address inside a valid marked heap object. */
  174. GC_API void * GC_CALL GC_generate_random_valid_address(void)
  175. {
  176. ptr_t result;
  177. ptr_t base;
  178. do {
  179. result = (ptr_t)GC_generate_random_heap_address();
  180. base = (ptr_t)GC_base(result);
  181. } while (NULL == base || !GC_is_marked(base));
  182. return result;
  183. }
  184. /* Print back trace for p */
  185. GC_API void GC_CALL GC_print_backtrace(void *p)
  186. {
  187. void *current = p;
  188. int i;
  189. GC_ref_kind source;
  190. size_t offset;
  191. void *base;
  192. GC_print_heap_obj((ptr_t)GC_base(current));
  193. for (i = 0; ; ++i) {
  194. source = GC_get_back_ptr_info(current, &base, &offset);
  195. if (GC_UNREFERENCED == source) {
  196. GC_err_printf("Reference could not be found\n");
  197. goto out;
  198. }
  199. if (GC_NO_SPACE == source) {
  200. GC_err_printf("No debug info in object: Can't find reference\n");
  201. goto out;
  202. }
  203. GC_err_printf("Reachable via %d levels of pointers from ", i);
  204. switch(source) {
  205. case GC_REFD_FROM_ROOT:
  206. GC_err_printf("root at %p\n\n", base);
  207. goto out;
  208. case GC_REFD_FROM_REG:
  209. GC_err_printf("root in register\n\n");
  210. goto out;
  211. case GC_FINALIZER_REFD:
  212. GC_err_printf("list of finalizable objects\n\n");
  213. goto out;
  214. case GC_REFD_FROM_HEAP:
  215. GC_err_printf("offset %ld in object:\n", (long)offset);
  216. /* Take GC_base(base) to get real base, i.e. header. */
  217. GC_print_heap_obj((ptr_t)GC_base(base));
  218. break;
  219. default:
  220. GC_err_printf("INTERNAL ERROR: UNEXPECTED SOURCE!!!!\n");
  221. goto out;
  222. }
  223. current = base;
  224. }
  225. out:;
  226. }
  227. /* Force a garbage collection and generate/print a backtrace */
  228. /* from a random heap address. */
  229. GC_INNER void GC_generate_random_backtrace_no_gc(void)
  230. {
  231. void * current;
  232. current = GC_generate_random_valid_address();
  233. GC_printf("\n****Chosen address %p in object\n", current);
  234. GC_print_backtrace(current);
  235. }
  236. GC_API void GC_CALL GC_generate_random_backtrace(void)
  237. {
  238. if (GC_try_to_collect(GC_never_stop_func) == 0) {
  239. GC_err_printf("Cannot generate a backtrace: "
  240. "garbage collection is disabled!\n");
  241. return;
  242. }
  243. GC_generate_random_backtrace_no_gc();
  244. }
  245. #endif /* KEEP_BACK_PTRS */
  246. # define CROSSES_HBLK(p, sz) \
  247. (((word)((p) + sizeof(oh) + (sz) - 1) ^ (word)(p)) >= HBLKSIZE)
  248. GC_INNER void *GC_store_debug_info_inner(void *p, word sz GC_ATTR_UNUSED,
  249. const char *string, int linenum)
  250. {
  251. word * result = (word *)((oh *)p + 1);
  252. GC_ASSERT(I_HOLD_LOCK());
  253. GC_ASSERT(GC_size(p) >= sizeof(oh) + sz);
  254. GC_ASSERT(!(SMALL_OBJ(sz) && CROSSES_HBLK((ptr_t)p, sz)));
  255. # ifdef KEEP_BACK_PTRS
  256. ((oh *)p) -> oh_back_ptr = HIDE_BACK_PTR(NOT_MARKED);
  257. # endif
  258. # ifdef MAKE_BACK_GRAPH
  259. ((oh *)p) -> oh_bg_ptr = HIDE_BACK_PTR((ptr_t)0);
  260. # endif
  261. ((oh *)p) -> oh_string = string;
  262. ((oh *)p) -> oh_int = (word)linenum;
  263. # ifndef SHORT_DBG_HDRS
  264. ((oh *)p) -> oh_sz = sz;
  265. ((oh *)p) -> oh_sf = START_FLAG ^ (word)result;
  266. ((word *)p)[BYTES_TO_WORDS(GC_size(p))-1] =
  267. result[SIMPLE_ROUNDED_UP_WORDS(sz)] = END_FLAG ^ (word)result;
  268. # endif
  269. return result;
  270. }
  271. /* Check the allocation is successful, store debugging info into p, */
  272. /* start the debugging mode (if not yet), and return displaced pointer. */
  273. static void *store_debug_info(void *p, size_t lb,
  274. const char *fn, GC_EXTRA_PARAMS)
  275. {
  276. void *result;
  277. DCL_LOCK_STATE;
  278. if (NULL == p) {
  279. GC_err_printf("%s(%lu) returning NULL (%s:%d)\n",
  280. fn, (unsigned long)lb, s, i);
  281. return NULL;
  282. }
  283. LOCK();
  284. if (!GC_debugging_started)
  285. GC_start_debugging_inner();
  286. ADD_CALL_CHAIN(p, ra);
  287. result = GC_store_debug_info_inner(p, (word)lb, s, i);
  288. UNLOCK();
  289. return result;
  290. }
  291. #ifndef SHORT_DBG_HDRS
  292. /* Check the object with debugging info at ohdr. */
  293. /* Return NULL if it's OK. Else return clobbered */
  294. /* address. */
  295. STATIC ptr_t GC_check_annotated_obj(oh *ohdr)
  296. {
  297. ptr_t body = (ptr_t)(ohdr + 1);
  298. word gc_sz = GC_size((ptr_t)ohdr);
  299. if (ohdr -> oh_sz + DEBUG_BYTES > gc_sz) {
  300. return((ptr_t)(&(ohdr -> oh_sz)));
  301. }
  302. if (ohdr -> oh_sf != (START_FLAG ^ (word)body)) {
  303. return((ptr_t)(&(ohdr -> oh_sf)));
  304. }
  305. if (((word *)ohdr)[BYTES_TO_WORDS(gc_sz)-1] != (END_FLAG ^ (word)body)) {
  306. return((ptr_t)((word *)ohdr + BYTES_TO_WORDS(gc_sz)-1));
  307. }
  308. if (((word *)body)[SIMPLE_ROUNDED_UP_WORDS(ohdr -> oh_sz)]
  309. != (END_FLAG ^ (word)body)) {
  310. return((ptr_t)((word *)body + SIMPLE_ROUNDED_UP_WORDS(ohdr->oh_sz)));
  311. }
  312. return(0);
  313. }
  314. #endif /* !SHORT_DBG_HDRS */
  315. STATIC GC_describe_type_fn GC_describe_type_fns[MAXOBJKINDS] = {0};
  316. GC_API void GC_CALL GC_register_describe_type_fn(int kind,
  317. GC_describe_type_fn fn)
  318. {
  319. GC_describe_type_fns[kind] = fn;
  320. }
  321. #define GET_OH_LINENUM(ohdr) ((int)(ohdr)->oh_int)
  322. #ifndef SHORT_DBG_HDRS
  323. # define IF_NOT_SHORTDBG_HDRS(x) x
  324. # define COMMA_IFNOT_SHORTDBG_HDRS(x) /* comma */, x
  325. #else
  326. # define IF_NOT_SHORTDBG_HDRS(x) /* empty */
  327. # define COMMA_IFNOT_SHORTDBG_HDRS(x) /* empty */
  328. #endif
  329. /* Print a human-readable description of the object to stderr. */
  330. /* p points to somewhere inside an object with the debugging info. */
  331. STATIC void GC_print_obj(ptr_t p)
  332. {
  333. oh * ohdr = (oh *)GC_base(p);
  334. ptr_t q;
  335. hdr * hhdr;
  336. int kind;
  337. const char *kind_str;
  338. char buffer[GC_TYPE_DESCR_LEN + 1];
  339. GC_ASSERT(I_DONT_HOLD_LOCK());
  340. # ifdef LINT2
  341. if (!ohdr) ABORT("Invalid GC_print_obj argument");
  342. # endif
  343. q = (ptr_t)(ohdr + 1);
  344. /* Print a type description for the object whose client-visible */
  345. /* address is q. */
  346. hhdr = GC_find_header(q);
  347. kind = hhdr -> hb_obj_kind;
  348. if (0 != GC_describe_type_fns[kind] && GC_is_marked(ohdr)) {
  349. /* This should preclude free list objects except with */
  350. /* thread-local allocation. */
  351. buffer[GC_TYPE_DESCR_LEN] = 0;
  352. (GC_describe_type_fns[kind])(q, buffer);
  353. GC_ASSERT(buffer[GC_TYPE_DESCR_LEN] == 0);
  354. kind_str = buffer;
  355. } else {
  356. switch(kind) {
  357. case PTRFREE:
  358. kind_str = "PTRFREE";
  359. break;
  360. case NORMAL:
  361. kind_str = "NORMAL";
  362. break;
  363. case UNCOLLECTABLE:
  364. kind_str = "UNCOLLECTABLE";
  365. break;
  366. # ifdef GC_ATOMIC_UNCOLLECTABLE
  367. case AUNCOLLECTABLE:
  368. kind_str = "ATOMIC_UNCOLLECTABLE";
  369. break;
  370. # endif
  371. default:
  372. kind_str = NULL;
  373. /* The alternative is to use snprintf(buffer) but it is */
  374. /* not quite portable (see vsnprintf in misc.c). */
  375. }
  376. }
  377. if (NULL != kind_str) {
  378. GC_err_printf("%p (%s:%d," IF_NOT_SHORTDBG_HDRS(" sz=%lu,") " %s)\n",
  379. (void *)((ptr_t)ohdr + sizeof(oh)),
  380. ohdr->oh_string, GET_OH_LINENUM(ohdr) /*, */
  381. COMMA_IFNOT_SHORTDBG_HDRS((unsigned long)ohdr->oh_sz),
  382. kind_str);
  383. } else {
  384. GC_err_printf("%p (%s:%d," IF_NOT_SHORTDBG_HDRS(" sz=%lu,")
  385. " kind=%d descr=0x%lx)\n",
  386. (void *)((ptr_t)ohdr + sizeof(oh)),
  387. ohdr->oh_string, GET_OH_LINENUM(ohdr) /*, */
  388. COMMA_IFNOT_SHORTDBG_HDRS((unsigned long)ohdr->oh_sz),
  389. kind, (unsigned long)hhdr->hb_descr);
  390. }
  391. PRINT_CALL_CHAIN(ohdr);
  392. }
  393. STATIC void GC_debug_print_heap_obj_proc(ptr_t p)
  394. {
  395. GC_ASSERT(I_DONT_HOLD_LOCK());
  396. if (GC_HAS_DEBUG_INFO(p)) {
  397. GC_print_obj(p);
  398. } else {
  399. GC_default_print_heap_obj_proc(p);
  400. }
  401. }
  402. #ifndef SHORT_DBG_HDRS
  403. /* Use GC_err_printf and friends to print a description of the object */
  404. /* whose client-visible address is p, and which was smashed at */
  405. /* clobbered_addr. */
  406. STATIC void GC_print_smashed_obj(const char *msg, void *p,
  407. ptr_t clobbered_addr)
  408. {
  409. oh * ohdr = (oh *)GC_base(p);
  410. GC_ASSERT(I_DONT_HOLD_LOCK());
  411. # ifdef LINT2
  412. if (!ohdr) ABORT("Invalid GC_print_smashed_obj argument");
  413. # endif
  414. if ((word)clobbered_addr <= (word)(&ohdr->oh_sz)
  415. || ohdr -> oh_string == 0) {
  416. GC_err_printf(
  417. "%s %p in or near object at %p(<smashed>, appr. sz = %lu)\n",
  418. msg, (void *)clobbered_addr, p,
  419. (unsigned long)(GC_size((ptr_t)ohdr) - DEBUG_BYTES));
  420. } else {
  421. GC_err_printf("%s %p in or near object at %p (%s:%d, sz=%lu)\n",
  422. msg, (void *)clobbered_addr, p,
  423. (word)(ohdr -> oh_string) < HBLKSIZE ? "(smashed string)" :
  424. ohdr -> oh_string[0] == '\0' ? "EMPTY(smashed?)" :
  425. ohdr -> oh_string,
  426. GET_OH_LINENUM(ohdr), (unsigned long)(ohdr -> oh_sz));
  427. PRINT_CALL_CHAIN(ohdr);
  428. }
  429. }
  430. STATIC void GC_check_heap_proc (void);
  431. STATIC void GC_print_all_smashed_proc (void);
  432. #else
  433. STATIC void GC_do_nothing(void) {}
  434. #endif
  435. GC_INNER void GC_start_debugging_inner(void)
  436. {
  437. GC_ASSERT(I_HOLD_LOCK());
  438. # ifndef SHORT_DBG_HDRS
  439. GC_check_heap = GC_check_heap_proc;
  440. GC_print_all_smashed = GC_print_all_smashed_proc;
  441. # else
  442. GC_check_heap = GC_do_nothing;
  443. GC_print_all_smashed = GC_do_nothing;
  444. # endif
  445. GC_print_heap_obj = GC_debug_print_heap_obj_proc;
  446. GC_debugging_started = TRUE;
  447. GC_register_displacement_inner((word)sizeof(oh));
  448. }
  449. size_t GC_debug_header_size = sizeof(oh);
  450. GC_API void GC_CALL GC_debug_register_displacement(size_t offset)
  451. {
  452. DCL_LOCK_STATE;
  453. LOCK();
  454. GC_register_displacement_inner(offset);
  455. GC_register_displacement_inner((word)sizeof(oh) + offset);
  456. UNLOCK();
  457. }
  458. #ifdef GC_ADD_CALLER
  459. # if defined(HAVE_DLADDR) && defined(GC_HAVE_RETURN_ADDR_PARENT)
  460. # include <dlfcn.h>
  461. STATIC void GC_caller_func_offset(word ad, const char **symp, int *offp)
  462. {
  463. Dl_info caller;
  464. if (ad && dladdr((void *)ad, &caller) && caller.dli_sname != NULL) {
  465. *symp = caller.dli_sname;
  466. *offp = (int)((char *)ad - (char *)caller.dli_saddr);
  467. }
  468. if (NULL == *symp) {
  469. *symp = "unknown";
  470. }
  471. }
  472. # else
  473. # define GC_caller_func_offset(ad, symp, offp) (void)(*(symp) = "unknown")
  474. # endif
  475. #endif /* GC_ADD_CALLER */
  476. GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc(size_t lb,
  477. GC_EXTRA_PARAMS)
  478. {
  479. void * result;
  480. /* Note that according to malloc() specification, if size is 0 then */
  481. /* malloc() returns either NULL, or a unique pointer value that can */
  482. /* later be successfully passed to free(). We always do the latter. */
  483. result = GC_malloc(SIZET_SAT_ADD(lb, DEBUG_BYTES));
  484. # ifdef GC_ADD_CALLER
  485. if (s == NULL) {
  486. GC_caller_func_offset(ra, &s, &i);
  487. }
  488. # endif
  489. return store_debug_info(result, lb, "GC_debug_malloc", OPT_RA s, i);
  490. }
  491. GC_API GC_ATTR_MALLOC void * GC_CALL
  492. GC_debug_malloc_ignore_off_page(size_t lb, GC_EXTRA_PARAMS)
  493. {
  494. void * result = GC_malloc_ignore_off_page(SIZET_SAT_ADD(lb, DEBUG_BYTES));
  495. return store_debug_info(result, lb, "GC_debug_malloc_ignore_off_page",
  496. OPT_RA s, i);
  497. }
  498. GC_API GC_ATTR_MALLOC void * GC_CALL
  499. GC_debug_malloc_atomic_ignore_off_page(size_t lb, GC_EXTRA_PARAMS)
  500. {
  501. void * result = GC_malloc_atomic_ignore_off_page(
  502. SIZET_SAT_ADD(lb, DEBUG_BYTES));
  503. return store_debug_info(result, lb,
  504. "GC_debug_malloc_atomic_ignore_off_page",
  505. OPT_RA s, i);
  506. }
  507. STATIC void * GC_debug_generic_malloc(size_t lb, int knd, GC_EXTRA_PARAMS)
  508. {
  509. void * result = GC_generic_malloc(SIZET_SAT_ADD(lb, DEBUG_BYTES), knd);
  510. return store_debug_info(result, lb, "GC_debug_generic_malloc",
  511. OPT_RA s, i);
  512. }
  513. #ifdef DBG_HDRS_ALL
  514. /* An allocation function for internal use. Normally internally */
  515. /* allocated objects do not have debug information. But in this */
  516. /* case, we need to make sure that all objects have debug headers. */
  517. GC_INNER void * GC_debug_generic_malloc_inner(size_t lb, int k)
  518. {
  519. void * result;
  520. GC_ASSERT(I_HOLD_LOCK());
  521. result = GC_generic_malloc_inner(SIZET_SAT_ADD(lb, DEBUG_BYTES), k);
  522. if (NULL == result) {
  523. GC_err_printf("GC internal allocation (%lu bytes) returning NULL\n",
  524. (unsigned long) lb);
  525. return(0);
  526. }
  527. if (!GC_debugging_started) {
  528. GC_start_debugging_inner();
  529. }
  530. ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
  531. return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", 0));
  532. }
  533. GC_INNER void * GC_debug_generic_malloc_inner_ignore_off_page(size_t lb,
  534. int k)
  535. {
  536. void * result;
  537. GC_ASSERT(I_HOLD_LOCK());
  538. result = GC_generic_malloc_inner_ignore_off_page(
  539. SIZET_SAT_ADD(lb, DEBUG_BYTES), k);
  540. if (NULL == result) {
  541. GC_err_printf("GC internal allocation (%lu bytes) returning NULL\n",
  542. (unsigned long) lb);
  543. return(0);
  544. }
  545. if (!GC_debugging_started) {
  546. GC_start_debugging_inner();
  547. }
  548. ADD_CALL_CHAIN(result, GC_RETURN_ADDR);
  549. return (GC_store_debug_info_inner(result, (word)lb, "INTERNAL", 0));
  550. }
  551. #endif /* DBG_HDRS_ALL */
  552. GC_API void * GC_CALL GC_debug_malloc_stubborn(size_t lb, GC_EXTRA_PARAMS)
  553. {
  554. return GC_debug_malloc(lb, OPT_RA s, i);
  555. }
  556. GC_API void GC_CALL GC_debug_change_stubborn(
  557. const void * p GC_ATTR_UNUSED) {}
  558. GC_API void GC_CALL GC_debug_end_stubborn_change(const void *p)
  559. {
  560. const void * q = GC_base_C(p);
  561. if (NULL == q) {
  562. ABORT_ARG1("GC_debug_end_stubborn_change: bad arg", ": %p", p);
  563. }
  564. GC_end_stubborn_change(q);
  565. }
  566. GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_atomic(size_t lb,
  567. GC_EXTRA_PARAMS)
  568. {
  569. void * result = GC_malloc_atomic(SIZET_SAT_ADD(lb, DEBUG_BYTES));
  570. return store_debug_info(result, lb, "GC_debug_malloc_atomic",
  571. OPT_RA s, i);
  572. }
  573. GC_API GC_ATTR_MALLOC char * GC_CALL GC_debug_strdup(const char *str,
  574. GC_EXTRA_PARAMS)
  575. {
  576. char *copy;
  577. size_t lb;
  578. if (str == NULL) {
  579. if (GC_find_leak)
  580. GC_err_printf("strdup(NULL) behavior is undefined\n");
  581. return NULL;
  582. }
  583. lb = strlen(str) + 1;
  584. copy = (char *)GC_debug_malloc_atomic(lb, OPT_RA s, i);
  585. if (copy == NULL) {
  586. # ifndef MSWINCE
  587. errno = ENOMEM;
  588. # endif
  589. return NULL;
  590. }
  591. BCOPY(str, copy, lb);
  592. return copy;
  593. }
  594. GC_API GC_ATTR_MALLOC char * GC_CALL GC_debug_strndup(const char *str,
  595. size_t size, GC_EXTRA_PARAMS)
  596. {
  597. char *copy;
  598. size_t len = strlen(str); /* str is expected to be non-NULL */
  599. if (len > size)
  600. len = size;
  601. copy = (char *)GC_debug_malloc_atomic(len + 1, OPT_RA s, i);
  602. if (copy == NULL) {
  603. # ifndef MSWINCE
  604. errno = ENOMEM;
  605. # endif
  606. return NULL;
  607. }
  608. if (len > 0)
  609. BCOPY(str, copy, len);
  610. copy[len] = '\0';
  611. return copy;
  612. }
  613. #ifdef GC_REQUIRE_WCSDUP
  614. # include <wchar.h> /* for wcslen() */
  615. GC_API GC_ATTR_MALLOC wchar_t * GC_CALL GC_debug_wcsdup(const wchar_t *str,
  616. GC_EXTRA_PARAMS)
  617. {
  618. size_t lb = (wcslen(str) + 1) * sizeof(wchar_t);
  619. wchar_t *copy = (wchar_t *)GC_debug_malloc_atomic(lb, OPT_RA s, i);
  620. if (copy == NULL) {
  621. # ifndef MSWINCE
  622. errno = ENOMEM;
  623. # endif
  624. return NULL;
  625. }
  626. BCOPY(str, copy, lb);
  627. return copy;
  628. }
  629. #endif /* GC_REQUIRE_WCSDUP */
  630. GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_uncollectable(size_t lb,
  631. GC_EXTRA_PARAMS)
  632. {
  633. void * result = GC_malloc_uncollectable(
  634. SIZET_SAT_ADD(lb, UNCOLLECTABLE_DEBUG_BYTES));
  635. return store_debug_info(result, lb, "GC_debug_malloc_uncollectable",
  636. OPT_RA s, i);
  637. }
  638. #ifdef GC_ATOMIC_UNCOLLECTABLE
  639. GC_API GC_ATTR_MALLOC void * GC_CALL
  640. GC_debug_malloc_atomic_uncollectable(size_t lb, GC_EXTRA_PARAMS)
  641. {
  642. void * result = GC_malloc_atomic_uncollectable(
  643. SIZET_SAT_ADD(lb, UNCOLLECTABLE_DEBUG_BYTES));
  644. return store_debug_info(result, lb,
  645. "GC_debug_malloc_atomic_uncollectable",
  646. OPT_RA s, i);
  647. }
  648. #endif /* GC_ATOMIC_UNCOLLECTABLE */
  649. #ifndef GC_FREED_MEM_MARKER
  650. # if CPP_WORDSZ == 32
  651. # define GC_FREED_MEM_MARKER 0xdeadbeef
  652. # else
  653. # define GC_FREED_MEM_MARKER GC_WORD_C(0xEFBEADDEdeadbeef)
  654. # endif
  655. #endif
  656. GC_API void GC_CALL GC_debug_free(void * p)
  657. {
  658. ptr_t base;
  659. if (0 == p) return;
  660. base = (ptr_t)GC_base(p);
  661. if (NULL == base) {
  662. # if defined(REDIRECT_MALLOC) \
  663. && ((defined(NEED_CALLINFO) && defined(GC_HAVE_BUILTIN_BACKTRACE)) \
  664. || defined(GC_LINUX_THREADS) || defined(GC_SOLARIS_THREADS) \
  665. || defined(MSWIN32))
  666. /* In some cases, we should ignore objects that do not belong */
  667. /* to the GC heap. See the comment in GC_free. */
  668. if (!GC_is_heap_ptr(p)) return;
  669. # endif
  670. ABORT_ARG1("Invalid pointer passed to free()", ": %p", p);
  671. }
  672. if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
  673. # if defined(REDIRECT_FREE) && defined(USE_PROC_FOR_LIBRARIES)
  674. /* TODO: Suppress the warning if free() caller is in libpthread */
  675. /* or libdl. */
  676. # endif
  677. GC_err_printf(
  678. "GC_debug_free called on pointer %p w/o debugging info\n", p);
  679. } else {
  680. # ifndef SHORT_DBG_HDRS
  681. ptr_t clobbered = GC_check_annotated_obj((oh *)base);
  682. word sz = GC_size(base);
  683. if (clobbered != 0) {
  684. GC_have_errors = TRUE;
  685. if (((oh *)base) -> oh_sz == sz) {
  686. GC_print_smashed_obj(
  687. "GC_debug_free: found previously deallocated (?) object at",
  688. p, clobbered);
  689. return; /* ignore double free */
  690. } else {
  691. GC_print_smashed_obj("GC_debug_free: found smashed location at",
  692. p, clobbered);
  693. }
  694. }
  695. /* Invalidate size (mark the object as deallocated) */
  696. ((oh *)base) -> oh_sz = sz;
  697. # endif /* SHORT_DBG_HDRS */
  698. }
  699. if (GC_find_leak
  700. # ifndef SHORT_DBG_HDRS
  701. && ((ptr_t)p - (ptr_t)base != sizeof(oh) || !GC_findleak_delay_free)
  702. # endif
  703. ) {
  704. GC_free(base);
  705. } else {
  706. hdr * hhdr = HDR(p);
  707. if (hhdr -> hb_obj_kind == UNCOLLECTABLE
  708. # ifdef GC_ATOMIC_UNCOLLECTABLE
  709. || hhdr -> hb_obj_kind == AUNCOLLECTABLE
  710. # endif
  711. ) {
  712. GC_free(base);
  713. } else {
  714. word i;
  715. word obj_sz = BYTES_TO_WORDS(hhdr->hb_sz - sizeof(oh));
  716. for (i = 0; i < obj_sz; ++i)
  717. ((word *)p)[i] = GC_FREED_MEM_MARKER;
  718. GC_ASSERT((word *)p + i == (word *)(base + hhdr -> hb_sz));
  719. }
  720. } /* !GC_find_leak */
  721. }
  722. #if defined(THREADS) && defined(DBG_HDRS_ALL)
  723. /* Used internally; we assume it's called correctly. */
  724. GC_INNER void GC_debug_free_inner(void * p)
  725. {
  726. ptr_t base = (ptr_t)GC_base(p);
  727. GC_ASSERT((ptr_t)p - (ptr_t)base == sizeof(oh));
  728. # ifdef LINT2
  729. if (!base) ABORT("Invalid GC_debug_free_inner argument");
  730. # endif
  731. # ifndef SHORT_DBG_HDRS
  732. /* Invalidate size */
  733. ((oh *)base) -> oh_sz = GC_size(base);
  734. # endif
  735. GC_free_inner(base);
  736. }
  737. #endif
  738. GC_API void * GC_CALL GC_debug_realloc(void * p, size_t lb, GC_EXTRA_PARAMS)
  739. {
  740. void * base;
  741. void * result;
  742. hdr * hhdr;
  743. if (p == 0) {
  744. return GC_debug_malloc(lb, OPT_RA s, i);
  745. }
  746. if (0 == lb) /* and p != NULL */ {
  747. GC_debug_free(p);
  748. return NULL;
  749. }
  750. # ifdef GC_ADD_CALLER
  751. if (s == NULL) {
  752. GC_caller_func_offset(ra, &s, &i);
  753. }
  754. # endif
  755. base = GC_base(p);
  756. if (base == 0) {
  757. ABORT_ARG1("Invalid pointer passed to realloc()", ": %p", p);
  758. }
  759. if ((ptr_t)p - (ptr_t)base != sizeof(oh)) {
  760. GC_err_printf(
  761. "GC_debug_realloc called on pointer %p w/o debugging info\n", p);
  762. return(GC_realloc(p, lb));
  763. }
  764. hhdr = HDR(base);
  765. switch (hhdr -> hb_obj_kind) {
  766. case NORMAL:
  767. result = GC_debug_malloc(lb, OPT_RA s, i);
  768. break;
  769. case PTRFREE:
  770. result = GC_debug_malloc_atomic(lb, OPT_RA s, i);
  771. break;
  772. case UNCOLLECTABLE:
  773. result = GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
  774. break;
  775. # ifdef GC_ATOMIC_UNCOLLECTABLE
  776. case AUNCOLLECTABLE:
  777. result = GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
  778. break;
  779. # endif
  780. default:
  781. result = NULL; /* initialized to prevent warning. */
  782. ABORT_RET("GC_debug_realloc: encountered bad kind");
  783. }
  784. if (result != NULL) {
  785. size_t old_sz;
  786. # ifdef SHORT_DBG_HDRS
  787. old_sz = GC_size(base) - sizeof(oh);
  788. # else
  789. old_sz = ((oh *)base) -> oh_sz;
  790. # endif
  791. if (old_sz > 0)
  792. BCOPY(p, result, old_sz < lb ? old_sz : lb);
  793. GC_debug_free(p);
  794. }
  795. return(result);
  796. }
  797. GC_API GC_ATTR_MALLOC void * GC_CALL
  798. GC_debug_generic_or_special_malloc(size_t lb, int knd, GC_EXTRA_PARAMS)
  799. {
  800. switch (knd) {
  801. case PTRFREE:
  802. return GC_debug_malloc_atomic(lb, OPT_RA s, i);
  803. case NORMAL:
  804. return GC_debug_malloc(lb, OPT_RA s, i);
  805. case UNCOLLECTABLE:
  806. return GC_debug_malloc_uncollectable(lb, OPT_RA s, i);
  807. # ifdef GC_ATOMIC_UNCOLLECTABLE
  808. case AUNCOLLECTABLE:
  809. return GC_debug_malloc_atomic_uncollectable(lb, OPT_RA s, i);
  810. # endif
  811. default:
  812. return GC_debug_generic_malloc(lb, knd, OPT_RA s, i);
  813. }
  814. }
  815. #ifndef SHORT_DBG_HDRS
  816. /* List of smashed (clobbered) locations. We defer printing these, */
  817. /* since we can't always print them nicely with the allocation lock */
  818. /* held. We put them here instead of in GC_arrays, since it may be */
  819. /* useful to be able to look at them with the debugger. */
  820. #ifndef MAX_SMASHED
  821. # define MAX_SMASHED 20
  822. #endif
  823. STATIC ptr_t GC_smashed[MAX_SMASHED] = {0};
  824. STATIC unsigned GC_n_smashed = 0;
  825. STATIC void GC_add_smashed(ptr_t smashed)
  826. {
  827. GC_ASSERT(GC_is_marked(GC_base(smashed)));
  828. /* FIXME: Prevent adding an object while printing smashed list. */
  829. GC_smashed[GC_n_smashed] = smashed;
  830. if (GC_n_smashed < MAX_SMASHED - 1) ++GC_n_smashed;
  831. /* In case of overflow, we keep the first MAX_SMASHED-1 */
  832. /* entries plus the last one. */
  833. GC_have_errors = TRUE;
  834. }
  835. /* Print all objects on the list. Clear the list. */
  836. STATIC void GC_print_all_smashed_proc(void)
  837. {
  838. unsigned i;
  839. GC_ASSERT(I_DONT_HOLD_LOCK());
  840. if (GC_n_smashed == 0) return;
  841. GC_err_printf("GC_check_heap_block: found %u smashed heap objects:\n",
  842. GC_n_smashed);
  843. for (i = 0; i < GC_n_smashed; ++i) {
  844. ptr_t base = (ptr_t)GC_base(GC_smashed[i]);
  845. # ifdef LINT2
  846. if (!base) ABORT("Invalid GC_smashed element");
  847. # endif
  848. GC_print_smashed_obj("", base + sizeof(oh), GC_smashed[i]);
  849. GC_smashed[i] = 0;
  850. }
  851. GC_n_smashed = 0;
  852. }
  853. /* Check all marked objects in the given block for validity */
  854. /* Avoid GC_apply_to_each_object for performance reasons. */
  855. STATIC void GC_check_heap_block(struct hblk *hbp, word dummy GC_ATTR_UNUSED)
  856. {
  857. struct hblkhdr * hhdr = HDR(hbp);
  858. word sz = hhdr -> hb_sz;
  859. word bit_no;
  860. char *p, *plim;
  861. p = hbp->hb_body;
  862. if (sz > MAXOBJBYTES) {
  863. plim = p;
  864. } else {
  865. plim = hbp->hb_body + HBLKSIZE - sz;
  866. }
  867. /* go through all words in block */
  868. for (bit_no = 0; (word)p <= (word)plim;
  869. bit_no += MARK_BIT_OFFSET(sz), p += sz) {
  870. if (mark_bit_from_hdr(hhdr, bit_no) && GC_HAS_DEBUG_INFO((ptr_t)p)) {
  871. ptr_t clobbered = GC_check_annotated_obj((oh *)p);
  872. if (clobbered != 0)
  873. GC_add_smashed(clobbered);
  874. }
  875. }
  876. }
  877. /* This assumes that all accessible objects are marked, and that */
  878. /* I hold the allocation lock. Normally called by collector. */
  879. STATIC void GC_check_heap_proc(void)
  880. {
  881. GC_STATIC_ASSERT((sizeof(oh) & (GRANULE_BYTES - 1)) == 0);
  882. /* FIXME: Should we check for twice that alignment? */
  883. GC_apply_to_all_blocks(GC_check_heap_block, 0);
  884. }
  885. GC_INNER GC_bool GC_check_leaked(ptr_t base)
  886. {
  887. word i;
  888. word obj_sz;
  889. word *p;
  890. if (
  891. # if defined(KEEP_BACK_PTRS) || defined(MAKE_BACK_GRAPH)
  892. (*(word *)base & 1) != 0 &&
  893. # endif
  894. GC_has_other_debug_info(base) >= 0)
  895. return TRUE; /* object has leaked */
  896. /* Validate freed object's content. */
  897. p = (word *)(base + sizeof(oh));
  898. obj_sz = BYTES_TO_WORDS(HDR(base)->hb_sz - sizeof(oh));
  899. for (i = 0; i < obj_sz; ++i)
  900. if (p[i] != GC_FREED_MEM_MARKER) {
  901. GC_set_mark_bit(base); /* do not reclaim it in this cycle */
  902. GC_add_smashed((ptr_t)(&p[i])); /* alter-after-free detected */
  903. break; /* don't report any other smashed locations in the object */
  904. }
  905. return FALSE; /* GC_debug_free() has been called */
  906. }
  907. #endif /* !SHORT_DBG_HDRS */
  908. #ifndef GC_NO_FINALIZATION
  909. struct closure {
  910. GC_finalization_proc cl_fn;
  911. void * cl_data;
  912. };
  913. STATIC void * GC_make_closure(GC_finalization_proc fn, void * data)
  914. {
  915. struct closure * result =
  916. # ifdef DBG_HDRS_ALL
  917. (struct closure *) GC_debug_malloc(sizeof (struct closure),
  918. GC_EXTRAS);
  919. # else
  920. (struct closure *) GC_malloc(sizeof (struct closure));
  921. # endif
  922. if (result != 0) {
  923. result -> cl_fn = fn;
  924. result -> cl_data = data;
  925. }
  926. return((void *)result);
  927. }
  928. /* An auxiliary fns to make finalization work correctly with displaced */
  929. /* pointers introduced by the debugging allocators. */
  930. STATIC void GC_CALLBACK GC_debug_invoke_finalizer(void * obj, void * data)
  931. {
  932. struct closure * cl = (struct closure *) data;
  933. (*(cl -> cl_fn))((void *)((char *)obj + sizeof(oh)), cl -> cl_data);
  934. }
  935. /* Special finalizer_proc value to detect GC_register_finalizer() failure. */
  936. #define OFN_UNSET ((GC_finalization_proc)~(signed_word)0)
  937. /* Set ofn and ocd to reflect the values we got back. */
  938. static void store_old(void *obj, GC_finalization_proc my_old_fn,
  939. struct closure *my_old_cd, GC_finalization_proc *ofn,
  940. void **ocd)
  941. {
  942. if (0 != my_old_fn) {
  943. if (my_old_fn == OFN_UNSET) {
  944. /* register_finalizer() failed; (*ofn) and (*ocd) are unchanged. */
  945. return;
  946. }
  947. if (my_old_fn != GC_debug_invoke_finalizer) {
  948. GC_err_printf("Debuggable object at %p had a non-debug finalizer\n",
  949. obj);
  950. /* This should probably be fatal. */
  951. } else {
  952. if (ofn) *ofn = my_old_cd -> cl_fn;
  953. if (ocd) *ocd = my_old_cd -> cl_data;
  954. }
  955. } else {
  956. if (ofn) *ofn = 0;
  957. if (ocd) *ocd = 0;
  958. }
  959. }
  960. GC_API void GC_CALL GC_debug_register_finalizer(void * obj,
  961. GC_finalization_proc fn,
  962. void * cd, GC_finalization_proc *ofn,
  963. void * *ocd)
  964. {
  965. GC_finalization_proc my_old_fn = OFN_UNSET;
  966. void * my_old_cd;
  967. ptr_t base = (ptr_t)GC_base(obj);
  968. if (NULL == base) {
  969. /* We won't collect it, hence finalizer wouldn't be run. */
  970. if (ocd) *ocd = 0;
  971. if (ofn) *ofn = 0;
  972. return;
  973. }
  974. if ((ptr_t)obj - base != sizeof(oh)) {
  975. GC_err_printf("GC_debug_register_finalizer called with"
  976. " non-base-pointer %p\n", obj);
  977. }
  978. if (0 == fn) {
  979. GC_register_finalizer(base, 0, 0, &my_old_fn, &my_old_cd);
  980. } else {
  981. cd = GC_make_closure(fn, cd);
  982. if (cd == 0) return; /* out of memory */
  983. GC_register_finalizer(base, GC_debug_invoke_finalizer,
  984. cd, &my_old_fn, &my_old_cd);
  985. }
  986. store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
  987. }
  988. GC_API void GC_CALL GC_debug_register_finalizer_no_order
  989. (void * obj, GC_finalization_proc fn,
  990. void * cd, GC_finalization_proc *ofn,
  991. void * *ocd)
  992. {
  993. GC_finalization_proc my_old_fn = OFN_UNSET;
  994. void * my_old_cd;
  995. ptr_t base = (ptr_t)GC_base(obj);
  996. if (NULL == base) {
  997. /* We won't collect it, hence finalizer wouldn't be run. */
  998. if (ocd) *ocd = 0;
  999. if (ofn) *ofn = 0;
  1000. return;
  1001. }
  1002. if ((ptr_t)obj - base != sizeof(oh)) {
  1003. GC_err_printf("GC_debug_register_finalizer_no_order called with"
  1004. " non-base-pointer %p\n", obj);
  1005. }
  1006. if (0 == fn) {
  1007. GC_register_finalizer_no_order(base, 0, 0, &my_old_fn, &my_old_cd);
  1008. } else {
  1009. cd = GC_make_closure(fn, cd);
  1010. if (cd == 0) return; /* out of memory */
  1011. GC_register_finalizer_no_order(base, GC_debug_invoke_finalizer,
  1012. cd, &my_old_fn, &my_old_cd);
  1013. }
  1014. store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
  1015. }
  1016. GC_API void GC_CALL GC_debug_register_finalizer_unreachable
  1017. (void * obj, GC_finalization_proc fn,
  1018. void * cd, GC_finalization_proc *ofn,
  1019. void * *ocd)
  1020. {
  1021. GC_finalization_proc my_old_fn = OFN_UNSET;
  1022. void * my_old_cd;
  1023. ptr_t base = (ptr_t)GC_base(obj);
  1024. if (NULL == base) {
  1025. /* We won't collect it, hence finalizer wouldn't be run. */
  1026. if (ocd) *ocd = 0;
  1027. if (ofn) *ofn = 0;
  1028. return;
  1029. }
  1030. if ((ptr_t)obj - base != sizeof(oh)) {
  1031. GC_err_printf("GC_debug_register_finalizer_unreachable called with"
  1032. " non-base-pointer %p\n", obj);
  1033. }
  1034. if (0 == fn) {
  1035. GC_register_finalizer_unreachable(base, 0, 0, &my_old_fn, &my_old_cd);
  1036. } else {
  1037. cd = GC_make_closure(fn, cd);
  1038. if (cd == 0) return; /* out of memory */
  1039. GC_register_finalizer_unreachable(base, GC_debug_invoke_finalizer,
  1040. cd, &my_old_fn, &my_old_cd);
  1041. }
  1042. store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
  1043. }
  1044. GC_API void GC_CALL GC_debug_register_finalizer_ignore_self
  1045. (void * obj, GC_finalization_proc fn,
  1046. void * cd, GC_finalization_proc *ofn,
  1047. void * *ocd)
  1048. {
  1049. GC_finalization_proc my_old_fn = OFN_UNSET;
  1050. void * my_old_cd;
  1051. ptr_t base = (ptr_t)GC_base(obj);
  1052. if (NULL == base) {
  1053. /* We won't collect it, hence finalizer wouldn't be run. */
  1054. if (ocd) *ocd = 0;
  1055. if (ofn) *ofn = 0;
  1056. return;
  1057. }
  1058. if ((ptr_t)obj - base != sizeof(oh)) {
  1059. GC_err_printf("GC_debug_register_finalizer_ignore_self called with"
  1060. " non-base-pointer %p\n", obj);
  1061. }
  1062. if (0 == fn) {
  1063. GC_register_finalizer_ignore_self(base, 0, 0, &my_old_fn, &my_old_cd);
  1064. } else {
  1065. cd = GC_make_closure(fn, cd);
  1066. if (cd == 0) return; /* out of memory */
  1067. GC_register_finalizer_ignore_self(base, GC_debug_invoke_finalizer,
  1068. cd, &my_old_fn, &my_old_cd);
  1069. }
  1070. store_old(obj, my_old_fn, (struct closure *)my_old_cd, ofn, ocd);
  1071. }
  1072. #endif /* !GC_NO_FINALIZATION */
  1073. GC_API GC_ATTR_MALLOC void * GC_CALL GC_debug_malloc_replacement(size_t lb)
  1074. {
  1075. return GC_debug_malloc(lb, GC_DBG_EXTRAS);
  1076. }
  1077. GC_API void * GC_CALL GC_debug_realloc_replacement(void *p, size_t lb)
  1078. {
  1079. return GC_debug_realloc(p, lb, GC_DBG_EXTRAS);
  1080. }