Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / net / tipc / dbg.c
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.
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 "dbg.h"
40
41 #define MAX_STRING 512
42
43 static char print_string[MAX_STRING];
44 static DEFINE_SPINLOCK(print_lock);
45
46 static struct print_buf cons_buf = { NULL, 0, NULL, NULL };
47 struct print_buf *TIPC_CONS = &cons_buf;
48
49 static struct print_buf log_buf = { NULL, 0, NULL, NULL };
50 struct print_buf *TIPC_LOG = &log_buf;
51
52
53 #define FORMAT(PTR,LEN,FMT) \
54 {\
55        va_list args;\
56        va_start(args, FMT);\
57        LEN = vsprintf(PTR, FMT, args);\
58        va_end(args);\
59        *(PTR + LEN) = '\0';\
60 }
61
62 /*
63  * Locking policy when using print buffers.
64  *
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.
71  */
72
73 /**
74  * tipc_printbuf_init - initialize print buffer to empty
75  */
76
77 void tipc_printbuf_init(struct print_buf *pb, char *raw, u32 sz)
78 {
79         if (!pb || !raw || (sz < (MAX_STRING + 1)))
80                 return;
81
82         pb->crs = pb->buf = raw;
83         pb->size = sz;
84         pb->next = NULL;
85         pb->buf[0] = 0;
86         pb->buf[sz-1] = ~0;
87 }
88
89 /**
90  * tipc_printbuf_reset - reinitialize print buffer to empty state
91  */
92
93 void tipc_printbuf_reset(struct print_buf *pb)
94 {
95         if (pb && pb->buf)
96                 tipc_printbuf_init(pb, pb->buf, pb->size);
97 }
98
99 /**
100  * tipc_printbuf_empty - test if print buffer is in empty state
101  */
102
103 int tipc_printbuf_empty(struct print_buf *pb)
104 {
105         return (!pb || !pb->buf || (pb->crs == pb->buf));
106 }
107
108 /**
109  * tipc_printbuf_validate - check for print buffer overflow
110  * 
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
113  * 
114  * Returns length of print buffer data string (including trailing NULL)
115  */
116
117 int tipc_printbuf_validate(struct print_buf *pb)
118 {
119         char *err = "             *** PRINT BUFFER WRAPPED AROUND ***\n";
120         char *cp_buf;
121         struct print_buf cb;
122
123         if (!pb || !pb->buf)
124                 return 0;
125
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);
137                 }
138         }
139         return (pb->crs - pb->buf + 1);
140 }
141
142 /**
143  * tipc_printbuf_move - move print buffer contents to another print buffer
144  * 
145  * Current contents of destination print buffer (if any) are discarded.
146  * Source print buffer becomes empty if a successful move occurs.
147  */
148
149 void tipc_printbuf_move(struct print_buf *pb_to, struct print_buf *pb_from)
150 {
151         int len;
152
153         /* Handle the cases where contents can't be moved */
154
155         if (!pb_to || !pb_to->buf)
156                 return;
157
158         if (!pb_from || !pb_from->buf) {
159                 tipc_printbuf_reset(pb_to);
160                 return;
161         }
162
163         if (pb_to->size < pb_from->size) {
164                 tipc_printbuf_reset(pb_to);
165                 tipc_printf(pb_to, "*** PRINT BUFFER OVERFLOW ***");
166                 return;
167         }
168
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;
176
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;
181
182         tipc_printbuf_reset(pb_from);
183 }
184
185 /**
186  * tipc_printf - append formatted output to print buffer chain
187  */
188
189 void tipc_printf(struct print_buf *pb, const char *fmt, ...)
190 {
191         int chars_to_add;
192         int chars_left;
193         char save_char;
194         struct print_buf *pb_next;
195
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 ***");
200
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;
216                         }
217                 }
218                 pb_next = pb->next;
219                 pb->next = NULL;
220                 pb = pb_next;
221         }
222         spin_unlock_bh(&print_lock);
223 }
224
225 /**
226  * TIPC_TEE - perform next output operation on both print buffers  
227  */
228
229 struct print_buf *TIPC_TEE(struct print_buf *b0, struct print_buf *b1)
230 {
231         struct print_buf *pb = b0;
232
233         if (!b0 || (b0 == b1))
234                 return b1;
235         if (!b1)
236                 return b0;
237
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;
244         }
245         pb->next = b1;
246         spin_unlock_bh(&print_lock);
247         return b0;
248 }
249
250 /**
251  * print_to_console - write string of bytes to console in multiple chunks
252  */
253
254 static void print_to_console(char *crs, int len)
255 {
256         int rest = len;
257
258         while (rest > 0) {
259                 int sz = rest < MAX_STRING ? rest : MAX_STRING;
260                 char c = crs[sz];
261
262                 crs[sz] = 0;
263                 printk((const char *)crs);
264                 crs[sz] = c;
265                 rest -= sz;
266                 crs += sz;
267         }
268 }
269
270 /**
271  * printbuf_dump - write print buffer contents to console
272  */
273
274 static void printbuf_dump(struct print_buf *pb)
275 {
276         int len;
277
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);
282
283         /* Dump print buffer from start to cursor (always) */
284         len = pb->crs - pb->buf;
285         print_to_console(pb->buf, len);
286 }
287
288 /**
289  * tipc_dump - dump non-console print buffer(s) to console
290  */
291
292 void tipc_dump(struct print_buf *pb, const char *fmt, ...)
293 {
294         int len;
295
296         spin_lock_bh(&print_lock);
297         FORMAT(TIPC_CONS->buf, len, fmt);
298         printk(TIPC_CONS->buf);
299
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");
308         }
309         spin_unlock_bh(&print_lock);
310 }
311
312 /**
313  * tipc_log_stop - free up TIPC log print buffer 
314  */
315
316 void tipc_log_stop(void)
317 {
318         spin_lock_bh(&print_lock);
319         if (TIPC_LOG->buf) {
320                 kfree(TIPC_LOG->buf);
321                 TIPC_LOG->buf = NULL;
322         }
323         spin_unlock_bh(&print_lock);
324 }
325
326 /**
327  * tipc_log_reinit - set TIPC log print buffer to specified size
328  */
329
330 void tipc_log_reinit(int log_size)
331 {
332         tipc_log_stop();
333
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);
340         }
341 }
342
343 /**
344  * tipc_log_resize - reconfigure size of TIPC log buffer
345  */
346
347 struct sk_buff *tipc_log_resize(const void *req_tlv_area, int req_tlv_space)
348 {
349         u32 value;
350
351         if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
352                 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
353
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();
361 }
362
363 /**
364  * tipc_log_dump - capture TIPC log buffer contents in configuration message
365  */
366
367 struct sk_buff *tipc_log_dump(void)
368 {
369         struct sk_buff *reply;
370
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;
380
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);
390                 }
391         }
392         spin_unlock_bh(&print_lock);
393         return reply;
394 }
395