synclink_gt fix receive tty error handling
[linux/fpc-iii.git] / net / tipc / dbg.c
blob4f4beefa783037f772cb4c49eb4fe02b253990dd
1 /*
2 * net/tipc/dbg.c: TIPC print buffer routines for debuggign
3 *
4 * Copyright (c) 1996-2006, Ericsson AB
5 * Copyright (c) 2005, Wind River Systems
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
37 #include "core.h"
38 #include "config.h"
39 #include "dbg.h"
41 #define MAX_STRING 512
43 static char print_string[MAX_STRING];
44 static spinlock_t print_lock = SPIN_LOCK_UNLOCKED;
46 static struct print_buf cons_buf = { NULL, 0, NULL, NULL };
47 struct print_buf *TIPC_CONS = &cons_buf;
49 static struct print_buf log_buf = { NULL, 0, NULL, NULL };
50 struct print_buf *TIPC_LOG = &log_buf;
53 #define FORMAT(PTR,LEN,FMT) \
55 va_list args;\
56 va_start(args, FMT);\
57 LEN = vsprintf(PTR, FMT, args);\
58 va_end(args);\
59 *(PTR + LEN) = '\0';\
63 * Locking policy when using print buffers.
65 * 1) Routines of the form printbuf_XXX() rely on the caller to prevent
66 * simultaneous use of the print buffer(s) being manipulated.
67 * 2) tipc_printf() uses 'print_lock' to prevent simultaneous use of
68 * 'print_string' and to protect its print buffer(s).
69 * 3) TIPC_TEE() uses 'print_lock' to protect its print buffer(s).
70 * 4) Routines of the form log_XXX() uses 'print_lock' to protect TIPC_LOG.
73 /**
74 * tipc_printbuf_init - initialize print buffer to empty
77 void tipc_printbuf_init(struct print_buf *pb, char *raw, u32 sz)
79 if (!pb || !raw || (sz < (MAX_STRING + 1)))
80 return;
82 pb->crs = pb->buf = raw;
83 pb->size = sz;
84 pb->next = 0;
85 pb->buf[0] = 0;
86 pb->buf[sz-1] = ~0;
89 /**
90 * tipc_printbuf_reset - reinitialize print buffer to empty state
93 void tipc_printbuf_reset(struct print_buf *pb)
95 if (pb && pb->buf)
96 tipc_printbuf_init(pb, pb->buf, pb->size);
99 /**
100 * tipc_printbuf_empty - test if print buffer is in empty state
103 int tipc_printbuf_empty(struct print_buf *pb)
105 return (!pb || !pb->buf || (pb->crs == pb->buf));
109 * tipc_printbuf_validate - check for print buffer overflow
111 * Verifies that a print buffer has captured all data written to it.
112 * If data has been lost, linearize buffer and prepend an error message
114 * Returns length of print buffer data string (including trailing NULL)
117 int tipc_printbuf_validate(struct print_buf *pb)
119 char *err = " *** PRINT BUFFER WRAPPED AROUND ***\n";
120 char *cp_buf;
121 struct print_buf cb;
123 if (!pb || !pb->buf)
124 return 0;
126 if (pb->buf[pb->size - 1] == '\0') {
127 cp_buf = kmalloc(pb->size, GFP_ATOMIC);
128 if (cp_buf != NULL){
129 tipc_printbuf_init(&cb, cp_buf, pb->size);
130 tipc_printbuf_move(&cb, pb);
131 tipc_printbuf_move(pb, &cb);
132 kfree(cp_buf);
133 memcpy(pb->buf, err, strlen(err));
134 } else {
135 tipc_printbuf_reset(pb);
136 tipc_printf(pb, err);
139 return (pb->crs - pb->buf + 1);
143 * tipc_printbuf_move - move print buffer contents to another print buffer
145 * Current contents of destination print buffer (if any) are discarded.
146 * Source print buffer becomes empty if a successful move occurs.
149 void tipc_printbuf_move(struct print_buf *pb_to, struct print_buf *pb_from)
151 int len;
153 /* Handle the cases where contents can't be moved */
155 if (!pb_to || !pb_to->buf)
156 return;
158 if (!pb_from || !pb_from->buf) {
159 tipc_printbuf_reset(pb_to);
160 return;
163 if (pb_to->size < pb_from->size) {
164 tipc_printbuf_reset(pb_to);
165 tipc_printf(pb_to, "*** PRINT BUFFER OVERFLOW ***");
166 return;
169 /* Copy data from char after cursor to end (if used) */
170 len = pb_from->buf + pb_from->size - pb_from->crs - 2;
171 if ((pb_from->buf[pb_from->size-1] == 0) && (len > 0)) {
172 strcpy(pb_to->buf, pb_from->crs + 1);
173 pb_to->crs = pb_to->buf + len;
174 } else
175 pb_to->crs = pb_to->buf;
177 /* Copy data from start to cursor (always) */
178 len = pb_from->crs - pb_from->buf;
179 strcpy(pb_to->crs, pb_from->buf);
180 pb_to->crs += len;
182 tipc_printbuf_reset(pb_from);
186 * tipc_printf - append formatted output to print buffer chain
189 void tipc_printf(struct print_buf *pb, const char *fmt, ...)
191 int chars_to_add;
192 int chars_left;
193 char save_char;
194 struct print_buf *pb_next;
196 spin_lock_bh(&print_lock);
197 FORMAT(print_string, chars_to_add, fmt);
198 if (chars_to_add >= MAX_STRING)
199 strcpy(print_string, "*** STRING TOO LONG ***");
201 while (pb) {
202 if (pb == TIPC_CONS)
203 printk(print_string);
204 else if (pb->buf) {
205 chars_left = pb->buf + pb->size - pb->crs - 1;
206 if (chars_to_add <= chars_left) {
207 strcpy(pb->crs, print_string);
208 pb->crs += chars_to_add;
209 } else {
210 strcpy(pb->buf, print_string + chars_left);
211 save_char = print_string[chars_left];
212 print_string[chars_left] = 0;
213 strcpy(pb->crs, print_string);
214 print_string[chars_left] = save_char;
215 pb->crs = pb->buf + chars_to_add - chars_left;
218 pb_next = pb->next;
219 pb->next = 0;
220 pb = pb_next;
222 spin_unlock_bh(&print_lock);
226 * TIPC_TEE - perform next output operation on both print buffers
229 struct print_buf *TIPC_TEE(struct print_buf *b0, struct print_buf *b1)
231 struct print_buf *pb = b0;
233 if (!b0 || (b0 == b1))
234 return b1;
235 if (!b1)
236 return b0;
238 spin_lock_bh(&print_lock);
239 while (pb->next) {
240 if ((pb->next == b1) || (pb->next == b0))
241 pb->next = pb->next->next;
242 else
243 pb = pb->next;
245 pb->next = b1;
246 spin_unlock_bh(&print_lock);
247 return b0;
251 * print_to_console - write string of bytes to console in multiple chunks
254 static void print_to_console(char *crs, int len)
256 int rest = len;
258 while (rest > 0) {
259 int sz = rest < MAX_STRING ? rest : MAX_STRING;
260 char c = crs[sz];
262 crs[sz] = 0;
263 printk((const char *)crs);
264 crs[sz] = c;
265 rest -= sz;
266 crs += sz;
271 * printbuf_dump - write print buffer contents to console
274 static void printbuf_dump(struct print_buf *pb)
276 int len;
278 /* Dump print buffer from char after cursor to end (if used) */
279 len = pb->buf + pb->size - pb->crs - 2;
280 if ((pb->buf[pb->size - 1] == 0) && (len > 0))
281 print_to_console(pb->crs + 1, len);
283 /* Dump print buffer from start to cursor (always) */
284 len = pb->crs - pb->buf;
285 print_to_console(pb->buf, len);
289 * tipc_dump - dump non-console print buffer(s) to console
292 void tipc_dump(struct print_buf *pb, const char *fmt, ...)
294 int len;
296 spin_lock_bh(&print_lock);
297 FORMAT(TIPC_CONS->buf, len, fmt);
298 printk(TIPC_CONS->buf);
300 for (; pb; pb = pb->next) {
301 if (pb == TIPC_CONS)
302 continue;
303 printk("\n---- Start of dump,%s log ----\n\n",
304 (pb == TIPC_LOG) ? "global" : "local");
305 printbuf_dump(pb);
306 tipc_printbuf_reset(pb);
307 printk("\n-------- End of dump --------\n");
309 spin_unlock_bh(&print_lock);
313 * tipc_log_stop - free up TIPC log print buffer
316 void tipc_log_stop(void)
318 spin_lock_bh(&print_lock);
319 if (TIPC_LOG->buf) {
320 kfree(TIPC_LOG->buf);
321 TIPC_LOG->buf = NULL;
323 spin_unlock_bh(&print_lock);
327 * tipc_log_reinit - set TIPC log print buffer to specified size
330 void tipc_log_reinit(int log_size)
332 tipc_log_stop();
334 if (log_size) {
335 if (log_size <= MAX_STRING)
336 log_size = MAX_STRING + 1;
337 spin_lock_bh(&print_lock);
338 tipc_printbuf_init(TIPC_LOG, kmalloc(log_size, GFP_ATOMIC), log_size);
339 spin_unlock_bh(&print_lock);
344 * tipc_log_resize - reconfigure size of TIPC log buffer
347 struct sk_buff *tipc_log_resize(const void *req_tlv_area, int req_tlv_space)
349 u32 value;
351 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
352 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
354 value = *(u32 *)TLV_DATA(req_tlv_area);
355 value = ntohl(value);
356 if (value != delimit(value, 0, 32768))
357 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
358 " (log size must be 0-32768)");
359 tipc_log_reinit(value);
360 return tipc_cfg_reply_none();
364 * tipc_log_dump - capture TIPC log buffer contents in configuration message
367 struct sk_buff *tipc_log_dump(void)
369 struct sk_buff *reply;
371 spin_lock_bh(&print_lock);
372 if (!TIPC_LOG->buf)
373 reply = tipc_cfg_reply_ultra_string("log not activated\n");
374 else if (tipc_printbuf_empty(TIPC_LOG))
375 reply = tipc_cfg_reply_ultra_string("log is empty\n");
376 else {
377 struct tlv_desc *rep_tlv;
378 struct print_buf pb;
379 int str_len;
381 str_len = min(TIPC_LOG->size, 32768u);
382 reply = tipc_cfg_reply_alloc(TLV_SPACE(str_len));
383 if (reply) {
384 rep_tlv = (struct tlv_desc *)reply->data;
385 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), str_len);
386 tipc_printbuf_move(&pb, TIPC_LOG);
387 str_len = strlen(TLV_DATA(rep_tlv)) + 1;
388 skb_put(reply, TLV_SPACE(str_len));
389 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
392 spin_unlock_bh(&print_lock);
393 return reply;