blob: 6d4d1b28718f671fad01f755a6d4bf1417cd6cfc [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>
28
29#include <openbsc/debug.h>
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +010030#include <openbsc/talloc.h>
31#include <openbsc/gsm_data.h>
32#include <openbsc/gsm_subscriber.h>
Holger Freyther32636e82008-12-27 11:07:15 +000033
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +010034/* default categories */
35static struct debug_category default_categories[Debug_LastEntry] = {
36 [DRLL] = { .enabled = 1, .loglevel = 0},
37 [DCC] = { .enabled = 1, .loglevel = 0},
Harald Welte61cc3062009-12-23 23:03:57 +010038 [DNM] = { .enabled = 1, .loglevel = 0},
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +010039 [DRR] = { .enabled = 1, .loglevel = 0},
40 [DRSL] = { .enabled = 1, .loglevel = 0},
41 [DMM] = { .enabled = 1, .loglevel = 0},
42 [DMNCC] = { .enabled = 1, .loglevel = 0},
43 [DSMS] = { .enabled = 1, .loglevel = 0},
44 [DPAG] = { .enabled = 1, .loglevel = 0},
45 [DMEAS] = { .enabled = 0, .loglevel = 0},
46 [DMI] = { .enabled = 0, .loglevel = 0},
47 [DMIB] = { .enabled = 0, .loglevel = 0},
48 [DMUX] = { .enabled = 1, .loglevel = 0},
49 [DINP] = { .enabled = 1, .loglevel = 0},
50 [DSCCP] = { .enabled = 1, .loglevel = 0},
51 [DMSC] = { .enabled = 1, .loglevel = 0},
52 [DMGCP] = { .enabled = 1, .loglevel = 0},
53 [DHO] = { .enabled = 1, .loglevel = 0},
Harald Welteae1f1592009-12-24 11:39:14 +010054 [DDB] = { .enabled = 1, .loglevel = 0},
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +010055};
Holger Freyther32636e82008-12-27 11:07:15 +000056
Holger Freytherd546e312008-12-27 12:03:07 +000057struct debug_info {
58 const char *name;
59 const char *color;
60 const char *description;
61 int number;
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +010062 int position;
Holger Freytherd546e312008-12-27 12:03:07 +000063};
64
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +010065struct debug_context {
66 struct gsm_lchan *lchan;
67 struct gsm_subscriber *subscr;
68 struct gsm_bts *bts;
69};
70
71static struct debug_context debug_context;
72static void *tall_dbg_ctx = NULL;
73static LLIST_HEAD(target_list);
74
Holger Freytherd546e312008-12-27 12:03:07 +000075#define DEBUG_CATEGORY(NUMBER, NAME, COLOR, DESCRIPTION) \
76 { .name = NAME, .color = COLOR, .description = DESCRIPTION, .number = NUMBER },
77
Holger Freytherd546e312008-12-27 12:03:07 +000078static const struct debug_info debug_info[] = {
Holger Freyther7c03e4c2008-12-27 12:46:48 +000079 DEBUG_CATEGORY(DRLL, "DRLL", "\033[1;31m", "")
80 DEBUG_CATEGORY(DCC, "DCC", "\033[1;32m", "")
Holger Freyther3770b762009-06-02 02:35:12 +000081 DEBUG_CATEGORY(DMM, "DMM", "\033[1;33m", "")
Holger Freyther7c03e4c2008-12-27 12:46:48 +000082 DEBUG_CATEGORY(DRR, "DRR", "\033[1;34m", "")
Harald Weltec1d2aae2009-05-23 06:43:35 +000083 DEBUG_CATEGORY(DRSL, "DRSL", "\033[1;35m", "")
Holger Freyther7c03e4c2008-12-27 12:46:48 +000084 DEBUG_CATEGORY(DNM, "DNM", "\033[1;36m", "")
Daniel Willmann10d06f62008-12-28 21:38:25 +000085 DEBUG_CATEGORY(DSMS, "DSMS", "\033[1;37m", "")
Harald Welted35b6a72008-12-29 04:06:41 +000086 DEBUG_CATEGORY(DPAG, "DPAG", "\033[1;38m", "")
Harald Weltec125a682009-05-23 06:42:38 +000087 DEBUG_CATEGORY(DMNCC, "DMNCC","\033[1;39m", "")
Harald Welteedb37782009-05-01 14:59:07 +000088 DEBUG_CATEGORY(DINP, "DINP", "", "")
Harald Welteb60fa592009-02-06 12:02:53 +000089 DEBUG_CATEGORY(DMI, "DMI", "", "")
90 DEBUG_CATEGORY(DMIB, "DMIB", "", "")
Harald Welteba59baf2009-02-23 00:04:04 +000091 DEBUG_CATEGORY(DMUX, "DMUX", "", "")
Harald Welte10d0e672009-06-27 02:53:10 +020092 DEBUG_CATEGORY(DMEAS, "DMEAS", "", "")
Holger Hans Peter Freythered0a47b2009-08-01 16:54:45 +020093 DEBUG_CATEGORY(DSCCP, "DSCCP", "", "")
Holger Hans Peter Freyther32201c52009-08-18 12:54:50 +020094 DEBUG_CATEGORY(DMSC, "DMSC", "", "")
Holger Hans Peter Freytherff5fa4e2009-11-20 13:05:48 +010095 DEBUG_CATEGORY(DMGCP, "DMGCP", "", "")
Harald Welte8d77b952009-12-17 00:31:10 +010096 DEBUG_CATEGORY(DHO, "DHO", "", "")
Harald Welteae1f1592009-12-24 11:39:14 +010097 DEBUG_CATEGORY(DDB, "DDB", "", "")
Holger Freytherd546e312008-12-27 12:03:07 +000098};
99
100/*
101 * Parse the category mask.
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100102 * The format can be this: category1:category2:category3
103 * or category1,2:category2,3:...
Holger Freytherd546e312008-12-27 12:03:07 +0000104 */
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100105void debug_parse_category_mask(struct debug_target* target, const char *_mask)
Holger Freytherd546e312008-12-27 12:03:07 +0000106{
Holger Freytherd546e312008-12-27 12:03:07 +0000107 int i = 0;
108 char *mask = strdup(_mask);
109 char *category_token = NULL;
110
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100111 /* Disable everything to enable it afterwards */
112 for (i = 0; i < ARRAY_SIZE(target->categories); ++i)
113 target->categories[i].enabled = 0;
114
Holger Freytherd546e312008-12-27 12:03:07 +0000115 category_token = strtok(mask, ":");
116 do {
117 for (i = 0; i < ARRAY_SIZE(debug_info); ++i) {
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100118 char* colon = strstr(category_token, ",");
119 int length = strlen(category_token);
120
121 if (colon)
122 length = colon - category_token;
123
124 if (strncasecmp(debug_info[i].name, category_token, length) == 0) {
125 int number = debug_info[i].number;
126 int level = 0;
127
128 if (colon)
129 level = atoi(colon+1);
130
131 target->categories[number].enabled = 1;
132 target->categories[number].loglevel = level;
133 }
Holger Freytherd546e312008-12-27 12:03:07 +0000134 }
Holger Freytherca362a62009-01-04 21:05:01 +0000135 } while ((category_token = strtok(NULL, ":")));
Holger Freytherd546e312008-12-27 12:03:07 +0000136
Holger Freytherd546e312008-12-27 12:03:07 +0000137 free(mask);
Holger Freytherd546e312008-12-27 12:03:07 +0000138}
139
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100140static const char* color(int subsys)
Holger Freyther7c03e4c2008-12-27 12:46:48 +0000141{
142 int i = 0;
143
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100144 for (i = 0; i < ARRAY_SIZE(debug_info); ++i) {
Holger Freyther7c03e4c2008-12-27 12:46:48 +0000145 if (debug_info[i].number == subsys)
146 return debug_info[i].color;
147 }
148
149 return "";
150}
151
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100152static void _output(struct debug_target *target, unsigned int subsys, char *file, int line,
153 int cont, const char *format, va_list ap)
Holger Freyther32636e82008-12-27 11:07:15 +0000154{
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100155 char col[30];
156 char sub[30];
157 char tim[30];
158 char buf[4096];
159 char final[4096];
Holger Freyther32636e82008-12-27 11:07:15 +0000160
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100161 /* prepare the data */
162 col[0] = '\0';
163 sub[0] = '\0';
164 tim[0] = '\0';
165 buf[0] = '\0';
Holger Freyther32636e82008-12-27 11:07:15 +0000166
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100167 /* are we using color */
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100168 if (target->use_color) {
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100169 snprintf(col, sizeof(col), "%s", color(subsys));
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100170 col[sizeof(col)-1] = '\0';
171 }
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100172 vsnprintf(buf, sizeof(buf), format, ap);
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100173 buf[sizeof(buf)-1] = '\0';
Harald Welte6ddd1682009-02-06 12:38:29 +0000174
175 if (!cont) {
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100176 if (target->print_timestamp) {
Harald Welted3ff51d2009-06-09 20:21:57 +0000177 char *timestr;
178 time_t tm;
179 tm = time(NULL);
180 timestr = ctime(&tm);
181 timestr[strlen(timestr)-1] = '\0';
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100182 snprintf(tim, sizeof(tim), "%s ", timestr);
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100183 tim[sizeof(tim)-1] = '\0';
Harald Welted3ff51d2009-06-09 20:21:57 +0000184 }
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100185 snprintf(sub, sizeof(sub), "<%4.4x> %s:%d ", subsys, file, line);
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100186 sub[sizeof(sub)-1] = '\0';
Harald Welte6ddd1682009-02-06 12:38:29 +0000187 }
Holger Freyther32636e82008-12-27 11:07:15 +0000188
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100189 snprintf(final, sizeof(final), "%s%s%s%s\033[0;m", col, tim, sub, buf);
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100190 final[sizeof(final)-1] = '\0';
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100191 target->output(target, final);
192}
193
194
195static void _debugp(unsigned int subsys, int level, char *file, int line,
196 int cont, const char *format, va_list ap)
197{
198 struct debug_target *tar;
199
200 llist_for_each_entry(tar, &target_list, entry) {
201 struct debug_category *category;
202 int output = 0;
203
204 category = &tar->categories[subsys];
205 /* subsystem is not supposed to be debugged */
206 if (!category->enabled)
207 continue;
208
209 /* Check the global log level */
210 if (tar->loglevel != 0 && level < tar->loglevel)
211 continue;
212
213 /* Check the category log level */
214 if (category->loglevel != 0 && level < category->loglevel)
215 continue;
216
217 /*
218 * Apply filters here... if that becomes messy we will need to put
219 * filters in a list and each filter will say stop, continue, output
220 */
221 if ((tar->filter_map & DEBUG_FILTER_ALL) != 0) {
222 output = 1;
223 } else if ((tar->filter_map & DEBUG_FILTER_IMSI) != 0
224 && debug_context.subscr && strcmp(debug_context.subscr->imsi, tar->imsi_filter) == 0) {
225 output = 1;
226 }
227
Harald Welteaa8989c2009-12-24 11:12:11 +0100228 if (output) {
229 /* FIXME: copying the va_list is an ugly workaround against a bug
230 * hidden somewhere in _output. If we do not copy here, the first
231 * call to _output() will corrupt the va_list contents, and any
232 * further _output() calls with the same va_list will segfault */
233 va_list bp;
234 va_copy(bp, ap);
235 _output(tar, subsys, file, line, cont, format, bp);
Harald Welte7ed25292009-12-24 11:14:03 +0100236 va_end(bp);
Harald Welteaa8989c2009-12-24 11:12:11 +0100237 }
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100238 }
239}
240
241void debugp(unsigned int subsys, char *file, int line, int cont, const char *format, ...)
242{
243 va_list ap;
244
245 va_start(ap, format);
246 _debugp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Holger Freyther32636e82008-12-27 11:07:15 +0000247 va_end(ap);
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100248}
Holger Freyther32636e82008-12-27 11:07:15 +0000249
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100250void debugp2(unsigned int subsys, unsigned int level, char *file, int line, int cont, const char *format, ...)
251{
252 va_list ap;
253
254 va_start(ap, format);
255 _debugp(subsys, level, file, line, cont, format, ap);
256 va_end(ap);
Holger Freyther32636e82008-12-27 11:07:15 +0000257}
258
Harald Welte3cc4bf52009-02-28 13:08:01 +0000259static char hexd_buff[4096];
260
Holger Hans Peter Freythere78074e2009-08-20 13:16:26 +0200261char *hexdump(const unsigned char *buf, int len)
Holger Freytherca362a62009-01-04 21:05:01 +0000262{
263 int i;
Harald Welte3cc4bf52009-02-28 13:08:01 +0000264 char *cur = hexd_buff;
265
266 hexd_buff[0] = 0;
Holger Freytherca362a62009-01-04 21:05:01 +0000267 for (i = 0; i < len; i++) {
Harald Welte3cc4bf52009-02-28 13:08:01 +0000268 int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
269 int rc = snprintf(cur, len_remain, "%02x ", buf[i]);
270 if (rc <= 0)
271 break;
272 cur += rc;
Holger Freytherca362a62009-01-04 21:05:01 +0000273 }
Harald Welte3cc4bf52009-02-28 13:08:01 +0000274 hexd_buff[sizeof(hexd_buff)-1] = 0;
275 return hexd_buff;
Holger Freytherca362a62009-01-04 21:05:01 +0000276}
277
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100278
279
280void debug_add_target(struct debug_target *target)
281{
282 llist_add_tail(&target->entry, &target_list);
283}
284
285void debug_del_target(struct debug_target *target)
286{
287 llist_del(&target->entry);
288}
289
290void debug_reset_context(void)
291{
292 memset(&debug_context, 0, sizeof(debug_context));
293}
294
295/* currently we are not reffing these */
296void debug_set_context(int ctx, void *value)
297{
298 switch (ctx) {
299 case BSC_CTX_LCHAN:
300 debug_context.lchan = (struct gsm_lchan *) value;
301 break;
302 case BSC_CTX_SUBSCR:
303 debug_context.subscr = (struct gsm_subscriber *) value;
304 break;
305 case BSC_CTX_BTS:
306 debug_context.bts = (struct gsm_bts *) value;
307 break;
308 case BSC_CTX_SCCP:
309 break;
310 default:
311 break;
312 }
313}
314
315void debug_set_imsi_filter(struct debug_target *target, const char *imsi)
316{
317 if (imsi) {
318 target->filter_map |= DEBUG_FILTER_IMSI;
319 target->imsi_filter = talloc_strdup(target, imsi);
320 } else if (target->imsi_filter) {
321 target->filter_map &= ~DEBUG_FILTER_IMSI;
322 talloc_free(target->imsi_filter);
323 target->imsi_filter = NULL;
324 }
325}
326
327void debug_set_all_filter(struct debug_target *target, int all)
328{
329 if (all)
330 target->filter_map |= DEBUG_FILTER_ALL;
331 else
332 target->filter_map &= ~DEBUG_FILTER_ALL;
333}
334
335void debug_set_use_color(struct debug_target *target, int use_color)
336{
337 target->use_color = use_color;
338}
339
340void debug_set_print_timestamp(struct debug_target *target, int print_timestamp)
341{
342 target->print_timestamp = print_timestamp;
343}
344
345void debug_set_log_level(struct debug_target *target, int log_level)
346{
347 target->loglevel = log_level;
348}
349
350void debug_set_category_filter(struct debug_target *target, int category, int enable, int level)
351{
352 if (category >= Debug_LastEntry)
353 return;
354 target->categories[category].enabled = !!enable;
355 target->categories[category].loglevel = level;
356}
357
358static void _stderr_output(struct debug_target *target, const char *log)
359{
360 fprintf(target->tgt_stdout.out, "%s", log);
361 fflush(target->tgt_stdout.out);
362}
363
364struct debug_target *debug_target_create(void)
365{
366 struct debug_target *target;
367
368 target = talloc_zero(tall_dbg_ctx, struct debug_target);
369 if (!target)
370 return NULL;
371
372 INIT_LLIST_HEAD(&target->entry);
373 memcpy(target->categories, default_categories, sizeof(default_categories));
374 target->use_color = 1;
375 target->print_timestamp = 0;
376 target->loglevel = 0;
377 return target;
378}
379
380struct debug_target *debug_target_create_stderr(void)
381{
382 struct debug_target *target;
383
384 target = debug_target_create();
385 if (!target)
386 return NULL;
387
388 target->tgt_stdout.out = stderr;
389 target->output = _stderr_output;
390 return target;
391}
392
393void debug_init(void)
394{
395 tall_dbg_ctx = talloc_named_const(NULL, 1, "debug");
396}