blob: 26ef95d5fe38601611e6d95a4d6248276235b4cb [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/dbg.c: TIPC print buffer routines for debuggign
3 *
Per Liden593a5f22006-01-11 19:14:19 +01004 * Copyright (c) 1996-2006, Ericsson AB
Per Lidenb97bf3f2006-01-02 19:04:38 +01005 * Copyright (c) 2005, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 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.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * 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
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * 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
43static char print_string[MAX_STRING];
44static spinlock_t print_lock = SPIN_LOCK_UNLOCKED;
45
46static struct print_buf cons_buf = { NULL, 0, NULL, NULL };
Per Liden4323add2006-01-18 00:38:21 +010047struct print_buf *TIPC_CONS = &cons_buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +010048
49static struct print_buf log_buf = { NULL, 0, NULL, NULL };
Per Liden4323add2006-01-18 00:38:21 +010050struct print_buf *TIPC_LOG = &log_buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +010051
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).
Per Liden4323add2006-01-18 00:38:21 +010069 * 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.
Per Lidenb97bf3f2006-01-02 19:04:38 +010071 */
72
73/**
Per Liden4323add2006-01-18 00:38:21 +010074 * tipc_printbuf_init - initialize print buffer to empty
Per Lidenb97bf3f2006-01-02 19:04:38 +010075 */
76
Per Liden4323add2006-01-18 00:38:21 +010077void tipc_printbuf_init(struct print_buf *pb, char *raw, u32 sz)
Per Lidenb97bf3f2006-01-02 19:04:38 +010078{
79 if (!pb || !raw || (sz < (MAX_STRING + 1)))
80 return;
81
82 pb->crs = pb->buf = raw;
83 pb->size = sz;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -080084 pb->next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +010085 pb->buf[0] = 0;
86 pb->buf[sz-1] = ~0;
87}
88
89/**
Per Liden4323add2006-01-18 00:38:21 +010090 * tipc_printbuf_reset - reinitialize print buffer to empty state
Per Lidenb97bf3f2006-01-02 19:04:38 +010091 */
92
Per Liden4323add2006-01-18 00:38:21 +010093void tipc_printbuf_reset(struct print_buf *pb)
Per Lidenb97bf3f2006-01-02 19:04:38 +010094{
95 if (pb && pb->buf)
Per Liden4323add2006-01-18 00:38:21 +010096 tipc_printbuf_init(pb, pb->buf, pb->size);
Per Lidenb97bf3f2006-01-02 19:04:38 +010097}
98
99/**
Per Liden4323add2006-01-18 00:38:21 +0100100 * tipc_printbuf_empty - test if print buffer is in empty state
Per Lidenb97bf3f2006-01-02 19:04:38 +0100101 */
102
Per Liden4323add2006-01-18 00:38:21 +0100103int tipc_printbuf_empty(struct print_buf *pb)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100104{
105 return (!pb || !pb->buf || (pb->crs == pb->buf));
106}
107
108/**
Per Liden4323add2006-01-18 00:38:21 +0100109 * tipc_printbuf_validate - check for print buffer overflow
Per Lidenb97bf3f2006-01-02 19:04:38 +0100110 *
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
Per Liden4323add2006-01-18 00:38:21 +0100117int tipc_printbuf_validate(struct print_buf *pb)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118{
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){
Per Liden4323add2006-01-18 00:38:21 +0100129 tipc_printbuf_init(&cb, cp_buf, pb->size);
130 tipc_printbuf_move(&cb, pb);
131 tipc_printbuf_move(pb, &cb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100132 kfree(cp_buf);
133 memcpy(pb->buf, err, strlen(err));
134 } else {
Per Liden4323add2006-01-18 00:38:21 +0100135 tipc_printbuf_reset(pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100136 tipc_printf(pb, err);
137 }
138 }
139 return (pb->crs - pb->buf + 1);
140}
141
142/**
Per Liden4323add2006-01-18 00:38:21 +0100143 * tipc_printbuf_move - move print buffer contents to another print buffer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100144 *
145 * Current contents of destination print buffer (if any) are discarded.
146 * Source print buffer becomes empty if a successful move occurs.
147 */
148
Per Liden4323add2006-01-18 00:38:21 +0100149void tipc_printbuf_move(struct print_buf *pb_to, struct print_buf *pb_from)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100150{
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) {
Per Liden4323add2006-01-18 00:38:21 +0100159 tipc_printbuf_reset(pb_to);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 return;
161 }
162
163 if (pb_to->size < pb_from->size) {
Per Liden4323add2006-01-18 00:38:21 +0100164 tipc_printbuf_reset(pb_to);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165 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
Per Liden4323add2006-01-18 00:38:21 +0100182 tipc_printbuf_reset(pb_from);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183}
184
185/**
186 * tipc_printf - append formatted output to print buffer chain
187 */
188
189void 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) {
Per Liden4323add2006-01-18 00:38:21 +0100202 if (pb == TIPC_CONS)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100203 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;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800219 pb->next = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 pb = pb_next;
221 }
222 spin_unlock_bh(&print_lock);
223}
224
225/**
Per Liden4323add2006-01-18 00:38:21 +0100226 * TIPC_TEE - perform next output operation on both print buffers
Per Lidenb97bf3f2006-01-02 19:04:38 +0100227 */
228
Per Liden4323add2006-01-18 00:38:21 +0100229struct print_buf *TIPC_TEE(struct print_buf *b0, struct print_buf *b1)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100230{
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
254static 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
274static 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
292void tipc_dump(struct print_buf *pb, const char *fmt, ...)
293{
294 int len;
295
296 spin_lock_bh(&print_lock);
Per Liden4323add2006-01-18 00:38:21 +0100297 FORMAT(TIPC_CONS->buf, len, fmt);
298 printk(TIPC_CONS->buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100299
300 for (; pb; pb = pb->next) {
Per Liden4323add2006-01-18 00:38:21 +0100301 if (pb == TIPC_CONS)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302 continue;
303 printk("\n---- Start of dump,%s log ----\n\n",
Per Liden4323add2006-01-18 00:38:21 +0100304 (pb == TIPC_LOG) ? "global" : "local");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305 printbuf_dump(pb);
Per Liden4323add2006-01-18 00:38:21 +0100306 tipc_printbuf_reset(pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100307 printk("\n-------- End of dump --------\n");
308 }
309 spin_unlock_bh(&print_lock);
310}
311
312/**
Per Liden4323add2006-01-18 00:38:21 +0100313 * tipc_log_stop - free up TIPC log print buffer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314 */
315
Per Liden4323add2006-01-18 00:38:21 +0100316void tipc_log_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100317{
318 spin_lock_bh(&print_lock);
Per Liden4323add2006-01-18 00:38:21 +0100319 if (TIPC_LOG->buf) {
320 kfree(TIPC_LOG->buf);
321 TIPC_LOG->buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322 }
323 spin_unlock_bh(&print_lock);
324}
325
326/**
Per Liden4323add2006-01-18 00:38:21 +0100327 * tipc_log_reinit - set TIPC log print buffer to specified size
Per Lidenb97bf3f2006-01-02 19:04:38 +0100328 */
329
Per Liden4323add2006-01-18 00:38:21 +0100330void tipc_log_reinit(int log_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331{
Per Liden4323add2006-01-18 00:38:21 +0100332 tipc_log_stop();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333
334 if (log_size) {
335 if (log_size <= MAX_STRING)
336 log_size = MAX_STRING + 1;
337 spin_lock_bh(&print_lock);
Per Liden4323add2006-01-18 00:38:21 +0100338 tipc_printbuf_init(TIPC_LOG, kmalloc(log_size, GFP_ATOMIC), log_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100339 spin_unlock_bh(&print_lock);
340 }
341}
342
343/**
Per Liden4323add2006-01-18 00:38:21 +0100344 * tipc_log_resize - reconfigure size of TIPC log buffer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 */
346
Per Liden4323add2006-01-18 00:38:21 +0100347struct sk_buff *tipc_log_resize(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348{
349 u32 value;
350
351 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
Per Liden4323add2006-01-18 00:38:21 +0100352 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100353
354 value = *(u32 *)TLV_DATA(req_tlv_area);
355 value = ntohl(value);
356 if (value != delimit(value, 0, 32768))
Per Liden4323add2006-01-18 00:38:21 +0100357 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();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100361}
362
363/**
Per Liden4323add2006-01-18 00:38:21 +0100364 * tipc_log_dump - capture TIPC log buffer contents in configuration message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365 */
366
Per Liden4323add2006-01-18 00:38:21 +0100367struct sk_buff *tipc_log_dump(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368{
369 struct sk_buff *reply;
370
371 spin_lock_bh(&print_lock);
Per Liden4323add2006-01-18 00:38:21 +0100372 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");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376 else {
377 struct tlv_desc *rep_tlv;
378 struct print_buf pb;
379 int str_len;
380
Per Liden4323add2006-01-18 00:38:21 +0100381 str_len = min(TIPC_LOG->size, 32768u);
382 reply = tipc_cfg_reply_alloc(TLV_SPACE(str_len));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383 if (reply) {
384 rep_tlv = (struct tlv_desc *)reply->data;
Per Liden4323add2006-01-18 00:38:21 +0100385 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), str_len);
386 tipc_printbuf_move(&pb, TIPC_LOG);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387 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