Wed Jan 8 2020 09:49:42

Asterisk developer's documentation


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