blob: 3bb5309a6c21edd163920c420449da57c99efca5 [file] [log] [blame]
Holger Freyther32636e82008-12-27 11:07:15 +00001/* Debugging/Logging support code */
2/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
Holger Freytherd546e312008-12-27 12:03:07 +00003 * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org>
Holger Freyther32636e82008-12-27 11:07:15 +00004 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
22#include <stdarg.h>
Holger Freytherd546e312008-12-27 12:03:07 +000023#include <stdlib.h>
Holger Freyther32636e82008-12-27 11:07:15 +000024#include <stdio.h>
25#include <string.h>
Holger Freytherd546e312008-12-27 12:03:07 +000026#include <strings.h>
Holger Freyther32636e82008-12-27 11:07:15 +000027#include <time.h>
Harald Welte (local)b79bdd92009-12-26 19:45:22 +010028#include <errno.h>
Holger Freyther32636e82008-12-27 11:07:15 +000029
30#include <openbsc/debug.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010031#include <osmocore/talloc.h>
Harald Welte2e411c72010-03-01 21:59:06 +010032#include <osmocore/utils.h>
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +010033#include <openbsc/gsm_data.h>
34#include <openbsc/gsm_subscriber.h>
Holger Freyther32636e82008-12-27 11:07:15 +000035
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +010036/* default categories */
37static struct debug_category default_categories[Debug_LastEntry] = {
Harald Welte84105972009-12-24 13:48:33 +010038 [DRLL] = { .enabled = 1, .loglevel = LOGL_NOTICE },
39 [DCC] = { .enabled = 1, .loglevel = LOGL_NOTICE },
40 [DNM] = { .enabled = 1, .loglevel = LOGL_NOTICE },
41 [DRR] = { .enabled = 1, .loglevel = LOGL_NOTICE },
42 [DRSL] = { .enabled = 1, .loglevel = LOGL_NOTICE },
43 [DMM] = { .enabled = 1, .loglevel = LOGL_INFO },
44 [DMNCC] = { .enabled = 1, .loglevel = LOGL_NOTICE },
45 [DSMS] = { .enabled = 1, .loglevel = LOGL_NOTICE },
46 [DPAG] = { .enabled = 1, .loglevel = LOGL_NOTICE },
47 [DMEAS] = { .enabled = 0, .loglevel = LOGL_NOTICE },
48 [DMI] = { .enabled = 0, .loglevel = LOGL_NOTICE },
49 [DMIB] = { .enabled = 0, .loglevel = LOGL_NOTICE },
50 [DMUX] = { .enabled = 1, .loglevel = LOGL_NOTICE },
51 [DINP] = { .enabled = 1, .loglevel = LOGL_NOTICE },
52 [DSCCP] = { .enabled = 1, .loglevel = LOGL_NOTICE },
53 [DMSC] = { .enabled = 1, .loglevel = LOGL_NOTICE },
54 [DMGCP] = { .enabled = 1, .loglevel = LOGL_NOTICE },
55 [DHO] = { .enabled = 1, .loglevel = LOGL_NOTICE },
56 [DDB] = { .enabled = 1, .loglevel = LOGL_NOTICE },
57 [DREF] = { .enabled = 0, .loglevel = LOGL_NOTICE },
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +010058};
Holger Freyther32636e82008-12-27 11:07:15 +000059
Holger Freytherd546e312008-12-27 12:03:07 +000060struct debug_info {
61 const char *name;
62 const char *color;
63 const char *description;
64 int number;
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +010065 int position;
Holger Freytherd546e312008-12-27 12:03:07 +000066};
67
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +010068struct debug_context {
69 struct gsm_lchan *lchan;
70 struct gsm_subscriber *subscr;
71 struct gsm_bts *bts;
72};
73
74static struct debug_context debug_context;
75static void *tall_dbg_ctx = NULL;
76static LLIST_HEAD(target_list);
77
Holger Freytherd546e312008-12-27 12:03:07 +000078#define DEBUG_CATEGORY(NUMBER, NAME, COLOR, DESCRIPTION) \
79 { .name = NAME, .color = COLOR, .description = DESCRIPTION, .number = NUMBER },
80
Holger Freytherd546e312008-12-27 12:03:07 +000081static const struct debug_info debug_info[] = {
Holger Freyther7c03e4c2008-12-27 12:46:48 +000082 DEBUG_CATEGORY(DRLL, "DRLL", "\033[1;31m", "")
83 DEBUG_CATEGORY(DCC, "DCC", "\033[1;32m", "")
Holger Freyther3770b762009-06-02 02:35:12 +000084 DEBUG_CATEGORY(DMM, "DMM", "\033[1;33m", "")
Holger Freyther7c03e4c2008-12-27 12:46:48 +000085 DEBUG_CATEGORY(DRR, "DRR", "\033[1;34m", "")
Harald Weltec1d2aae2009-05-23 06:43:35 +000086 DEBUG_CATEGORY(DRSL, "DRSL", "\033[1;35m", "")
Holger Freyther7c03e4c2008-12-27 12:46:48 +000087 DEBUG_CATEGORY(DNM, "DNM", "\033[1;36m", "")
Daniel Willmann10d06f62008-12-28 21:38:25 +000088 DEBUG_CATEGORY(DSMS, "DSMS", "\033[1;37m", "")
Harald Welted35b6a72008-12-29 04:06:41 +000089 DEBUG_CATEGORY(DPAG, "DPAG", "\033[1;38m", "")
Harald Weltec125a682009-05-23 06:42:38 +000090 DEBUG_CATEGORY(DMNCC, "DMNCC","\033[1;39m", "")
Harald Welteedb37782009-05-01 14:59:07 +000091 DEBUG_CATEGORY(DINP, "DINP", "", "")
Harald Welteb60fa592009-02-06 12:02:53 +000092 DEBUG_CATEGORY(DMI, "DMI", "", "")
93 DEBUG_CATEGORY(DMIB, "DMIB", "", "")
Harald Welteba59baf2009-02-23 00:04:04 +000094 DEBUG_CATEGORY(DMUX, "DMUX", "", "")
Harald Welte10d0e672009-06-27 02:53:10 +020095 DEBUG_CATEGORY(DMEAS, "DMEAS", "", "")
Holger Hans Peter Freythered0a47b2009-08-01 16:54:45 +020096 DEBUG_CATEGORY(DSCCP, "DSCCP", "", "")
Holger Hans Peter Freyther32201c52009-08-18 12:54:50 +020097 DEBUG_CATEGORY(DMSC, "DMSC", "", "")
Holger Hans Peter Freytherff5fa4e2009-11-20 13:05:48 +010098 DEBUG_CATEGORY(DMGCP, "DMGCP", "", "")
Harald Welte8d77b952009-12-17 00:31:10 +010099 DEBUG_CATEGORY(DHO, "DHO", "", "")
Harald Welteae1f1592009-12-24 11:39:14 +0100100 DEBUG_CATEGORY(DDB, "DDB", "", "")
Holger Hans Peter Freyther8e9559d2010-03-22 07:49:12 +0100101 DEBUG_CATEGORY(DREF, "DREF", "", "")
Holger Freytherd546e312008-12-27 12:03:07 +0000102};
103
Harald Welte (local)b79bdd92009-12-26 19:45:22 +0100104static const struct value_string loglevel_strs[] = {
105 { 0, "EVERYTHING" },
106 { 1, "DEBUG" },
107 { 3, "INFO" },
108 { 5, "NOTICE" },
109 { 7, "ERROR" },
110 { 8, "FATAL" },
111 { 0, NULL },
112};
113
114int debug_parse_level(const char *lvl)
115{
116 return get_string_value(loglevel_strs, lvl);
117}
118
119int debug_parse_category(const char *category)
120{
121 int i;
122
123 for (i = 0; i < ARRAY_SIZE(debug_info); ++i) {
124 if (!strcasecmp(debug_info[i].name+1, category))
125 return debug_info[i].number;
126 }
127
128 return -EINVAL;
129}
130
Holger Freytherd546e312008-12-27 12:03:07 +0000131/*
132 * Parse the category mask.
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100133 * The format can be this: category1:category2:category3
134 * or category1,2:category2,3:...
Holger Freytherd546e312008-12-27 12:03:07 +0000135 */
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100136void debug_parse_category_mask(struct debug_target* target, const char *_mask)
Holger Freytherd546e312008-12-27 12:03:07 +0000137{
Holger Freytherd546e312008-12-27 12:03:07 +0000138 int i = 0;
139 char *mask = strdup(_mask);
140 char *category_token = NULL;
141
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100142 /* Disable everything to enable it afterwards */
143 for (i = 0; i < ARRAY_SIZE(target->categories); ++i)
144 target->categories[i].enabled = 0;
145
Holger Freytherd546e312008-12-27 12:03:07 +0000146 category_token = strtok(mask, ":");
147 do {
148 for (i = 0; i < ARRAY_SIZE(debug_info); ++i) {
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100149 char* colon = strstr(category_token, ",");
150 int length = strlen(category_token);
151
152 if (colon)
153 length = colon - category_token;
154
155 if (strncasecmp(debug_info[i].name, category_token, length) == 0) {
156 int number = debug_info[i].number;
157 int level = 0;
158
159 if (colon)
160 level = atoi(colon+1);
161
162 target->categories[number].enabled = 1;
163 target->categories[number].loglevel = level;
164 }
Holger Freytherd546e312008-12-27 12:03:07 +0000165 }
Holger Freytherca362a62009-01-04 21:05:01 +0000166 } while ((category_token = strtok(NULL, ":")));
Holger Freytherd546e312008-12-27 12:03:07 +0000167
Holger Freytherd546e312008-12-27 12:03:07 +0000168 free(mask);
Holger Freytherd546e312008-12-27 12:03:07 +0000169}
170
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100171static const char* color(int subsys)
Holger Freyther7c03e4c2008-12-27 12:46:48 +0000172{
173 int i = 0;
174
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100175 for (i = 0; i < ARRAY_SIZE(debug_info); ++i) {
Holger Freyther7c03e4c2008-12-27 12:46:48 +0000176 if (debug_info[i].number == subsys)
177 return debug_info[i].color;
178 }
179
180 return "";
181}
182
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100183static void _output(struct debug_target *target, unsigned int subsys, char *file, int line,
184 int cont, const char *format, va_list ap)
Holger Freyther32636e82008-12-27 11:07:15 +0000185{
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100186 char col[30];
187 char sub[30];
188 char tim[30];
189 char buf[4096];
190 char final[4096];
Holger Freyther32636e82008-12-27 11:07:15 +0000191
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100192 /* prepare the data */
193 col[0] = '\0';
194 sub[0] = '\0';
195 tim[0] = '\0';
196 buf[0] = '\0';
Holger Freyther32636e82008-12-27 11:07:15 +0000197
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100198 /* are we using color */
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100199 if (target->use_color) {
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100200 snprintf(col, sizeof(col), "%s", color(subsys));
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100201 col[sizeof(col)-1] = '\0';
202 }
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100203 vsnprintf(buf, sizeof(buf), format, ap);
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100204 buf[sizeof(buf)-1] = '\0';
Harald Welte6ddd1682009-02-06 12:38:29 +0000205
206 if (!cont) {
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100207 if (target->print_timestamp) {
Harald Welted3ff51d2009-06-09 20:21:57 +0000208 char *timestr;
209 time_t tm;
210 tm = time(NULL);
211 timestr = ctime(&tm);
212 timestr[strlen(timestr)-1] = '\0';
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100213 snprintf(tim, sizeof(tim), "%s ", timestr);
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100214 tim[sizeof(tim)-1] = '\0';
Harald Welted3ff51d2009-06-09 20:21:57 +0000215 }
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100216 snprintf(sub, sizeof(sub), "<%4.4x> %s:%d ", subsys, file, line);
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100217 sub[sizeof(sub)-1] = '\0';
Harald Welte6ddd1682009-02-06 12:38:29 +0000218 }
Holger Freyther32636e82008-12-27 11:07:15 +0000219
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100220 snprintf(final, sizeof(final), "%s%s%s%s\033[0;m", col, tim, sub, buf);
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100221 final[sizeof(final)-1] = '\0';
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100222 target->output(target, final);
223}
224
225
226static void _debugp(unsigned int subsys, int level, char *file, int line,
227 int cont, const char *format, va_list ap)
228{
229 struct debug_target *tar;
230
231 llist_for_each_entry(tar, &target_list, entry) {
232 struct debug_category *category;
233 int output = 0;
234
235 category = &tar->categories[subsys];
236 /* subsystem is not supposed to be debugged */
237 if (!category->enabled)
238 continue;
239
240 /* Check the global log level */
241 if (tar->loglevel != 0 && level < tar->loglevel)
242 continue;
243
244 /* Check the category log level */
245 if (category->loglevel != 0 && level < category->loglevel)
246 continue;
247
248 /*
249 * Apply filters here... if that becomes messy we will need to put
250 * filters in a list and each filter will say stop, continue, output
251 */
252 if ((tar->filter_map & DEBUG_FILTER_ALL) != 0) {
253 output = 1;
254 } else if ((tar->filter_map & DEBUG_FILTER_IMSI) != 0
255 && debug_context.subscr && strcmp(debug_context.subscr->imsi, tar->imsi_filter) == 0) {
256 output = 1;
257 }
258
Harald Welteaa8989c2009-12-24 11:12:11 +0100259 if (output) {
260 /* FIXME: copying the va_list is an ugly workaround against a bug
261 * hidden somewhere in _output. If we do not copy here, the first
262 * call to _output() will corrupt the va_list contents, and any
263 * further _output() calls with the same va_list will segfault */
264 va_list bp;
265 va_copy(bp, ap);
266 _output(tar, subsys, file, line, cont, format, bp);
Harald Welte7ed25292009-12-24 11:14:03 +0100267 va_end(bp);
Harald Welteaa8989c2009-12-24 11:12:11 +0100268 }
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100269 }
270}
271
272void debugp(unsigned int subsys, char *file, int line, int cont, const char *format, ...)
273{
274 va_list ap;
275
276 va_start(ap, format);
277 _debugp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Holger Freyther32636e82008-12-27 11:07:15 +0000278 va_end(ap);
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100279}
Holger Freyther32636e82008-12-27 11:07:15 +0000280
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100281void debugp2(unsigned int subsys, unsigned int level, char *file, int line, int cont, const char *format, ...)
282{
283 va_list ap;
284
285 va_start(ap, format);
286 _debugp(subsys, level, file, line, cont, format, ap);
287 va_end(ap);
Holger Freyther32636e82008-12-27 11:07:15 +0000288}
289
Harald Welte3cc4bf52009-02-28 13:08:01 +0000290static char hexd_buff[4096];
291
Holger Hans Peter Freythere78074e2009-08-20 13:16:26 +0200292char *hexdump(const unsigned char *buf, int len)
Holger Freytherca362a62009-01-04 21:05:01 +0000293{
294 int i;
Harald Welte3cc4bf52009-02-28 13:08:01 +0000295 char *cur = hexd_buff;
296
297 hexd_buff[0] = 0;
Holger Freytherca362a62009-01-04 21:05:01 +0000298 for (i = 0; i < len; i++) {
Harald Welte3cc4bf52009-02-28 13:08:01 +0000299 int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
300 int rc = snprintf(cur, len_remain, "%02x ", buf[i]);
301 if (rc <= 0)
302 break;
303 cur += rc;
Holger Freytherca362a62009-01-04 21:05:01 +0000304 }
Harald Welte3cc4bf52009-02-28 13:08:01 +0000305 hexd_buff[sizeof(hexd_buff)-1] = 0;
306 return hexd_buff;
Holger Freytherca362a62009-01-04 21:05:01 +0000307}
308
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100309
310
311void debug_add_target(struct debug_target *target)
312{
313 llist_add_tail(&target->entry, &target_list);
314}
315
316void debug_del_target(struct debug_target *target)
317{
318 llist_del(&target->entry);
319}
320
321void debug_reset_context(void)
322{
323 memset(&debug_context, 0, sizeof(debug_context));
324}
325
326/* currently we are not reffing these */
327void debug_set_context(int ctx, void *value)
328{
329 switch (ctx) {
330 case BSC_CTX_LCHAN:
331 debug_context.lchan = (struct gsm_lchan *) value;
332 break;
333 case BSC_CTX_SUBSCR:
334 debug_context.subscr = (struct gsm_subscriber *) value;
335 break;
336 case BSC_CTX_BTS:
337 debug_context.bts = (struct gsm_bts *) value;
338 break;
339 case BSC_CTX_SCCP:
340 break;
341 default:
342 break;
343 }
344}
345
346void debug_set_imsi_filter(struct debug_target *target, const char *imsi)
347{
348 if (imsi) {
349 target->filter_map |= DEBUG_FILTER_IMSI;
350 target->imsi_filter = talloc_strdup(target, imsi);
351 } else if (target->imsi_filter) {
352 target->filter_map &= ~DEBUG_FILTER_IMSI;
353 talloc_free(target->imsi_filter);
354 target->imsi_filter = NULL;
355 }
356}
357
358void debug_set_all_filter(struct debug_target *target, int all)
359{
360 if (all)
361 target->filter_map |= DEBUG_FILTER_ALL;
362 else
363 target->filter_map &= ~DEBUG_FILTER_ALL;
364}
365
366void debug_set_use_color(struct debug_target *target, int use_color)
367{
368 target->use_color = use_color;
369}
370
371void debug_set_print_timestamp(struct debug_target *target, int print_timestamp)
372{
373 target->print_timestamp = print_timestamp;
374}
375
376void debug_set_log_level(struct debug_target *target, int log_level)
377{
378 target->loglevel = log_level;
379}
380
381void debug_set_category_filter(struct debug_target *target, int category, int enable, int level)
382{
383 if (category >= Debug_LastEntry)
384 return;
385 target->categories[category].enabled = !!enable;
386 target->categories[category].loglevel = level;
387}
388
389static void _stderr_output(struct debug_target *target, const char *log)
390{
391 fprintf(target->tgt_stdout.out, "%s", log);
392 fflush(target->tgt_stdout.out);
393}
394
395struct debug_target *debug_target_create(void)
396{
397 struct debug_target *target;
398
399 target = talloc_zero(tall_dbg_ctx, struct debug_target);
400 if (!target)
401 return NULL;
402
403 INIT_LLIST_HEAD(&target->entry);
404 memcpy(target->categories, default_categories, sizeof(default_categories));
405 target->use_color = 1;
406 target->print_timestamp = 0;
407 target->loglevel = 0;
408 return target;
409}
410
411struct debug_target *debug_target_create_stderr(void)
412{
413 struct debug_target *target;
414
415 target = debug_target_create();
416 if (!target)
417 return NULL;
418
419 target->tgt_stdout.out = stderr;
420 target->output = _stderr_output;
421 return target;
422}
423
424void debug_init(void)
425{
426 tall_dbg_ctx = talloc_named_const(NULL, 1, "debug");
427}