Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1 | /* |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 2 | * net/tipc/dbg.c: TIPC print buffer routines for debugging |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 3 | * |
Per Liden | 593a5f2 | 2006-01-11 19:14:19 +0100 | [diff] [blame] | 4 | * Copyright (c) 1996-2006, Ericsson AB |
Allan Stephens | fb98ec7 | 2008-05-05 01:20:42 -0700 | [diff] [blame] | 5 | * Copyright (c) 2005-2007, Wind River Systems |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 6 | * All rights reserved. |
| 7 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 8 | * Redistribution and use in source and binary forms, with or without |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. Neither the names of the copyright holders nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 19 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 20 | * Alternatively, this software may be distributed under the terms of the |
| 21 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 22 | * Software Foundation. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 34 | * POSSIBILITY OF SUCH DAMAGE. |
| 35 | */ |
| 36 | |
| 37 | #include "core.h" |
| 38 | #include "config.h" |
| 39 | #include "dbg.h" |
| 40 | |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 41 | static char print_string[TIPC_PB_MAX_STR]; |
Ingo Molnar | 34af946 | 2006-06-27 02:53:55 -0700 | [diff] [blame] | 42 | static DEFINE_SPINLOCK(print_lock); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 43 | |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 44 | static struct print_buf null_buf = { NULL, 0, NULL, NULL }; |
| 45 | struct print_buf *TIPC_NULL = &null_buf; |
| 46 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 47 | static struct print_buf cons_buf = { NULL, 0, NULL, NULL }; |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 48 | struct print_buf *TIPC_CONS = &cons_buf; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 49 | |
| 50 | static struct print_buf log_buf = { NULL, 0, NULL, NULL }; |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 51 | struct print_buf *TIPC_LOG = &log_buf; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 52 | |
| 53 | |
| 54 | #define FORMAT(PTR,LEN,FMT) \ |
| 55 | {\ |
| 56 | va_list args;\ |
| 57 | va_start(args, FMT);\ |
| 58 | LEN = vsprintf(PTR, FMT, args);\ |
| 59 | va_end(args);\ |
| 60 | *(PTR + LEN) = '\0';\ |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Locking policy when using print buffers. |
| 65 | * |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 66 | * The following routines use 'print_lock' for protection: |
| 67 | * 1) tipc_printf() - to protect its print buffer(s) and 'print_string' |
| 68 | * 2) TIPC_TEE() - to protect its print buffer(s) |
| 69 | * 3) tipc_dump() - to protect its print buffer(s) and 'print_string' |
| 70 | * 4) tipc_log_XXX() - to protect TIPC_LOG |
| 71 | * |
| 72 | * All routines of the form tipc_printbuf_XXX() rely on the caller to prevent |
| 73 | * simultaneous use of the print buffer(s) being manipulated. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 74 | */ |
| 75 | |
| 76 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 77 | * tipc_printbuf_init - initialize print buffer to empty |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 78 | * @pb: pointer to print buffer structure |
| 79 | * @raw: pointer to character array used by print buffer |
| 80 | * @size: size of character array |
| 81 | * |
| 82 | * Makes the print buffer a null device that discards anything written to it |
| 83 | * if the character array is too small (or absent). |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 84 | */ |
| 85 | |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 86 | void tipc_printbuf_init(struct print_buf *pb, char *raw, u32 size) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 87 | { |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 88 | pb->buf = raw; |
| 89 | pb->crs = raw; |
| 90 | pb->size = size; |
Sam Ravnborg | 1fc54d8 | 2006-03-20 22:36:47 -0800 | [diff] [blame] | 91 | pb->next = NULL; |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 92 | |
| 93 | if (size < TIPC_PB_MIN_SIZE) { |
| 94 | pb->buf = NULL; |
| 95 | } else if (raw) { |
| 96 | pb->buf[0] = 0; |
| 97 | pb->buf[size-1] = ~0; |
| 98 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 102 | * tipc_printbuf_reset - reinitialize print buffer to empty state |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 103 | * @pb: pointer to print buffer structure |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 104 | */ |
| 105 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 106 | void tipc_printbuf_reset(struct print_buf *pb) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 107 | { |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 108 | tipc_printbuf_init(pb, pb->buf, pb->size); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 112 | * tipc_printbuf_empty - test if print buffer is in empty state |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 113 | * @pb: pointer to print buffer structure |
| 114 | * |
| 115 | * Returns non-zero if print buffer is empty. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 116 | */ |
| 117 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 118 | int tipc_printbuf_empty(struct print_buf *pb) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 119 | { |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 120 | return (!pb->buf || (pb->crs == pb->buf)); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 124 | * tipc_printbuf_validate - check for print buffer overflow |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 125 | * @pb: pointer to print buffer structure |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 126 | * |
| 127 | * Verifies that a print buffer has captured all data written to it. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 128 | * If data has been lost, linearize buffer and prepend an error message |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 129 | * |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 130 | * Returns length of print buffer data string (including trailing NUL) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 131 | */ |
| 132 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 133 | int tipc_printbuf_validate(struct print_buf *pb) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 134 | { |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 135 | char *err = "\n\n*** PRINT BUFFER OVERFLOW ***\n\n"; |
| 136 | char *cp_buf; |
| 137 | struct print_buf cb; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 138 | |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 139 | if (!pb->buf) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 140 | return 0; |
| 141 | |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 142 | if (pb->buf[pb->size - 1] == 0) { |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 143 | cp_buf = kmalloc(pb->size, GFP_ATOMIC); |
| 144 | if (cp_buf != NULL){ |
| 145 | tipc_printbuf_init(&cb, cp_buf, pb->size); |
| 146 | tipc_printbuf_move(&cb, pb); |
| 147 | tipc_printbuf_move(pb, &cb); |
| 148 | kfree(cp_buf); |
| 149 | memcpy(pb->buf, err, strlen(err)); |
| 150 | } else { |
| 151 | tipc_printbuf_reset(pb); |
| 152 | tipc_printf(pb, err); |
| 153 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 154 | } |
| 155 | return (pb->crs - pb->buf + 1); |
| 156 | } |
| 157 | |
| 158 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 159 | * tipc_printbuf_move - move print buffer contents to another print buffer |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 160 | * @pb_to: pointer to destination print buffer structure |
| 161 | * @pb_from: pointer to source print buffer structure |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 162 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 163 | * Current contents of destination print buffer (if any) are discarded. |
| 164 | * Source print buffer becomes empty if a successful move occurs. |
| 165 | */ |
| 166 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 167 | void tipc_printbuf_move(struct print_buf *pb_to, struct print_buf *pb_from) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 168 | { |
| 169 | int len; |
| 170 | |
| 171 | /* Handle the cases where contents can't be moved */ |
| 172 | |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 173 | if (!pb_to->buf) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 174 | return; |
| 175 | |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 176 | if (!pb_from->buf) { |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 177 | tipc_printbuf_reset(pb_to); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | |
| 181 | if (pb_to->size < pb_from->size) { |
Allan Stephens | 93758c3 | 2008-05-05 01:21:12 -0700 | [diff] [blame^] | 182 | strcpy(pb_to->buf, "*** PRINT BUFFER MOVE ERROR ***"); |
| 183 | pb_to->buf[pb_to->size - 1] = ~0; |
| 184 | pb_to->crs = strchr(pb_to->buf, 0); |
| 185 | pb_to->next = NULL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 186 | return; |
| 187 | } |
| 188 | |
| 189 | /* Copy data from char after cursor to end (if used) */ |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 190 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 191 | len = pb_from->buf + pb_from->size - pb_from->crs - 2; |
| 192 | if ((pb_from->buf[pb_from->size-1] == 0) && (len > 0)) { |
| 193 | strcpy(pb_to->buf, pb_from->crs + 1); |
| 194 | pb_to->crs = pb_to->buf + len; |
| 195 | } else |
| 196 | pb_to->crs = pb_to->buf; |
| 197 | |
| 198 | /* Copy data from start to cursor (always) */ |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 199 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 200 | len = pb_from->crs - pb_from->buf; |
| 201 | strcpy(pb_to->crs, pb_from->buf); |
| 202 | pb_to->crs += len; |
| 203 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 204 | tipc_printbuf_reset(pb_from); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | /** |
| 208 | * tipc_printf - append formatted output to print buffer chain |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 209 | * @pb: pointer to chain of print buffers (may be NULL) |
| 210 | * @fmt: formatted info to be printed |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 211 | */ |
| 212 | |
| 213 | void tipc_printf(struct print_buf *pb, const char *fmt, ...) |
| 214 | { |
| 215 | int chars_to_add; |
| 216 | int chars_left; |
| 217 | char save_char; |
| 218 | struct print_buf *pb_next; |
| 219 | |
| 220 | spin_lock_bh(&print_lock); |
| 221 | FORMAT(print_string, chars_to_add, fmt); |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 222 | if (chars_to_add >= TIPC_PB_MAX_STR) |
| 223 | strcpy(print_string, "*** PRINT BUFFER STRING TOO LONG ***"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 224 | |
| 225 | while (pb) { |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 226 | if (pb == TIPC_CONS) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 227 | printk(print_string); |
| 228 | else if (pb->buf) { |
| 229 | chars_left = pb->buf + pb->size - pb->crs - 1; |
| 230 | if (chars_to_add <= chars_left) { |
| 231 | strcpy(pb->crs, print_string); |
| 232 | pb->crs += chars_to_add; |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 233 | } else if (chars_to_add >= (pb->size - 1)) { |
| 234 | strcpy(pb->buf, print_string + chars_to_add + 1 |
| 235 | - pb->size); |
| 236 | pb->crs = pb->buf + pb->size - 1; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 237 | } else { |
| 238 | strcpy(pb->buf, print_string + chars_left); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 239 | save_char = print_string[chars_left]; |
| 240 | print_string[chars_left] = 0; |
| 241 | strcpy(pb->crs, print_string); |
| 242 | print_string[chars_left] = save_char; |
| 243 | pb->crs = pb->buf + chars_to_add - chars_left; |
| 244 | } |
| 245 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 246 | pb_next = pb->next; |
Sam Ravnborg | 1fc54d8 | 2006-03-20 22:36:47 -0800 | [diff] [blame] | 247 | pb->next = NULL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 248 | pb = pb_next; |
| 249 | } |
| 250 | spin_unlock_bh(&print_lock); |
| 251 | } |
| 252 | |
| 253 | /** |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 254 | * TIPC_TEE - perform next output operation on both print buffers |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 255 | * @b0: pointer to chain of print buffers (may be NULL) |
| 256 | * @b1: pointer to print buffer to add to chain |
| 257 | * |
| 258 | * Returns pointer to print buffer chain. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 259 | */ |
| 260 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 261 | struct print_buf *TIPC_TEE(struct print_buf *b0, struct print_buf *b1) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 262 | { |
| 263 | struct print_buf *pb = b0; |
| 264 | |
| 265 | if (!b0 || (b0 == b1)) |
| 266 | return b1; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 267 | |
| 268 | spin_lock_bh(&print_lock); |
| 269 | while (pb->next) { |
| 270 | if ((pb->next == b1) || (pb->next == b0)) |
| 271 | pb->next = pb->next->next; |
| 272 | else |
| 273 | pb = pb->next; |
| 274 | } |
| 275 | pb->next = b1; |
| 276 | spin_unlock_bh(&print_lock); |
| 277 | return b0; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * print_to_console - write string of bytes to console in multiple chunks |
| 282 | */ |
| 283 | |
| 284 | static void print_to_console(char *crs, int len) |
| 285 | { |
| 286 | int rest = len; |
| 287 | |
| 288 | while (rest > 0) { |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 289 | int sz = rest < TIPC_PB_MAX_STR ? rest : TIPC_PB_MAX_STR; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 290 | char c = crs[sz]; |
| 291 | |
| 292 | crs[sz] = 0; |
| 293 | printk((const char *)crs); |
| 294 | crs[sz] = c; |
| 295 | rest -= sz; |
| 296 | crs += sz; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * printbuf_dump - write print buffer contents to console |
| 302 | */ |
| 303 | |
| 304 | static void printbuf_dump(struct print_buf *pb) |
| 305 | { |
| 306 | int len; |
| 307 | |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 308 | if (!pb->buf) { |
| 309 | printk("*** PRINT BUFFER NOT ALLOCATED ***"); |
| 310 | return; |
| 311 | } |
| 312 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 313 | /* Dump print buffer from char after cursor to end (if used) */ |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 314 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 315 | len = pb->buf + pb->size - pb->crs - 2; |
| 316 | if ((pb->buf[pb->size - 1] == 0) && (len > 0)) |
| 317 | print_to_console(pb->crs + 1, len); |
| 318 | |
| 319 | /* Dump print buffer from start to cursor (always) */ |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 320 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 321 | len = pb->crs - pb->buf; |
| 322 | print_to_console(pb->buf, len); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * tipc_dump - dump non-console print buffer(s) to console |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 327 | * @pb: pointer to chain of print buffers |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 328 | */ |
| 329 | |
| 330 | void tipc_dump(struct print_buf *pb, const char *fmt, ...) |
| 331 | { |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 332 | struct print_buf *pb_next; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 333 | int len; |
| 334 | |
| 335 | spin_lock_bh(&print_lock); |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 336 | FORMAT(print_string, len, fmt); |
| 337 | printk(print_string); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 338 | |
| 339 | for (; pb; pb = pb->next) { |
Allan Stephens | 29ede24 | 2006-10-16 21:42:04 -0700 | [diff] [blame] | 340 | if (pb != TIPC_CONS) { |
| 341 | printk("\n---- Start of %s log dump ----\n\n", |
| 342 | (pb == TIPC_LOG) ? "global" : "local"); |
| 343 | printbuf_dump(pb); |
| 344 | tipc_printbuf_reset(pb); |
| 345 | printk("\n---- End of dump ----\n"); |
| 346 | } |
| 347 | pb_next = pb->next; |
| 348 | pb->next = NULL; |
| 349 | pb = pb_next; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 350 | } |
| 351 | spin_unlock_bh(&print_lock); |
| 352 | } |
| 353 | |
| 354 | /** |
Allan Stephens | 025adbe | 2008-05-05 01:20:04 -0700 | [diff] [blame] | 355 | * tipc_log_resize - change the size of the TIPC log buffer |
| 356 | * @log_size: print buffer size to use |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 357 | */ |
| 358 | |
Allan Stephens | fb98ec7 | 2008-05-05 01:20:42 -0700 | [diff] [blame] | 359 | int tipc_log_resize(int log_size) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 360 | { |
Allan Stephens | fb98ec7 | 2008-05-05 01:20:42 -0700 | [diff] [blame] | 361 | int res = 0; |
| 362 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 363 | spin_lock_bh(&print_lock); |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 364 | if (TIPC_LOG->buf) { |
| 365 | kfree(TIPC_LOG->buf); |
| 366 | TIPC_LOG->buf = NULL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 367 | } |
Allan Stephens | 025adbe | 2008-05-05 01:20:04 -0700 | [diff] [blame] | 368 | if (log_size) { |
| 369 | if (log_size < TIPC_PB_MIN_SIZE) |
| 370 | log_size = TIPC_PB_MIN_SIZE; |
| 371 | tipc_printbuf_init(TIPC_LOG, kmalloc(log_size, GFP_ATOMIC), |
| 372 | log_size); |
Allan Stephens | fb98ec7 | 2008-05-05 01:20:42 -0700 | [diff] [blame] | 373 | res = !TIPC_LOG->buf; |
Allan Stephens | 025adbe | 2008-05-05 01:20:04 -0700 | [diff] [blame] | 374 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 375 | spin_unlock_bh(&print_lock); |
Allan Stephens | fb98ec7 | 2008-05-05 01:20:42 -0700 | [diff] [blame] | 376 | |
| 377 | return res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | /** |
Allan Stephens | 025adbe | 2008-05-05 01:20:04 -0700 | [diff] [blame] | 381 | * tipc_log_resize_cmd - reconfigure size of TIPC log buffer |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 382 | */ |
| 383 | |
Allan Stephens | 025adbe | 2008-05-05 01:20:04 -0700 | [diff] [blame] | 384 | struct sk_buff *tipc_log_resize_cmd(const void *req_tlv_area, int req_tlv_space) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 385 | { |
| 386 | u32 value; |
| 387 | |
| 388 | if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED)) |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 389 | return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 390 | |
Al Viro | 3e6c8cd | 2006-11-08 00:19:09 -0800 | [diff] [blame] | 391 | value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area)); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 392 | if (value != delimit(value, 0, 32768)) |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 393 | return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE |
| 394 | " (log size must be 0-32768)"); |
Allan Stephens | fb98ec7 | 2008-05-05 01:20:42 -0700 | [diff] [blame] | 395 | if (tipc_log_resize(value)) |
| 396 | return tipc_cfg_reply_error_string( |
| 397 | "unable to create specified log (log size is now 0)"); |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 398 | return tipc_cfg_reply_none(); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 402 | * tipc_log_dump - capture TIPC log buffer contents in configuration message |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 403 | */ |
| 404 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 405 | struct sk_buff *tipc_log_dump(void) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 406 | { |
| 407 | struct sk_buff *reply; |
| 408 | |
| 409 | spin_lock_bh(&print_lock); |
Allan Stephens | 93758c3 | 2008-05-05 01:21:12 -0700 | [diff] [blame^] | 410 | if (!TIPC_LOG->buf) { |
| 411 | spin_unlock_bh(&print_lock); |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 412 | reply = tipc_cfg_reply_ultra_string("log not activated\n"); |
Allan Stephens | 93758c3 | 2008-05-05 01:21:12 -0700 | [diff] [blame^] | 413 | } else if (tipc_printbuf_empty(TIPC_LOG)) { |
| 414 | spin_unlock_bh(&print_lock); |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 415 | reply = tipc_cfg_reply_ultra_string("log is empty\n"); |
Allan Stephens | 93758c3 | 2008-05-05 01:21:12 -0700 | [diff] [blame^] | 416 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 417 | else { |
| 418 | struct tlv_desc *rep_tlv; |
| 419 | struct print_buf pb; |
| 420 | int str_len; |
| 421 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 422 | str_len = min(TIPC_LOG->size, 32768u); |
Allan Stephens | 93758c3 | 2008-05-05 01:21:12 -0700 | [diff] [blame^] | 423 | spin_unlock_bh(&print_lock); |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 424 | reply = tipc_cfg_reply_alloc(TLV_SPACE(str_len)); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 425 | if (reply) { |
| 426 | rep_tlv = (struct tlv_desc *)reply->data; |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 427 | tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), str_len); |
Allan Stephens | 93758c3 | 2008-05-05 01:21:12 -0700 | [diff] [blame^] | 428 | spin_lock_bh(&print_lock); |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 429 | tipc_printbuf_move(&pb, TIPC_LOG); |
Allan Stephens | 93758c3 | 2008-05-05 01:21:12 -0700 | [diff] [blame^] | 430 | spin_unlock_bh(&print_lock); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 431 | str_len = strlen(TLV_DATA(rep_tlv)) + 1; |
| 432 | skb_put(reply, TLV_SPACE(str_len)); |
| 433 | TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len); |
| 434 | } |
| 435 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 436 | return reply; |
| 437 | } |
| 438 | |