Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 1 | /* Debugging/Logging support code */ |
| 2 | /* (C) 2008 by Harald Welte <laforge@gnumonks.org> |
| 3 | * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 4 | * 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> |
| 23 | #include <stdlib.h> |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <strings.h> |
| 27 | #include <time.h> |
| 28 | |
| 29 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 30 | #include <openbsc/talloc.h> |
| 31 | #include <openbsc/gsm_data.h> |
| 32 | #include <openbsc/gsm_subscriber.h> |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 33 | |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 34 | /* default categories */ |
| 35 | static struct debug_category default_categories[Debug_LastEntry] = { |
| 36 | [DRLL] = { .enabled = 1, .loglevel = 0}, |
| 37 | [DCC] = { .enabled = 1, .loglevel = 0}, |
| 38 | [DMM] = { .enabled = 1, .loglevel = 0}, |
| 39 | [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 | }; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 55 | |
| 56 | struct debug_info { |
| 57 | const char *name; |
| 58 | const char *color; |
| 59 | const char *description; |
| 60 | int number; |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 61 | int position; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 62 | }; |
| 63 | |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 64 | struct debug_context { |
| 65 | struct gsm_lchan *lchan; |
| 66 | struct gsm_subscriber *subscr; |
| 67 | struct gsm_bts *bts; |
| 68 | }; |
| 69 | |
| 70 | static struct debug_context debug_context; |
| 71 | static void *tall_dbg_ctx = NULL; |
| 72 | static LLIST_HEAD(target_list); |
| 73 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 74 | #define DEBUG_CATEGORY(NUMBER, NAME, COLOR, DESCRIPTION) \ |
| 75 | { .name = NAME, .color = COLOR, .description = DESCRIPTION, .number = NUMBER }, |
| 76 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 77 | static const struct debug_info debug_info[] = { |
| 78 | DEBUG_CATEGORY(DRLL, "DRLL", "\033[1;31m", "") |
| 79 | DEBUG_CATEGORY(DCC, "DCC", "\033[1;32m", "") |
| 80 | DEBUG_CATEGORY(DMM, "DMM", "\033[1;33m", "") |
| 81 | DEBUG_CATEGORY(DRR, "DRR", "\033[1;34m", "") |
| 82 | DEBUG_CATEGORY(DRSL, "DRSL", "\033[1;35m", "") |
| 83 | DEBUG_CATEGORY(DNM, "DNM", "\033[1;36m", "") |
| 84 | DEBUG_CATEGORY(DSMS, "DSMS", "\033[1;37m", "") |
| 85 | DEBUG_CATEGORY(DPAG, "DPAG", "\033[1;38m", "") |
| 86 | DEBUG_CATEGORY(DMNCC, "DMNCC","\033[1;39m", "") |
| 87 | DEBUG_CATEGORY(DINP, "DINP", "", "") |
| 88 | DEBUG_CATEGORY(DMI, "DMI", "", "") |
| 89 | DEBUG_CATEGORY(DMIB, "DMIB", "", "") |
| 90 | DEBUG_CATEGORY(DMUX, "DMUX", "", "") |
Harald Welte | 0299368 | 2009-06-27 02:53:10 +0200 | [diff] [blame] | 91 | DEBUG_CATEGORY(DMEAS, "DMEAS", "", "") |
Holger Hans Peter Freyther | b9cf781 | 2009-08-01 16:54:45 +0200 | [diff] [blame] | 92 | DEBUG_CATEGORY(DSCCP, "DSCCP", "", "") |
Holger Hans Peter Freyther | acf289e | 2009-08-18 12:54:50 +0200 | [diff] [blame] | 93 | DEBUG_CATEGORY(DMSC, "DMSC", "", "") |
Holger Hans Peter Freyther | 66f78e6 | 2009-11-20 13:05:48 +0100 | [diff] [blame] | 94 | DEBUG_CATEGORY(DMGCP, "DMGCP", "", "") |
Harald Welte | b90d7bd | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 95 | DEBUG_CATEGORY(DHO, "DHO", "", "") |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 96 | }; |
| 97 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 98 | /* |
| 99 | * Parse the category mask. |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 100 | * The format can be this: category1:category2:category3 |
| 101 | * or category1,2:category2,3:... |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 102 | */ |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 103 | void debug_parse_category_mask(struct debug_target* target, const char *_mask) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 104 | { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 105 | int i = 0; |
| 106 | char *mask = strdup(_mask); |
| 107 | char *category_token = NULL; |
| 108 | |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 109 | /* Disable everything to enable it afterwards */ |
| 110 | for (i = 0; i < ARRAY_SIZE(target->categories); ++i) |
| 111 | target->categories[i].enabled = 0; |
| 112 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 113 | category_token = strtok(mask, ":"); |
| 114 | do { |
| 115 | for (i = 0; i < ARRAY_SIZE(debug_info); ++i) { |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 116 | 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 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 132 | } |
| 133 | } while ((category_token = strtok(NULL, ":"))); |
| 134 | |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 135 | free(mask); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 136 | } |
| 137 | |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 138 | static const char* color(int subsys) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 139 | { |
| 140 | int i = 0; |
| 141 | |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 142 | for (i = 0; i < ARRAY_SIZE(debug_info); ++i) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 143 | if (debug_info[i].number == subsys) |
| 144 | return debug_info[i].color; |
| 145 | } |
| 146 | |
| 147 | return ""; |
| 148 | } |
| 149 | |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 150 | static void _output(struct debug_target *target, unsigned int subsys, char *file, int line, |
| 151 | int cont, const char *format, va_list ap) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 152 | { |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 153 | char col[30]; |
| 154 | char sub[30]; |
| 155 | char tim[30]; |
| 156 | char buf[4096]; |
| 157 | char final[4096]; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 158 | |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 159 | /* prepare the data */ |
| 160 | col[0] = '\0'; |
| 161 | sub[0] = '\0'; |
| 162 | tim[0] = '\0'; |
| 163 | buf[0] = '\0'; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 164 | |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 165 | /* are we using color */ |
| 166 | if (target->use_color) |
| 167 | snprintf(col, sizeof(col), "%s", color(subsys)); |
| 168 | vsnprintf(buf, sizeof(buf), format, ap); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 169 | |
| 170 | if (!cont) { |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 171 | if (target->print_timestamp) { |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 172 | char *timestr; |
| 173 | time_t tm; |
| 174 | tm = time(NULL); |
| 175 | timestr = ctime(&tm); |
| 176 | timestr[strlen(timestr)-1] = '\0'; |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 177 | snprintf(tim, sizeof(tim), "%s ", timestr); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 178 | } |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 179 | snprintf(sub, sizeof(sub), "<%4.4x> %s:%d ", subsys, file, line); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 180 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 181 | |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 182 | snprintf(final, sizeof(final), "%s%s%s%s\033[0;m", col, tim, sub, buf); |
| 183 | target->output(target, final); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | static void _debugp(unsigned int subsys, int level, char *file, int line, |
| 188 | int cont, const char *format, va_list ap) |
| 189 | { |
| 190 | struct debug_target *tar; |
| 191 | |
| 192 | llist_for_each_entry(tar, &target_list, entry) { |
| 193 | struct debug_category *category; |
| 194 | int output = 0; |
| 195 | |
| 196 | category = &tar->categories[subsys]; |
| 197 | /* subsystem is not supposed to be debugged */ |
| 198 | if (!category->enabled) |
| 199 | continue; |
| 200 | |
| 201 | /* Check the global log level */ |
| 202 | if (tar->loglevel != 0 && level < tar->loglevel) |
| 203 | continue; |
| 204 | |
| 205 | /* Check the category log level */ |
| 206 | if (category->loglevel != 0 && level < category->loglevel) |
| 207 | continue; |
| 208 | |
| 209 | /* |
| 210 | * Apply filters here... if that becomes messy we will need to put |
| 211 | * filters in a list and each filter will say stop, continue, output |
| 212 | */ |
| 213 | if ((tar->filter_map & DEBUG_FILTER_ALL) != 0) { |
| 214 | output = 1; |
| 215 | } else if ((tar->filter_map & DEBUG_FILTER_IMSI) != 0 |
| 216 | && debug_context.subscr && strcmp(debug_context.subscr->imsi, tar->imsi_filter) == 0) { |
| 217 | output = 1; |
| 218 | } |
| 219 | |
| 220 | if (output) |
| 221 | _output(tar, subsys, file, line, cont, format, ap); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void debugp(unsigned int subsys, char *file, int line, int cont, const char *format, ...) |
| 226 | { |
| 227 | va_list ap; |
| 228 | |
| 229 | va_start(ap, format); |
| 230 | _debugp(subsys, LOGL_DEBUG, file, line, cont, format, ap); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 231 | va_end(ap); |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 232 | } |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 233 | |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 234 | void debugp2(unsigned int subsys, unsigned int level, char *file, int line, int cont, const char *format, ...) |
| 235 | { |
| 236 | va_list ap; |
| 237 | |
| 238 | va_start(ap, format); |
| 239 | _debugp(subsys, level, file, line, cont, format, ap); |
| 240 | va_end(ap); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static char hexd_buff[4096]; |
| 244 | |
Holger Hans Peter Freyther | 26d15d1 | 2009-08-20 13:16:26 +0200 | [diff] [blame] | 245 | char *hexdump(const unsigned char *buf, int len) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 246 | { |
| 247 | int i; |
| 248 | char *cur = hexd_buff; |
| 249 | |
| 250 | hexd_buff[0] = 0; |
| 251 | for (i = 0; i < len; i++) { |
| 252 | int len_remain = sizeof(hexd_buff) - (cur - hexd_buff); |
| 253 | int rc = snprintf(cur, len_remain, "%02x ", buf[i]); |
| 254 | if (rc <= 0) |
| 255 | break; |
| 256 | cur += rc; |
| 257 | } |
| 258 | hexd_buff[sizeof(hexd_buff)-1] = 0; |
| 259 | return hexd_buff; |
| 260 | } |
| 261 | |
Holger Hans Peter Freyther | c8d862e | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 262 | |
| 263 | |
| 264 | void debug_add_target(struct debug_target *target) |
| 265 | { |
| 266 | llist_add_tail(&target->entry, &target_list); |
| 267 | } |
| 268 | |
| 269 | void debug_del_target(struct debug_target *target) |
| 270 | { |
| 271 | llist_del(&target->entry); |
| 272 | } |
| 273 | |
| 274 | void debug_reset_context(void) |
| 275 | { |
| 276 | memset(&debug_context, 0, sizeof(debug_context)); |
| 277 | } |
| 278 | |
| 279 | /* currently we are not reffing these */ |
| 280 | void debug_set_context(int ctx, void *value) |
| 281 | { |
| 282 | switch (ctx) { |
| 283 | case BSC_CTX_LCHAN: |
| 284 | debug_context.lchan = (struct gsm_lchan *) value; |
| 285 | break; |
| 286 | case BSC_CTX_SUBSCR: |
| 287 | debug_context.subscr = (struct gsm_subscriber *) value; |
| 288 | break; |
| 289 | case BSC_CTX_BTS: |
| 290 | debug_context.bts = (struct gsm_bts *) value; |
| 291 | break; |
| 292 | case BSC_CTX_SCCP: |
| 293 | break; |
| 294 | default: |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | void debug_set_imsi_filter(struct debug_target *target, const char *imsi) |
| 300 | { |
| 301 | if (imsi) { |
| 302 | target->filter_map |= DEBUG_FILTER_IMSI; |
| 303 | target->imsi_filter = talloc_strdup(target, imsi); |
| 304 | } else if (target->imsi_filter) { |
| 305 | target->filter_map &= ~DEBUG_FILTER_IMSI; |
| 306 | talloc_free(target->imsi_filter); |
| 307 | target->imsi_filter = NULL; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | void debug_set_all_filter(struct debug_target *target, int all) |
| 312 | { |
| 313 | if (all) |
| 314 | target->filter_map |= DEBUG_FILTER_ALL; |
| 315 | else |
| 316 | target->filter_map &= ~DEBUG_FILTER_ALL; |
| 317 | } |
| 318 | |
| 319 | void debug_set_use_color(struct debug_target *target, int use_color) |
| 320 | { |
| 321 | target->use_color = use_color; |
| 322 | } |
| 323 | |
| 324 | void debug_set_print_timestamp(struct debug_target *target, int print_timestamp) |
| 325 | { |
| 326 | target->print_timestamp = print_timestamp; |
| 327 | } |
| 328 | |
| 329 | void debug_set_log_level(struct debug_target *target, int log_level) |
| 330 | { |
| 331 | target->loglevel = log_level; |
| 332 | } |
| 333 | |
| 334 | void debug_set_category_filter(struct debug_target *target, int category, int enable, int level) |
| 335 | { |
| 336 | if (category >= Debug_LastEntry) |
| 337 | return; |
| 338 | target->categories[category].enabled = !!enable; |
| 339 | target->categories[category].loglevel = level; |
| 340 | } |
| 341 | |
| 342 | static void _stderr_output(struct debug_target *target, const char *log) |
| 343 | { |
| 344 | fprintf(target->tgt_stdout.out, "%s", log); |
| 345 | fflush(target->tgt_stdout.out); |
| 346 | } |
| 347 | |
| 348 | struct debug_target *debug_target_create(void) |
| 349 | { |
| 350 | struct debug_target *target; |
| 351 | |
| 352 | target = talloc_zero(tall_dbg_ctx, struct debug_target); |
| 353 | if (!target) |
| 354 | return NULL; |
| 355 | |
| 356 | INIT_LLIST_HEAD(&target->entry); |
| 357 | memcpy(target->categories, default_categories, sizeof(default_categories)); |
| 358 | target->use_color = 1; |
| 359 | target->print_timestamp = 0; |
| 360 | target->loglevel = 0; |
| 361 | return target; |
| 362 | } |
| 363 | |
| 364 | struct debug_target *debug_target_create_stderr(void) |
| 365 | { |
| 366 | struct debug_target *target; |
| 367 | |
| 368 | target = debug_target_create(); |
| 369 | if (!target) |
| 370 | return NULL; |
| 371 | |
| 372 | target->tgt_stdout.out = stderr; |
| 373 | target->output = _stderr_output; |
| 374 | return target; |
| 375 | } |
| 376 | |
| 377 | void debug_init(void) |
| 378 | { |
| 379 | tall_dbg_ctx = talloc_named_const(NULL, 1, "debug"); |
| 380 | } |