2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Kevin P. Fleming <kpfleming@digium.com>
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.
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.
21 * \brief Debugging support for thread-local-storage objects
23 * \author Kevin P. Fleming <kpfleming@digium.com>
27 #include "asterisk/_private.h"
29 #if !defined(DEBUG_THREADLOCALS)
31 void threadstorage_init(void)
35 #else /* !defined(DEBUG_THREADLOCALS) */
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39 #include "asterisk/strings.h"
40 #include "asterisk/utils.h"
41 #include "asterisk/threadstorage.h"
42 #include "asterisk/linkedlists.h"
43 #include "asterisk/cli.h"
52 AST_LIST_ENTRY(tls_object) entry;
55 static AST_LIST_HEAD_NOLOCK_STATIC(tls_objects, tls_object);
57 /* Allow direct use of pthread_mutex_t and friends */
58 #undef pthread_mutex_t
59 #undef pthread_mutex_lock
60 #undef pthread_mutex_unlock
61 #undef pthread_mutex_init
62 #undef pthread_mutex_destroy
65 * \brief lock for the tls_objects list
67 * \note We can not use an ast_mutex_t for this. The reason is that this
68 * lock is used within the context of thread-local data destructors,
69 * and the ast_mutex_* API uses thread-local data. Allocating more
70 * thread-local data at that point just causes a memory leak.
72 static pthread_mutex_t threadstoragelock;
74 void __ast_threadstorage_object_add(void *key, size_t len, const char *file, const char *function, unsigned int line)
76 struct tls_object *to;
78 if (!(to = ast_calloc(1, sizeof(*to))))
84 to->function = function;
86 to->thread = pthread_self();
88 pthread_mutex_lock(&threadstoragelock);
89 AST_LIST_INSERT_TAIL(&tls_objects, to, entry);
90 pthread_mutex_unlock(&threadstoragelock);
93 void __ast_threadstorage_object_remove(void *key)
95 struct tls_object *to;
97 pthread_mutex_lock(&threadstoragelock);
98 AST_LIST_TRAVERSE_SAFE_BEGIN(&tls_objects, to, entry) {
100 AST_LIST_REMOVE_CURRENT(entry);
104 AST_LIST_TRAVERSE_SAFE_END;
105 pthread_mutex_unlock(&threadstoragelock);
110 void __ast_threadstorage_object_replace(void *key_old, void *key_new, size_t len)
112 struct tls_object *to;
114 pthread_mutex_lock(&threadstoragelock);
115 AST_LIST_TRAVERSE_SAFE_BEGIN(&tls_objects, to, entry) {
116 if (to->key == key_old) {
122 AST_LIST_TRAVERSE_SAFE_END;
123 pthread_mutex_unlock(&threadstoragelock);
126 static char *handle_cli_threadstorage_show_allocations(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
128 const char *fn = NULL;
130 unsigned int count = 0;
131 struct tls_object *to;
135 e->command = "threadstorage show allocations";
137 "Usage: threadstorage show allocations [<file>]\n"
138 " Dumps a list of all thread-specific memory allocations,\n"
139 " optionally limited to those from a specific file\n";
146 return CLI_SHOWUSAGE;
151 pthread_mutex_lock(&threadstoragelock);
153 AST_LIST_TRAVERSE(&tls_objects, to, entry) {
154 if (fn && strcasecmp(to->file, fn))
157 ast_cli(a->fd, "%10d bytes allocated in %20s at line %5d of %25s (thread %p)\n",
158 (int) to->size, to->function, to->line, to->file, (void *) to->thread);
163 pthread_mutex_unlock(&threadstoragelock);
165 ast_cli(a->fd, "%10d bytes allocated in %d allocation%s\n", (int) len, count, count > 1 ? "s" : "");
170 static char *handle_cli_threadstorage_show_summary(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
172 const char *fn = NULL;
174 unsigned int count = 0;
175 struct tls_object *to;
180 AST_LIST_ENTRY(file) entry;
182 AST_LIST_HEAD_NOLOCK_STATIC(file_summary, file);
186 e->command = "threadstorage show summary";
188 "Usage: threadstorage show summary [<file>]\n"
189 " Summarizes thread-specific memory allocations by file, or optionally\n"
190 " by function, if a file is specified\n";
197 return CLI_SHOWUSAGE;
202 pthread_mutex_lock(&threadstoragelock);
204 AST_LIST_TRAVERSE(&tls_objects, to, entry) {
205 if (fn && strcasecmp(to->file, fn))
208 AST_LIST_TRAVERSE(&file_summary, file, entry) {
209 if ((!fn && (file->name == to->file)) || (fn && (file->name == to->function)))
214 file = alloca(sizeof(*file));
215 memset(file, 0, sizeof(*file));
216 file->name = fn ? to->function : to->file;
217 AST_LIST_INSERT_TAIL(&file_summary, file, entry);
220 file->len += to->size;
224 pthread_mutex_unlock(&threadstoragelock);
226 AST_LIST_TRAVERSE(&file_summary, file, entry) {
228 count += file->count;
230 ast_cli(a->fd, "%10d bytes in %d allocation%ss in function %s\n",
231 (int) file->len, file->count, file->count > 1 ? "s" : "", file->name);
233 ast_cli(a->fd, "%10d bytes in %d allocation%s in file %s\n",
234 (int) file->len, file->count, file->count > 1 ? "s" : "", file->name);
238 ast_cli(a->fd, "%10d bytes allocated in %d allocation%s\n", (int) len, count, count > 1 ? "s" : "");
243 static struct ast_cli_entry cli[] = {
244 AST_CLI_DEFINE(handle_cli_threadstorage_show_allocations, "Display outstanding thread local storage allocations"),
245 AST_CLI_DEFINE(handle_cli_threadstorage_show_summary, "Summarize outstanding memory allocations")
248 void threadstorage_init(void)
250 pthread_mutex_init(&threadstoragelock, NULL);
251 ast_cli_register_multiple(cli, ARRAY_LEN(cli));
254 #endif /* !defined(DEBUG_THREADLOCALS) */