2 * net/tipc/dbg.c: TIPC print buffer routines for debuggign
4 * Copyright (c) 2003-2005, Ericsson Research Canada
5 * Copyright (c) 2005, Wind River Systems
6 * Copyright (c) 2005-2006, Ericsson AB
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
21 * Alternatively, this software may be distributed under the terms of the
22 * GNU General Public License ("GPL") version 2 as published by the Free
23 * Software Foundation.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
42 #define MAX_STRING 512
44 static char print_string[MAX_STRING];
45 static spinlock_t print_lock = SPIN_LOCK_UNLOCKED;
47 static struct print_buf cons_buf = { NULL, 0, NULL, NULL };
48 struct print_buf *CONS = &cons_buf;
50 static struct print_buf log_buf = { NULL, 0, NULL, NULL };
51 struct print_buf *LOG = &log_buf;
54 #define FORMAT(PTR,LEN,FMT) \
58 LEN = vsprintf(PTR, FMT, args);\
64 * Locking policy when using print buffers.
66 * 1) Routines of the form printbuf_XXX() rely on the caller to prevent
67 * simultaneous use of the print buffer(s) being manipulated.
68 * 2) tipc_printf() uses 'print_lock' to prevent simultaneous use of
69 * 'print_string' and to protect its print buffer(s).
70 * 3) TEE() uses 'print_lock' to protect its print buffer(s).
71 * 4) Routines of the form log_XXX() uses 'print_lock' to protect LOG.
75 * printbuf_init - initialize print buffer to empty
78 void printbuf_init(struct print_buf *pb, char *raw, u32 sz)
80 if (!pb || !raw || (sz < (MAX_STRING + 1)))
83 pb->crs = pb->buf = raw;
91 * printbuf_reset - reinitialize print buffer to empty state
94 void printbuf_reset(struct print_buf *pb)
97 printbuf_init(pb, pb->buf, pb->size);
101 * printbuf_empty - test if print buffer is in empty state
104 int printbuf_empty(struct print_buf *pb)
106 return (!pb || !pb->buf || (pb->crs == pb->buf));
110 * printbuf_validate - check for print buffer overflow
112 * Verifies that a print buffer has captured all data written to it.
113 * If data has been lost, linearize buffer and prepend an error message
115 * Returns length of print buffer data string (including trailing NULL)
118 int printbuf_validate(struct print_buf *pb)
120 char *err = " *** PRINT BUFFER WRAPPED AROUND ***\n";
127 if (pb->buf[pb->size - 1] == '\0') {
128 cp_buf = kmalloc(pb->size, GFP_ATOMIC);
130 printbuf_init(&cb, cp_buf, pb->size);
131 printbuf_move(&cb, pb);
132 printbuf_move(pb, &cb);
134 memcpy(pb->buf, err, strlen(err));
137 tipc_printf(pb, err);
140 return (pb->crs - pb->buf + 1);
144 * printbuf_move - move print buffer contents to another print buffer
146 * Current contents of destination print buffer (if any) are discarded.
147 * Source print buffer becomes empty if a successful move occurs.
150 void printbuf_move(struct print_buf *pb_to, struct print_buf *pb_from)
154 /* Handle the cases where contents can't be moved */
156 if (!pb_to || !pb_to->buf)
159 if (!pb_from || !pb_from->buf) {
160 printbuf_reset(pb_to);
164 if (pb_to->size < pb_from->size) {
165 printbuf_reset(pb_to);
166 tipc_printf(pb_to, "*** PRINT BUFFER OVERFLOW ***");
170 /* Copy data from char after cursor to end (if used) */
171 len = pb_from->buf + pb_from->size - pb_from->crs - 2;
172 if ((pb_from->buf[pb_from->size-1] == 0) && (len > 0)) {
173 strcpy(pb_to->buf, pb_from->crs + 1);
174 pb_to->crs = pb_to->buf + len;
176 pb_to->crs = pb_to->buf;
178 /* Copy data from start to cursor (always) */
179 len = pb_from->crs - pb_from->buf;
180 strcpy(pb_to->crs, pb_from->buf);
183 printbuf_reset(pb_from);
187 * tipc_printf - append formatted output to print buffer chain
190 void tipc_printf(struct print_buf *pb, const char *fmt, ...)
195 struct print_buf *pb_next;
197 spin_lock_bh(&print_lock);
198 FORMAT(print_string, chars_to_add, fmt);
199 if (chars_to_add >= MAX_STRING)
200 strcpy(print_string, "*** STRING TOO LONG ***");
204 printk(print_string);
206 chars_left = pb->buf + pb->size - pb->crs - 1;
207 if (chars_to_add <= chars_left) {
208 strcpy(pb->crs, print_string);
209 pb->crs += chars_to_add;
211 strcpy(pb->buf, print_string + chars_left);
212 save_char = print_string[chars_left];
213 print_string[chars_left] = 0;
214 strcpy(pb->crs, print_string);
215 print_string[chars_left] = save_char;
216 pb->crs = pb->buf + chars_to_add - chars_left;
223 spin_unlock_bh(&print_lock);
227 * TEE - perform next output operation on both print buffers
230 struct print_buf *TEE(struct print_buf *b0, struct print_buf *b1)
232 struct print_buf *pb = b0;
234 if (!b0 || (b0 == b1))
239 spin_lock_bh(&print_lock);
241 if ((pb->next == b1) || (pb->next == b0))
242 pb->next = pb->next->next;
247 spin_unlock_bh(&print_lock);
252 * print_to_console - write string of bytes to console in multiple chunks
255 static void print_to_console(char *crs, int len)
260 int sz = rest < MAX_STRING ? rest : MAX_STRING;
264 printk((const char *)crs);
272 * printbuf_dump - write print buffer contents to console
275 static void printbuf_dump(struct print_buf *pb)
279 /* Dump print buffer from char after cursor to end (if used) */
280 len = pb->buf + pb->size - pb->crs - 2;
281 if ((pb->buf[pb->size - 1] == 0) && (len > 0))
282 print_to_console(pb->crs + 1, len);
284 /* Dump print buffer from start to cursor (always) */
285 len = pb->crs - pb->buf;
286 print_to_console(pb->buf, len);
290 * tipc_dump - dump non-console print buffer(s) to console
293 void tipc_dump(struct print_buf *pb, const char *fmt, ...)
297 spin_lock_bh(&print_lock);
298 FORMAT(CONS->buf, len, fmt);
301 for (; pb; pb = pb->next) {
304 printk("\n---- Start of dump,%s log ----\n\n",
305 (pb == LOG) ? "global" : "local");
308 printk("\n-------- End of dump --------\n");
310 spin_unlock_bh(&print_lock);
314 * log_stop - free up TIPC log print buffer
319 spin_lock_bh(&print_lock);
324 spin_unlock_bh(&print_lock);
328 * log_reinit - set TIPC log print buffer to specified size
331 void log_reinit(int log_size)
336 if (log_size <= MAX_STRING)
337 log_size = MAX_STRING + 1;
338 spin_lock_bh(&print_lock);
339 printbuf_init(LOG, kmalloc(log_size, GFP_ATOMIC), log_size);
340 spin_unlock_bh(&print_lock);
345 * log_resize - reconfigure size of TIPC log buffer
348 struct sk_buff *log_resize(const void *req_tlv_area, int req_tlv_space)
352 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
353 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
355 value = *(u32 *)TLV_DATA(req_tlv_area);
356 value = ntohl(value);
357 if (value != delimit(value, 0, 32768))
358 return cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
359 " (log size must be 0-32768)");
361 return cfg_reply_none();
365 * log_dump - capture TIPC log buffer contents in configuration message
368 struct sk_buff *log_dump(void)
370 struct sk_buff *reply;
372 spin_lock_bh(&print_lock);
374 reply = cfg_reply_ultra_string("log not activated\n");
375 else if (printbuf_empty(LOG))
376 reply = cfg_reply_ultra_string("log is empty\n");
378 struct tlv_desc *rep_tlv;
382 str_len = min(LOG->size, 32768u);
383 reply = cfg_reply_alloc(TLV_SPACE(str_len));
385 rep_tlv = (struct tlv_desc *)reply->data;
386 printbuf_init(&pb, TLV_DATA(rep_tlv), str_len);
387 printbuf_move(&pb, LOG);
388 str_len = strlen(TLV_DATA(rep_tlv)) + 1;
389 skb_put(reply, TLV_SPACE(str_len));
390 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
393 spin_unlock_bh(&print_lock);