at v3.5-rc5 337 lines 9.9 kB view raw
1/* 2 * net/tipc/log.c: TIPC print buffer routines for debugging 3 * 4 * Copyright (c) 1996-2006, Ericsson AB 5 * Copyright (c) 2005-2007, Wind River Systems 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 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. 19 * 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 34 * POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37#include "core.h" 38#include "config.h" 39#include "log.h" 40 41/* 42 * TIPC pre-defines the following print buffers: 43 * 44 * TIPC_NULL : null buffer (i.e. print nowhere) 45 * TIPC_CONS : system console 46 * TIPC_LOG : TIPC log buffer 47 * 48 * Additional user-defined print buffers are also permitted. 49 */ 50static struct print_buf null_buf = { NULL, 0, NULL, 0 }; 51struct print_buf *const TIPC_NULL = &null_buf; 52 53static struct print_buf cons_buf = { NULL, 0, NULL, 1 }; 54struct print_buf *const TIPC_CONS = &cons_buf; 55 56static struct print_buf log_buf = { NULL, 0, NULL, 1 }; 57struct print_buf *const TIPC_LOG = &log_buf; 58 59/* 60 * Locking policy when using print buffers. 61 * 62 * 1) tipc_printf() uses 'print_lock' to protect against concurrent access to 63 * 'print_string' when writing to a print buffer. This also protects against 64 * concurrent writes to the print buffer being written to. 65 * 66 * 2) tipc_log_XXX() leverages the aforementioned use of 'print_lock' to 67 * protect against all types of concurrent operations on their associated 68 * print buffer (not just write operations). 69 * 70 * Note: All routines of the form tipc_printbuf_XXX() are lock-free, and rely 71 * on the caller to prevent simultaneous use of the print buffer(s) being 72 * manipulated. 73 */ 74static char print_string[TIPC_PB_MAX_STR]; 75static DEFINE_SPINLOCK(print_lock); 76 77static void tipc_printbuf_move(struct print_buf *pb_to, 78 struct print_buf *pb_from); 79 80#define FORMAT(PTR, LEN, FMT) \ 81{\ 82 va_list args;\ 83 va_start(args, FMT);\ 84 LEN = vsprintf(PTR, FMT, args);\ 85 va_end(args);\ 86 *(PTR + LEN) = '\0';\ 87} 88 89/** 90 * tipc_printbuf_init - initialize print buffer to empty 91 * @pb: pointer to print buffer structure 92 * @raw: pointer to character array used by print buffer 93 * @size: size of character array 94 * 95 * Note: If the character array is too small (or absent), the print buffer 96 * becomes a null device that discards anything written to it. 97 */ 98void tipc_printbuf_init(struct print_buf *pb, char *raw, u32 size) 99{ 100 pb->buf = raw; 101 pb->crs = raw; 102 pb->size = size; 103 pb->echo = 0; 104 105 if (size < TIPC_PB_MIN_SIZE) { 106 pb->buf = NULL; 107 } else if (raw) { 108 pb->buf[0] = 0; 109 pb->buf[size - 1] = ~0; 110 } 111} 112 113/** 114 * tipc_printbuf_reset - reinitialize print buffer to empty state 115 * @pb: pointer to print buffer structure 116 */ 117static void tipc_printbuf_reset(struct print_buf *pb) 118{ 119 if (pb->buf) { 120 pb->crs = pb->buf; 121 pb->buf[0] = 0; 122 pb->buf[pb->size - 1] = ~0; 123 } 124} 125 126/** 127 * tipc_printbuf_empty - test if print buffer is in empty state 128 * @pb: pointer to print buffer structure 129 * 130 * Returns non-zero if print buffer is empty. 131 */ 132static int tipc_printbuf_empty(struct print_buf *pb) 133{ 134 return !pb->buf || (pb->crs == pb->buf); 135} 136 137/** 138 * tipc_printbuf_validate - check for print buffer overflow 139 * @pb: pointer to print buffer structure 140 * 141 * Verifies that a print buffer has captured all data written to it. 142 * If data has been lost, linearize buffer and prepend an error message 143 * 144 * Returns length of print buffer data string (including trailing NUL) 145 */ 146int tipc_printbuf_validate(struct print_buf *pb) 147{ 148 char *err = "\n\n*** PRINT BUFFER OVERFLOW ***\n\n"; 149 char *cp_buf; 150 struct print_buf cb; 151 152 if (!pb->buf) 153 return 0; 154 155 if (pb->buf[pb->size - 1] == 0) { 156 cp_buf = kmalloc(pb->size, GFP_ATOMIC); 157 if (cp_buf) { 158 tipc_printbuf_init(&cb, cp_buf, pb->size); 159 tipc_printbuf_move(&cb, pb); 160 tipc_printbuf_move(pb, &cb); 161 kfree(cp_buf); 162 memcpy(pb->buf, err, strlen(err)); 163 } else { 164 tipc_printbuf_reset(pb); 165 tipc_printf(pb, err); 166 } 167 } 168 return pb->crs - pb->buf + 1; 169} 170 171/** 172 * tipc_printbuf_move - move print buffer contents to another print buffer 173 * @pb_to: pointer to destination print buffer structure 174 * @pb_from: pointer to source print buffer structure 175 * 176 * Current contents of destination print buffer (if any) are discarded. 177 * Source print buffer becomes empty if a successful move occurs. 178 */ 179static void tipc_printbuf_move(struct print_buf *pb_to, 180 struct print_buf *pb_from) 181{ 182 int len; 183 184 /* Handle the cases where contents can't be moved */ 185 if (!pb_to->buf) 186 return; 187 188 if (!pb_from->buf) { 189 tipc_printbuf_reset(pb_to); 190 return; 191 } 192 193 if (pb_to->size < pb_from->size) { 194 strcpy(pb_to->buf, "*** PRINT BUFFER MOVE ERROR ***"); 195 pb_to->buf[pb_to->size - 1] = ~0; 196 pb_to->crs = strchr(pb_to->buf, 0); 197 return; 198 } 199 200 /* Copy data from char after cursor to end (if used) */ 201 len = pb_from->buf + pb_from->size - pb_from->crs - 2; 202 if ((pb_from->buf[pb_from->size - 1] == 0) && (len > 0)) { 203 strcpy(pb_to->buf, pb_from->crs + 1); 204 pb_to->crs = pb_to->buf + len; 205 } else 206 pb_to->crs = pb_to->buf; 207 208 /* Copy data from start to cursor (always) */ 209 len = pb_from->crs - pb_from->buf; 210 strcpy(pb_to->crs, pb_from->buf); 211 pb_to->crs += len; 212 213 tipc_printbuf_reset(pb_from); 214} 215 216/** 217 * tipc_printf - append formatted output to print buffer 218 * @pb: pointer to print buffer 219 * @fmt: formatted info to be printed 220 */ 221void tipc_printf(struct print_buf *pb, const char *fmt, ...) 222{ 223 int chars_to_add; 224 int chars_left; 225 char save_char; 226 227 spin_lock_bh(&print_lock); 228 229 FORMAT(print_string, chars_to_add, fmt); 230 if (chars_to_add >= TIPC_PB_MAX_STR) 231 strcpy(print_string, "*** PRINT BUFFER STRING TOO LONG ***"); 232 233 if (pb->buf) { 234 chars_left = pb->buf + pb->size - pb->crs - 1; 235 if (chars_to_add <= chars_left) { 236 strcpy(pb->crs, print_string); 237 pb->crs += chars_to_add; 238 } else if (chars_to_add >= (pb->size - 1)) { 239 strcpy(pb->buf, print_string + chars_to_add + 1 240 - pb->size); 241 pb->crs = pb->buf + pb->size - 1; 242 } else { 243 strcpy(pb->buf, print_string + chars_left); 244 save_char = print_string[chars_left]; 245 print_string[chars_left] = 0; 246 strcpy(pb->crs, print_string); 247 print_string[chars_left] = save_char; 248 pb->crs = pb->buf + chars_to_add - chars_left; 249 } 250 } 251 252 if (pb->echo) 253 printk("%s", print_string); 254 255 spin_unlock_bh(&print_lock); 256} 257 258/** 259 * tipc_log_resize - change the size of the TIPC log buffer 260 * @log_size: print buffer size to use 261 */ 262int tipc_log_resize(int log_size) 263{ 264 int res = 0; 265 266 spin_lock_bh(&print_lock); 267 kfree(TIPC_LOG->buf); 268 TIPC_LOG->buf = NULL; 269 if (log_size) { 270 if (log_size < TIPC_PB_MIN_SIZE) 271 log_size = TIPC_PB_MIN_SIZE; 272 res = TIPC_LOG->echo; 273 tipc_printbuf_init(TIPC_LOG, kmalloc(log_size, GFP_ATOMIC), 274 log_size); 275 TIPC_LOG->echo = res; 276 res = !TIPC_LOG->buf; 277 } 278 spin_unlock_bh(&print_lock); 279 280 return res; 281} 282 283/** 284 * tipc_log_resize_cmd - reconfigure size of TIPC log buffer 285 */ 286struct sk_buff *tipc_log_resize_cmd(const void *req_tlv_area, int req_tlv_space) 287{ 288 u32 value; 289 290 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED)) 291 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR); 292 293 value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area)); 294 if (value > 32768) 295 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE 296 " (log size must be 0-32768)"); 297 if (tipc_log_resize(value)) 298 return tipc_cfg_reply_error_string( 299 "unable to create specified log (log size is now 0)"); 300 return tipc_cfg_reply_none(); 301} 302 303/** 304 * tipc_log_dump - capture TIPC log buffer contents in configuration message 305 */ 306struct sk_buff *tipc_log_dump(void) 307{ 308 struct sk_buff *reply; 309 310 spin_lock_bh(&print_lock); 311 if (!TIPC_LOG->buf) { 312 spin_unlock_bh(&print_lock); 313 reply = tipc_cfg_reply_ultra_string("log not activated\n"); 314 } else if (tipc_printbuf_empty(TIPC_LOG)) { 315 spin_unlock_bh(&print_lock); 316 reply = tipc_cfg_reply_ultra_string("log is empty\n"); 317 } else { 318 struct tlv_desc *rep_tlv; 319 struct print_buf pb; 320 int str_len; 321 322 str_len = min(TIPC_LOG->size, 32768u); 323 spin_unlock_bh(&print_lock); 324 reply = tipc_cfg_reply_alloc(TLV_SPACE(str_len)); 325 if (reply) { 326 rep_tlv = (struct tlv_desc *)reply->data; 327 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), str_len); 328 spin_lock_bh(&print_lock); 329 tipc_printbuf_move(&pb, TIPC_LOG); 330 spin_unlock_bh(&print_lock); 331 str_len = strlen(TLV_DATA(rep_tlv)) + 1; 332 skb_put(reply, TLV_SPACE(str_len)); 333 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len); 334 } 335 } 336 return reply; 337}