Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 1 | /* Debugging/Logging support code */ |
| 2 | /* (C) 2008 by Harald Welte <laforge@gnumonks.org> |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 3 | * (C) 2008 by Holger Hans Peter Freyther <zecke@selfish.org> |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 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> |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <string.h> |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 26 | #include <strings.h> |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 27 | #include <time.h> |
Harald Welte (local) | b79bdd9 | 2009-12-26 19:45:22 +0100 | [diff] [blame] | 28 | #include <errno.h> |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 29 | |
| 30 | #include <openbsc/debug.h> |
Harald Welte | dfe6c7d | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 31 | #include <osmocore/talloc.h> |
Harald Welte | 2e411c7 | 2010-03-01 21:59:06 +0100 | [diff] [blame] | 32 | #include <osmocore/utils.h> |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 33 | #include <openbsc/gsm_data.h> |
| 34 | #include <openbsc/gsm_subscriber.h> |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 35 | |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 36 | /* default categories */ |
| 37 | static struct debug_category default_categories[Debug_LastEntry] = { |
Harald Welte | 8410597 | 2009-12-24 13:48:33 +0100 | [diff] [blame] | 38 | [DRLL] = { .enabled = 1, .loglevel = LOGL_NOTICE }, |
| 39 | [DCC] = { .enabled = 1, .loglevel = LOGL_NOTICE }, |
| 40 | [DNM] = { .enabled = 1, .loglevel = LOGL_NOTICE }, |
| 41 | [DRR] = { .enabled = 1, .loglevel = LOGL_NOTICE }, |
| 42 | [DRSL] = { .enabled = 1, .loglevel = LOGL_NOTICE }, |
| 43 | [DMM] = { .enabled = 1, .loglevel = LOGL_INFO }, |
| 44 | [DMNCC] = { .enabled = 1, .loglevel = LOGL_NOTICE }, |
| 45 | [DSMS] = { .enabled = 1, .loglevel = LOGL_NOTICE }, |
| 46 | [DPAG] = { .enabled = 1, .loglevel = LOGL_NOTICE }, |
| 47 | [DMEAS] = { .enabled = 0, .loglevel = LOGL_NOTICE }, |
| 48 | [DMI] = { .enabled = 0, .loglevel = LOGL_NOTICE }, |
| 49 | [DMIB] = { .enabled = 0, .loglevel = LOGL_NOTICE }, |
| 50 | [DMUX] = { .enabled = 1, .loglevel = LOGL_NOTICE }, |
| 51 | [DINP] = { .enabled = 1, .loglevel = LOGL_NOTICE }, |
| 52 | [DSCCP] = { .enabled = 1, .loglevel = LOGL_NOTICE }, |
| 53 | [DMSC] = { .enabled = 1, .loglevel = LOGL_NOTICE }, |
| 54 | [DMGCP] = { .enabled = 1, .loglevel = LOGL_NOTICE }, |
| 55 | [DHO] = { .enabled = 1, .loglevel = LOGL_NOTICE }, |
| 56 | [DDB] = { .enabled = 1, .loglevel = LOGL_NOTICE }, |
| 57 | [DREF] = { .enabled = 0, .loglevel = LOGL_NOTICE }, |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 58 | }; |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 59 | |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 60 | struct debug_info { |
| 61 | const char *name; |
| 62 | const char *color; |
| 63 | const char *description; |
| 64 | int number; |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 65 | int position; |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 68 | struct debug_context { |
| 69 | struct gsm_lchan *lchan; |
| 70 | struct gsm_subscriber *subscr; |
| 71 | struct gsm_bts *bts; |
| 72 | }; |
| 73 | |
| 74 | static struct debug_context debug_context; |
| 75 | static void *tall_dbg_ctx = NULL; |
| 76 | static LLIST_HEAD(target_list); |
| 77 | |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 78 | #define DEBUG_CATEGORY(NUMBER, NAME, COLOR, DESCRIPTION) \ |
| 79 | { .name = NAME, .color = COLOR, .description = DESCRIPTION, .number = NUMBER }, |
| 80 | |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 81 | static const struct debug_info debug_info[] = { |
Holger Freyther | 7c03e4c | 2008-12-27 12:46:48 +0000 | [diff] [blame] | 82 | DEBUG_CATEGORY(DRLL, "DRLL", "\033[1;31m", "") |
| 83 | DEBUG_CATEGORY(DCC, "DCC", "\033[1;32m", "") |
Holger Freyther | 3770b76 | 2009-06-02 02:35:12 +0000 | [diff] [blame] | 84 | DEBUG_CATEGORY(DMM, "DMM", "\033[1;33m", "") |
Holger Freyther | 7c03e4c | 2008-12-27 12:46:48 +0000 | [diff] [blame] | 85 | DEBUG_CATEGORY(DRR, "DRR", "\033[1;34m", "") |
Harald Welte | c1d2aae | 2009-05-23 06:43:35 +0000 | [diff] [blame] | 86 | DEBUG_CATEGORY(DRSL, "DRSL", "\033[1;35m", "") |
Holger Freyther | 7c03e4c | 2008-12-27 12:46:48 +0000 | [diff] [blame] | 87 | DEBUG_CATEGORY(DNM, "DNM", "\033[1;36m", "") |
Daniel Willmann | 10d06f6 | 2008-12-28 21:38:25 +0000 | [diff] [blame] | 88 | DEBUG_CATEGORY(DSMS, "DSMS", "\033[1;37m", "") |
Harald Welte | d35b6a7 | 2008-12-29 04:06:41 +0000 | [diff] [blame] | 89 | DEBUG_CATEGORY(DPAG, "DPAG", "\033[1;38m", "") |
Harald Welte | c125a68 | 2009-05-23 06:42:38 +0000 | [diff] [blame] | 90 | DEBUG_CATEGORY(DMNCC, "DMNCC","\033[1;39m", "") |
Harald Welte | edb3778 | 2009-05-01 14:59:07 +0000 | [diff] [blame] | 91 | DEBUG_CATEGORY(DINP, "DINP", "", "") |
Harald Welte | b60fa59 | 2009-02-06 12:02:53 +0000 | [diff] [blame] | 92 | DEBUG_CATEGORY(DMI, "DMI", "", "") |
| 93 | DEBUG_CATEGORY(DMIB, "DMIB", "", "") |
Harald Welte | ba59baf | 2009-02-23 00:04:04 +0000 | [diff] [blame] | 94 | DEBUG_CATEGORY(DMUX, "DMUX", "", "") |
Harald Welte | 10d0e67 | 2009-06-27 02:53:10 +0200 | [diff] [blame] | 95 | DEBUG_CATEGORY(DMEAS, "DMEAS", "", "") |
Holger Hans Peter Freyther | ed0a47b | 2009-08-01 16:54:45 +0200 | [diff] [blame] | 96 | DEBUG_CATEGORY(DSCCP, "DSCCP", "", "") |
Holger Hans Peter Freyther | 32201c5 | 2009-08-18 12:54:50 +0200 | [diff] [blame] | 97 | DEBUG_CATEGORY(DMSC, "DMSC", "", "") |
Holger Hans Peter Freyther | ff5fa4e | 2009-11-20 13:05:48 +0100 | [diff] [blame] | 98 | DEBUG_CATEGORY(DMGCP, "DMGCP", "", "") |
Harald Welte | 8d77b95 | 2009-12-17 00:31:10 +0100 | [diff] [blame] | 99 | DEBUG_CATEGORY(DHO, "DHO", "", "") |
Harald Welte | ae1f159 | 2009-12-24 11:39:14 +0100 | [diff] [blame] | 100 | DEBUG_CATEGORY(DDB, "DDB", "", "") |
Holger Hans Peter Freyther | 8e9559d | 2010-03-22 07:49:12 +0100 | [diff] [blame] | 101 | DEBUG_CATEGORY(DREF, "DREF", "", "") |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
Harald Welte (local) | b79bdd9 | 2009-12-26 19:45:22 +0100 | [diff] [blame] | 104 | static const struct value_string loglevel_strs[] = { |
| 105 | { 0, "EVERYTHING" }, |
| 106 | { 1, "DEBUG" }, |
| 107 | { 3, "INFO" }, |
| 108 | { 5, "NOTICE" }, |
| 109 | { 7, "ERROR" }, |
| 110 | { 8, "FATAL" }, |
| 111 | { 0, NULL }, |
| 112 | }; |
| 113 | |
| 114 | int debug_parse_level(const char *lvl) |
| 115 | { |
| 116 | return get_string_value(loglevel_strs, lvl); |
| 117 | } |
| 118 | |
| 119 | int debug_parse_category(const char *category) |
| 120 | { |
| 121 | int i; |
| 122 | |
| 123 | for (i = 0; i < ARRAY_SIZE(debug_info); ++i) { |
| 124 | if (!strcasecmp(debug_info[i].name+1, category)) |
| 125 | return debug_info[i].number; |
| 126 | } |
| 127 | |
| 128 | return -EINVAL; |
| 129 | } |
| 130 | |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 131 | /* |
| 132 | * Parse the category mask. |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 133 | * The format can be this: category1:category2:category3 |
| 134 | * or category1,2:category2,3:... |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 135 | */ |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 136 | void debug_parse_category_mask(struct debug_target* target, const char *_mask) |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 137 | { |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 138 | int i = 0; |
| 139 | char *mask = strdup(_mask); |
| 140 | char *category_token = NULL; |
| 141 | |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 142 | /* Disable everything to enable it afterwards */ |
| 143 | for (i = 0; i < ARRAY_SIZE(target->categories); ++i) |
| 144 | target->categories[i].enabled = 0; |
| 145 | |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 146 | category_token = strtok(mask, ":"); |
| 147 | do { |
| 148 | for (i = 0; i < ARRAY_SIZE(debug_info); ++i) { |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 149 | char* colon = strstr(category_token, ","); |
| 150 | int length = strlen(category_token); |
| 151 | |
| 152 | if (colon) |
| 153 | length = colon - category_token; |
| 154 | |
| 155 | if (strncasecmp(debug_info[i].name, category_token, length) == 0) { |
| 156 | int number = debug_info[i].number; |
| 157 | int level = 0; |
| 158 | |
| 159 | if (colon) |
| 160 | level = atoi(colon+1); |
| 161 | |
| 162 | target->categories[number].enabled = 1; |
| 163 | target->categories[number].loglevel = level; |
| 164 | } |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 165 | } |
Holger Freyther | ca362a6 | 2009-01-04 21:05:01 +0000 | [diff] [blame] | 166 | } while ((category_token = strtok(NULL, ":"))); |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 167 | |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 168 | free(mask); |
Holger Freyther | d546e31 | 2008-12-27 12:03:07 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 171 | static const char* color(int subsys) |
Holger Freyther | 7c03e4c | 2008-12-27 12:46:48 +0000 | [diff] [blame] | 172 | { |
| 173 | int i = 0; |
| 174 | |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 175 | for (i = 0; i < ARRAY_SIZE(debug_info); ++i) { |
Holger Freyther | 7c03e4c | 2008-12-27 12:46:48 +0000 | [diff] [blame] | 176 | if (debug_info[i].number == subsys) |
| 177 | return debug_info[i].color; |
| 178 | } |
| 179 | |
| 180 | return ""; |
| 181 | } |
| 182 | |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 183 | static void _output(struct debug_target *target, unsigned int subsys, char *file, int line, |
| 184 | int cont, const char *format, va_list ap) |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 185 | { |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 186 | char col[30]; |
| 187 | char sub[30]; |
| 188 | char tim[30]; |
| 189 | char buf[4096]; |
| 190 | char final[4096]; |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 191 | |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 192 | /* prepare the data */ |
| 193 | col[0] = '\0'; |
| 194 | sub[0] = '\0'; |
| 195 | tim[0] = '\0'; |
| 196 | buf[0] = '\0'; |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 197 | |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 198 | /* are we using color */ |
Harald Welte | aa6c9ca | 2009-12-24 11:11:54 +0100 | [diff] [blame] | 199 | if (target->use_color) { |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 200 | snprintf(col, sizeof(col), "%s", color(subsys)); |
Harald Welte | aa6c9ca | 2009-12-24 11:11:54 +0100 | [diff] [blame] | 201 | col[sizeof(col)-1] = '\0'; |
| 202 | } |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 203 | vsnprintf(buf, sizeof(buf), format, ap); |
Harald Welte | aa6c9ca | 2009-12-24 11:11:54 +0100 | [diff] [blame] | 204 | buf[sizeof(buf)-1] = '\0'; |
Harald Welte | 6ddd168 | 2009-02-06 12:38:29 +0000 | [diff] [blame] | 205 | |
| 206 | if (!cont) { |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 207 | if (target->print_timestamp) { |
Harald Welte | d3ff51d | 2009-06-09 20:21:57 +0000 | [diff] [blame] | 208 | char *timestr; |
| 209 | time_t tm; |
| 210 | tm = time(NULL); |
| 211 | timestr = ctime(&tm); |
| 212 | timestr[strlen(timestr)-1] = '\0'; |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 213 | snprintf(tim, sizeof(tim), "%s ", timestr); |
Harald Welte | aa6c9ca | 2009-12-24 11:11:54 +0100 | [diff] [blame] | 214 | tim[sizeof(tim)-1] = '\0'; |
Harald Welte | d3ff51d | 2009-06-09 20:21:57 +0000 | [diff] [blame] | 215 | } |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 216 | snprintf(sub, sizeof(sub), "<%4.4x> %s:%d ", subsys, file, line); |
Harald Welte | aa6c9ca | 2009-12-24 11:11:54 +0100 | [diff] [blame] | 217 | sub[sizeof(sub)-1] = '\0'; |
Harald Welte | 6ddd168 | 2009-02-06 12:38:29 +0000 | [diff] [blame] | 218 | } |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 219 | |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 220 | snprintf(final, sizeof(final), "%s%s%s%s\033[0;m", col, tim, sub, buf); |
Harald Welte | aa6c9ca | 2009-12-24 11:11:54 +0100 | [diff] [blame] | 221 | final[sizeof(final)-1] = '\0'; |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 222 | target->output(target, final); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | static void _debugp(unsigned int subsys, int level, char *file, int line, |
| 227 | int cont, const char *format, va_list ap) |
| 228 | { |
| 229 | struct debug_target *tar; |
| 230 | |
| 231 | llist_for_each_entry(tar, &target_list, entry) { |
| 232 | struct debug_category *category; |
| 233 | int output = 0; |
| 234 | |
| 235 | category = &tar->categories[subsys]; |
| 236 | /* subsystem is not supposed to be debugged */ |
| 237 | if (!category->enabled) |
| 238 | continue; |
| 239 | |
| 240 | /* Check the global log level */ |
| 241 | if (tar->loglevel != 0 && level < tar->loglevel) |
| 242 | continue; |
| 243 | |
| 244 | /* Check the category log level */ |
| 245 | if (category->loglevel != 0 && level < category->loglevel) |
| 246 | continue; |
| 247 | |
| 248 | /* |
| 249 | * Apply filters here... if that becomes messy we will need to put |
| 250 | * filters in a list and each filter will say stop, continue, output |
| 251 | */ |
| 252 | if ((tar->filter_map & DEBUG_FILTER_ALL) != 0) { |
| 253 | output = 1; |
| 254 | } else if ((tar->filter_map & DEBUG_FILTER_IMSI) != 0 |
| 255 | && debug_context.subscr && strcmp(debug_context.subscr->imsi, tar->imsi_filter) == 0) { |
| 256 | output = 1; |
| 257 | } |
| 258 | |
Harald Welte | aa8989c | 2009-12-24 11:12:11 +0100 | [diff] [blame] | 259 | if (output) { |
| 260 | /* FIXME: copying the va_list is an ugly workaround against a bug |
| 261 | * hidden somewhere in _output. If we do not copy here, the first |
| 262 | * call to _output() will corrupt the va_list contents, and any |
| 263 | * further _output() calls with the same va_list will segfault */ |
| 264 | va_list bp; |
| 265 | va_copy(bp, ap); |
| 266 | _output(tar, subsys, file, line, cont, format, bp); |
Harald Welte | 7ed2529 | 2009-12-24 11:14:03 +0100 | [diff] [blame] | 267 | va_end(bp); |
Harald Welte | aa8989c | 2009-12-24 11:12:11 +0100 | [diff] [blame] | 268 | } |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
| 272 | void debugp(unsigned int subsys, char *file, int line, int cont, const char *format, ...) |
| 273 | { |
| 274 | va_list ap; |
| 275 | |
| 276 | va_start(ap, format); |
| 277 | _debugp(subsys, LOGL_DEBUG, file, line, cont, format, ap); |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 278 | va_end(ap); |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 279 | } |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 280 | |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 281 | void debugp2(unsigned int subsys, unsigned int level, char *file, int line, int cont, const char *format, ...) |
| 282 | { |
| 283 | va_list ap; |
| 284 | |
| 285 | va_start(ap, format); |
| 286 | _debugp(subsys, level, file, line, cont, format, ap); |
| 287 | va_end(ap); |
Holger Freyther | 32636e8 | 2008-12-27 11:07:15 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Harald Welte | 3cc4bf5 | 2009-02-28 13:08:01 +0000 | [diff] [blame] | 290 | static char hexd_buff[4096]; |
| 291 | |
Holger Hans Peter Freyther | e78074e | 2009-08-20 13:16:26 +0200 | [diff] [blame] | 292 | char *hexdump(const unsigned char *buf, int len) |
Holger Freyther | ca362a6 | 2009-01-04 21:05:01 +0000 | [diff] [blame] | 293 | { |
| 294 | int i; |
Harald Welte | 3cc4bf5 | 2009-02-28 13:08:01 +0000 | [diff] [blame] | 295 | char *cur = hexd_buff; |
| 296 | |
| 297 | hexd_buff[0] = 0; |
Holger Freyther | ca362a6 | 2009-01-04 21:05:01 +0000 | [diff] [blame] | 298 | for (i = 0; i < len; i++) { |
Harald Welte | 3cc4bf5 | 2009-02-28 13:08:01 +0000 | [diff] [blame] | 299 | int len_remain = sizeof(hexd_buff) - (cur - hexd_buff); |
| 300 | int rc = snprintf(cur, len_remain, "%02x ", buf[i]); |
| 301 | if (rc <= 0) |
| 302 | break; |
| 303 | cur += rc; |
Holger Freyther | ca362a6 | 2009-01-04 21:05:01 +0000 | [diff] [blame] | 304 | } |
Harald Welte | 3cc4bf5 | 2009-02-28 13:08:01 +0000 | [diff] [blame] | 305 | hexd_buff[sizeof(hexd_buff)-1] = 0; |
| 306 | return hexd_buff; |
Holger Freyther | ca362a6 | 2009-01-04 21:05:01 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Holger Hans Peter Freyther | b61e3b2 | 2009-12-22 22:32:51 +0100 | [diff] [blame] | 309 | |
| 310 | |
| 311 | void debug_add_target(struct debug_target *target) |
| 312 | { |
| 313 | llist_add_tail(&target->entry, &target_list); |
| 314 | } |
| 315 | |
| 316 | void debug_del_target(struct debug_target *target) |
| 317 | { |
| 318 | llist_del(&target->entry); |
| 319 | } |
| 320 | |
| 321 | void debug_reset_context(void) |
| 322 | { |
| 323 | memset(&debug_context, 0, sizeof(debug_context)); |
| 324 | } |
| 325 | |
| 326 | /* currently we are not reffing these */ |
| 327 | void debug_set_context(int ctx, void *value) |
| 328 | { |
| 329 | switch (ctx) { |
| 330 | case BSC_CTX_LCHAN: |
| 331 | debug_context.lchan = (struct gsm_lchan *) value; |
| 332 | break; |
| 333 | case BSC_CTX_SUBSCR: |
| 334 | debug_context.subscr = (struct gsm_subscriber *) value; |
| 335 | break; |
| 336 | case BSC_CTX_BTS: |
| 337 | debug_context.bts = (struct gsm_bts *) value; |
| 338 | break; |
| 339 | case BSC_CTX_SCCP: |
| 340 | break; |
| 341 | default: |
| 342 | break; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | void debug_set_imsi_filter(struct debug_target *target, const char *imsi) |
| 347 | { |
| 348 | if (imsi) { |
| 349 | target->filter_map |= DEBUG_FILTER_IMSI; |
| 350 | target->imsi_filter = talloc_strdup(target, imsi); |
| 351 | } else if (target->imsi_filter) { |
| 352 | target->filter_map &= ~DEBUG_FILTER_IMSI; |
| 353 | talloc_free(target->imsi_filter); |
| 354 | target->imsi_filter = NULL; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | void debug_set_all_filter(struct debug_target *target, int all) |
| 359 | { |
| 360 | if (all) |
| 361 | target->filter_map |= DEBUG_FILTER_ALL; |
| 362 | else |
| 363 | target->filter_map &= ~DEBUG_FILTER_ALL; |
| 364 | } |
| 365 | |
| 366 | void debug_set_use_color(struct debug_target *target, int use_color) |
| 367 | { |
| 368 | target->use_color = use_color; |
| 369 | } |
| 370 | |
| 371 | void debug_set_print_timestamp(struct debug_target *target, int print_timestamp) |
| 372 | { |
| 373 | target->print_timestamp = print_timestamp; |
| 374 | } |
| 375 | |
| 376 | void debug_set_log_level(struct debug_target *target, int log_level) |
| 377 | { |
| 378 | target->loglevel = log_level; |
| 379 | } |
| 380 | |
| 381 | void debug_set_category_filter(struct debug_target *target, int category, int enable, int level) |
| 382 | { |
| 383 | if (category >= Debug_LastEntry) |
| 384 | return; |
| 385 | target->categories[category].enabled = !!enable; |
| 386 | target->categories[category].loglevel = level; |
| 387 | } |
| 388 | |
| 389 | static void _stderr_output(struct debug_target *target, const char *log) |
| 390 | { |
| 391 | fprintf(target->tgt_stdout.out, "%s", log); |
| 392 | fflush(target->tgt_stdout.out); |
| 393 | } |
| 394 | |
| 395 | struct debug_target *debug_target_create(void) |
| 396 | { |
| 397 | struct debug_target *target; |
| 398 | |
| 399 | target = talloc_zero(tall_dbg_ctx, struct debug_target); |
| 400 | if (!target) |
| 401 | return NULL; |
| 402 | |
| 403 | INIT_LLIST_HEAD(&target->entry); |
| 404 | memcpy(target->categories, default_categories, sizeof(default_categories)); |
| 405 | target->use_color = 1; |
| 406 | target->print_timestamp = 0; |
| 407 | target->loglevel = 0; |
| 408 | return target; |
| 409 | } |
| 410 | |
| 411 | struct debug_target *debug_target_create_stderr(void) |
| 412 | { |
| 413 | struct debug_target *target; |
| 414 | |
| 415 | target = debug_target_create(); |
| 416 | if (!target) |
| 417 | return NULL; |
| 418 | |
| 419 | target->tgt_stdout.out = stderr; |
| 420 | target->output = _stderr_output; |
| 421 | return target; |
| 422 | } |
| 423 | |
| 424 | void debug_init(void) |
| 425 | { |
| 426 | tall_dbg_ctx = talloc_named_const(NULL, 1, "debug"); |
| 427 | } |