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 * Redistributions of source code must retain the above copyright notice, this
13 * list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
38 #define MAX_STRING 512
40 static char print_string[MAX_STRING];
41 static spinlock_t print_lock = SPIN_LOCK_UNLOCKED;
43 static struct print_buf cons_buf = { NULL, 0, NULL, NULL };
44 struct print_buf *CONS = &cons_buf;
46 static struct print_buf log_buf = { NULL, 0, NULL, NULL };
47 struct print_buf *LOG = &log_buf;
50 #define FORMAT(PTR,LEN,FMT) \
54 LEN = vsprintf(PTR, FMT, args);\
60 * Locking policy when using print buffers.
62 * 1) Routines of the form printbuf_XXX() rely on the caller to prevent
63 * simultaneous use of the print buffer(s) being manipulated.
64 * 2) tipc_printf() uses 'print_lock' to prevent simultaneous use of
65 * 'print_string' and to protect its print buffer(s).
66 * 3) TEE() uses 'print_lock' to protect its print buffer(s).
67 * 4) Routines of the form log_XXX() uses 'print_lock' to protect LOG.
71 * printbuf_init - initialize print buffer to empty
74 void printbuf_init(struct print_buf *pb, char *raw, u32 sz)
76 if (!pb || !raw || (sz < (MAX_STRING + 1)))
79 pb->crs = pb->buf = raw;
87 * printbuf_reset - reinitialize print buffer to empty state
90 void printbuf_reset(struct print_buf *pb)
93 printbuf_init(pb, pb->buf, pb->size);
97 * printbuf_empty - test if print buffer is in empty state
100 int printbuf_empty(struct print_buf *pb)
102 return (!pb || !pb->buf || (pb->crs == pb->buf));
106 * printbuf_validate - check for print buffer overflow
108 * Verifies that a print buffer has captured all data written to it.
109 * If data has been lost, linearize buffer and prepend an error message
111 * Returns length of print buffer data string (including trailing NULL)
114 int printbuf_validate(struct print_buf *pb)
116 char *err = " *** PRINT BUFFER WRAPPED AROUND ***\n";
123 if (pb->buf[pb->size - 1] == '\0') {
124 cp_buf = kmalloc(pb->size, GFP_ATOMIC);
126 printbuf_init(&cb, cp_buf, pb->size);
127 printbuf_move(&cb, pb);
128 printbuf_move(pb, &cb);
130 memcpy(pb->buf, err, strlen(err));
133 tipc_printf(pb, err);
136 return (pb->crs - pb->buf + 1);
140 * printbuf_move - move print buffer contents to another print buffer
142 * Current contents of destination print buffer (if any) are discarded.
143 * Source print buffer becomes empty if a successful move occurs.
146 void printbuf_move(struct print_buf *pb_to, struct print_buf *pb_from)
150 /* Handle the cases where contents can't be moved */
152 if (!pb_to || !pb_to->buf)
155 if (!pb_from || !pb_from->buf) {
156 printbuf_reset(pb_to);
160 if (pb_to->size < pb_from->size) {
161 printbuf_reset(pb_to);
162 tipc_printf(pb_to, "*** PRINT BUFFER OVERFLOW ***");
166 /* Copy data from char after cursor to end (if used) */
167 len = pb_from->buf + pb_from->size - pb_from->crs - 2;
168 if ((pb_from->buf[pb_from->size-1] == 0) && (len > 0)) {
169 strcpy(pb_to->buf, pb_from->crs + 1);
170 pb_to->crs = pb_to->buf + len;
172 pb_to->crs = pb_to->buf;
174 /* Copy data from start to cursor (always) */
175 len = pb_from->crs - pb_from->buf;
176 strcpy(pb_to->crs, pb_from->buf);
179 printbuf_reset(pb_from);
183 * tipc_printf - append formatted output to print buffer chain
186 void tipc_printf(struct print_buf *pb, const char *fmt, ...)
191 struct print_buf *pb_next;
193 spin_lock_bh(&print_lock);
194 FORMAT(print_string, chars_to_add, fmt);
195 if (chars_to_add >= MAX_STRING)
196 strcpy(print_string, "*** STRING TOO LONG ***");
200 printk(print_string);
202 chars_left = pb->buf + pb->size - pb->crs - 1;
203 if (chars_to_add <= chars_left) {
204 strcpy(pb->crs, print_string);
205 pb->crs += chars_to_add;
207 strcpy(pb->buf, print_string + chars_left);
208 save_char = print_string[chars_left];
209 print_string[chars_left] = 0;
210 strcpy(pb->crs, print_string);
211 print_string[chars_left] = save_char;
212 pb->crs = pb->buf + chars_to_add - chars_left;
219 spin_unlock_bh(&print_lock);
223 * TEE - perform next output operation on both print buffers
226 struct print_buf *TEE(struct print_buf *b0, struct print_buf *b1)
228 struct print_buf *pb = b0;
230 if (!b0 || (b0 == b1))
235 spin_lock_bh(&print_lock);
237 if ((pb->next == b1) || (pb->next == b0))
238 pb->next = pb->next->next;
243 spin_unlock_bh(&print_lock);
248 * print_to_console - write string of bytes to console in multiple chunks
251 static void print_to_console(char *crs, int len)
256 int sz = rest < MAX_STRING ? rest : MAX_STRING;
260 printk((const char *)crs);
268 * printbuf_dump - write print buffer contents to console
271 static void printbuf_dump(struct print_buf *pb)
275 /* Dump print buffer from char after cursor to end (if used) */
276 len = pb->buf + pb->size - pb->crs - 2;
277 if ((pb->buf[pb->size - 1] == 0) && (len > 0))
278 print_to_console(pb->crs + 1, len);
280 /* Dump print buffer from start to cursor (always) */
281 len = pb->crs - pb->buf;
282 print_to_console(pb->buf, len);
286 * tipc_dump - dump non-console print buffer(s) to console
289 void tipc_dump(struct print_buf *pb, const char *fmt, ...)
293 spin_lock_bh(&print_lock);
294 FORMAT(CONS->buf, len, fmt);
297 for (; pb; pb = pb->next) {
300 printk("\n---- Start of dump,%s log ----\n\n",
301 (pb == LOG) ? "global" : "local");
304 printk("\n-------- End of dump --------\n");
306 spin_unlock_bh(&print_lock);
310 * log_stop - free up TIPC log print buffer
315 spin_lock_bh(&print_lock);
320 spin_unlock_bh(&print_lock);
324 * log_reinit - set TIPC log print buffer to specified size
327 void log_reinit(int log_size)
332 if (log_size <= MAX_STRING)
333 log_size = MAX_STRING + 1;
334 spin_lock_bh(&print_lock);
335 printbuf_init(LOG, kmalloc(log_size, GFP_ATOMIC), log_size);
336 spin_unlock_bh(&print_lock);
341 * log_resize - reconfigure size of TIPC log buffer
344 struct sk_buff *log_resize(const void *req_tlv_area, int req_tlv_space)
348 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
349 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
351 value = *(u32 *)TLV_DATA(req_tlv_area);
352 value = ntohl(value);
353 if (value != delimit(value, 0, 32768))
354 return cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
355 " (log size must be 0-32768)");
357 return cfg_reply_none();
361 * log_dump - capture TIPC log buffer contents in configuration message
364 struct sk_buff *log_dump(void)
366 struct sk_buff *reply;
368 spin_lock_bh(&print_lock);
370 reply = cfg_reply_ultra_string("log not activated\n");
371 else if (printbuf_empty(LOG))
372 reply = cfg_reply_ultra_string("log is empty\n");
374 struct tlv_desc *rep_tlv;
378 str_len = min(LOG->size, 32768u);
379 reply = cfg_reply_alloc(TLV_SPACE(str_len));
381 rep_tlv = (struct tlv_desc *)reply->data;
382 printbuf_init(&pb, TLV_DATA(rep_tlv), str_len);
383 printbuf_move(&pb, LOG);
384 str_len = strlen(TLV_DATA(rep_tlv)) + 1;
385 skb_put(reply, TLV_SPACE(str_len));
386 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
389 spin_unlock_bh(&print_lock);