[TIPC] License header update
[deliverable/linux.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 * 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.
20 *
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.
24 *
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.
36 */
37
38 #include "core.h"
39 #include "config.h"
40 #include "dbg.h"
41
42 #define MAX_STRING 512
43
44 static char print_string[MAX_STRING];
45 static spinlock_t print_lock = SPIN_LOCK_UNLOCKED;
46
47 static struct print_buf cons_buf = { NULL, 0, NULL, NULL };
48 struct print_buf *CONS = &cons_buf;
49
50 static struct print_buf log_buf = { NULL, 0, NULL, NULL };
51 struct print_buf *LOG = &log_buf;
52
53
54 #define FORMAT(PTR,LEN,FMT) \
55 {\
56 va_list args;\
57 va_start(args, FMT);\
58 LEN = vsprintf(PTR, FMT, args);\
59 va_end(args);\
60 *(PTR + LEN) = '\0';\
61 }
62
63 /*
64 * Locking policy when using print buffers.
65 *
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.
72 */
73
74 /**
75 * printbuf_init - initialize print buffer to empty
76 */
77
78 void printbuf_init(struct print_buf *pb, char *raw, u32 sz)
79 {
80 if (!pb || !raw || (sz < (MAX_STRING + 1)))
81 return;
82
83 pb->crs = pb->buf = raw;
84 pb->size = sz;
85 pb->next = 0;
86 pb->buf[0] = 0;
87 pb->buf[sz-1] = ~0;
88 }
89
90 /**
91 * printbuf_reset - reinitialize print buffer to empty state
92 */
93
94 void printbuf_reset(struct print_buf *pb)
95 {
96 if (pb && pb->buf)
97 printbuf_init(pb, pb->buf, pb->size);
98 }
99
100 /**
101 * printbuf_empty - test if print buffer is in empty state
102 */
103
104 int printbuf_empty(struct print_buf *pb)
105 {
106 return (!pb || !pb->buf || (pb->crs == pb->buf));
107 }
108
109 /**
110 * printbuf_validate - check for print buffer overflow
111 *
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
114 *
115 * Returns length of print buffer data string (including trailing NULL)
116 */
117
118 int printbuf_validate(struct print_buf *pb)
119 {
120 char *err = " *** PRINT BUFFER WRAPPED AROUND ***\n";
121 char *cp_buf;
122 struct print_buf cb;
123
124 if (!pb || !pb->buf)
125 return 0;
126
127 if (pb->buf[pb->size - 1] == '\0') {
128 cp_buf = kmalloc(pb->size, GFP_ATOMIC);
129 if (cp_buf != NULL){
130 printbuf_init(&cb, cp_buf, pb->size);
131 printbuf_move(&cb, pb);
132 printbuf_move(pb, &cb);
133 kfree(cp_buf);
134 memcpy(pb->buf, err, strlen(err));
135 } else {
136 printbuf_reset(pb);
137 tipc_printf(pb, err);
138 }
139 }
140 return (pb->crs - pb->buf + 1);
141 }
142
143 /**
144 * printbuf_move - move print buffer contents to another print buffer
145 *
146 * Current contents of destination print buffer (if any) are discarded.
147 * Source print buffer becomes empty if a successful move occurs.
148 */
149
150 void printbuf_move(struct print_buf *pb_to, struct print_buf *pb_from)
151 {
152 int len;
153
154 /* Handle the cases where contents can't be moved */
155
156 if (!pb_to || !pb_to->buf)
157 return;
158
159 if (!pb_from || !pb_from->buf) {
160 printbuf_reset(pb_to);
161 return;
162 }
163
164 if (pb_to->size < pb_from->size) {
165 printbuf_reset(pb_to);
166 tipc_printf(pb_to, "*** PRINT BUFFER OVERFLOW ***");
167 return;
168 }
169
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;
175 } else
176 pb_to->crs = pb_to->buf;
177
178 /* Copy data from start to cursor (always) */
179 len = pb_from->crs - pb_from->buf;
180 strcpy(pb_to->crs, pb_from->buf);
181 pb_to->crs += len;
182
183 printbuf_reset(pb_from);
184 }
185
186 /**
187 * tipc_printf - append formatted output to print buffer chain
188 */
189
190 void tipc_printf(struct print_buf *pb, const char *fmt, ...)
191 {
192 int chars_to_add;
193 int chars_left;
194 char save_char;
195 struct print_buf *pb_next;
196
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 ***");
201
202 while (pb) {
203 if (pb == CONS)
204 printk(print_string);
205 else if (pb->buf) {
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;
210 } else {
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;
217 }
218 }
219 pb_next = pb->next;
220 pb->next = 0;
221 pb = pb_next;
222 }
223 spin_unlock_bh(&print_lock);
224 }
225
226 /**
227 * TEE - perform next output operation on both print buffers
228 */
229
230 struct print_buf *TEE(struct print_buf *b0, struct print_buf *b1)
231 {
232 struct print_buf *pb = b0;
233
234 if (!b0 || (b0 == b1))
235 return b1;
236 if (!b1)
237 return b0;
238
239 spin_lock_bh(&print_lock);
240 while (pb->next) {
241 if ((pb->next == b1) || (pb->next == b0))
242 pb->next = pb->next->next;
243 else
244 pb = pb->next;
245 }
246 pb->next = b1;
247 spin_unlock_bh(&print_lock);
248 return b0;
249 }
250
251 /**
252 * print_to_console - write string of bytes to console in multiple chunks
253 */
254
255 static void print_to_console(char *crs, int len)
256 {
257 int rest = len;
258
259 while (rest > 0) {
260 int sz = rest < MAX_STRING ? rest : MAX_STRING;
261 char c = crs[sz];
262
263 crs[sz] = 0;
264 printk((const char *)crs);
265 crs[sz] = c;
266 rest -= sz;
267 crs += sz;
268 }
269 }
270
271 /**
272 * printbuf_dump - write print buffer contents to console
273 */
274
275 static void printbuf_dump(struct print_buf *pb)
276 {
277 int len;
278
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);
283
284 /* Dump print buffer from start to cursor (always) */
285 len = pb->crs - pb->buf;
286 print_to_console(pb->buf, len);
287 }
288
289 /**
290 * tipc_dump - dump non-console print buffer(s) to console
291 */
292
293 void tipc_dump(struct print_buf *pb, const char *fmt, ...)
294 {
295 int len;
296
297 spin_lock_bh(&print_lock);
298 FORMAT(CONS->buf, len, fmt);
299 printk(CONS->buf);
300
301 for (; pb; pb = pb->next) {
302 if (pb == CONS)
303 continue;
304 printk("\n---- Start of dump,%s log ----\n\n",
305 (pb == LOG) ? "global" : "local");
306 printbuf_dump(pb);
307 printbuf_reset(pb);
308 printk("\n-------- End of dump --------\n");
309 }
310 spin_unlock_bh(&print_lock);
311 }
312
313 /**
314 * log_stop - free up TIPC log print buffer
315 */
316
317 void log_stop(void)
318 {
319 spin_lock_bh(&print_lock);
320 if (LOG->buf) {
321 kfree(LOG->buf);
322 LOG->buf = NULL;
323 }
324 spin_unlock_bh(&print_lock);
325 }
326
327 /**
328 * log_reinit - set TIPC log print buffer to specified size
329 */
330
331 void log_reinit(int log_size)
332 {
333 log_stop();
334
335 if (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);
341 }
342 }
343
344 /**
345 * log_resize - reconfigure size of TIPC log buffer
346 */
347
348 struct sk_buff *log_resize(const void *req_tlv_area, int req_tlv_space)
349 {
350 u32 value;
351
352 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
353 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
354
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)");
360 log_reinit(value);
361 return cfg_reply_none();
362 }
363
364 /**
365 * log_dump - capture TIPC log buffer contents in configuration message
366 */
367
368 struct sk_buff *log_dump(void)
369 {
370 struct sk_buff *reply;
371
372 spin_lock_bh(&print_lock);
373 if (!LOG->buf)
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");
377 else {
378 struct tlv_desc *rep_tlv;
379 struct print_buf pb;
380 int str_len;
381
382 str_len = min(LOG->size, 32768u);
383 reply = cfg_reply_alloc(TLV_SPACE(str_len));
384 if (reply) {
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);
391 }
392 }
393 spin_unlock_bh(&print_lock);
394 return reply;
395 }
396
This page took 0.039267 seconds and 5 git commands to generate.