blob: 15271fe72efe59a40008054a9563c0ada2033b81 [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},
54};
Holger Freyther32636e82008-12-27 11:07:15 +000055
Holger Freytherd546e312008-12-27 12:03:07 +000056struct debug_info {
57 const char *name;
58 const char *color;
59 const char *description;
60 int number;
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +010061 int position;
Holger Freytherd546e312008-12-27 12:03:07 +000062};
63
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +010064struct debug_context {
65 struct gsm_lchan *lchan;
66 struct gsm_subscriber *subscr;
67 struct gsm_bts *bts;
68};
69
70static struct debug_context debug_context;
71static void *tall_dbg_ctx = NULL;
72static LLIST_HEAD(target_list);
73
Holger Freytherd546e312008-12-27 12:03:07 +000074#define DEBUG_CATEGORY(NUMBER, NAME, COLOR, DESCRIPTION) \
75 { .name = NAME, .color = COLOR, .description = DESCRIPTION, .number = NUMBER },
76
Holger Freytherd546e312008-12-27 12:03:07 +000077static const struct debug_info debug_info[] = {
Holger Freyther7c03e4c2008-12-27 12:46:48 +000078 DEBUG_CATEGORY(DRLL, "DRLL", "\033[1;31m", "")
79 DEBUG_CATEGORY(DCC, "DCC", "\033[1;32m", "")
Holger Freyther3770b762009-06-02 02:35:12 +000080 DEBUG_CATEGORY(DMM, "DMM", "\033[1;33m", "")
Holger Freyther7c03e4c2008-12-27 12:46:48 +000081 DEBUG_CATEGORY(DRR, "DRR", "\033[1;34m", "")
Harald Weltec1d2aae2009-05-23 06:43:35 +000082 DEBUG_CATEGORY(DRSL, "DRSL", "\033[1;35m", "")
Holger Freyther7c03e4c2008-12-27 12:46:48 +000083 DEBUG_CATEGORY(DNM, "DNM", "\033[1;36m", "")
Daniel Willmann10d06f62008-12-28 21:38:25 +000084 DEBUG_CATEGORY(DSMS, "DSMS", "\033[1;37m", "")
Harald Welted35b6a72008-12-29 04:06:41 +000085 DEBUG_CATEGORY(DPAG, "DPAG", "\033[1;38m", "")
Harald Weltec125a682009-05-23 06:42:38 +000086 DEBUG_CATEGORY(DMNCC, "DMNCC","\033[1;39m", "")
Harald Welteedb37782009-05-01 14:59:07 +000087 DEBUG_CATEGORY(DINP, "DINP", "", "")
Harald Welteb60fa592009-02-06 12:02:53 +000088 DEBUG_CATEGORY(DMI, "DMI", "", "")
89 DEBUG_CATEGORY(DMIB, "DMIB", "", "")
Harald Welteba59baf2009-02-23 00:04:04 +000090 DEBUG_CATEGORY(DMUX, "DMUX", "", "")
Harald Welte10d0e672009-06-27 02:53:10 +020091 DEBUG_CATEGORY(DMEAS, "DMEAS", "", "")
Holger Hans Peter Freythered0a47b2009-08-01 16:54:45 +020092 DEBUG_CATEGORY(DSCCP, "DSCCP", "", "")
Holger Hans Peter Freyther32201c52009-08-18 12:54:50 +020093 DEBUG_CATEGORY(DMSC, "DMSC", "", "")
Holger Hans Peter Freytherff5fa4e2009-11-20 13:05:48 +010094 DEBUG_CATEGORY(DMGCP, "DMGCP", "", "")
Harald Welte8d77b952009-12-17 00:31:10 +010095 DEBUG_CATEGORY(DHO, "DHO", "", "")
Holger Freytherd546e312008-12-27 12:03:07 +000096};
97
98/*
99 * Parse the category mask.
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100100 * The format can be this: category1:category2:category3
101 * or category1,2:category2,3:...
Holger Freytherd546e312008-12-27 12:03:07 +0000102 */
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100103void debug_parse_category_mask(struct debug_target* target, const char *_mask)
Holger Freytherd546e312008-12-27 12:03:07 +0000104{
Holger Freytherd546e312008-12-27 12:03:07 +0000105 int i = 0;
106 char *mask = strdup(_mask);
107 char *category_token = NULL;
108
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100109 /* Disable everything to enable it afterwards */
110 for (i = 0; i < ARRAY_SIZE(target->categories); ++i)
111 target->categories[i].enabled = 0;
112
Holger Freytherd546e312008-12-27 12:03:07 +0000113 category_token = strtok(mask, ":");
114 do {
115 for (i = 0; i < ARRAY_SIZE(debug_info); ++i) {
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100116 char* colon = strstr(category_token, ",");
117 int length = strlen(category_token);
118
119 if (colon)
120 length = colon - category_token;
121
122 if (strncasecmp(debug_info[i].name, category_token, length) == 0) {
123 int number = debug_info[i].number;
124 int level = 0;
125
126 if (colon)
127 level = atoi(colon+1);
128
129 target->categories[number].enabled = 1;
130 target->categories[number].loglevel = level;
131 }
Holger Freytherd546e312008-12-27 12:03:07 +0000132 }
Holger Freytherca362a62009-01-04 21:05:01 +0000133 } while ((category_token = strtok(NULL, ":")));
Holger Freytherd546e312008-12-27 12:03:07 +0000134
Holger Freytherd546e312008-12-27 12:03:07 +0000135 free(mask);
Holger Freytherd546e312008-12-27 12:03:07 +0000136}
137
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100138static const char* color(int subsys)
Holger Freyther7c03e4c2008-12-27 12:46:48 +0000139{
140 int i = 0;
141
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100142 for (i = 0; i < ARRAY_SIZE(debug_info); ++i) {
Holger Freyther7c03e4c2008-12-27 12:46:48 +0000143 if (debug_info[i].number == subsys)
144 return debug_info[i].color;
145 }
146
147 return "";
148}
149
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100150static void _output(struct debug_target *target, unsigned int subsys, char *file, int line,
151 int cont, const char *format, va_list ap)
Holger Freyther32636e82008-12-27 11:07:15 +0000152{
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100153 char col[30];
154 char sub[30];
155 char tim[30];
156 char buf[4096];
157 char final[4096];
Holger Freyther32636e82008-12-27 11:07:15 +0000158
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100159 /* prepare the data */
160 col[0] = '\0';
161 sub[0] = '\0';
162 tim[0] = '\0';
163 buf[0] = '\0';
Holger Freyther32636e82008-12-27 11:07:15 +0000164
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100165 /* are we using color */
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100166 if (target->use_color) {
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100167 snprintf(col, sizeof(col), "%s", color(subsys));
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100168 col[sizeof(col)-1] = '\0';
169 }
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100170 vsnprintf(buf, sizeof(buf), format, ap);
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100171 buf[sizeof(buf)-1] = '\0';
Harald Welte6ddd1682009-02-06 12:38:29 +0000172
173 if (!cont) {
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100174 if (target->print_timestamp) {
Harald Welted3ff51d2009-06-09 20:21:57 +0000175 char *timestr;
176 time_t tm;
177 tm = time(NULL);
178 timestr = ctime(&tm);
179 timestr[strlen(timestr)-1] = '\0';
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100180 snprintf(tim, sizeof(tim), "%s ", timestr);
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100181 tim[sizeof(tim)-1] = '\0';
Harald Welted3ff51d2009-06-09 20:21:57 +0000182 }
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100183 snprintf(sub, sizeof(sub), "<%4.4x> %s:%d ", subsys, file, line);
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100184 sub[sizeof(sub)-1] = '\0';
Harald Welte6ddd1682009-02-06 12:38:29 +0000185 }
Holger Freyther32636e82008-12-27 11:07:15 +0000186
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100187 snprintf(final, sizeof(final), "%s%s%s%s\033[0;m", col, tim, sub, buf);
Harald Welteaa6c9ca2009-12-24 11:11:54 +0100188 final[sizeof(final)-1] = '\0';
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100189 target->output(target, final);
190}
191
192
193static void _debugp(unsigned int subsys, int level, char *file, int line,
194 int cont, const char *format, va_list ap)
195{
196 struct debug_target *tar;
197
198 llist_for_each_entry(tar, &target_list, entry) {
199 struct debug_category *category;
200 int output = 0;
201
202 category = &tar->categories[subsys];
203 /* subsystem is not supposed to be debugged */
204 if (!category->enabled)
205 continue;
206
207 /* Check the global log level */
208 if (tar->loglevel != 0 && level < tar->loglevel)
209 continue;
210
211 /* Check the category log level */
212 if (category->loglevel != 0 && level < category->loglevel)
213 continue;
214
215 /*
216 * Apply filters here... if that becomes messy we will need to put
217 * filters in a list and each filter will say stop, continue, output
218 */
219 if ((tar->filter_map & DEBUG_FILTER_ALL) != 0) {
220 output = 1;
221 } else if ((tar->filter_map & DEBUG_FILTER_IMSI) != 0
222 && debug_context.subscr && strcmp(debug_context.subscr->imsi, tar->imsi_filter) == 0) {
223 output = 1;
224 }
225
Harald Welteaa8989c2009-12-24 11:12:11 +0100226 if (output) {
227 /* FIXME: copying the va_list is an ugly workaround against a bug
228 * hidden somewhere in _output. If we do not copy here, the first
229 * call to _output() will corrupt the va_list contents, and any
230 * further _output() calls with the same va_list will segfault */
231 va_list bp;
232 va_copy(bp, ap);
233 _output(tar, subsys, file, line, cont, format, bp);
234 }
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100235 }
236}
237
238void debugp(unsigned int subsys, char *file, int line, int cont, const char *format, ...)
239{
240 va_list ap;
241
242 va_start(ap, format);
243 _debugp(subsys, LOGL_DEBUG, file, line, cont, format, ap);
Holger Freyther32636e82008-12-27 11:07:15 +0000244 va_end(ap);
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100245}
Holger Freyther32636e82008-12-27 11:07:15 +0000246
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100247void debugp2(unsigned int subsys, unsigned int level, char *file, int line, int cont, const char *format, ...)
248{
249 va_list ap;
250
251 va_start(ap, format);
252 _debugp(subsys, level, file, line, cont, format, ap);
253 va_end(ap);
Holger Freyther32636e82008-12-27 11:07:15 +0000254}
255
Harald Welte3cc4bf52009-02-28 13:08:01 +0000256static char hexd_buff[4096];
257
Holger Hans Peter Freythere78074e2009-08-20 13:16:26 +0200258char *hexdump(const unsigned char *buf, int len)
Holger Freytherca362a62009-01-04 21:05:01 +0000259{
260 int i;
Harald Welte3cc4bf52009-02-28 13:08:01 +0000261 char *cur = hexd_buff;
262
263 hexd_buff[0] = 0;
Holger Freytherca362a62009-01-04 21:05:01 +0000264 for (i = 0; i < len; i++) {
Harald Welte3cc4bf52009-02-28 13:08:01 +0000265 int len_remain = sizeof(hexd_buff) - (cur - hexd_buff);
266 int rc = snprintf(cur, len_remain, "%02x ", buf[i]);
267 if (rc <= 0)
268 break;
269 cur += rc;
Holger Freytherca362a62009-01-04 21:05:01 +0000270 }
Harald Welte3cc4bf52009-02-28 13:08:01 +0000271 hexd_buff[sizeof(hexd_buff)-1] = 0;
272 return hexd_buff;
Holger Freytherca362a62009-01-04 21:05:01 +0000273}
274
Holger Hans Peter Freytherb61e3b22009-12-22 22:32:51 +0100275
276
277void debug_add_target(struct debug_target *target)
278{
279 llist_add_tail(&target->entry, &target_list);
280}
281
282void debug_del_target(struct debug_target *target)
283{
284 llist_del(&target->entry);
285}
286
287void debug_reset_context(void)
288{
289 memset(&debug_context, 0, sizeof(debug_context));
290}
291
292/* currently we are not reffing these */
293void debug_set_context(int ctx, void *value)
294{
295 switch (ctx) {
296 case BSC_CTX_LCHAN:
297 debug_context.lchan = (struct gsm_lchan *) value;
298 break;
299 case BSC_CTX_SUBSCR:
300 debug_context.subscr = (struct gsm_subscriber *) value;
301 break;
302 case BSC_CTX_BTS:
303 debug_context.bts = (struct gsm_bts *) value;
304 break;
305 case BSC_CTX_SCCP:
306 break;
307 default:
308 break;
309 }
310}
311
312void debug_set_imsi_filter(struct debug_target *target, const char *imsi)
313{
314 if (imsi) {
315 target->filter_map |= DEBUG_FILTER_IMSI;
316 target->imsi_filter = talloc_strdup(target, imsi);
317 } else if (target->imsi_filter) {
318 target->filter_map &= ~DEBUG_FILTER_IMSI;
319 talloc_free(target->imsi_filter);
320 target->imsi_filter = NULL;
321 }
322}
323
324void debug_set_all_filter(struct debug_target *target, int all)
325{
326 if (all)
327 target->filter_map |= DEBUG_FILTER_ALL;
328 else
329 target->filter_map &= ~DEBUG_FILTER_ALL;
330}
331
332void debug_set_use_color(struct debug_target *target, int use_color)
333{
334 target->use_color = use_color;
335}
336
337void debug_set_print_timestamp(struct debug_target *target, int print_timestamp)
338{
339 target->print_timestamp = print_timestamp;
340}
341
342void debug_set_log_level(struct debug_target *target, int log_level)
343{
344 target->loglevel = log_level;
345}
346
347void debug_set_category_filter(struct debug_target *target, int category, int enable, int level)
348{
349 if (category >= Debug_LastEntry)
350 return;
351 target->categories[category].enabled = !!enable;
352 target->categories[category].loglevel = level;
353}
354
355static void _stderr_output(struct debug_target *target, const char *log)
356{
357 fprintf(target->tgt_stdout.out, "%s", log);
358 fflush(target->tgt_stdout.out);
359}
360
361struct debug_target *debug_target_create(void)
362{
363 struct debug_target *target;
364
365 target = talloc_zero(tall_dbg_ctx, struct debug_target);
366 if (!target)
367 return NULL;
368
369 INIT_LLIST_HEAD(&target->entry);
370 memcpy(target->categories, default_categories, sizeof(default_categories));
371 target->use_color = 1;
372 target->print_timestamp = 0;
373 target->loglevel = 0;
374 return target;
375}
376
377struct debug_target *debug_target_create_stderr(void)
378{
379 struct debug_target *target;
380
381 target = debug_target_create();
382 if (!target)
383 return NULL;
384
385 target->tgt_stdout.out = stderr;
386 target->output = _stderr_output;
387 return target;
388}
389
390void debug_init(void)
391{
392 tall_dbg_ctx = talloc_named_const(NULL, 1, "debug");
393}