Let ExtensionState resolve dynamic hints.
[asterisk/asterisk.git] / main / threadstorage.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Kevin P. Fleming <kpfleming@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 Debugging support for thread-local-storage objects
22  *
23  * \author Kevin P. Fleming <kpfleming@digium.com>
24  */
25
26 #include "asterisk.h"
27 #include "asterisk/_private.h"
28
29 #if !defined(DEBUG_THREADLOCALS)
30
31 void threadstorage_init(void)
32 {
33 }
34
35 #else /* !defined(DEBUG_THREADLOCALS) */
36
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38
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"
44
45 struct tls_object {
46         void *key;
47         size_t size;
48         const char *file;
49         const char *function;
50         unsigned int line;
51         pthread_t thread;
52         AST_LIST_ENTRY(tls_object) entry;
53 };
54
55 static AST_LIST_HEAD_NOLOCK_STATIC(tls_objects, tls_object);
56
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
63
64 /*!
65  * \brief lock for the tls_objects list
66  *
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.
71  */
72 static pthread_mutex_t threadstoragelock;
73
74 void __ast_threadstorage_object_add(void *key, size_t len, const char *file, const char *function, unsigned int line)
75 {
76         struct tls_object *to;
77
78         if (!(to = ast_calloc(1, sizeof(*to))))
79                 return;
80
81         to->key = key;
82         to->size = len;
83         to->file = file;
84         to->function = function;
85         to->line = line;
86         to->thread = pthread_self();
87
88         pthread_mutex_lock(&threadstoragelock);
89         AST_LIST_INSERT_TAIL(&tls_objects, to, entry);
90         pthread_mutex_unlock(&threadstoragelock);
91 }
92
93 void __ast_threadstorage_object_remove(void *key)
94 {
95         struct tls_object *to;
96
97         pthread_mutex_lock(&threadstoragelock);
98         AST_LIST_TRAVERSE_SAFE_BEGIN(&tls_objects, to, entry) {
99                 if (to->key == key) {
100                         AST_LIST_REMOVE_CURRENT(entry);
101                         break;
102                 }
103         }
104         AST_LIST_TRAVERSE_SAFE_END;
105         pthread_mutex_unlock(&threadstoragelock);
106         if (to)
107                 ast_free(to);
108 }
109
110 void __ast_threadstorage_object_replace(void *key_old, void *key_new, size_t len)
111 {
112         struct tls_object *to;
113
114         pthread_mutex_lock(&threadstoragelock);
115         AST_LIST_TRAVERSE_SAFE_BEGIN(&tls_objects, to, entry) {
116                 if (to->key == key_old) {
117                         to->key = key_new;
118                         to->size = len;
119                         break;
120                 }
121         }
122         AST_LIST_TRAVERSE_SAFE_END;
123         pthread_mutex_unlock(&threadstoragelock);
124 }
125
126 static char *handle_cli_threadstorage_show_allocations(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
127 {
128         const char *fn = NULL;
129         size_t len = 0;
130         unsigned int count = 0;
131         struct tls_object *to;
132
133         switch (cmd) {
134         case CLI_INIT:
135                 e->command = "threadstorage show allocations";
136                 e->usage =
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";
140                 return NULL;
141         case CLI_GENERATE:
142                 return NULL;
143         }
144
145         if (a->argc > 4)
146                 return CLI_SHOWUSAGE;
147
148         if (a->argc > 3)
149                 fn = a->argv[3];
150
151         pthread_mutex_lock(&threadstoragelock);
152
153         AST_LIST_TRAVERSE(&tls_objects, to, entry) {
154                 if (fn && strcasecmp(to->file, fn))
155                         continue;
156
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);
159                 len += to->size;
160                 count++;
161         }
162
163         pthread_mutex_unlock(&threadstoragelock);
164
165         ast_cli(a->fd, "%10d bytes allocated in %d allocation%s\n", (int) len, count, count > 1 ? "s" : "");
166         
167         return CLI_SUCCESS;
168 }
169
170 static char *handle_cli_threadstorage_show_summary(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
171 {
172         const char *fn = NULL;
173         size_t len = 0;
174         unsigned int count = 0;
175         struct tls_object *to;
176         struct file {
177                 const char *name;
178                 size_t len;
179                 unsigned int count;
180                 AST_LIST_ENTRY(file) entry;
181         } *file;
182         AST_LIST_HEAD_NOLOCK_STATIC(file_summary, file);
183
184         switch (cmd) {
185         case CLI_INIT:
186                 e->command = "threadstorage show summary";
187                 e->usage =
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";
191                 return NULL;
192         case CLI_GENERATE:
193                 return NULL;
194         }
195
196         if (a->argc > 4)
197                 return CLI_SHOWUSAGE;
198
199         if (a->argc > 3)
200                 fn = a->argv[3];
201
202         pthread_mutex_lock(&threadstoragelock);
203
204         AST_LIST_TRAVERSE(&tls_objects, to, entry) {
205                 if (fn && strcasecmp(to->file, fn))
206                         continue;
207
208                 AST_LIST_TRAVERSE(&file_summary, file, entry) {
209                         if ((!fn && (file->name == to->file)) || (fn && (file->name == to->function)))
210                                 break;
211                 }
212
213                 if (!file) {
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);
218                 }
219
220                 file->len += to->size;
221                 file->count++;
222         }
223
224         pthread_mutex_unlock(&threadstoragelock);
225         
226         AST_LIST_TRAVERSE(&file_summary, file, entry) {
227                 len += file->len;
228                 count += file->count;
229                 if (fn) {
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);
232                 } else {
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);
235                 }
236         }
237
238         ast_cli(a->fd, "%10d bytes allocated in %d allocation%s\n", (int) len, count, count > 1 ? "s" : "");
239
240         return CLI_SUCCESS;
241 }
242
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")
246 };
247
248 void threadstorage_init(void)
249 {
250         pthread_mutex_init(&threadstoragelock, NULL);
251         ast_cli_register_multiple(cli, ARRAY_LEN(cli));
252 }
253
254 #endif /* !defined(DEBUG_THREADLOCALS) */
255