1
0

context.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. #include <string.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <assert.h>
  6. #include "private.h"
  7. #include "context.h"
  8. #include "node.h"
  9. unsigned long lub_heap_frame_count;
  10. #define CONTEXT_CHUNK_SIZE 100 /* number of contexts per chunk */
  11. #define CONTEXT_MAX_CHUNKS 100 /* allow upto 10000 contexts */
  12. /*---------------------------------------------------------
  13. * PRIVATE META FUNCTIONS
  14. *--------------------------------------------------------- */
  15. void
  16. lub_heap_context_meta_init(void)
  17. {
  18. static bool_t initialised = BOOL_FALSE;
  19. if(BOOL_FALSE == initialised)
  20. {
  21. lub_heap_leak_t *leak = lub_heap_leak_instance();
  22. initialised = BOOL_TRUE;
  23. /* initialise the context tree */
  24. lub_bintree_init(&leak->m_context_tree,
  25. offsetof(lub_heap_context_t,bt_node),
  26. lub_heap_context_compare,
  27. lub_heap_context_getkey);
  28. lub_heap_leak_release(leak);
  29. }
  30. }
  31. /*--------------------------------------------------------- */
  32. int
  33. lub_heap_context_compare(const void *clientnode,
  34. const void *clientkey)
  35. {
  36. int i,delta=0;
  37. const lub_heap_context_t *node = clientnode;
  38. const lub_heap_context_key_t *key = clientkey;
  39. function_t * const *node_fn = node->key.backtrace;
  40. function_t * const *key_fn = key->backtrace;
  41. for(i = lub_heap_frame_count;
  42. i;
  43. --i,++node_fn,++key_fn)
  44. {
  45. delta = ((unsigned long)*node_fn - (unsigned long)*key_fn);
  46. if(0 != delta)
  47. {
  48. break;
  49. }
  50. }
  51. return delta;
  52. }
  53. /*--------------------------------------------------------- */
  54. /* we simply use the embedded key as the index */
  55. void
  56. lub_heap_context_getkey(const void *clientnode,
  57. lub_bintree_key_t *key)
  58. {
  59. const lub_heap_context_t * context = clientnode;
  60. memcpy(key,&context->key,sizeof(lub_heap_context_key_t));
  61. }
  62. /*--------------------------------------------------------- */
  63. static bool_t
  64. lub_heap_foreach_context(bool_t (*fn)(lub_heap_context_t *,void *),
  65. void *arg)
  66. {
  67. bool_t result = BOOL_FALSE;
  68. lub_heap_context_t * context;
  69. lub_bintree_iterator_t iter;
  70. lub_heap_context_meta_init();
  71. {
  72. lub_heap_leak_t *leak = lub_heap_leak_instance();
  73. for(context = lub_bintree_findfirst(&leak->m_context_tree),
  74. context ? lub_bintree_iterator_init(&iter,&leak->m_context_tree,context) : (void)0;
  75. context;
  76. context = lub_bintree_iterator_next(&iter))
  77. {
  78. lub_heap_leak_release(leak);
  79. /* invoke the specified method on this context */
  80. result = fn(context,arg);
  81. leak = lub_heap_leak_instance();
  82. }
  83. lub_heap_leak_release(leak);
  84. }
  85. return result;
  86. }
  87. /*--------------------------------------------------------- */
  88. static void
  89. lub_heap_show_summary(void)
  90. {
  91. lub_heap_leak_t *leak = lub_heap_leak_instance();
  92. size_t ok_allocs = leak->m_stats.allocs;
  93. size_t ok_bytes = leak->m_stats.alloc_bytes;
  94. size_t ok_overhead = leak->m_stats.alloc_overhead;
  95. ok_allocs -= leak->m_stats.partials;
  96. ok_allocs -= leak->m_stats.leaks;
  97. ok_overhead -= leak->m_stats.partial_overhead;
  98. ok_overhead -= leak->m_stats.leaked_overhead;
  99. printf("\n"
  100. " +----------+----------+----------+----------+\n");
  101. printf(" TOTALS | blocks| bytes| average| overhead|\n");
  102. printf("+---------+----------+----------+----------+----------+\n");
  103. printf("|contexts |%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|%10s|\n",
  104. leak->m_stats.contexts,
  105. leak->m_stats.contexts * sizeof(lub_heap_context_t),
  106. leak->m_stats.contexts ? sizeof(lub_heap_context_t) : 0,
  107. "");
  108. printf("|allocs |%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|\n",
  109. ok_allocs,
  110. ok_bytes,
  111. ok_allocs ? (ok_bytes / ok_allocs) : 0,
  112. ok_overhead);
  113. if(leak->m_stats.partials)
  114. {
  115. printf("|partials |%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|\n",
  116. leak->m_stats.partials,
  117. leak->m_stats.partial_bytes,
  118. leak->m_stats.partials ? (leak->m_stats.partial_bytes / leak->m_stats.partials) : 0,
  119. leak->m_stats.partial_overhead);
  120. }
  121. if(leak->m_stats.leaks)
  122. {
  123. printf("|leaks |%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|\n",
  124. leak->m_stats.leaks,
  125. leak->m_stats.leaked_bytes,
  126. leak->m_stats.leaks ? (leak->m_stats.leaked_bytes / leak->m_stats.leaks) : 0,
  127. leak->m_stats.leaked_overhead);
  128. }
  129. printf("+---------+----------+----------+----------+----------+\n");
  130. lub_heap_leak_release(leak);
  131. }
  132. /*--------------------------------------------------------- */
  133. /*---------------------------------------------------------
  134. * PRIVATE METHODS
  135. *--------------------------------------------------------- */
  136. static void
  137. lub_heap_context_foreach_node(lub_heap_context_t *this,
  138. void (*fn)(lub_heap_node_t *))
  139. {
  140. lub_heap_node_t * node;
  141. for(node = this->first_node;
  142. node;
  143. node = lub_heap_node__get_next(node))
  144. {
  145. /* invoke the specified method on this node */
  146. fn(node);
  147. }
  148. }
  149. /*--------------------------------------------------------- */
  150. /*---------------------------------------------------------
  151. * PUBLIC METHODS
  152. *--------------------------------------------------------- */
  153. void
  154. lub_heap_context_init(lub_heap_context_t * this,
  155. lub_heap_t * heap,
  156. const stackframe_t * stack)
  157. {
  158. lub_heap_context_meta_init();
  159. this->heap = heap;
  160. this->allocs = 0;
  161. this->alloc_bytes = 0;
  162. this->alloc_overhead = 0;
  163. this->leaks = 0;
  164. this->leaked_bytes = 0;
  165. this->leaked_overhead = 0;
  166. this->partials = 0;
  167. this->partial_bytes = 0;
  168. this->partial_overhead = 0;
  169. this->first_node = NULL;
  170. /* set the current backtrace for the context */
  171. this->key = *stack;
  172. /* intialise this context's binary tree node */
  173. lub_bintree_node_init(&this->bt_node);
  174. {
  175. lub_heap_leak_t *leak = lub_heap_leak_instance();
  176. /* add this context to the context_tree */
  177. lub_bintree_insert(&leak->m_context_tree,this);
  178. lub_heap_leak_release(leak);
  179. }
  180. }
  181. /*--------------------------------------------------------- */
  182. void
  183. lub_heap_context_fini(lub_heap_context_t * this)
  184. {
  185. lub_heap_leak_t *leak = lub_heap_leak_instance();
  186. /* remove this node from the context_tree */
  187. lub_bintree_remove(&leak->m_context_tree,this);
  188. lub_heap_leak_release(leak);
  189. /* cleanup the context */
  190. lub_heap_context_clear(this);
  191. }
  192. /*--------------------------------------------------------- */
  193. void
  194. lub_heap_context_insert_node(lub_heap_context_t * this,
  195. lub_heap_node_t * node)
  196. {
  197. /* add the node to the linked list */
  198. lub_heap_node__set_next(node,this->first_node);
  199. node->prev = NULL;
  200. if(this->first_node)
  201. {
  202. this->first_node->prev = node;
  203. }
  204. this->first_node = node;
  205. }
  206. /*--------------------------------------------------------- */
  207. void
  208. lub_heap_context_remove_node(lub_heap_context_t *this,
  209. lub_heap_node_t *node)
  210. {
  211. lub_heap_node_t *next,*prev = NULL;
  212. /* remove the node from the context list */
  213. next = lub_heap_node__get_next(node);
  214. prev = node->prev;
  215. if(NULL == prev)
  216. {
  217. this->first_node = next;
  218. }
  219. else
  220. {
  221. lub_heap_node__set_next(prev,next);
  222. }
  223. if(NULL != next)
  224. {
  225. next->prev = prev;
  226. }
  227. /* clear the pointers */
  228. lub_heap_node__set_next(node,NULL);
  229. node->prev = NULL;
  230. /* is this the last node in a context? */
  231. if(0 == this->allocs)
  232. {
  233. /* removing the last node deletes the context */
  234. lub_heap_context_delete(this);
  235. }
  236. }
  237. /*--------------------------------------------------------- */
  238. void
  239. lub_heap_context_show_frame(lub_heap_context_t * this,
  240. int frame)
  241. {
  242. if(frame >= 0)
  243. {
  244. long address = (long)this->key.backtrace[frame];
  245. if(address)
  246. {
  247. lub_heap_symShow(address);
  248. }
  249. }
  250. printf("\n");
  251. }
  252. /*--------------------------------------------------------- */
  253. void
  254. lub_heap_node_post(lub_heap_node_t *node)
  255. {
  256. /* assume the worst */
  257. if(BOOL_TRUE == lub_heap_node__get_leaked(node))
  258. {
  259. /* this is a full leak */
  260. lub_heap_node__set_partial(node,BOOL_FALSE);
  261. }
  262. }
  263. /*--------------------------------------------------------- */
  264. void
  265. lub_heap_node_prep(lub_heap_node_t *node,
  266. void *arg)
  267. {
  268. /* assume the worst */
  269. lub_heap_node__set_leaked(node,BOOL_TRUE);
  270. lub_heap_node__set_partial(node,BOOL_TRUE);
  271. lub_heap_node__set_scanned(node,BOOL_FALSE);
  272. }
  273. /*--------------------------------------------------------- */
  274. void
  275. lub_heap_node_scan(lub_heap_node_t *node,
  276. void *arg)
  277. {
  278. /* only scan nodes which have references */
  279. if( (BOOL_FALSE == lub_heap_node__get_leaked(node) )
  280. && (BOOL_FALSE == lub_heap_node__get_scanned(node)) )
  281. {
  282. lub_heap_node__set_scanned(node,BOOL_TRUE);
  283. lub_heap_scan_memory(lub_heap_node__get_ptr(node),
  284. lub_heap_node__get_size(node));
  285. }
  286. }
  287. /*--------------------------------------------------------- */
  288. static bool_t
  289. lub_heap_context_prep(lub_heap_context_t *this,
  290. void *arg)
  291. {
  292. /* start off by assuming the worst */
  293. this->partials = this->leaks = this->allocs;
  294. this->partial_bytes = this->leaked_bytes = this->alloc_bytes;
  295. this->partial_overhead = this->leaked_overhead = this->alloc_overhead;
  296. {
  297. lub_heap_leak_t *leak = lub_heap_leak_instance();
  298. /* initialised the global stats */
  299. leak->m_stats.allocs += this->allocs;
  300. leak->m_stats.alloc_bytes += this->alloc_bytes;
  301. leak->m_stats.alloc_overhead += this->alloc_overhead;
  302. lub_heap_leak_release(leak);
  303. }
  304. return BOOL_TRUE;
  305. }
  306. /*--------------------------------------------------------- */
  307. static bool_t
  308. lub_heap_context_post(lub_heap_context_t *this,
  309. void *arg)
  310. {
  311. /* don't count full leaks as partials */
  312. this->partials -= this->leaks;
  313. this->partial_bytes -= this->leaked_bytes;
  314. this->partial_overhead -= this->leaked_overhead;
  315. /* post process the contained nodes */
  316. lub_heap_context_foreach_node(this,lub_heap_node_post);
  317. return BOOL_TRUE;
  318. }
  319. /*--------------------------------------------------------- */
  320. static void
  321. scan_segment(void *ptr,
  322. unsigned index,
  323. size_t size,
  324. void *arg)
  325. {
  326. lub_heap_segment_t *segment = ptr;
  327. const char *memory = (const char*)lub_heap_block_getfirst(segment);
  328. /* now scan the memory in this segment */
  329. printf(".");
  330. lub_heap_scan_memory(memory,size);
  331. }
  332. /*--------------------------------------------------------- */
  333. /**
  334. * This function scans all the nodes currently allocated in the
  335. * system for references to other allocated nodes.
  336. * First of all we mark all nodes as leaked, then scan all the nodes
  337. * for any references to other ones. If found those other ones
  338. * are cleared from being leaked.
  339. * At the end of the process all nodes which are leaked then
  340. * update their context leak count.
  341. */
  342. void
  343. lub_heap_scan_all(void)
  344. {
  345. lub_heap_t *heap;
  346. lub_heap_leak_t *leak = lub_heap_leak_instance();
  347. /* clear the summary stats */
  348. memset(&leak->m_stats,0,sizeof(leak->m_stats));
  349. lub_heap_leak_release(leak);
  350. /* first of all prepare the contexts for scanning */
  351. lub_heap_foreach_context(lub_heap_context_prep,0);
  352. /* then prepare all the nodes (including those who have no context) */
  353. lub_heap_foreach_node(lub_heap_node_prep,0);
  354. printf(" Scanning memory");
  355. /* clear out the stacks in the system */
  356. lub_heap_clean_stacks();
  357. /* Scan the current stack */
  358. printf(".");
  359. lub_heap_scan_stack();
  360. /* Scan the BSS segment */
  361. printf(".");
  362. lub_heap_scan_bss();
  363. /* Scan the DATA segment */
  364. printf(".");
  365. lub_heap_scan_data();
  366. /* Scan the non-monitored blocks which are allocated in the system */
  367. leak = lub_heap_leak_instance();
  368. for(heap = leak->m_heap_list;
  369. heap;
  370. heap = heap->next)
  371. {
  372. lub_heap_leak_release(leak);
  373. lub_heap_foreach_segment(heap,scan_segment,0);
  374. leak = lub_heap_leak_instance();
  375. }
  376. lub_heap_leak_release(leak);
  377. /*
  378. * now scan the nodes NB. only referenced nodes will be scanned
  379. * we loop until we stop scanning new nodes
  380. */
  381. leak = lub_heap_leak_instance();
  382. do
  383. {
  384. leak->m_stats.scanned = 0;
  385. /* scan each node */
  386. lub_heap_leak_release(leak);
  387. printf(".");
  388. lub_heap_foreach_node(lub_heap_node_scan,NULL);
  389. leak = lub_heap_leak_instance();
  390. } while(leak->m_stats.scanned);
  391. printf("done\n\n");
  392. /* post process each context and contained nodes */
  393. lub_heap_leak_release(leak);
  394. lub_heap_foreach_context(lub_heap_context_post,0);
  395. leak = lub_heap_leak_instance();
  396. lub_heap_leak_release(leak);
  397. }
  398. /*--------------------------------------------------------- */
  399. void lub_heap_node_show(lub_heap_node_t *node)
  400. {
  401. printf("%s%p[%"SIZE_FMT"] ",
  402. lub_heap_node__get_leaked(node) ? "*" : lub_heap_node__get_partial(node) ? "+" : "",
  403. lub_heap_node__get_ptr(node),
  404. lub_heap_node__get_size(node));
  405. }
  406. /*--------------------------------------------------------- */
  407. static bool_t
  408. lub_heap_context_match(lub_heap_context_t *this,
  409. const char *substring)
  410. {
  411. bool_t result = BOOL_TRUE;
  412. if(substring)
  413. {
  414. int i;
  415. long address;
  416. result = BOOL_FALSE;
  417. /*
  418. * search the stacktrace for this context
  419. * to see whether it matches
  420. */
  421. for(i = 0;
  422. (address = (long)this->key.backtrace[i]);
  423. ++i)
  424. {
  425. if(lub_heap_symMatch(address,substring))
  426. {
  427. result = BOOL_TRUE;
  428. break;
  429. }
  430. }
  431. }
  432. return result;
  433. }
  434. /*--------------------------------------------------------- */
  435. typedef struct
  436. {
  437. lub_heap_show_e how;
  438. const char *substring;
  439. } context_show_arg_t;
  440. /*--------------------------------------------------------- */
  441. bool_t
  442. lub_heap_context_show_fn(lub_heap_context_t *this,
  443. void *arg)
  444. {
  445. bool_t result = BOOL_FALSE;
  446. context_show_arg_t *show_arg = arg;
  447. lub_heap_leak_t *leak = lub_heap_leak_instance();
  448. /* add in the context details */
  449. ++leak->m_stats.contexts;
  450. leak->m_stats.allocs += this->allocs;
  451. leak->m_stats.alloc_bytes += this->alloc_bytes;
  452. leak->m_stats.alloc_overhead += this->alloc_overhead;
  453. leak->m_stats.partials += this->partials;
  454. leak->m_stats.partial_bytes += this->partial_bytes;
  455. leak->m_stats.partial_overhead += this->partial_overhead;
  456. leak->m_stats.leaks += this->leaks;
  457. leak->m_stats.leaked_bytes += this->leaked_bytes;
  458. leak->m_stats.leaked_overhead += this->leaked_overhead;
  459. lub_heap_leak_release(leak);
  460. if(lub_heap_context_match(this,show_arg->substring))
  461. {
  462. result = lub_heap_context_show(this,show_arg->how);
  463. }
  464. return result;
  465. }
  466. /*--------------------------------------------------------- */
  467. bool_t
  468. lub_heap_context_show(lub_heap_context_t *this,
  469. lub_heap_show_e how)
  470. {
  471. long frame = lub_heap_frame_count-1;
  472. size_t ok_allocs = this->allocs;
  473. size_t ok_bytes = this->alloc_bytes;
  474. size_t ok_overhead = this->alloc_overhead;
  475. ok_allocs -= this->partials;
  476. ok_allocs -= this->leaks;
  477. ok_bytes -= this->partial_bytes;
  478. ok_bytes -= this->leaked_bytes;
  479. ok_overhead -= this->partial_overhead;
  480. ok_overhead -= this->leaked_overhead;
  481. switch(how)
  482. {
  483. case LUB_HEAP_SHOW_ALL:
  484. {
  485. /* show everything */
  486. break;
  487. }
  488. case LUB_HEAP_SHOW_PARTIALS:
  489. {
  490. if(0 < this->partials)
  491. {
  492. /* there's at least one partial in this context */
  493. break;
  494. }
  495. /*lint -e(616) fall through */
  496. }
  497. case LUB_HEAP_SHOW_LEAKS:
  498. {
  499. if(0 < this->leaks)
  500. {
  501. /* there's at least one leak in this context */
  502. break;
  503. }
  504. /*lint -e(616) fall through */
  505. }
  506. default:
  507. {
  508. /* nothing to be shown */
  509. return BOOL_FALSE;
  510. }
  511. }
  512. /* find the top of the stack trace */
  513. while((frame >= 0) && (0 == this->key.backtrace[frame]))
  514. {
  515. --frame;
  516. }
  517. printf(" +----------+----------+----------+----------+");
  518. lub_heap_context_show_frame(this,frame--);
  519. printf( "%10p| blocks| bytes| average| overhead|",(void*)this);
  520. lub_heap_context_show_frame(this,frame--);
  521. printf("+---------+----------+----------+----------+----------+");
  522. lub_heap_context_show_frame(this,frame--);
  523. printf("|allocs |%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|",
  524. ok_allocs,
  525. ok_bytes,
  526. ok_allocs ? (ok_bytes / ok_allocs) : 0,
  527. ok_overhead);
  528. lub_heap_context_show_frame(this,frame--);
  529. if(this->partials)
  530. {
  531. printf("|partials |%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|",
  532. this->partials,
  533. this->partial_bytes,
  534. this->partials ? (this->partial_bytes/this->partials) : 0,
  535. this->partial_overhead);
  536. lub_heap_context_show_frame(this,frame--);
  537. }
  538. if(this->leaks)
  539. {
  540. printf("|leaks |%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|%10"SIZE_FMT"|",
  541. this->leaks,
  542. this->leaked_bytes,
  543. this->leaks ? (this->leaked_bytes/this->leaks) : 0,
  544. this->leaked_overhead);
  545. lub_heap_context_show_frame(this,frame--);
  546. }
  547. printf("+---------+----------+----------+----------+----------+");
  548. lub_heap_context_show_frame(this,frame--);
  549. while(frame >= 0)
  550. {
  551. printf("%55s","");
  552. lub_heap_context_show_frame(this,frame--);
  553. }
  554. printf("ALLOCATED BLOCKS: ");
  555. /* now iterate the allocated nodes */
  556. lub_heap_context_foreach_node(this,lub_heap_node_show);
  557. printf("\n\n");
  558. return BOOL_TRUE;
  559. }
  560. /*--------------------------------------------------------- */
  561. void
  562. lub_heap_context_clear(lub_heap_context_t *this)
  563. {
  564. /* should only ever get cleared when all nodes have left the context */
  565. assert(0 == this->first_node);
  566. this->heap = 0;
  567. /* reset the counters */
  568. this->allocs = 0;
  569. this->alloc_bytes = 0;
  570. this->leaks = 0;
  571. this->leaked_bytes = 0;
  572. this->partials = 0;
  573. this->partial_bytes = 0;
  574. }
  575. /*--------------------------------------------------------- */
  576. void
  577. lub_heap_show_leak_trees(void)
  578. {
  579. lub_heap_leak_t *leak = lub_heap_leak_instance();
  580. printf("context_tree : ");
  581. lub_bintree_dump(&leak->m_context_tree);
  582. printf("\n");
  583. printf("node_tree : ");
  584. lub_bintree_dump(&leak->m_node_tree);
  585. printf("\n");
  586. printf("clear_node_tree: ");
  587. lub_bintree_dump(&leak->m_clear_node_tree);
  588. printf("\n");
  589. lub_heap_leak_release(leak);
  590. }
  591. /*--------------------------------------------------------- */
  592. void
  593. lub_heap_leak_scan(void)
  594. {
  595. if(0 < lub_heap_frame_count)
  596. {
  597. /* check for memory leaks */
  598. if(!lub_heap_is_tainting())
  599. {
  600. printf("******************************************"
  601. "*** Memory tainting is disabled so results\n"
  602. "*** of scan may miss some real leaks!\n"
  603. "******************************************\n\n");
  604. }
  605. lub_heap_scan_all();
  606. }
  607. else
  608. {
  609. printf("Leak detection currently disabled\n\n");
  610. }
  611. }
  612. /*--------------------------------------------------------- */
  613. bool_t
  614. lub_heap_leak_report(lub_heap_show_e how,
  615. const char *substring)
  616. {
  617. bool_t result = BOOL_FALSE;
  618. if(0 < lub_heap_frame_count)
  619. {
  620. bool_t bad_arg = BOOL_FALSE;
  621. lub_heap_leak_t *leak = lub_heap_leak_instance();
  622. context_show_arg_t show_arg;
  623. show_arg.how = how;
  624. show_arg.substring = substring;
  625. /* zero these stats which will be accumulated from the contexts */
  626. leak->m_stats.contexts = 0;
  627. leak->m_stats.allocs = 0;
  628. leak->m_stats.alloc_bytes = 0;
  629. leak->m_stats.alloc_overhead = 0;
  630. leak->m_stats.partials = 0;
  631. leak->m_stats.partial_bytes = 0;
  632. leak->m_stats.partial_overhead = 0;
  633. leak->m_stats.leaks = 0;
  634. leak->m_stats.leaked_bytes = 0;
  635. leak->m_stats.leaked_overhead = 0;
  636. lub_heap_leak_release(leak);
  637. switch(how)
  638. {
  639. case LUB_HEAP_SHOW_LEAKS:
  640. case LUB_HEAP_SHOW_PARTIALS:
  641. case LUB_HEAP_SHOW_ALL:
  642. {
  643. result = lub_heap_foreach_context(lub_heap_context_show_fn,(void*)&show_arg);
  644. break;
  645. }
  646. default:
  647. {
  648. printf("Invalid argument: 0=leaks, 1=partials and leaks, 2=all allocations\n\n");
  649. bad_arg = BOOL_TRUE;
  650. break;
  651. }
  652. }
  653. if(BOOL_FALSE == bad_arg)
  654. {
  655. leak = lub_heap_leak_instance();
  656. if(leak->m_stats.leaks)
  657. {
  658. printf(" '*' = leaked memory\n");
  659. }
  660. if(leak->m_stats.partials)
  661. {
  662. printf(" '+' = partially leaked memory\n");
  663. }
  664. lub_heap_leak_release(leak);
  665. lub_heap_show_summary();
  666. printf(" %lu stack frames held for each allocation.\n\n",lub_heap_frame_count);
  667. }
  668. }
  669. else
  670. {
  671. printf("Leak detection currently disabled\n\n");
  672. }
  673. return result;
  674. }
  675. /*--------------------------------------------------------- */
  676. /*---------------------------------------------------------
  677. * PUBLIC META FUNCTIONS
  678. *--------------------------------------------------------- */
  679. void
  680. lub_heap__set_framecount(unsigned frames)
  681. {
  682. static bool_t clear_tainting_on_disable = BOOL_FALSE;
  683. lub_heap_leak_t *leak;
  684. if(frames == lub_heap_frame_count)
  685. return;
  686. /* iterate around clearing all the nodes in the node_tree */
  687. lub_heap_foreach_node(lub_heap_node_clear,0);
  688. leak = lub_heap_leak_instance();
  689. if(frames)
  690. {
  691. static bool_t registered = BOOL_FALSE;
  692. if(frames <= MAX_BACKTRACE)
  693. {
  694. /* change the number of frames held */
  695. lub_heap_frame_count = frames;
  696. }
  697. else
  698. {
  699. fprintf(stderr,"--- leak-detection frame count set to a maximum of %d\n",MAX_BACKTRACE);
  700. lub_heap_frame_count = MAX_BACKTRACE;
  701. }
  702. if(!lub_heap_is_tainting())
  703. {
  704. /* we need to taint memory to ensure old pointers don't mask leaks */
  705. clear_tainting_on_disable = BOOL_TRUE;
  706. lub_heap_taint(BOOL_TRUE);
  707. }
  708. if(BOOL_FALSE == registered)
  709. {
  710. registered = BOOL_TRUE;
  711. /*
  712. * set up the context pool
  713. */
  714. lub_dblockpool_init(&leak->m_context_pool,
  715. sizeof(lub_heap_context_t),
  716. CONTEXT_CHUNK_SIZE,
  717. CONTEXT_MAX_CHUNKS);
  718. }
  719. }
  720. else
  721. {
  722. /* switch off leak detection */
  723. lub_heap_frame_count = 0;
  724. if(clear_tainting_on_disable)
  725. {
  726. lub_heap_taint(BOOL_FALSE);
  727. clear_tainting_on_disable = BOOL_FALSE;
  728. }
  729. }
  730. lub_heap_leak_release(leak);
  731. }
  732. /*--------------------------------------------------------- */
  733. unsigned
  734. lub_heap__get_framecount(void)
  735. {
  736. return lub_heap_frame_count;
  737. }
  738. /*--------------------------------------------------------- */
  739. size_t
  740. lub_heap_context__get_instanceSize(void)
  741. {
  742. return (sizeof(lub_heap_context_t));
  743. }
  744. /*--------------------------------------------------------- */
  745. lub_heap_context_t *
  746. lub_heap_context_find(const stackframe_t *stack)
  747. {
  748. lub_heap_context_t *result;
  749. lub_heap_leak_t *leak = lub_heap_leak_instance();
  750. lub_heap_context_key_t key = *stack;
  751. result = lub_bintree_find(&leak->m_context_tree,&key);
  752. lub_heap_leak_release(leak);
  753. return result;
  754. }
  755. /*--------------------------------------------------------- */