]> git.karo-electronics.de Git - mv-sheeva.git/blob - net/tipc/dbg.c
[TIPC} Fixed bug in disc_timeout()
[mv-sheeva.git] / net / tipc / dbg.c
1 /*
2  * net/tipc/dbg.c: TIPC print buffer routines for debuggign
3  * 
4  * Copyright (c) 2003-2005, Ericsson Research Canada
5  * Copyright (c) 2005, Wind River Systems
6  * Copyright (c) 2005-2006, Ericsson AB
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without 
10  * modification, are permitted provided that the following conditions are met:
11  *
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.
20  *
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.
32  */
33
34 #include "core.h"
35 #include "config.h"
36 #include "dbg.h"
37
38 #define MAX_STRING 512
39
40 static char print_string[MAX_STRING];
41 static spinlock_t print_lock = SPIN_LOCK_UNLOCKED;
42
43 static struct print_buf cons_buf = { NULL, 0, NULL, NULL };
44 struct print_buf *CONS = &cons_buf;
45
46 static struct print_buf log_buf = { NULL, 0, NULL, NULL };
47 struct print_buf *LOG = &log_buf;
48
49
50 #define FORMAT(PTR,LEN,FMT) \
51 {\
52        va_list args;\
53        va_start(args, FMT);\
54        LEN = vsprintf(PTR, FMT, args);\
55        va_end(args);\
56        *(PTR + LEN) = '\0';\
57 }
58
59 /*
60  * Locking policy when using print buffers.
61  *
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.
68  */
69
70 /**
71  * printbuf_init - initialize print buffer to empty
72  */
73
74 void printbuf_init(struct print_buf *pb, char *raw, u32 sz)
75 {
76         if (!pb || !raw || (sz < (MAX_STRING + 1)))
77                 return;
78
79         pb->crs = pb->buf = raw;
80         pb->size = sz;
81         pb->next = 0;
82         pb->buf[0] = 0;
83         pb->buf[sz-1] = ~0;
84 }
85
86 /**
87  * printbuf_reset - reinitialize print buffer to empty state
88  */
89
90 void printbuf_reset(struct print_buf *pb)
91 {
92         if (pb && pb->buf)
93                 printbuf_init(pb, pb->buf, pb->size);
94 }
95
96 /**
97  * printbuf_empty - test if print buffer is in empty state
98  */
99
100 int printbuf_empty(struct print_buf *pb)
101 {
102         return (!pb || !pb->buf || (pb->crs == pb->buf));
103 }
104
105 /**
106  * printbuf_validate - check for print buffer overflow
107  * 
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
110  * 
111  * Returns length of print buffer data string (including trailing NULL)
112  */
113
114 int printbuf_validate(struct print_buf *pb)
115 {
116         char *err = "             *** PRINT BUFFER WRAPPED AROUND ***\n";
117         char *cp_buf;
118         struct print_buf cb;
119
120         if (!pb || !pb->buf)
121                 return 0;
122
123         if (pb->buf[pb->size - 1] == '\0') {
124                 cp_buf = kmalloc(pb->size, GFP_ATOMIC);
125                 if (cp_buf != NULL){
126                         printbuf_init(&cb, cp_buf, pb->size);
127                         printbuf_move(&cb, pb);
128                         printbuf_move(pb, &cb);
129                         kfree(cp_buf);
130                         memcpy(pb->buf, err, strlen(err));
131                 } else {
132                         printbuf_reset(pb);
133                         tipc_printf(pb, err);
134                 }
135         }
136         return (pb->crs - pb->buf + 1);
137 }
138
139 /**
140  * printbuf_move - move print buffer contents to another print buffer
141  * 
142  * Current contents of destination print buffer (if any) are discarded.
143  * Source print buffer becomes empty if a successful move occurs.
144  */
145
146 void printbuf_move(struct print_buf *pb_to, struct print_buf *pb_from)
147 {
148         int len;
149
150         /* Handle the cases where contents can't be moved */
151
152         if (!pb_to || !pb_to->buf)
153                 return;
154
155         if (!pb_from || !pb_from->buf) {
156                 printbuf_reset(pb_to);
157                 return;
158         }
159
160         if (pb_to->size < pb_from->size) {
161                 printbuf_reset(pb_to);
162                 tipc_printf(pb_to, "*** PRINT BUFFER OVERFLOW ***");
163                 return;
164         }
165
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;
171         } else
172                 pb_to->crs = pb_to->buf;
173
174         /* Copy data from start to cursor (always) */
175         len = pb_from->crs - pb_from->buf;
176         strcpy(pb_to->crs, pb_from->buf);
177         pb_to->crs += len;
178
179         printbuf_reset(pb_from);
180 }
181
182 /**
183  * tipc_printf - append formatted output to print buffer chain
184  */
185
186 void tipc_printf(struct print_buf *pb, const char *fmt, ...)
187 {
188         int chars_to_add;
189         int chars_left;
190         char save_char;
191         struct print_buf *pb_next;
192
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 ***");
197
198         while (pb) {
199                 if (pb == CONS)
200                         printk(print_string);
201                 else if (pb->buf) {
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;
206                         } else {
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;
213                         }
214                 }
215                 pb_next = pb->next;
216                 pb->next = 0;
217                 pb = pb_next;
218         }
219         spin_unlock_bh(&print_lock);
220 }
221
222 /**
223  * TEE - perform next output operation on both print buffers  
224  */
225
226 struct print_buf *TEE(struct print_buf *b0, struct print_buf *b1)
227 {
228         struct print_buf *pb = b0;
229
230         if (!b0 || (b0 == b1))
231                 return b1;
232         if (!b1)
233                 return b0;
234
235         spin_lock_bh(&print_lock);
236         while (pb->next) {
237                 if ((pb->next == b1) || (pb->next == b0))
238                         pb->next = pb->next->next;
239                 else
240                         pb = pb->next;
241         }
242         pb->next = b1;
243         spin_unlock_bh(&print_lock);
244         return b0;
245 }
246
247 /**
248  * print_to_console - write string of bytes to console in multiple chunks
249  */
250
251 static void print_to_console(char *crs, int len)
252 {
253         int rest = len;
254
255         while (rest > 0) {
256                 int sz = rest < MAX_STRING ? rest : MAX_STRING;
257                 char c = crs[sz];
258
259                 crs[sz] = 0;
260                 printk((const char *)crs);
261                 crs[sz] = c;
262                 rest -= sz;
263                 crs += sz;
264         }
265 }
266
267 /**
268  * printbuf_dump - write print buffer contents to console
269  */
270
271 static void printbuf_dump(struct print_buf *pb)
272 {
273         int len;
274
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);
279
280         /* Dump print buffer from start to cursor (always) */
281         len = pb->crs - pb->buf;
282         print_to_console(pb->buf, len);
283 }
284
285 /**
286  * tipc_dump - dump non-console print buffer(s) to console
287  */
288
289 void tipc_dump(struct print_buf *pb, const char *fmt, ...)
290 {
291         int len;
292
293         spin_lock_bh(&print_lock);
294         FORMAT(CONS->buf, len, fmt);
295         printk(CONS->buf);
296
297         for (; pb; pb = pb->next) {
298                 if (pb == CONS)
299                         continue;
300                 printk("\n---- Start of dump,%s log ----\n\n", 
301                        (pb == LOG) ? "global" : "local");
302                 printbuf_dump(pb);
303                 printbuf_reset(pb);
304                 printk("\n-------- End of dump --------\n");
305         }
306         spin_unlock_bh(&print_lock);
307 }
308
309 /**
310  * log_stop - free up TIPC log print buffer 
311  */
312
313 void log_stop(void)
314 {
315         spin_lock_bh(&print_lock);
316         if (LOG->buf) {
317                 kfree(LOG->buf);
318                 LOG->buf = NULL;
319         }
320         spin_unlock_bh(&print_lock);
321 }
322
323 /**
324  * log_reinit - set TIPC log print buffer to specified size
325  */
326
327 void log_reinit(int log_size)
328 {
329         log_stop();
330
331         if (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);
337         }
338 }
339
340 /**
341  * log_resize - reconfigure size of TIPC log buffer
342  */
343
344 struct sk_buff *log_resize(const void *req_tlv_area, int req_tlv_space)
345 {
346         u32 value;
347
348         if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
349                 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
350
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)");
356         log_reinit(value);
357         return cfg_reply_none();
358 }
359
360 /**
361  * log_dump - capture TIPC log buffer contents in configuration message
362  */
363
364 struct sk_buff *log_dump(void)
365 {
366         struct sk_buff *reply;
367
368         spin_lock_bh(&print_lock);
369         if (!LOG->buf)
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");
373         else {
374                 struct tlv_desc *rep_tlv;
375                 struct print_buf pb;
376                 int str_len;
377
378                 str_len = min(LOG->size, 32768u);
379                 reply = cfg_reply_alloc(TLV_SPACE(str_len));
380                 if (reply) {
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);
387                 }
388         }
389         spin_unlock_bh(&print_lock);
390         return reply;
391 }
392