blob: 1aad1336d22268c47ec83131de900d25b216c71e [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] = {
Harald Welte84105972009-12-24 13:48:33 +010036 [DRLL] = { .enabled = 1, .loglevel = LOGL_NOTICE },
37 [DCC] = { .enabled = 1, .loglevel = LOGL_NOTICE },
38 [DNM] = { .enabled = 1, .loglevel = LOGL_NOTICE },
39 [DRR] = { .enabled = 1, .loglevel = LOGL_NOTICE },
40 [DRSL] = { .enabled = 1, .loglevel = LOGL_NOTICE },
41 [DMM] = { .enabled = 1, .loglevel = LOGL_INFO },
42 [DMNCC] = { .enabled = 1, .loglevel = LOGL_NOTICE },
43 [DSMS] = { .enabled = 1, .loglevel = LOGL_NOTICE },
44 [DPAG] = { .enabled = 1, .loglevel = LOGL_NOTICE },
45 [DMEAS] = { .enabled = 0, .loglevel = LOGL_NOTICE },
46 [DMI] = { .enabled = 0, .loglevel = LOGL_NOTICE },
47 [DMIB] = { .enabled = 0, .loglevel = LOGL_NOTICE },
48 [DMUX] = { .enabled = 1, .loglevel = LOGL_NOTICE },
49 [DINP] = { .enabled = 1, .loglevel = LOGL_NOTICE },
50 [DSCCP] = { .enabled = 1, .loglevel = LOGL_NOTICE },
51 [DMSC] = { .enabled = 1, .loglevel = LOGL_NOTICE },
52 [DMGCP] = { .enabled = 1, .loglevel = LOGL_NOTICE },
53 [DHO] = { .enabled = 1, .loglevel = LOGL_NOTICE },
54 [DDB] = { .enabled = 1, .loglevel = LOGL_NOTICE },
55 [DREF] = { .enabled = 0, .loglevel = LOGL_NOTICE },
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +010056};
Holger Freyther32636e82008-12-27 11:07:15 +000057
Holger Freytherd546e312008-12-27 12:03:07 +000058struct debug_info {
59 const char *name;
60 const char *color;
61 const char *description;
62 int number;
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +010063 int position;
Holger Freytherd546e312008-12-27 12:03:07 +000064};
65
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +010066struct debug_context {
67 struct gsm_lchan *lchan;
68 struct gsm_subscriber *subscr;
69 struct gsm_bts *bts;
70};
71
72static struct debug_context debug_context;
73static void *tall_dbg_ctx = NULL;
74static LLIST_HEAD(target_list);
75
Holger Freytherd546e312008-12-27 12:03:07 +000076#define DEBUG_CATEGORY(NUMBER, NAME, COLOR, DESCRIPTION) \
77 { .name = NAME, .color = COLOR, .description = DESCRIPTION, .number = NUMBER },
78
Holger Freytherd546e312008-12-27 12:03:07 +000079static const struct debug_info debug_info[] = {
Holger Freyther7c03e4c2008-12-27 12:46:48 +000080 DEBUG_CATEGORY(DRLL, "DRLL", "\033[1;31m", "")
81 DEBUG_CATEGORY(DCC, "DCC", "\033[1;32m", "")
Holger Freyther3770b762009-06-02 02:35:12 +000082 DEBUG_CATEGORY(DMM, "DMM", "\033[1;33m", "")
Holger Freyther7c03e4c2008-12-27 12:46:48 +000083 DEBUG_CATEGORY(DRR, "DRR", "\033[1;34m", "")
Harald Weltec1d2aae2009-05-23 06:43:35 +000084 DEBUG_CATEGORY(DRSL, "DRSL", "\033[1;35m", "")
Holger Freyther7c03e4c2008-12-27 12:46:48 +000085 DEBUG_CATEGORY(DNM, "DNM", "\033[1;36m", "")
Daniel Willmann10d06f62008-12-28 21:38:25 +000086 DEBUG_CATEGORY(DSMS, "DSMS", "\033[1;37m", "")
Harald Welted35b6a72008-12-29 04:06:41 +000087 DEBUG_CATEGORY(DPAG, "DPAG", "\033[1;38m", "")
Harald Weltec125a682009-05-23 06:42:38 +000088 DEBUG_CATEGORY(DMNCC, "DMNCC","\033[1;39m", "")
Harald Welteedb37782009-05-01 14:59:07 +000089 DEBUG_CATEGORY(DINP, "DINP", "", "")
Harald Welteb60fa592009-02-06 12:02:53 +000090 DEBUG_CATEGORY(DMI, "DMI", "", "")
91 DEBUG_CATEGORY(DMIB, "DMIB", "", "")
Harald Welteba59baf2009-02-23 00:04:04 +000092 DEBUG_CATEGORY(DMUX, "DMUX", "", "")
Harald Welte10d0e672009-06-27 02:53:10 +020093 DEBUG_CATEGORY(DMEAS, "DMEAS", "", "")
Holger Hans Peter Freythered0a47b2009-08-01 16:54:45 +020094 DEBUG_CATEGORY(DSCCP, "DSCCP", "", "")
Holger Hans Peter Freyther32201c52009-08-18 12:54:50 +020095 DEBUG_CATEGORY(DMSC, "DMSC", "", "")
Holger Hans Peter Freytherff5fa4e2009-11-20 13:05:48 +010096 DEBUG_CATEGORY(DMGCP, "DMGCP", "", "")
Harald Welte8d77b952009-12-17 00:31:10 +010097 DEBUG_CATEGORY(DHO, "DHO", "", "")
Harald Welteae1f1592009-12-24 11:39:14 +010098 DEBUG_CATEGORY(DDB, "DDB", "", "")
Harald Welted0c19142009-12-24 11:46:44 +010099 DEBUG_CATEGORY(DDB, "DREF", "", "")
Holger Freytherd546e312008-12-27 12:03:07 +0000100};
101
102/*
103 * Parse the category mask.
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100104 * The format can be this: category1:category2:category3
105 * or category1,2:category2,3:...
Holger Freytherd546e312008-12-27 12:03:07 +0000106 */
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100107void debug_parse_category_mask(struct debug_target* target, const char *_mask)
Holger Freytherd546e312008-12-27 12:03:07 +0000108{
Holger Freytherd546e312008-12-27 12:03:07 +0000109 int i = 0;
110 char *mask = strdup(_mask);
111 char *category_token = NULL;
112
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100113 /* Disable everything to enable it afterwards */
114 for (i = 0; i < ARRAY_SIZE(target->categories); ++i)
115 target->categories[i].enabled = 0;
116
Holger Freytherd546e312008-12-27 12:03:07 +0000117 category_token = strtok(mask, ":");
118 do {
119 for (i = 0; i < ARRAY_SIZE(debug_info); ++i) {
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100120 char* colon = strstr(category_token, ",");
121 int length = strlen(category_token);
122
123 if (colon)
124 length = colon - category_token;
125
126 if (strncasecmp(debug_info[i].name, category_token, length) == 0) {
127 int number = debug_info[i].number;
128 int level = 0;
129
130 if (colon)
131 level = atoi(colon+1);
132
133 target->categories[number].enabled = 1;
134 target->categories[number].loglevel = level;
135 }
Holger Freytherd546e312008-12-27 12:03:07 +0000136 }
Holger Freytherca362a62009-01-04 21:05:01 +0000137 } while ((category_token = strtok(NULL, ":")));
Holger Freytherd546e312008-12-27 12:03:07 +0000138
Holger Freytherd546e312008-12-27 12:03:07 +0000139 free(mask);
Holger Freytherd546e312008-12-27 12:03:07 +0000140}
141
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100142static const char* color(int subsys)
Holger Freyther7c03e4c2008-12-27 12:46:48 +0000143{
144 int i = 0;
145
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100146 for (i = 0; i < ARRAY_SIZE(debug_info); ++i) {
Holger Freyther7c03e4c2008-12-27 12:46:48 +0000147 if (debug_info[i].number == subsys)
148 return debug_info[i].color;
149 }
150
151 return "";
152}
153
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100154static void _output(struct debug_target *target, unsigned int subsys, char *file, int line,
155 int cont, const char *format, va_list ap)
Holger Freyther32636e82008-12-27 11:07:15 +0000156{
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100157 char col[30];
158 char sub[30];
159 char tim[30];
160 char buf[4096];
161 char final[4096];
Holger Freyther32636e82008-12-27 11:07:15 +0000162
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100163 /* prepare the data */
164 col[0] = '\0';
165 sub[0] = '\0';
166 tim[0] = '\0';
167 buf[0] = '\0';
Holger Freyther32636e82008-12-27 11:07:15 +0000168
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100169 /* are we using color */
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100170 if (target->use_color) {
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100171 snprintf(col, sizeof(col), "%s", color(subsys));
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100172 col[sizeof(col)-1] = '\0';
173 }
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100174 vsnprintf(buf, sizeof(buf), format, ap);
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100175 buf[sizeof(buf)-1] = '\0';
Harald Welte6ddd1682009-02-06 12:38:29 +0000176
177 if (!cont) {
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100178 if (target->print_timestamp) {
Harald Welted3ff51d2009-06-09 20:21:57 +0000179 char *timestr;
180 time_t tm;
181 tm = time(NULL);
182 timestr = ctime(&tm);
183 timestr[strlen(timestr)-1] = '\0';
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100184 snprintf(tim, sizeof(tim), "%s ", timestr);
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100185 tim[sizeof(tim)-1] = '\0';
Harald Welted3ff51d2009-06-09 20:21:57 +0000186 }
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100187 snprintf(sub, sizeof(sub), "<%4.4x> %s:%d ", subsys, file, line);
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100188 sub[sizeof(sub)-1] = '\0';
Harald Welte6ddd1682009-02-06 12:38:29 +0000189 }
Holger Freyther32636e82008-12-27 11:07:15 +0000190
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100191 snprintf(final, sizeof(final), "%s%s%s%s\033[0;m", col, tim, sub, buf);
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100192 final[sizeof(final)-1] = '\0';
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100193 target->output(target, final);
194}
195
196
197static void _debugp(unsigned int subsys, int level, char *file, int line,
198 int cont, const char *format, va_list ap)
199{
200 struct debug_target *tar;
201
202 llist_for_each_entry(tar, &target_list, entry) {
203 struct debug_category *category;
204 int output = 0;
205
206 category = &tar->categories[subsys];
207 /* subsystem is not supposed to be debugged */
208 if (!category->enabled)
209 continue;
210
211 /* Check the global log level */
212 if (tar->loglevel != 0 && level < tar->loglevel)
213 continue;
214
215 /* Check the category log level */
216 if (category->loglevel != 0 && level < category->loglevel)
217 continue;
218
219 /*
220 * Apply filters here... if that becomes messy we will need to put
221 * filters in a list and each filter will say stop, continue, output
222 */
223 if ((tar->filter_map & DEBUG_FILTER_ALL) != 0) {
224 output = 1;
225 } else if ((tar->filter_map & DEBUG_FILTER_IMSI) != 0
226 && debug_context.subscr && strcmp(debug_context.subscr->imsi, tar->imsi_filter) == 0) {
227 output = 1;
228 }
229
Harald Welteaa8989c2009-12-24 11:12:11 +0100230 if (output) {
231 /* FIXME: copying the va_list is an ugly workaround against a bug
232 * hidden somewhere in _output. If we do not copy here, the first
233 * call to _output() will corrupt the va_list contents, and any
234 * further _output() calls with the same va_list will segfault */
235 va_list bp;
236 va_copy(bp, ap);
237 _output(tar, subsys, file, line, cont, format, bp);
Harald Welte7ed25292009-12-24 11:14:03 +0100238 va_end(bp);
Harald Welteaa8989c2009-12-24 11:12:11 +0100239 }
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100240 }
241}
242
243void debugp(unsigned int subsys, char *file, int line, int cont, const char *format, ...)
244{
245 va_list ap;
246
247 va_start(ap, format);
248 _debugp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Holger Freyther32636e82008-12-27 11:07:15 +0000249 va_end(ap);
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100250}
Holger Freyther32636e82008-12-27 11:07:15 +0000251
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100252void debugp2(unsigned int subsys, unsigned int level, char *file, int line, int cont, const char *format, ...)
253{
254 va_list ap;
255
256 va_start(ap, format);
257 _debugp(subsys, level, file, line, cont, format, ap);
258 va_end(ap);
Holger Freyther32636e82008-12-27 11:07:15 +0000259}
260
Harald Welte3cc4bf52009-02-28 13:08:01 +0000261static char hexd_buff[4096];
262
Holger Hans Peter Freythere78074e2009-08-20 13:16:26 +0200263char *hexdump(const unsigned char *buf, int len)
Holger Freytherca362a62009-01-04 21:05:01 +0000264{
265 int i;
Harald Welte3cc4bf52009-02-28 13:08:01 +0000266 char *cur = hexd_buff;
267
268 hexd_buff[0] = 0;
Holger Freytherca362a62009-01-04 21:05:01 +0000269 for (i = 0; i < len; i++) {
Harald Welte3cc4bf52009-02-28 13:08:01 +0000270 int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
271 int rc = snprintf(cur, len_remain, "%02x ", buf[i]);
272 if (rc <= 0)
273 break;
274 cur += rc;
Holger Freytherca362a62009-01-04 21:05:01 +0000275 }
Harald Welte3cc4bf52009-02-28 13:08:01 +0000276 hexd_buff[sizeof(hexd_buff)-1] = 0;
277 return hexd_buff;
Holger Freytherca362a62009-01-04 21:05:01 +0000278}
279
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100280
281
282void debug_add_target(struct debug_target *target)
283{
284 llist_add_tail(&target->entry, &target_list);
285}
286
287void debug_del_target(struct debug_target *target)
288{
289 llist_del(&target->entry);
290}
291
292void debug_reset_context(void)
293{
294 memset(&debug_context, 0, sizeof(debug_context));
295}
296
297/* currently we are not reffing these */
298void debug_set_context(int ctx, void *value)
299{
300 switch (ctx) {
301 case BSC_CTX_LCHAN:
302 debug_context.lchan = (struct gsm_lchan *) value;
303 break;
304 case BSC_CTX_SUBSCR:
305 debug_context.subscr = (struct gsm_subscriber *) value;
306 break;
307 case BSC_CTX_BTS:
308 debug_context.bts = (struct gsm_bts *) value;
309 break;
310 case BSC_CTX_SCCP:
311 break;
312 default:
313 break;
314 }
315}
316
317void debug_set_imsi_filter(struct debug_target *target, const char *imsi)
318{
319 if (imsi) {
320 target->filter_map |= DEBUG_FILTER_IMSI;
321 target->imsi_filter = talloc_strdup(target, imsi);
322 } else if (target->imsi_filter) {
323 target->filter_map &= ~DEBUG_FILTER_IMSI;
324 talloc_free(target->imsi_filter);
325 target->imsi_filter = NULL;
326 }
327}
328
329void debug_set_all_filter(struct debug_target *target, int all)
330{
331 if (all)
332 target->filter_map |= DEBUG_FILTER_ALL;
333 else
334 target->filter_map &= ~DEBUG_FILTER_ALL;
335}
336
337void debug_set_use_color(struct debug_target *target, int use_color)
338{
339 target->use_color = use_color;
340}
341
342void debug_set_print_timestamp(struct debug_target *target, int print_timestamp)
343{
344 target->print_timestamp = print_timestamp;
345}
346
347void debug_set_log_level(struct debug_target *target, int log_level)
348{
349 target->loglevel = log_level;
350}
351
352void debug_set_category_filter(struct debug_target *target, int category, int enable, int level)
353{
354 if (category >= Debug_LastEntry)
355 return;
356 target->categories[category].enabled = !!enable;
357 target->categories[category].loglevel = level;
358}
359
360static void _stderr_output(struct debug_target *target, const char *log)
361{
362 fprintf(target->tgt_stdout.out, "%s", log);
363 fflush(target->tgt_stdout.out);
364}
365
366struct debug_target *debug_target_create(void)
367{
368 struct debug_target *target;
369
370 target = talloc_zero(tall_dbg_ctx, struct debug_target);
371 if (!target)
372 return NULL;
373
374 INIT_LLIST_HEAD(&target->entry);
375 memcpy(target->categories, default_categories, sizeof(default_categories));
376 target->use_color = 1;
377 target->print_timestamp = 0;
378 target->loglevel = 0;
379 return target;
380}
381
382struct debug_target *debug_target_create_stderr(void)
383{
384 struct debug_target *target;
385
386 target = debug_target_create();
387 if (!target)
388 return NULL;
389
390 target->tgt_stdout.out = stderr;
391 target->output = _stderr_output;
392 return target;
393}
394
395void debug_init(void)
396{
397 tall_dbg_ctx = talloc_named_const(NULL, 1, "debug");
398}