forked from sba1/simplemail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.c
395 lines (329 loc) · 8.13 KB
/
debug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/***************************************************************************
SimpleMail - Copyright (C) 2000 Hynek Schlawack and Sebastian Bauer
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***************************************************************************/
/**
* @file debug.c
*/
#include "debug.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hash.h"
#include "lists.h"
#include "support_indep.h"
#include "archdebug.h"
#include "subthreads.h"
#include "support.h" /* sm_put_on_serial_line() */
#ifndef DEFAULT_DEBUG_LEVEL
#define DEFAULT_DEBUG_LEVEL 0
#endif
int __debuglevel = DEFAULT_DEBUG_LEVEL;
static semaphore_t debug_sem;
static FILE *debug_file;
/** Contains the names of modules of which debugging information should be considered */
static struct hash_table debug_modules_table;
/** Indicates whether table should be really used */
static int debug_use_modules_table;
/** List that keeps track of all resources */
static struct list tracking_list;
static int tracking_elements;
static int tracking_initialized;
static int inside_tracking;
struct tracked_resource
{
struct node node;
/** @brief the tracked resource. First part of the key. */
void *resource;
/** @brief the class of the tracked resource. Second part of the key */
char *cl;
char *args;
/** @brief the origin of the resource allocation */
char *filename;
char *function;
int line;
/** @brief architecture dependent debug information */
struct bt *bt;
};
/*******************************************************
Sets the debug level
********************************************************/
void debug_set_level(int ndl)
{
SM_DEBUGF(0,("Debuglevel set to %d\n",ndl));
__debuglevel = ndl;
}
/*******************************************************
Sets the debug output
********************************************************/
void debug_set_out(char *out)
{
if (debug_file) fclose(debug_file);
debug_file = fopen(out,"a+");
}
/*******************************************************
Private function. Used to insert a module.
********************************************************/
static void debug_insert_module(char *mod)
{
int len;
char *newmod;
len = strlen(mod);
if (len > 2)
{
if (mod[len-1]=='c' && mod[len-2]=='.')
hash_table_insert(&debug_modules_table,mod,1);
}
if ((newmod = (char*)malloc(len+3)))
{
strcpy(newmod,mod);
strcat(newmod,".c");
hash_table_insert(&debug_modules_table,newmod,1);
}
}
/*****************************************************************************/
/* Declare weak when using gcc so it can be replaced by the true list
* that is generated during compilation.
*/
#ifdef __GNUC__
#ifdef __cplusplus
extern
#endif
__attribute__((weak))
#endif
const char * const debugmodules[] =
{
NULL
};
const char * const *debug_get_loggable_modules(void)
{
return debugmodules;
}
/*****************************************************************************/
/**
* Sets the modules (modules is comma separated)
* @param modules
*/
void debug_set_modules(char *modules)
{
char *mod;
if ((mod = mystrdup(modules)))
{
char *begin = mod;
char *end;
hash_table_init(&debug_modules_table,9,NULL);
while ((end = strchr(begin,',')))
{
*end = 0;
debug_insert_module(begin);
begin = end+1;
}
debug_insert_module(begin);
debug_use_modules_table = 1;
}
}
/**
* Initialize debug sub system.
*
* @return
*/
int debug_init(void)
{
if (!(debug_sem = thread_create_semaphore()))
return 0;
list_init(&tracking_list);
tracking_initialized = 1;
return 1;
}
/**
* Deinitializes the debug subsystem.
*/
void debug_deinit(void)
{
if (tracking_initialized)
{
struct tracked_resource *tr;
int num = 0;
inside_tracking = 1;
if (tracking_elements > 0)
__debug_print("There were %d resources that got not freed!\n",tracking_elements);
tr = (struct tracked_resource*)list_last(&tracking_list);
while (tr && num < 20)
{
char *call = tr->cl;
char *more;
if (!call) call = tr->cl;
__debug_print("Resource %p of call %s(%s) not freed! Origin: %s/%d\n",tr->resource, tr->cl,tr->args,tr->filename,tr->line);
if ((more = arch_debug_bt2string(tr->bt)))
{
/* Print out architecture dependent string, but line for line */
char *lf;
char *str = more;
while ((lf = strchr(str,'\n')))
{
*lf = 0;
__debug_print(str);
__debug_print("\n");
str = lf + 1;
}
free(more);
}
tr = (struct tracked_resource*)node_prev(&tr->node);
num++;
}
}
thread_dispose_semaphore(debug_sem);
inside_tracking = 0;
}
/**
* Lookup the given resource for the given class.
*
* @param res
* @param cl
* @return the tracked resource or NULL if the resource couldn't be found.
*/
struct tracked_resource *debug_find_tracked_resource(void *res, char *cl)
{
struct tracked_resource *tr;
tr = (struct tracked_resource*)list_last(&tracking_list);
while (tr)
{
if (tr->resource == res)
{
if ((tr->cl == cl) ||
(tr->cl && cl && !strcmp(tr->cl,cl)))
{
return tr;
}
}
tr = (struct tracked_resource*)node_prev(&tr->node);
}
return NULL;
}
/**
* Track the resource.
*
* @param res
* @param cl the name of the tracked function.
* @param args to the function.
* @param filename
* @param function
* @param line
*/
void debug_track(void *res, char *cl, char *args, char *filename, char *function, int line)
{
if (tracking_initialized)
{
thread_lock_semaphore(debug_sem);
if (!inside_tracking)
{
struct tracked_resource *tr;
inside_tracking = 1;
if ((tr = (struct tracked_resource*)malloc(sizeof(*tr))))
{
tr->resource = res;
tr->cl = cl;
tr->args = strdup(args);
tr->filename = filename;
tr->function = function;
tr->line = line;
tr->bt = arch_debug_get_bt();
list_insert_tail(&tracking_list,&tr->node);
tracking_elements++;
}
inside_tracking = 0;
}
thread_unlock_semaphore(debug_sem);
}
}
/**
* Remove the given resource from the tracking list.
*
* @param res
* @param cl the name of the tracked function
* @param call
* @param args
* @param filename
* @param function
* @param line
*/
void debug_untrack(void *res, char *cl, char *call, char *args, char *filename, char *function, int line)
{
if (tracking_initialized)
{
thread_lock_semaphore(debug_sem);
if (!inside_tracking)
{
struct tracked_resource *tr;
inside_tracking = 1;
if ((tr = debug_find_tracked_resource(res,cl)))
{
node_remove(&tr->node);
arch_debug_free_bt(tr->bt);
free(tr->args);
free(tr);
tracking_elements--;
} else
{
__debug_print("Trying to untrack untracked resource at %p of class %s. The call was %s(%s) at %s/%d\n",res,cl?cl:"UNKNOWN",call,args,filename,line);
}
inside_tracking = 0;
}
thread_unlock_semaphore(debug_sem);
}
}
/**
* Check, whether we want to debug this.
*
* @param file
* @param line
* @return
*/
int debug_check(const char *file, int line)
{
if (!debug_use_modules_table)
return 1;
if (hash_table_lookup(&debug_modules_table,file))
return 1;
return 0;
}
/**
* Use this if debug output should be sequential
*/
void __debug_begin(void)
{
thread_lock_semaphore(debug_sem);
}
/**
* Use this after debugging output had been emitting.
*/
void __debug_end(void)
{
thread_unlock_semaphore(debug_sem);
}
/**
* Emits debugging
*
* @param fmt
*/
void __debug_print(const char *fmt, ...)
{
va_list ap;
char buf[512];
va_start(ap, fmt);
vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if (debug_file) fputs(buf,debug_file);
else sm_put_on_serial_line(buf);
}