blob: 7488cd63ab0660f5934ad47b655cfc8e4c0ce757 [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>
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +010032#include <openbsc/gsm_data.h>
33#include <openbsc/gsm_subscriber.h>
Holger Freyther32636e82008-12-27 11:07:15 +000034
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +010035/* default categories */
36static struct debug_category default_categories[Debug_LastEntry] = {
Harald Welte84105972009-12-24 13:48:33 +010037 [DRLL] = { .enabled = 1, .loglevel = LOGL_NOTICE },
38 [DCC] = { .enabled = 1, .loglevel = LOGL_NOTICE },
39 [DNM] = { .enabled = 1, .loglevel = LOGL_NOTICE },
40 [DRR] = { .enabled = 1, .loglevel = LOGL_NOTICE },
41 [DRSL] = { .enabled = 1, .loglevel = LOGL_NOTICE },
42 [DMM] = { .enabled = 1, .loglevel = LOGL_INFO },
43 [DMNCC] = { .enabled = 1, .loglevel = LOGL_NOTICE },
44 [DSMS] = { .enabled = 1, .loglevel = LOGL_NOTICE },
45 [DPAG] = { .enabled = 1, .loglevel = LOGL_NOTICE },
46 [DMEAS] = { .enabled = 0, .loglevel = LOGL_NOTICE },
47 [DMI] = { .enabled = 0, .loglevel = LOGL_NOTICE },
48 [DMIB] = { .enabled = 0, .loglevel = LOGL_NOTICE },
49 [DMUX] = { .enabled = 1, .loglevel = LOGL_NOTICE },
50 [DINP] = { .enabled = 1, .loglevel = LOGL_NOTICE },
51 [DSCCP] = { .enabled = 1, .loglevel = LOGL_NOTICE },
52 [DMSC] = { .enabled = 1, .loglevel = LOGL_NOTICE },
53 [DMGCP] = { .enabled = 1, .loglevel = LOGL_NOTICE },
54 [DHO] = { .enabled = 1, .loglevel = LOGL_NOTICE },
55 [DDB] = { .enabled = 1, .loglevel = LOGL_NOTICE },
56 [DREF] = { .enabled = 0, .loglevel = LOGL_NOTICE },
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +010057};
Holger Freyther32636e82008-12-27 11:07:15 +000058
Harald Weltef6f2ef92009-12-27 11:24:55 +010059const char *get_value_string(const struct value_string *vs, u_int32_t val)
60{
61 int i;
62
63 for (i = 0;; i++) {
64 if (vs[i].value == 0 && vs[i].str == NULL)
65 break;
66 if (vs[i].value == val)
67 return vs[i].str;
68 }
69 return "unknown";
70}
71
72int get_string_value(const struct value_string *vs, const char *str)
73{
74 int i;
75
76 for (i = 0;; i++) {
77 if (vs[i].value == 0 && vs[i].str == NULL)
78 break;
79 if (!strcasecmp(vs[i].str, str))
80 return vs[i].value;
81 }
82 return -EINVAL;
83}
84
Holger Freytherd546e312008-12-27 12:03:07 +000085struct debug_info {
86 const char *name;
87 const char *color;
88 const char *description;
89 int number;
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +010090 int position;
Holger Freytherd546e312008-12-27 12:03:07 +000091};
92
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +010093struct debug_context {
94 struct gsm_lchan *lchan;
95 struct gsm_subscriber *subscr;
96 struct gsm_bts *bts;
97};
98
99static struct debug_context debug_context;
100static void *tall_dbg_ctx = NULL;
101static LLIST_HEAD(target_list);
102
Holger Freytherd546e312008-12-27 12:03:07 +0000103#define DEBUG_CATEGORY(NUMBER, NAME, COLOR, DESCRIPTION) \
104 { .name = NAME, .color = COLOR, .description = DESCRIPTION, .number = NUMBER },
105
Holger Freytherd546e312008-12-27 12:03:07 +0000106static const struct debug_info debug_info[] = {
Holger Freyther7c03e4c2008-12-27 12:46:48 +0000107 DEBUG_CATEGORY(DRLL, "DRLL", "\033[1;31m", "")
108 DEBUG_CATEGORY(DCC, "DCC", "\033[1;32m", "")
Holger Freyther3770b762009-06-02 02:35:12 +0000109 DEBUG_CATEGORY(DMM, "DMM", "\033[1;33m", "")
Holger Freyther7c03e4c2008-12-27 12:46:48 +0000110 DEBUG_CATEGORY(DRR, "DRR", "\033[1;34m", "")
Harald Weltec1d2aae2009-05-23 06:43:35 +0000111 DEBUG_CATEGORY(DRSL, "DRSL", "\033[1;35m", "")
Holger Freyther7c03e4c2008-12-27 12:46:48 +0000112 DEBUG_CATEGORY(DNM, "DNM", "\033[1;36m", "")
Daniel Willmann10d06f62008-12-28 21:38:25 +0000113 DEBUG_CATEGORY(DSMS, "DSMS", "\033[1;37m", "")
Harald Welted35b6a72008-12-29 04:06:41 +0000114 DEBUG_CATEGORY(DPAG, "DPAG", "\033[1;38m", "")
Harald Weltec125a682009-05-23 06:42:38 +0000115 DEBUG_CATEGORY(DMNCC, "DMNCC","\033[1;39m", "")
Harald Welteedb37782009-05-01 14:59:07 +0000116 DEBUG_CATEGORY(DINP, "DINP", "", "")
Harald Welteb60fa592009-02-06 12:02:53 +0000117 DEBUG_CATEGORY(DMI, "DMI", "", "")
118 DEBUG_CATEGORY(DMIB, "DMIB", "", "")
Harald Welteba59baf2009-02-23 00:04:04 +0000119 DEBUG_CATEGORY(DMUX, "DMUX", "", "")
Harald Welte10d0e672009-06-27 02:53:10 +0200120 DEBUG_CATEGORY(DMEAS, "DMEAS", "", "")
Holger Hans Peter Freythered0a47b2009-08-01 16:54:45 +0200121 DEBUG_CATEGORY(DSCCP, "DSCCP", "", "")
Holger Hans Peter Freyther32201c52009-08-18 12:54:50 +0200122 DEBUG_CATEGORY(DMSC, "DMSC", "", "")
Holger Hans Peter Freytherff5fa4e2009-11-20 13:05:48 +0100123 DEBUG_CATEGORY(DMGCP, "DMGCP", "", "")
Harald Welte8d77b952009-12-17 00:31:10 +0100124 DEBUG_CATEGORY(DHO, "DHO", "", "")
Harald Welteae1f1592009-12-24 11:39:14 +0100125 DEBUG_CATEGORY(DDB, "DDB", "", "")
Harald Welted0c19142009-12-24 11:46:44 +0100126 DEBUG_CATEGORY(DDB, "DREF", "", "")
Holger Freytherd546e312008-12-27 12:03:07 +0000127};
128
Harald Welte (local)b79bdd92009-12-26 19:45:22 +0100129static const struct value_string loglevel_strs[] = {
130 { 0, "EVERYTHING" },
131 { 1, "DEBUG" },
132 { 3, "INFO" },
133 { 5, "NOTICE" },
134 { 7, "ERROR" },
135 { 8, "FATAL" },
136 { 0, NULL },
137};
138
139int debug_parse_level(const char *lvl)
140{
141 return get_string_value(loglevel_strs, lvl);
142}
143
144int debug_parse_category(const char *category)
145{
146 int i;
147
148 for (i = 0; i < ARRAY_SIZE(debug_info); ++i) {
149 if (!strcasecmp(debug_info[i].name+1, category))
150 return debug_info[i].number;
151 }
152
153 return -EINVAL;
154}
155
Holger Freytherd546e312008-12-27 12:03:07 +0000156/*
157 * Parse the category mask.
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100158 * The format can be this: category1:category2:category3
159 * or category1,2:category2,3:...
Holger Freytherd546e312008-12-27 12:03:07 +0000160 */
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100161void debug_parse_category_mask(struct debug_target* target, const char *_mask)
Holger Freytherd546e312008-12-27 12:03:07 +0000162{
Holger Freytherd546e312008-12-27 12:03:07 +0000163 int i = 0;
164 char *mask = strdup(_mask);
165 char *category_token = NULL;
166
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100167 /* Disable everything to enable it afterwards */
168 for (i = 0; i < ARRAY_SIZE(target->categories); ++i)
169 target->categories[i].enabled = 0;
170
Holger Freytherd546e312008-12-27 12:03:07 +0000171 category_token = strtok(mask, ":");
172 do {
173 for (i = 0; i < ARRAY_SIZE(debug_info); ++i) {
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100174 char* colon = strstr(category_token, ",");
175 int length = strlen(category_token);
176
177 if (colon)
178 length = colon - category_token;
179
180 if (strncasecmp(debug_info[i].name, category_token, length) == 0) {
181 int number = debug_info[i].number;
182 int level = 0;
183
184 if (colon)
185 level = atoi(colon+1);
186
187 target->categories[number].enabled = 1;
188 target->categories[number].loglevel = level;
189 }
Holger Freytherd546e312008-12-27 12:03:07 +0000190 }
Holger Freytherca362a62009-01-04 21:05:01 +0000191 } while ((category_token = strtok(NULL, ":")));
Holger Freytherd546e312008-12-27 12:03:07 +0000192
Holger Freytherd546e312008-12-27 12:03:07 +0000193 free(mask);
Holger Freytherd546e312008-12-27 12:03:07 +0000194}
195
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100196static const char* color(int subsys)
Holger Freyther7c03e4c2008-12-27 12:46:48 +0000197{
198 int i = 0;
199
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100200 for (i = 0; i < ARRAY_SIZE(debug_info); ++i) {
Holger Freyther7c03e4c2008-12-27 12:46:48 +0000201 if (debug_info[i].number == subsys)
202 return debug_info[i].color;
203 }
204
205 return "";
206}
207
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100208static void _output(struct debug_target *target, unsigned int subsys, char *file, int line,
209 int cont, const char *format, va_list ap)
Holger Freyther32636e82008-12-27 11:07:15 +0000210{
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100211 char col[30];
212 char sub[30];
213 char tim[30];
214 char buf[4096];
215 char final[4096];
Holger Freyther32636e82008-12-27 11:07:15 +0000216
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100217 /* prepare the data */
218 col[0] = '\0';
219 sub[0] = '\0';
220 tim[0] = '\0';
221 buf[0] = '\0';
Holger Freyther32636e82008-12-27 11:07:15 +0000222
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100223 /* are we using color */
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100224 if (target->use_color) {
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100225 snprintf(col, sizeof(col), "%s", color(subsys));
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100226 col[sizeof(col)-1] = '\0';
227 }
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100228 vsnprintf(buf, sizeof(buf), format, ap);
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100229 buf[sizeof(buf)-1] = '\0';
Harald Welte6ddd1682009-02-06 12:38:29 +0000230
231 if (!cont) {
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100232 if (target->print_timestamp) {
Harald Welted3ff51d2009-06-09 20:21:57 +0000233 char *timestr;
234 time_t tm;
235 tm = time(NULL);
236 timestr = ctime(&tm);
237 timestr[strlen(timestr)-1] = '\0';
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100238 snprintf(tim, sizeof(tim), "%s ", timestr);
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100239 tim[sizeof(tim)-1] = '\0';
Harald Welted3ff51d2009-06-09 20:21:57 +0000240 }
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100241 snprintf(sub, sizeof(sub), "<%4.4x> %s:%d ", subsys, file, line);
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100242 sub[sizeof(sub)-1] = '\0';
Harald Welte6ddd1682009-02-06 12:38:29 +0000243 }
Holger Freyther32636e82008-12-27 11:07:15 +0000244
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100245 snprintf(final, sizeof(final), "%s%s%s%s\033[0;m", col, tim, sub, buf);
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100246 final[sizeof(final)-1] = '\0';
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100247 target->output(target, final);
248}
249
250
251static void _debugp(unsigned int subsys, int level, char *file, int line,
252 int cont, const char *format, va_list ap)
253{
254 struct debug_target *tar;
255
256 llist_for_each_entry(tar, &target_list, entry) {
257 struct debug_category *category;
258 int output = 0;
259
260 category = &tar->categories[subsys];
261 /* subsystem is not supposed to be debugged */
262 if (!category->enabled)
263 continue;
264
265 /* Check the global log level */
266 if (tar->loglevel != 0 && level < tar->loglevel)
267 continue;
268
269 /* Check the category log level */
270 if (category->loglevel != 0 && level < category->loglevel)
271 continue;
272
273 /*
274 * Apply filters here... if that becomes messy we will need to put
275 * filters in a list and each filter will say stop, continue, output
276 */
277 if ((tar->filter_map & DEBUG_FILTER_ALL) != 0) {
278 output = 1;
279 } else if ((tar->filter_map & DEBUG_FILTER_IMSI) != 0
280 && debug_context.subscr && strcmp(debug_context.subscr->imsi, tar->imsi_filter) == 0) {
281 output = 1;
282 }
283
Harald Welteaa8989c2009-12-24 11:12:11 +0100284 if (output) {
285 /* FIXME: copying the va_list is an ugly workaround against a bug
286 * hidden somewhere in _output. If we do not copy here, the first
287 * call to _output() will corrupt the va_list contents, and any
288 * further _output() calls with the same va_list will segfault */
289 va_list bp;
290 va_copy(bp, ap);
291 _output(tar, subsys, file, line, cont, format, bp);
Harald Welte7ed25292009-12-24 11:14:03 +0100292 va_end(bp);
Harald Welteaa8989c2009-12-24 11:12:11 +0100293 }
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100294 }
295}
296
297void debugp(unsigned int subsys, char *file, int line, int cont, const char *format, ...)
298{
299 va_list ap;
300
301 va_start(ap, format);
302 _debugp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Holger Freyther32636e82008-12-27 11:07:15 +0000303 va_end(ap);
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100304}
Holger Freyther32636e82008-12-27 11:07:15 +0000305
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100306void debugp2(unsigned int subsys, unsigned int level, char *file, int line, int cont, const char *format, ...)
307{
308 va_list ap;
309
310 va_start(ap, format);
311 _debugp(subsys, level, file, line, cont, format, ap);
312 va_end(ap);
Holger Freyther32636e82008-12-27 11:07:15 +0000313}
314
Harald Welte3cc4bf52009-02-28 13:08:01 +0000315static char hexd_buff[4096];
316
Holger Hans Peter Freythere78074e2009-08-20 13:16:26 +0200317char *hexdump(const unsigned char *buf, int len)
Holger Freytherca362a62009-01-04 21:05:01 +0000318{
319 int i;
Harald Welte3cc4bf52009-02-28 13:08:01 +0000320 char *cur = hexd_buff;
321
322 hexd_buff[0] = 0;
Holger Freytherca362a62009-01-04 21:05:01 +0000323 for (i = 0; i < len; i++) {
Harald Welte3cc4bf52009-02-28 13:08:01 +0000324 int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
325 int rc = snprintf(cur, len_remain, "%02x ", buf[i]);
326 if (rc <= 0)
327 break;
328 cur += rc;
Holger Freytherca362a62009-01-04 21:05:01 +0000329 }
Harald Welte3cc4bf52009-02-28 13:08:01 +0000330 hexd_buff[sizeof(hexd_buff)-1] = 0;
331 return hexd_buff;
Holger Freytherca362a62009-01-04 21:05:01 +0000332}
333
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100334
335
336void debug_add_target(struct debug_target *target)
337{
338 llist_add_tail(&target->entry, &target_list);
339}
340
341void debug_del_target(struct debug_target *target)
342{
343 llist_del(&target->entry);
344}
345
346void debug_reset_context(void)
347{
348 memset(&debug_context, 0, sizeof(debug_context));
349}
350
351/* currently we are not reffing these */
352void debug_set_context(int ctx, void *value)
353{
354 switch (ctx) {
355 case BSC_CTX_LCHAN:
356 debug_context.lchan = (struct gsm_lchan *) value;
357 break;
358 case BSC_CTX_SUBSCR:
359 debug_context.subscr = (struct gsm_subscriber *) value;
360 break;
361 case BSC_CTX_BTS:
362 debug_context.bts = (struct gsm_bts *) value;
363 break;
364 case BSC_CTX_SCCP:
365 break;
366 default:
367 break;
368 }
369}
370
371void debug_set_imsi_filter(struct debug_target *target, const char *imsi)
372{
373 if (imsi) {
374 target->filter_map |= DEBUG_FILTER_IMSI;
375 target->imsi_filter = talloc_strdup(target, imsi);
376 } else if (target->imsi_filter) {
377 target->filter_map &= ~DEBUG_FILTER_IMSI;
378 talloc_free(target->imsi_filter);
379 target->imsi_filter = NULL;
380 }
381}
382
383void debug_set_all_filter(struct debug_target *target, int all)
384{
385 if (all)
386 target->filter_map |= DEBUG_FILTER_ALL;
387 else
388 target->filter_map &= ~DEBUG_FILTER_ALL;
389}
390
391void debug_set_use_color(struct debug_target *target, int use_color)
392{
393 target->use_color = use_color;
394}
395
396void debug_set_print_timestamp(struct debug_target *target, int print_timestamp)
397{
398 target->print_timestamp = print_timestamp;
399}
400
401void debug_set_log_level(struct debug_target *target, int log_level)
402{
403 target->loglevel = log_level;
404}
405
406void debug_set_category_filter(struct debug_target *target, int category, int enable, int level)
407{
408 if (category >= Debug_LastEntry)
409 return;
410 target->categories[category].enabled = !!enable;
411 target->categories[category].loglevel = level;
412}
413
414static void _stderr_output(struct debug_target *target, const char *log)
415{
416 fprintf(target->tgt_stdout.out, "%s", log);
417 fflush(target->tgt_stdout.out);
418}
419
420struct debug_target *debug_target_create(void)
421{
422 struct debug_target *target;
423
424 target = talloc_zero(tall_dbg_ctx, struct debug_target);
425 if (!target)
426 return NULL;
427
428 INIT_LLIST_HEAD(&target->entry);
429 memcpy(target->categories, default_categories, sizeof(default_categories));
430 target->use_color = 1;
431 target->print_timestamp = 0;
432 target->loglevel = 0;
433 return target;
434}
435
436struct debug_target *debug_target_create_stderr(void)
437{
438 struct debug_target *target;
439
440 target = debug_target_create();
441 if (!target)
442 return NULL;
443
444 target->tgt_stdout.out = stderr;
445 target->output = _stderr_output;
446 return target;
447}
448
449void debug_init(void)
450{
451 tall_dbg_ctx = talloc_named_const(NULL, 1, "debug");
452}