astmm.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2012, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Memory Management
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. * \author Richard Mudgett <rmudgett@digium.com>
  24. */
  25. /*** MODULEINFO
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. #if defined(__AST_DEBUG_MALLOC)
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/paths.h" /* use ast_config_AST_LOG_DIR */
  32. #include <stddef.h>
  33. #include <time.h>
  34. #include "asterisk/cli.h"
  35. #include "asterisk/lock.h"
  36. #include "asterisk/strings.h"
  37. #include "asterisk/unaligned.h"
  38. /*!
  39. * The larger the number the faster memory can be freed.
  40. * However, more memory then is used for the regions[] hash
  41. * table.
  42. */
  43. #define SOME_PRIME 1567
  44. enum func_type {
  45. FUNC_CALLOC = 1,
  46. FUNC_MALLOC,
  47. FUNC_REALLOC,
  48. FUNC_STRDUP,
  49. FUNC_STRNDUP,
  50. FUNC_VASPRINTF,
  51. FUNC_ASPRINTF
  52. };
  53. /* Undefine all our macros */
  54. #undef malloc
  55. #undef calloc
  56. #undef realloc
  57. #undef strdup
  58. #undef strndup
  59. #undef free
  60. #undef vasprintf
  61. #undef asprintf
  62. #define FENCE_MAGIC 0xfeedbabe /*!< Allocated memory high/low fence overwrite check. */
  63. #define FREED_MAGIC 0xdeaddead /*!< Freed memory wipe filler. */
  64. #define MALLOC_FILLER 0x55 /*!< Malloced memory filler. Must not be zero. */
  65. static FILE *mmlog;
  66. struct ast_region {
  67. AST_LIST_ENTRY(ast_region) node;
  68. size_t len;
  69. unsigned int cache; /* region was allocated as part of a cache pool */
  70. unsigned int lineno;
  71. enum func_type which;
  72. char file[64];
  73. char func[40];
  74. /*!
  75. * \brief Lower guard fence.
  76. *
  77. * \note Must be right before data[].
  78. *
  79. * \note Padding between fence and data[] is irrelevent because
  80. * data[] is used to fill in the lower fence check value and not
  81. * the fence member. The fence member is to ensure that there
  82. * is space reserved for the fence check value.
  83. */
  84. unsigned int fence;
  85. /*!
  86. * \brief Location of the requested malloc block to return.
  87. *
  88. * \note Must have the same alignment that malloc returns.
  89. * i.e., It is suitably aligned for any kind of varible.
  90. */
  91. unsigned char data[0] __attribute__((aligned));
  92. };
  93. /*! Hash table of lists of active allocated memory regions. */
  94. static struct ast_region *regions[SOME_PRIME];
  95. /*! Number of freed regions to keep around to delay actually freeing them. */
  96. #define FREED_MAX_COUNT 1500
  97. /*! Maximum size of a minnow block */
  98. #define MINNOWS_MAX_SIZE 50
  99. struct ast_freed_regions {
  100. /*! Memory regions that have been freed. */
  101. struct ast_region *regions[FREED_MAX_COUNT];
  102. /*! Next index into freed regions[] to use. */
  103. int index;
  104. };
  105. /*! Large memory blocks that have been freed. */
  106. static struct ast_freed_regions whales;
  107. /*! Small memory blocks that have been freed. */
  108. static struct ast_freed_regions minnows;
  109. enum summary_opts {
  110. /*! No summary at exit. */
  111. SUMMARY_OFF,
  112. /*! Bit set if summary by line at exit. */
  113. SUMMARY_BY_LINE = (1 << 0),
  114. /*! Bit set if summary by function at exit. */
  115. SUMMARY_BY_FUNC = (1 << 1),
  116. /*! Bit set if summary by file at exit. */
  117. SUMMARY_BY_FILE = (1 << 2),
  118. };
  119. /*! Summary options of unfreed regions at exit. */
  120. static enum summary_opts atexit_summary;
  121. /*! Nonzero if the unfreed regions are listed at exit. */
  122. static int atexit_list;
  123. #define HASH(a) (((unsigned long)(a)) % ARRAY_LEN(regions))
  124. /*! Tracking this mutex will cause infinite recursion, as the mutex tracking
  125. * code allocates memory */
  126. AST_MUTEX_DEFINE_STATIC_NOTRACKING(reglock);
  127. #define astmm_log(...) \
  128. do { \
  129. fprintf(stderr, __VA_ARGS__); \
  130. if (mmlog) { \
  131. fprintf(mmlog, __VA_ARGS__); \
  132. fflush(mmlog); \
  133. } \
  134. } while (0)
  135. void *ast_std_malloc(size_t size)
  136. {
  137. return malloc(size);
  138. }
  139. void *ast_std_calloc(size_t nmemb, size_t size)
  140. {
  141. return calloc(nmemb, size);
  142. }
  143. void *ast_std_realloc(void *ptr, size_t size)
  144. {
  145. return realloc(ptr, size);
  146. }
  147. void ast_std_free(void *ptr)
  148. {
  149. free(ptr);
  150. }
  151. void ast_free_ptr(void *ptr)
  152. {
  153. ast_free(ptr);
  154. }
  155. /*!
  156. * \internal
  157. *
  158. * \note If DO_CRASH is not defined then the function returns.
  159. *
  160. * \return Nothing
  161. */
  162. static void my_do_crash(void)
  163. {
  164. /*
  165. * Give the logger a chance to get the message out, just in case
  166. * we abort(), or Asterisk crashes due to whatever problem just
  167. * happened.
  168. */
  169. usleep(1);
  170. ast_do_crash();
  171. }
  172. static void *__ast_alloc_region(size_t size, const enum func_type which, const char *file, int lineno, const char *func, unsigned int cache)
  173. {
  174. struct ast_region *reg;
  175. unsigned int *fence;
  176. int hash;
  177. if (!(reg = malloc(size + sizeof(*reg) + sizeof(*fence)))) {
  178. astmm_log("Memory Allocation Failure - '%d' bytes at %s %s() line %d\n",
  179. (int) size, file, func, lineno);
  180. return NULL;
  181. }
  182. reg->len = size;
  183. reg->cache = cache;
  184. reg->lineno = lineno;
  185. reg->which = which;
  186. ast_copy_string(reg->file, file, sizeof(reg->file));
  187. ast_copy_string(reg->func, func, sizeof(reg->func));
  188. /*
  189. * Init lower fence.
  190. *
  191. * We use the bytes just preceeding reg->data and not reg->fence
  192. * because there is likely to be padding between reg->fence and
  193. * reg->data for reg->data alignment.
  194. */
  195. fence = (unsigned int *) (reg->data - sizeof(*fence));
  196. *fence = FENCE_MAGIC;
  197. /* Init higher fence. */
  198. fence = (unsigned int *) (reg->data + reg->len);
  199. put_unaligned_uint32(fence, FENCE_MAGIC);
  200. hash = HASH(reg->data);
  201. ast_mutex_lock(&reglock);
  202. AST_LIST_NEXT(reg, node) = regions[hash];
  203. regions[hash] = reg;
  204. ast_mutex_unlock(&reglock);
  205. return reg->data;
  206. }
  207. /*!
  208. * \internal
  209. * \brief Wipe the region payload data with a known value.
  210. *
  211. * \param reg Region block to be wiped.
  212. *
  213. * \return Nothing
  214. */
  215. static void region_data_wipe(struct ast_region *reg)
  216. {
  217. void *end;
  218. unsigned int *pos;
  219. /*
  220. * Wipe the lower fence, the payload, and whatever amount of the
  221. * higher fence that falls into alignment with the payload.
  222. */
  223. end = reg->data + reg->len;
  224. for (pos = &reg->fence; (void *) pos <= end; ++pos) {
  225. *pos = FREED_MAGIC;
  226. }
  227. }
  228. /*!
  229. * \internal
  230. * \brief Check the region payload data for memory corruption.
  231. *
  232. * \param reg Region block to be checked.
  233. *
  234. * \return Nothing
  235. */
  236. static void region_data_check(struct ast_region *reg)
  237. {
  238. void *end;
  239. unsigned int *pos;
  240. /*
  241. * Check the lower fence, the payload, and whatever amount of
  242. * the higher fence that falls into alignment with the payload.
  243. */
  244. end = reg->data + reg->len;
  245. for (pos = &reg->fence; (void *) pos <= end; ++pos) {
  246. if (*pos != FREED_MAGIC) {
  247. astmm_log("WARNING: Memory corrupted after free of %p allocated at %s %s() line %d\n",
  248. reg->data, reg->file, reg->func, reg->lineno);
  249. my_do_crash();
  250. break;
  251. }
  252. }
  253. }
  254. /*!
  255. * \internal
  256. * \brief Flush the circular array of freed regions.
  257. *
  258. * \param freed Already freed region blocks storage.
  259. *
  260. * \return Nothing
  261. */
  262. static void freed_regions_flush(struct ast_freed_regions *freed)
  263. {
  264. int idx;
  265. struct ast_region *old;
  266. ast_mutex_lock(&reglock);
  267. for (idx = 0; idx < ARRAY_LEN(freed->regions); ++idx) {
  268. old = freed->regions[idx];
  269. freed->regions[idx] = NULL;
  270. if (old) {
  271. region_data_check(old);
  272. free(old);
  273. }
  274. }
  275. freed->index = 0;
  276. ast_mutex_unlock(&reglock);
  277. }
  278. /*!
  279. * \internal
  280. * \brief Delay freeing a region block.
  281. *
  282. * \param freed Already freed region blocks storage.
  283. * \param reg Region block to be freed.
  284. *
  285. * \return Nothing
  286. */
  287. static void region_free(struct ast_freed_regions *freed, struct ast_region *reg)
  288. {
  289. struct ast_region *old;
  290. region_data_wipe(reg);
  291. ast_mutex_lock(&reglock);
  292. old = freed->regions[freed->index];
  293. freed->regions[freed->index] = reg;
  294. ++freed->index;
  295. if (ARRAY_LEN(freed->regions) <= freed->index) {
  296. freed->index = 0;
  297. }
  298. ast_mutex_unlock(&reglock);
  299. if (old) {
  300. region_data_check(old);
  301. free(old);
  302. }
  303. }
  304. /*!
  305. * \internal
  306. * \brief Remove a region from the active regions.
  307. *
  308. * \param ptr Region payload data pointer.
  309. *
  310. * \retval region on success.
  311. * \retval NULL if not found.
  312. */
  313. static struct ast_region *region_remove(void *ptr)
  314. {
  315. int hash;
  316. struct ast_region *reg;
  317. struct ast_region *prev = NULL;
  318. hash = HASH(ptr);
  319. ast_mutex_lock(&reglock);
  320. for (reg = regions[hash]; reg; reg = AST_LIST_NEXT(reg, node)) {
  321. if (reg->data == ptr) {
  322. if (prev) {
  323. AST_LIST_NEXT(prev, node) = AST_LIST_NEXT(reg, node);
  324. } else {
  325. regions[hash] = AST_LIST_NEXT(reg, node);
  326. }
  327. break;
  328. }
  329. prev = reg;
  330. }
  331. ast_mutex_unlock(&reglock);
  332. return reg;
  333. }
  334. /*!
  335. * \internal
  336. * \brief Check the fences of a region.
  337. *
  338. * \param reg Region block to check.
  339. *
  340. * \return Nothing
  341. */
  342. static void region_check_fences(struct ast_region *reg)
  343. {
  344. unsigned int *fence;
  345. /*
  346. * We use the bytes just preceeding reg->data and not reg->fence
  347. * because there is likely to be padding between reg->fence and
  348. * reg->data for reg->data alignment.
  349. */
  350. fence = (unsigned int *) (reg->data - sizeof(*fence));
  351. if (*fence != FENCE_MAGIC) {
  352. astmm_log("WARNING: Low fence violation of %p allocated at %s %s() line %d\n",
  353. reg->data, reg->file, reg->func, reg->lineno);
  354. my_do_crash();
  355. }
  356. fence = (unsigned int *) (reg->data + reg->len);
  357. if (get_unaligned_uint32(fence) != FENCE_MAGIC) {
  358. astmm_log("WARNING: High fence violation of %p allocated at %s %s() line %d\n",
  359. reg->data, reg->file, reg->func, reg->lineno);
  360. my_do_crash();
  361. }
  362. }
  363. /*!
  364. * \internal
  365. * \brief Check the fences of all regions currently allocated.
  366. *
  367. * \return Nothing
  368. */
  369. static void regions_check_all_fences(void)
  370. {
  371. int idx;
  372. struct ast_region *reg;
  373. ast_mutex_lock(&reglock);
  374. for (idx = 0; idx < ARRAY_LEN(regions); ++idx) {
  375. for (reg = regions[idx]; reg; reg = AST_LIST_NEXT(reg, node)) {
  376. region_check_fences(reg);
  377. }
  378. }
  379. ast_mutex_unlock(&reglock);
  380. }
  381. static void __ast_free_region(void *ptr, const char *file, int lineno, const char *func)
  382. {
  383. struct ast_region *reg;
  384. if (!ptr) {
  385. return;
  386. }
  387. reg = region_remove(ptr);
  388. if (reg) {
  389. region_check_fences(reg);
  390. if (reg->len <= MINNOWS_MAX_SIZE) {
  391. region_free(&minnows, reg);
  392. } else {
  393. region_free(&whales, reg);
  394. }
  395. } else {
  396. /*
  397. * This memory region is not registered. It could be because of
  398. * a double free or the memory block was not allocated by the
  399. * malloc debug code.
  400. */
  401. astmm_log("WARNING: Freeing unregistered memory %p by %s %s() line %d\n",
  402. ptr, file, func, lineno);
  403. my_do_crash();
  404. }
  405. }
  406. void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func)
  407. {
  408. void *ptr;
  409. ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func, 0);
  410. if (ptr) {
  411. memset(ptr, 0, size * nmemb);
  412. }
  413. return ptr;
  414. }
  415. void *__ast_calloc_cache(size_t nmemb, size_t size, const char *file, int lineno, const char *func)
  416. {
  417. void *ptr;
  418. ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func, 1);
  419. if (ptr) {
  420. memset(ptr, 0, size * nmemb);
  421. }
  422. return ptr;
  423. }
  424. void *__ast_malloc(size_t size, const char *file, int lineno, const char *func)
  425. {
  426. void *ptr;
  427. ptr = __ast_alloc_region(size, FUNC_MALLOC, file, lineno, func, 0);
  428. if (ptr) {
  429. /* Make sure that the malloced memory is not zero. */
  430. memset(ptr, MALLOC_FILLER, size);
  431. }
  432. return ptr;
  433. }
  434. void __ast_free(void *ptr, const char *file, int lineno, const char *func)
  435. {
  436. __ast_free_region(ptr, file, lineno, func);
  437. }
  438. /*!
  439. * \note reglock must be locked before calling.
  440. */
  441. static struct ast_region *region_find(void *ptr)
  442. {
  443. int hash;
  444. struct ast_region *reg;
  445. hash = HASH(ptr);
  446. for (reg = regions[hash]; reg; reg = AST_LIST_NEXT(reg, node)) {
  447. if (reg->data == ptr) {
  448. break;
  449. }
  450. }
  451. return reg;
  452. }
  453. void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func)
  454. {
  455. size_t len;
  456. struct ast_region *found;
  457. void *new_mem;
  458. if (ptr) {
  459. ast_mutex_lock(&reglock);
  460. found = region_find(ptr);
  461. if (!found) {
  462. ast_mutex_unlock(&reglock);
  463. astmm_log("WARNING: Realloc of unregistered memory %p by %s %s() line %d\n",
  464. ptr, file, func, lineno);
  465. my_do_crash();
  466. return NULL;
  467. }
  468. len = found->len;
  469. ast_mutex_unlock(&reglock);
  470. } else {
  471. found = NULL;
  472. len = 0;
  473. }
  474. if (!size) {
  475. __ast_free_region(ptr, file, lineno, func);
  476. return NULL;
  477. }
  478. new_mem = __ast_alloc_region(size, FUNC_REALLOC, file, lineno, func, 0);
  479. if (new_mem) {
  480. if (found) {
  481. /* Copy the old data to the new malloced memory. */
  482. if (size <= len) {
  483. memcpy(new_mem, ptr, size);
  484. } else {
  485. memcpy(new_mem, ptr, len);
  486. /* Make sure that the added memory is not zero. */
  487. memset(new_mem + len, MALLOC_FILLER, size - len);
  488. }
  489. __ast_free_region(ptr, file, lineno, func);
  490. } else {
  491. /* Make sure that the malloced memory is not zero. */
  492. memset(new_mem, MALLOC_FILLER, size);
  493. }
  494. }
  495. return new_mem;
  496. }
  497. char *__ast_strdup(const char *s, const char *file, int lineno, const char *func)
  498. {
  499. size_t len;
  500. void *ptr;
  501. if (!s)
  502. return NULL;
  503. len = strlen(s) + 1;
  504. if ((ptr = __ast_alloc_region(len, FUNC_STRDUP, file, lineno, func, 0)))
  505. strcpy(ptr, s);
  506. return ptr;
  507. }
  508. char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func)
  509. {
  510. size_t len;
  511. char *ptr;
  512. if (!s) {
  513. return NULL;
  514. }
  515. len = strnlen(s, n);
  516. if ((ptr = __ast_alloc_region(len + 1, FUNC_STRNDUP, file, lineno, func, 0))) {
  517. memcpy(ptr, s, len);
  518. ptr[len] = '\0';
  519. }
  520. return ptr;
  521. }
  522. int __ast_asprintf(const char *file, int lineno, const char *func, char **strp, const char *fmt, ...)
  523. {
  524. int size;
  525. va_list ap, ap2;
  526. char s;
  527. *strp = NULL;
  528. va_start(ap, fmt);
  529. va_copy(ap2, ap);
  530. size = vsnprintf(&s, 1, fmt, ap2);
  531. va_end(ap2);
  532. if (!(*strp = __ast_alloc_region(size + 1, FUNC_ASPRINTF, file, lineno, func, 0))) {
  533. va_end(ap);
  534. return -1;
  535. }
  536. vsnprintf(*strp, size + 1, fmt, ap);
  537. va_end(ap);
  538. return size;
  539. }
  540. int __ast_vasprintf(char **strp, const char *fmt, va_list ap, const char *file, int lineno, const char *func)
  541. {
  542. int size;
  543. va_list ap2;
  544. char s;
  545. *strp = NULL;
  546. va_copy(ap2, ap);
  547. size = vsnprintf(&s, 1, fmt, ap2);
  548. va_end(ap2);
  549. if (!(*strp = __ast_alloc_region(size + 1, FUNC_VASPRINTF, file, lineno, func, 0))) {
  550. va_end(ap);
  551. return -1;
  552. }
  553. vsnprintf(*strp, size + 1, fmt, ap);
  554. return size;
  555. }
  556. static char *handle_memory_atexit_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  557. {
  558. switch (cmd) {
  559. case CLI_INIT:
  560. e->command = "memory atexit list";
  561. e->usage =
  562. "Usage: memory atexit list {on|off}\n"
  563. " Enable dumping a list of still allocated memory segments at exit.\n";
  564. return NULL;
  565. case CLI_GENERATE:
  566. if (a->pos == 3) {
  567. const char * const options[] = { "off", "on", NULL };
  568. return ast_cli_complete(a->word, options, a->n);
  569. }
  570. return NULL;
  571. }
  572. if (a->argc != 4) {
  573. return CLI_SHOWUSAGE;
  574. }
  575. if (ast_true(a->argv[3])) {
  576. atexit_list = 1;
  577. } else if (ast_false(a->argv[3])) {
  578. atexit_list = 0;
  579. } else {
  580. return CLI_SHOWUSAGE;
  581. }
  582. ast_cli(a->fd, "The atexit list is: %s\n", atexit_list ? "On" : "Off");
  583. return CLI_SUCCESS;
  584. }
  585. static char *handle_memory_atexit_summary(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  586. {
  587. char buf[80];
  588. switch (cmd) {
  589. case CLI_INIT:
  590. e->command = "memory atexit summary";
  591. e->usage =
  592. "Usage: memory atexit summary {off|byline|byfunc|byfile}\n"
  593. " Summary of still allocated memory segments at exit options.\n"
  594. " off - Disable at exit summary.\n"
  595. " byline - Enable at exit summary by file line number.\n"
  596. " byfunc - Enable at exit summary by function name.\n"
  597. " byfile - Enable at exit summary by file.\n"
  598. "\n"
  599. " Note: byline, byfunc, and byfile are cumulative enables.\n";
  600. return NULL;
  601. case CLI_GENERATE:
  602. if (a->pos == 3) {
  603. const char * const options[] = { "off", "byline", "byfunc", "byfile", NULL };
  604. return ast_cli_complete(a->word, options, a->n);
  605. }
  606. return NULL;
  607. }
  608. if (a->argc != 4) {
  609. return CLI_SHOWUSAGE;
  610. }
  611. if (ast_false(a->argv[3])) {
  612. atexit_summary = SUMMARY_OFF;
  613. } else if (!strcasecmp(a->argv[3], "byline")) {
  614. atexit_summary |= SUMMARY_BY_LINE;
  615. } else if (!strcasecmp(a->argv[3], "byfunc")) {
  616. atexit_summary |= SUMMARY_BY_FUNC;
  617. } else if (!strcasecmp(a->argv[3], "byfile")) {
  618. atexit_summary |= SUMMARY_BY_FILE;
  619. } else {
  620. return CLI_SHOWUSAGE;
  621. }
  622. if (atexit_summary) {
  623. buf[0] = '\0';
  624. if (atexit_summary & SUMMARY_BY_LINE) {
  625. strcat(buf, "byline");
  626. }
  627. if (atexit_summary & SUMMARY_BY_FUNC) {
  628. if (buf[0]) {
  629. strcat(buf, " | ");
  630. }
  631. strcat(buf, "byfunc");
  632. }
  633. if (atexit_summary & SUMMARY_BY_FILE) {
  634. if (buf[0]) {
  635. strcat(buf, " | ");
  636. }
  637. strcat(buf, "byfile");
  638. }
  639. } else {
  640. strcpy(buf, "Off");
  641. }
  642. ast_cli(a->fd, "The atexit summary is: %s\n", buf);
  643. return CLI_SUCCESS;
  644. }
  645. static char *handle_memory_show_allocations(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  646. {
  647. const char *fn = NULL;
  648. struct ast_region *reg;
  649. unsigned int idx;
  650. unsigned int len = 0;
  651. unsigned int cache_len = 0;
  652. unsigned int count = 0;
  653. switch (cmd) {
  654. case CLI_INIT:
  655. e->command = "memory show allocations";
  656. e->usage =
  657. "Usage: memory show allocations [<file>|anomalies]\n"
  658. " Dumps a list of segments of allocated memory.\n"
  659. " Defaults to listing all memory allocations.\n"
  660. " <file> - Restricts output to memory allocated by the file.\n"
  661. " anomalies - Only check for fence violations.\n";
  662. return NULL;
  663. case CLI_GENERATE:
  664. return NULL;
  665. }
  666. if (a->argc == 4) {
  667. fn = a->argv[3];
  668. } else if (a->argc != 3) {
  669. return CLI_SHOWUSAGE;
  670. }
  671. /* Look for historical misspelled option as well. */
  672. if (fn && (!strcasecmp(fn, "anomalies") || !strcasecmp(fn, "anomolies"))) {
  673. regions_check_all_fences();
  674. ast_cli(a->fd, "Anomaly check complete.\n");
  675. return CLI_SUCCESS;
  676. }
  677. ast_mutex_lock(&reglock);
  678. for (idx = 0; idx < ARRAY_LEN(regions); ++idx) {
  679. for (reg = regions[idx]; reg; reg = AST_LIST_NEXT(reg, node)) {
  680. if (fn && strcasecmp(fn, reg->file)) {
  681. continue;
  682. }
  683. region_check_fences(reg);
  684. ast_cli(a->fd, "%10u bytes allocated%s by %20s() line %5u of %s\n",
  685. (unsigned int) reg->len, reg->cache ? " (cache)" : "",
  686. reg->func, reg->lineno, reg->file);
  687. len += reg->len;
  688. if (reg->cache) {
  689. cache_len += reg->len;
  690. }
  691. ++count;
  692. }
  693. }
  694. ast_mutex_unlock(&reglock);
  695. if (cache_len) {
  696. ast_cli(a->fd, "%u bytes allocated (%u in caches) in %u allocations\n",
  697. len, cache_len, count);
  698. } else {
  699. ast_cli(a->fd, "%u bytes allocated in %u allocations\n", len, count);
  700. }
  701. return CLI_SUCCESS;
  702. }
  703. static char *handle_memory_show_summary(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  704. {
  705. #define my_max(a, b) ((a) >= (b) ? (a) : (b))
  706. const char *fn = NULL;
  707. int idx;
  708. int cmp;
  709. struct ast_region *reg;
  710. unsigned int len = 0;
  711. unsigned int cache_len = 0;
  712. unsigned int count = 0;
  713. struct file_summary {
  714. struct file_summary *next;
  715. unsigned int len;
  716. unsigned int cache_len;
  717. unsigned int count;
  718. unsigned int lineno;
  719. char name[my_max(sizeof(reg->file), sizeof(reg->func))];
  720. } *list = NULL, *cur, **prev;
  721. switch (cmd) {
  722. case CLI_INIT:
  723. e->command = "memory show summary";
  724. e->usage =
  725. "Usage: memory show summary [<file>]\n"
  726. " Summarizes heap memory allocations by file, or optionally\n"
  727. " by line, if a file is specified.\n";
  728. return NULL;
  729. case CLI_GENERATE:
  730. return NULL;
  731. }
  732. if (a->argc == 4) {
  733. fn = a->argv[3];
  734. } else if (a->argc != 3) {
  735. return CLI_SHOWUSAGE;
  736. }
  737. ast_mutex_lock(&reglock);
  738. for (idx = 0; idx < ARRAY_LEN(regions); ++idx) {
  739. for (reg = regions[idx]; reg; reg = AST_LIST_NEXT(reg, node)) {
  740. if (fn) {
  741. if (strcasecmp(fn, reg->file)) {
  742. continue;
  743. }
  744. /* Sort list by func/lineno. Find existing or place to insert. */
  745. for (prev = &list; (cur = *prev); prev = &cur->next) {
  746. cmp = strcmp(cur->name, reg->func);
  747. if (cmp < 0) {
  748. continue;
  749. }
  750. if (cmp > 0) {
  751. /* Insert before current */
  752. cur = NULL;
  753. break;
  754. }
  755. cmp = cur->lineno - reg->lineno;
  756. if (cmp < 0) {
  757. continue;
  758. }
  759. if (cmp > 0) {
  760. /* Insert before current */
  761. cur = NULL;
  762. }
  763. break;
  764. }
  765. } else {
  766. /* Sort list by filename. Find existing or place to insert. */
  767. for (prev = &list; (cur = *prev); prev = &cur->next) {
  768. cmp = strcmp(cur->name, reg->file);
  769. if (cmp < 0) {
  770. continue;
  771. }
  772. if (cmp > 0) {
  773. /* Insert before current */
  774. cur = NULL;
  775. }
  776. break;
  777. }
  778. }
  779. if (!cur) {
  780. cur = ast_alloca(sizeof(*cur));
  781. memset(cur, 0, sizeof(*cur));
  782. cur->lineno = reg->lineno;
  783. ast_copy_string(cur->name, fn ? reg->func : reg->file, sizeof(cur->name));
  784. cur->next = *prev;
  785. *prev = cur;
  786. }
  787. cur->len += reg->len;
  788. if (reg->cache) {
  789. cur->cache_len += reg->len;
  790. }
  791. ++cur->count;
  792. }
  793. }
  794. ast_mutex_unlock(&reglock);
  795. /* Dump the whole list */
  796. for (cur = list; cur; cur = cur->next) {
  797. len += cur->len;
  798. cache_len += cur->cache_len;
  799. count += cur->count;
  800. if (cur->cache_len) {
  801. if (fn) {
  802. ast_cli(a->fd, "%10u bytes (%10u cache) in %10u allocations by %20s() line %5u of %s\n",
  803. cur->len, cur->cache_len, cur->count, cur->name, cur->lineno, fn);
  804. } else {
  805. ast_cli(a->fd, "%10u bytes (%10u cache) in %10u allocations in file %s\n",
  806. cur->len, cur->cache_len, cur->count, cur->name);
  807. }
  808. } else {
  809. if (fn) {
  810. ast_cli(a->fd, "%10u bytes in %10u allocations by %20s() line %5u of %s\n",
  811. cur->len, cur->count, cur->name, cur->lineno, fn);
  812. } else {
  813. ast_cli(a->fd, "%10u bytes in %10u allocations in file %s\n",
  814. cur->len, cur->count, cur->name);
  815. }
  816. }
  817. }
  818. if (cache_len) {
  819. ast_cli(a->fd, "%u bytes allocated (%u in caches) in %u allocations\n",
  820. len, cache_len, count);
  821. } else {
  822. ast_cli(a->fd, "%u bytes allocated in %u allocations\n", len, count);
  823. }
  824. return CLI_SUCCESS;
  825. }
  826. static struct ast_cli_entry cli_memory[] = {
  827. AST_CLI_DEFINE(handle_memory_atexit_list, "Enable memory allocations not freed at exit list."),
  828. AST_CLI_DEFINE(handle_memory_atexit_summary, "Enable memory allocations not freed at exit summary."),
  829. AST_CLI_DEFINE(handle_memory_show_allocations, "Display outstanding memory allocations"),
  830. AST_CLI_DEFINE(handle_memory_show_summary, "Summarize outstanding memory allocations"),
  831. };
  832. AST_LIST_HEAD_NOLOCK(region_list, ast_region);
  833. /*!
  834. * \internal
  835. * \brief Convert the allocated regions hash table to a list.
  836. *
  837. * \param list Fill list with the allocated regions.
  838. *
  839. * \details
  840. * Take all allocated regions from the regions[] and put them
  841. * into the list.
  842. *
  843. * \note reglock must be locked before calling.
  844. *
  845. * \note This function is destructive to the regions[] lists.
  846. *
  847. * \return Length of list created.
  848. */
  849. static size_t mm_atexit_hash_list(struct region_list *list)
  850. {
  851. struct ast_region *reg;
  852. size_t total_length;
  853. int idx;
  854. total_length = 0;
  855. for (idx = 0; idx < ARRAY_LEN(regions); ++idx) {
  856. while ((reg = regions[idx])) {
  857. regions[idx] = AST_LIST_NEXT(reg, node);
  858. AST_LIST_NEXT(reg, node) = NULL;
  859. AST_LIST_INSERT_HEAD(list, reg, node);
  860. ++total_length;
  861. }
  862. }
  863. return total_length;
  864. }
  865. /*!
  866. * \internal
  867. * \brief Put the regions list into the allocated regions hash table.
  868. *
  869. * \param list List to put into the allocated regions hash table.
  870. *
  871. * \note reglock must be locked before calling.
  872. *
  873. * \return Nothing
  874. */
  875. static void mm_atexit_hash_restore(struct region_list *list)
  876. {
  877. struct ast_region *reg;
  878. int hash;
  879. while ((reg = AST_LIST_REMOVE_HEAD(list, node))) {
  880. hash = HASH(reg->data);
  881. AST_LIST_NEXT(reg, node) = regions[hash];
  882. regions[hash] = reg;
  883. }
  884. }
  885. /*!
  886. * \internal
  887. * \brief Sort regions comparision.
  888. *
  889. * \param left Region to compare.
  890. * \param right Region to compare.
  891. *
  892. * \retval <0 if left < right
  893. * \retval =0 if left == right
  894. * \retval >0 if left > right
  895. */
  896. static int mm_atexit_cmp(struct ast_region *left, struct ast_region *right)
  897. {
  898. int cmp;
  899. ptrdiff_t cmp_ptr;
  900. ssize_t cmp_size;
  901. /* Sort by filename. */
  902. cmp = strcmp(left->file, right->file);
  903. if (cmp) {
  904. return cmp;
  905. }
  906. /* Sort by line number. */
  907. cmp = left->lineno - right->lineno;
  908. if (cmp) {
  909. return cmp;
  910. }
  911. /* Sort by allocated size. */
  912. cmp_size = left->len - right->len;
  913. if (cmp_size) {
  914. if (cmp_size < 0) {
  915. return -1;
  916. }
  917. return 1;
  918. }
  919. /* Sort by allocated pointers just because. */
  920. cmp_ptr = left->data - right->data;
  921. if (cmp_ptr) {
  922. if (cmp_ptr < 0) {
  923. return -1;
  924. }
  925. return 1;
  926. }
  927. return 0;
  928. }
  929. /*!
  930. * \internal
  931. * \brief Merge the given sorted sublists into sorted order onto the end of the list.
  932. *
  933. * \param list Merge sublists onto this list.
  934. * \param sub1 First sublist to merge.
  935. * \param sub2 Second sublist to merge.
  936. *
  937. * \return Nothing
  938. */
  939. static void mm_atexit_list_merge(struct region_list *list, struct region_list *sub1, struct region_list *sub2)
  940. {
  941. struct ast_region *reg;
  942. for (;;) {
  943. if (AST_LIST_EMPTY(sub1)) {
  944. /* The remaining sublist goes onto the list. */
  945. AST_LIST_APPEND_LIST(list, sub2, node);
  946. break;
  947. }
  948. if (AST_LIST_EMPTY(sub2)) {
  949. /* The remaining sublist goes onto the list. */
  950. AST_LIST_APPEND_LIST(list, sub1, node);
  951. break;
  952. }
  953. if (mm_atexit_cmp(AST_LIST_FIRST(sub1), AST_LIST_FIRST(sub2)) <= 0) {
  954. reg = AST_LIST_REMOVE_HEAD(sub1, node);
  955. } else {
  956. reg = AST_LIST_REMOVE_HEAD(sub2, node);
  957. }
  958. AST_LIST_INSERT_TAIL(list, reg, node);
  959. }
  960. }
  961. /*!
  962. * \internal
  963. * \brief Take sublists off of the given list.
  964. *
  965. * \param list Source list to remove sublists from the beginning of list.
  966. * \param sub Array of sublists to fill. (Lists are empty on entry.)
  967. * \param num_lists Number of lists to remove from the source list.
  968. * \param size Size of the sublists to remove.
  969. * \param remaining Remaining number of elements on the source list.
  970. *
  971. * \return Nothing
  972. */
  973. static void mm_atexit_list_split(struct region_list *list, struct region_list sub[], size_t num_lists, size_t size, size_t *remaining)
  974. {
  975. int idx;
  976. for (idx = 0; idx < num_lists; ++idx) {
  977. size_t count;
  978. if (*remaining < size) {
  979. /* The remaining source list goes onto the sublist. */
  980. AST_LIST_APPEND_LIST(&sub[idx], list, node);
  981. *remaining = 0;
  982. break;
  983. }
  984. /* Take a sublist off the beginning of the source list. */
  985. *remaining -= size;
  986. for (count = size; count--;) {
  987. struct ast_region *reg;
  988. reg = AST_LIST_REMOVE_HEAD(list, node);
  989. AST_LIST_INSERT_TAIL(&sub[idx], reg, node);
  990. }
  991. }
  992. }
  993. /*!
  994. * \internal
  995. * \brief Sort the regions list using mergesort.
  996. *
  997. * \param list Allocated regions list to sort.
  998. * \param length Length of the list.
  999. *
  1000. * \return Nothing
  1001. */
  1002. static void mm_atexit_list_sort(struct region_list *list, size_t length)
  1003. {
  1004. /*! Semi-sorted merged list. */
  1005. struct region_list merged = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
  1006. /*! Sublists to merge. (Can only merge two sublists at this time.) */
  1007. struct region_list sub[2] = {
  1008. AST_LIST_HEAD_NOLOCK_INIT_VALUE,
  1009. AST_LIST_HEAD_NOLOCK_INIT_VALUE
  1010. };
  1011. /*! Sublist size. */
  1012. size_t size = 1;
  1013. /*! Remaining elements in the list. */
  1014. size_t remaining;
  1015. /*! Number of sublist merge passes to process the list. */
  1016. int passes;
  1017. for (;;) {
  1018. remaining = length;
  1019. passes = 0;
  1020. while (!AST_LIST_EMPTY(list)) {
  1021. mm_atexit_list_split(list, sub, ARRAY_LEN(sub), size, &remaining);
  1022. mm_atexit_list_merge(&merged, &sub[0], &sub[1]);
  1023. ++passes;
  1024. }
  1025. AST_LIST_APPEND_LIST(list, &merged, node);
  1026. if (passes <= 1) {
  1027. /* The list is now sorted. */
  1028. break;
  1029. }
  1030. /* Double the sublist size to remove for next round. */
  1031. size <<= 1;
  1032. }
  1033. }
  1034. /*!
  1035. * \internal
  1036. * \brief List all regions currently allocated.
  1037. *
  1038. * \param alloced regions list.
  1039. *
  1040. * \return Nothing
  1041. */
  1042. static void mm_atexit_regions_list(struct region_list *alloced)
  1043. {
  1044. struct ast_region *reg;
  1045. AST_LIST_TRAVERSE(alloced, reg, node) {
  1046. astmm_log("%s %s() line %u: %u bytes%s at %p\n",
  1047. reg->file, reg->func, reg->lineno,
  1048. (unsigned int) reg->len, reg->cache ? " (cache)" : "", reg->data);
  1049. }
  1050. }
  1051. /*!
  1052. * \internal
  1053. * \brief Summarize all regions currently allocated.
  1054. *
  1055. * \param alloced Sorted regions list.
  1056. *
  1057. * \return Nothing
  1058. */
  1059. static void mm_atexit_regions_summary(struct region_list *alloced)
  1060. {
  1061. struct ast_region *reg;
  1062. struct ast_region *next;
  1063. struct {
  1064. unsigned int count;
  1065. unsigned int len;
  1066. unsigned int cache_len;
  1067. } by_line, by_func, by_file, total;
  1068. by_line.count = 0;
  1069. by_line.len = 0;
  1070. by_line.cache_len = 0;
  1071. by_func.count = 0;
  1072. by_func.len = 0;
  1073. by_func.cache_len = 0;
  1074. by_file.count = 0;
  1075. by_file.len = 0;
  1076. by_file.cache_len = 0;
  1077. total.count = 0;
  1078. total.len = 0;
  1079. total.cache_len = 0;
  1080. AST_LIST_TRAVERSE(alloced, reg, node) {
  1081. next = AST_LIST_NEXT(reg, node);
  1082. ++by_line.count;
  1083. by_line.len += reg->len;
  1084. if (reg->cache) {
  1085. by_line.cache_len += reg->len;
  1086. }
  1087. if (next && !strcmp(reg->file, next->file) && reg->lineno == next->lineno) {
  1088. continue;
  1089. }
  1090. if (atexit_summary & SUMMARY_BY_LINE) {
  1091. if (by_line.cache_len) {
  1092. astmm_log("%10u bytes (%u in caches) in %u allocations. %s %s() line %u\n",
  1093. by_line.len, by_line.cache_len, by_line.count, reg->file, reg->func, reg->lineno);
  1094. } else {
  1095. astmm_log("%10u bytes in %5u allocations. %s %s() line %u\n",
  1096. by_line.len, by_line.count, reg->file, reg->func, reg->lineno);
  1097. }
  1098. }
  1099. by_func.count += by_line.count;
  1100. by_func.len += by_line.len;
  1101. by_func.cache_len += by_line.cache_len;
  1102. by_line.count = 0;
  1103. by_line.len = 0;
  1104. by_line.cache_len = 0;
  1105. if (next && !strcmp(reg->file, next->file) && !strcmp(reg->func, next->func)) {
  1106. continue;
  1107. }
  1108. if (atexit_summary & SUMMARY_BY_FUNC) {
  1109. if (by_func.cache_len) {
  1110. astmm_log("%10u bytes (%u in caches) in %u allocations. %s %s()\n",
  1111. by_func.len, by_func.cache_len, by_func.count, reg->file, reg->func);
  1112. } else {
  1113. astmm_log("%10u bytes in %5u allocations. %s %s()\n",
  1114. by_func.len, by_func.count, reg->file, reg->func);
  1115. }
  1116. }
  1117. by_file.count += by_func.count;
  1118. by_file.len += by_func.len;
  1119. by_file.cache_len += by_func.cache_len;
  1120. by_func.count = 0;
  1121. by_func.len = 0;
  1122. by_func.cache_len = 0;
  1123. if (next && !strcmp(reg->file, next->file)) {
  1124. continue;
  1125. }
  1126. if (atexit_summary & SUMMARY_BY_FILE) {
  1127. if (by_file.cache_len) {
  1128. astmm_log("%10u bytes (%u in caches) in %u allocations. %s\n",
  1129. by_file.len, by_file.cache_len, by_file.count, reg->file);
  1130. } else {
  1131. astmm_log("%10u bytes in %5u allocations. %s\n",
  1132. by_file.len, by_file.count, reg->file);
  1133. }
  1134. }
  1135. total.count += by_file.count;
  1136. total.len += by_file.len;
  1137. total.cache_len += by_file.cache_len;
  1138. by_file.count = 0;
  1139. by_file.len = 0;
  1140. by_file.cache_len = 0;
  1141. }
  1142. if (total.cache_len) {
  1143. astmm_log("%u bytes (%u in caches) in %u allocations.\n",
  1144. total.len, total.cache_len, total.count);
  1145. } else {
  1146. astmm_log("%u bytes in %u allocations.\n", total.len, total.count);
  1147. }
  1148. }
  1149. /*!
  1150. * \internal
  1151. * \brief Dump the memory allocations atexit.
  1152. *
  1153. * \note reglock must be locked before calling.
  1154. *
  1155. * \return Nothing
  1156. */
  1157. static void mm_atexit_dump(void)
  1158. {
  1159. struct region_list alloced_atexit = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
  1160. size_t length;
  1161. length = mm_atexit_hash_list(&alloced_atexit);
  1162. if (!length) {
  1163. /* Wow! This is amazing! */
  1164. astmm_log("Exiting with all memory freed.\n");
  1165. return;
  1166. }
  1167. mm_atexit_list_sort(&alloced_atexit, length);
  1168. astmm_log("Exiting with the following memory not freed:\n");
  1169. if (atexit_list) {
  1170. mm_atexit_regions_list(&alloced_atexit);
  1171. }
  1172. if (atexit_summary) {
  1173. mm_atexit_regions_summary(&alloced_atexit);
  1174. }
  1175. /*
  1176. * Put the alloced list back into regions[].
  1177. *
  1178. * We have do do this because we can get called before all other
  1179. * threads have terminated.
  1180. */
  1181. mm_atexit_hash_restore(&alloced_atexit);
  1182. }
  1183. /*!
  1184. * \internal
  1185. * \return Nothing
  1186. */
  1187. static void mm_atexit_final(void)
  1188. {
  1189. FILE *log;
  1190. /* Only wait if we want atexit allocation dumps. */
  1191. if (atexit_list || atexit_summary) {
  1192. fprintf(stderr, "Waiting 10 seconds to let other threads die.\n");
  1193. sleep(10);
  1194. }
  1195. regions_check_all_fences();
  1196. /* Flush all delayed memory free circular arrays. */
  1197. freed_regions_flush(&whales);
  1198. freed_regions_flush(&minnows);
  1199. /* Peform atexit allocation dumps. */
  1200. if (atexit_list || atexit_summary) {
  1201. ast_mutex_lock(&reglock);
  1202. mm_atexit_dump();
  1203. ast_mutex_unlock(&reglock);
  1204. }
  1205. /* Close the log file. */
  1206. log = mmlog;
  1207. mmlog = NULL;
  1208. if (log) {
  1209. fclose(log);
  1210. }
  1211. }
  1212. /*!
  1213. * \brief Initialize malloc debug phase 1.
  1214. *
  1215. * \note Must be called first thing in main().
  1216. *
  1217. * \return Nothing
  1218. */
  1219. void __ast_mm_init_phase_1(void)
  1220. {
  1221. atexit(mm_atexit_final);
  1222. }
  1223. /*!
  1224. * \internal
  1225. * \return Nothing
  1226. */
  1227. static void mm_atexit_ast(void)
  1228. {
  1229. ast_cli_unregister_multiple(cli_memory, ARRAY_LEN(cli_memory));
  1230. }
  1231. /*!
  1232. * \brief Initialize malloc debug phase 2.
  1233. *
  1234. * \return Nothing
  1235. */
  1236. void __ast_mm_init_phase_2(void)
  1237. {
  1238. char filename[PATH_MAX];
  1239. ast_cli_register_multiple(cli_memory, ARRAY_LEN(cli_memory));
  1240. snprintf(filename, sizeof(filename), "%s/mmlog", ast_config_AST_LOG_DIR);
  1241. ast_verb(1, "Asterisk Malloc Debugger Started (see %s))\n", filename);
  1242. mmlog = fopen(filename, "a+");
  1243. if (mmlog) {
  1244. fprintf(mmlog, "%ld - New session\n", (long) time(NULL));
  1245. fflush(mmlog);
  1246. } else {
  1247. ast_log(LOG_ERROR, "Could not open malloc debug log file: %s\n", filename);
  1248. }
  1249. ast_register_atexit(mm_atexit_ast);
  1250. }
  1251. #endif /* defined(__AST_DEBUG_MALLOC) */