astmm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, 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. */
  23. #ifdef __AST_DEBUG_MALLOC
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <time.h>
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/cli.h"
  30. #include "asterisk/logger.h"
  31. #include "asterisk/options.h"
  32. #include "asterisk/lock.h"
  33. #include "asterisk/strings.h"
  34. #include "asterisk/unaligned.h"
  35. #define SOME_PRIME 563
  36. #define FUNC_CALLOC 1
  37. #define FUNC_MALLOC 2
  38. #define FUNC_REALLOC 3
  39. #define FUNC_STRDUP 4
  40. #define FUNC_STRNDUP 5
  41. #define FUNC_VASPRINTF 6
  42. /* Undefine all our macros */
  43. #undef malloc
  44. #undef calloc
  45. #undef realloc
  46. #undef strdup
  47. #undef strndup
  48. #undef free
  49. #undef vasprintf
  50. #define FENCE_MAGIC 0xdeadbeef
  51. static FILE *mmlog;
  52. static struct ast_region {
  53. struct ast_region *next;
  54. char file[40];
  55. char func[40];
  56. int lineno;
  57. int which;
  58. size_t len;
  59. unsigned int fence;
  60. unsigned char data[0];
  61. } *regions[SOME_PRIME];
  62. #define HASH(a) \
  63. (((unsigned long)(a)) % SOME_PRIME)
  64. AST_MUTEX_DEFINE_STATIC(reglock);
  65. AST_MUTEX_DEFINE_STATIC(showmemorylock);
  66. static inline void *__ast_alloc_region(size_t size, int which, const char *file, int lineno, const char *func)
  67. {
  68. struct ast_region *reg;
  69. void *ptr = NULL;
  70. unsigned int *fence;
  71. int hash;
  72. reg = malloc(size + sizeof(struct ast_region) + sizeof(unsigned int));
  73. ast_mutex_lock(&reglock);
  74. if (reg) {
  75. ast_copy_string(reg->file, file, sizeof(reg->file));
  76. reg->file[sizeof(reg->file) - 1] = '\0';
  77. ast_copy_string(reg->func, func, sizeof(reg->func));
  78. reg->func[sizeof(reg->func) - 1] = '\0';
  79. reg->lineno = lineno;
  80. reg->len = size;
  81. reg->which = which;
  82. ptr = reg->data;
  83. hash = HASH(ptr);
  84. reg->next = regions[hash];
  85. regions[hash] = reg;
  86. reg->fence = FENCE_MAGIC;
  87. fence = (ptr + reg->len);
  88. put_unaligned_uint32(fence, FENCE_MAGIC);
  89. }
  90. ast_mutex_unlock(&reglock);
  91. if (!reg) {
  92. fprintf(stderr, "Out of memory :(\n");
  93. if (mmlog) {
  94. fprintf(mmlog, "%ld - Out of memory\n", time(NULL));
  95. fflush(mmlog);
  96. }
  97. }
  98. return ptr;
  99. }
  100. static inline size_t __ast_sizeof_region(void *ptr)
  101. {
  102. int hash = HASH(ptr);
  103. struct ast_region *reg;
  104. size_t len = 0;
  105. ast_mutex_lock(&reglock);
  106. reg = regions[hash];
  107. while (reg) {
  108. if (reg->data == ptr) {
  109. len = reg->len;
  110. break;
  111. }
  112. reg = reg->next;
  113. }
  114. ast_mutex_unlock(&reglock);
  115. return len;
  116. }
  117. static void __ast_free_region(void *ptr, const char *file, int lineno, const char *func)
  118. {
  119. int hash = HASH(ptr);
  120. struct ast_region *reg, *prev = NULL;
  121. unsigned int *fence;
  122. ast_mutex_lock(&reglock);
  123. reg = regions[hash];
  124. while (reg) {
  125. if (reg->data == ptr) {
  126. if (prev) {
  127. prev->next = reg->next;
  128. } else {
  129. regions[hash] = reg->next;
  130. }
  131. break;
  132. }
  133. prev = reg;
  134. reg = reg->next;
  135. }
  136. ast_mutex_unlock(&reglock);
  137. if (reg) {
  138. fence = (unsigned int *)(reg->data + reg->len);
  139. if (reg->fence != FENCE_MAGIC) {
  140. fprintf(stderr, "WARNING: Low fence violation at %p, in %s of %s, line %d\n", reg->data, reg->func, reg->file, reg->lineno);
  141. if (mmlog) {
  142. fprintf(mmlog, "%ld - WARNING: Low fence violation at %p, in %s of %s, line %d\n", time(NULL), reg->data, reg->func, reg->file, reg->lineno);
  143. fflush(mmlog);
  144. }
  145. }
  146. if (get_unaligned_uint32(fence) != FENCE_MAGIC) {
  147. fprintf(stderr, "WARNING: High fence violation at %p, in %s of %s, line %d\n", reg->data, reg->func, reg->file, reg->lineno);
  148. if (mmlog) {
  149. fprintf(mmlog, "%ld - WARNING: High fence violation at %p, in %s of %s, line %d\n", time(NULL), reg->data, reg->func, reg->file, reg->lineno);
  150. fflush(mmlog);
  151. }
  152. }
  153. free(reg);
  154. } else {
  155. fprintf(stderr, "WARNING: Freeing unused memory at %p, in %s of %s, line %d\n", ptr, func, file, lineno);
  156. if (mmlog) {
  157. fprintf(mmlog, "%ld - WARNING: Freeing unused memory at %p, in %s of %s, line %d\n", time(NULL), ptr, func, file, lineno);
  158. fflush(mmlog);
  159. }
  160. }
  161. }
  162. void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func)
  163. {
  164. void *ptr;
  165. ptr = __ast_alloc_region(size * nmemb, FUNC_CALLOC, file, lineno, func);
  166. if (ptr)
  167. memset(ptr, 0, size * nmemb);
  168. return ptr;
  169. }
  170. void *__ast_malloc(size_t size, const char *file, int lineno, const char *func)
  171. {
  172. return __ast_alloc_region(size, FUNC_MALLOC, file, lineno, func);
  173. }
  174. void __ast_free(void *ptr, const char *file, int lineno, const char *func)
  175. {
  176. __ast_free_region(ptr, file, lineno, func);
  177. }
  178. void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func)
  179. {
  180. void *tmp;
  181. size_t len = 0;
  182. if (ptr) {
  183. len = __ast_sizeof_region(ptr);
  184. if (!len) {
  185. fprintf(stderr, "WARNING: Realloc of unalloced memory at %p, in %s of %s, line %d\n", ptr, func, file, lineno);
  186. if (mmlog) {
  187. fprintf(mmlog, "%ld - WARNING: Realloc of unalloced memory at %p, in %s of %s, line %d\n", time(NULL), ptr, func, file, lineno);
  188. fflush(mmlog);
  189. }
  190. return NULL;
  191. }
  192. }
  193. tmp = __ast_alloc_region(size, FUNC_REALLOC, file, lineno, func);
  194. if (tmp) {
  195. if (len > size)
  196. len = size;
  197. if (ptr) {
  198. memcpy(tmp, ptr, len);
  199. __ast_free_region(ptr, file, lineno, func);
  200. }
  201. }
  202. return tmp;
  203. }
  204. char *__ast_strdup(const char *s, const char *file, int lineno, const char *func)
  205. {
  206. size_t len;
  207. void *ptr;
  208. if (!s)
  209. return NULL;
  210. len = strlen(s) + 1;
  211. ptr = __ast_alloc_region(len, FUNC_STRDUP, file, lineno, func);
  212. if (ptr)
  213. strcpy(ptr, s);
  214. return ptr;
  215. }
  216. char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func)
  217. {
  218. size_t len;
  219. void *ptr;
  220. if (!s)
  221. return NULL;
  222. len = strlen(s) + 1;
  223. if (len > n)
  224. len = n;
  225. ptr = __ast_alloc_region(len, FUNC_STRNDUP, file, lineno, func);
  226. if (ptr)
  227. strcpy(ptr, s);
  228. return ptr;
  229. }
  230. int __ast_vasprintf(char **strp, const char *fmt, va_list ap, const char *file, int lineno, const char *func)
  231. {
  232. int size;
  233. va_list ap2;
  234. char s;
  235. *strp = NULL;
  236. va_copy(ap2, ap);
  237. size = vsnprintf(&s, 1, fmt, ap2);
  238. va_end(ap2);
  239. *strp = __ast_alloc_region(size + 1, FUNC_VASPRINTF, file, lineno, func);
  240. if (!*strp)
  241. return -1;
  242. vsnprintf(*strp, size + 1, fmt, ap);
  243. return size;
  244. }
  245. static int handle_show_memory(int fd, int argc, char *argv[])
  246. {
  247. char *fn = NULL;
  248. int x;
  249. struct ast_region *reg;
  250. unsigned int len = 0;
  251. int count = 0;
  252. unsigned int *fence;
  253. if (argc > 3)
  254. fn = argv[3];
  255. /* try to lock applications list ... */
  256. ast_mutex_lock(&showmemorylock);
  257. for (x = 0; x < SOME_PRIME; x++) {
  258. reg = regions[x];
  259. while (reg) {
  260. if (!fn || !strcasecmp(fn, reg->file) || !strcasecmp(fn, "anomolies")) {
  261. fence = (unsigned int *)(reg->data + reg->len);
  262. if (reg->fence != FENCE_MAGIC) {
  263. fprintf(stderr, "WARNING: Low fence violation at %p, in %s of %s, line %d\n", reg->data, reg->func, reg->file, reg->lineno);
  264. if (mmlog) {
  265. fprintf(mmlog, "%ld - WARNING: Low fence violation at %p, in %s of %s, line %d\n", time(NULL), reg->data, reg->func, reg-> file, reg->lineno);
  266. fflush(mmlog);
  267. }
  268. }
  269. if (get_unaligned_uint32(fence) != FENCE_MAGIC) {
  270. fprintf(stderr, "WARNING: High fence violation at %p, in %s of %s, line %d\n", reg->data, reg->func, reg->file, reg->lineno);
  271. if (mmlog) {
  272. fprintf(mmlog, "%ld - WARNING: High fence violation at %p, in %s of %s, line %d\n", time(NULL), reg->data, reg->func, reg->file, reg->lineno);
  273. fflush(mmlog);
  274. }
  275. }
  276. }
  277. if (!fn || !strcasecmp(fn, reg->file)) {
  278. ast_cli(fd, "%10d bytes allocated in %20s at line %5d of %s\n", (int) reg->len, reg->func, reg->lineno, reg->file);
  279. len += reg->len;
  280. count++;
  281. }
  282. reg = reg->next;
  283. }
  284. }
  285. ast_cli(fd, "%d bytes allocated %d units total\n", len, count);
  286. ast_mutex_unlock(&showmemorylock);
  287. return RESULT_SUCCESS;
  288. }
  289. struct file_summary {
  290. char fn[80];
  291. int len;
  292. int count;
  293. struct file_summary *next;
  294. };
  295. static int handle_show_memory_summary(int fd, int argc, char *argv[])
  296. {
  297. char *fn = NULL;
  298. int x;
  299. struct ast_region *reg;
  300. unsigned int len = 0;
  301. int count = 0;
  302. struct file_summary *list = NULL, *cur;
  303. if (argc > 3)
  304. fn = argv[3];
  305. /* try to lock applications list ... */
  306. ast_mutex_lock(&reglock);
  307. for (x = 0; x < SOME_PRIME; x++) {
  308. reg = regions[x];
  309. while (reg) {
  310. if (!fn || !strcasecmp(fn, reg->file)) {
  311. cur = list;
  312. while (cur) {
  313. if ((!fn && !strcmp(cur->fn, reg->file)) || (fn && !strcmp(cur->fn, reg->func)))
  314. break;
  315. cur = cur->next;
  316. }
  317. if (!cur) {
  318. cur = alloca(sizeof(struct file_summary));
  319. memset(cur, 0, sizeof(struct file_summary));
  320. ast_copy_string(cur->fn, fn ? reg->func : reg->file, sizeof(cur->fn));
  321. cur->next = list;
  322. list = cur;
  323. }
  324. cur->len += reg->len;
  325. cur->count++;
  326. }
  327. reg = reg->next;
  328. }
  329. }
  330. ast_mutex_unlock(&reglock);
  331. /* Dump the whole list */
  332. while (list) {
  333. cur = list;
  334. len += list->len;
  335. count += list->count;
  336. if (fn) {
  337. ast_cli(fd, "%10d bytes in %5d allocations in function '%s' of '%s'\n", list->len, list->count, list->fn, fn);
  338. } else {
  339. ast_cli(fd, "%10d bytes in %5d allocations in file '%s'\n", list->len, list->count, list->fn);
  340. }
  341. list = list->next;
  342. #if 0
  343. free(cur);
  344. #endif
  345. }
  346. ast_cli(fd, "%d bytes allocated %d units total\n", len, count);
  347. return RESULT_SUCCESS;
  348. }
  349. static char show_memory_help[] =
  350. "Usage: show memory allocations [<file>]\n"
  351. " Dumps a list of all segments of allocated memory, optionally\n"
  352. "limited to those from a specific file\n";
  353. static char show_memory_summary_help[] =
  354. "Usage: show memory summary [<file>]\n"
  355. " Summarizes heap memory allocations by file, or optionally\n"
  356. "by function, if a file is specified\n";
  357. static struct ast_cli_entry show_memory_allocations_cli =
  358. { { "show", "memory", "allocations", NULL },
  359. handle_show_memory, "Display outstanding memory allocations",
  360. show_memory_help };
  361. static struct ast_cli_entry show_memory_summary_cli =
  362. { { "show", "memory", "summary", NULL },
  363. handle_show_memory_summary, "Summarize outstanding memory allocations",
  364. show_memory_summary_help };
  365. void __ast_mm_init(void)
  366. {
  367. char filename[80] = "";
  368. ast_cli_register(&show_memory_allocations_cli);
  369. ast_cli_register(&show_memory_summary_cli);
  370. snprintf(filename, sizeof(filename), "%s/mmlog", (char *)ast_config_AST_LOG_DIR);
  371. mmlog = fopen(filename, "a+");
  372. if (option_verbose)
  373. ast_verbose("Asterisk Malloc Debugger Started (see %s))\n", filename);
  374. if (mmlog) {
  375. fprintf(mmlog, "%ld - New session\n", time(NULL));
  376. fflush(mmlog);
  377. }
  378. }
  379. #endif