blob: 46f51d208e5e55193f410ae1c9c512f6144ff791 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Allan Stephens29ede242006-10-16 21:42:04 -07002 * net/tipc/dbg.c: TIPC print buffer routines for debugging
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Per Liden593a5f22006-01-11 19:14:19 +01004 * Copyright (c) 1996-2006, Ericsson AB
Allan Stephensfb98ec72008-05-05 01:20:42 -07005 * Copyright (c) 2005-2007, 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
Allan Stephensc8903982008-05-05 01:22:30 -070041/*
42 * TIPC pre-defines the following print buffers:
43 *
44 * TIPC_NULL : null buffer (i.e. print nowhere)
45 * TIPC_CONS : system console
46 * TIPC_LOG : TIPC log buffer
47 *
48 * Additional user-defined print buffers are also permitted.
49 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010050
Allan Stephensc8903982008-05-05 01:22:30 -070051static struct print_buf null_buf = { NULL, 0, NULL, 0 };
Allan Stephens7d3aa712008-05-05 01:22:59 -070052struct print_buf *const TIPC_NULL = &null_buf;
Allan Stephens29ede242006-10-16 21:42:04 -070053
Allan Stephensc8903982008-05-05 01:22:30 -070054static struct print_buf cons_buf = { NULL, 0, NULL, 1 };
stephen hemminger31e3c3f2010-10-13 13:20:35 +000055static struct print_buf *const TIPC_CONS = &cons_buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +010056
Allan Stephensc8903982008-05-05 01:22:30 -070057static struct print_buf log_buf = { NULL, 0, NULL, 1 };
Allan Stephens7d3aa712008-05-05 01:22:59 -070058struct print_buf *const TIPC_LOG = &log_buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +010059
Allan Stephensc8903982008-05-05 01:22:30 -070060/*
61 * Locking policy when using print buffers.
62 *
63 * 1) tipc_printf() uses 'print_lock' to protect against concurrent access to
64 * 'print_string' when writing to a print buffer. This also protects against
65 * concurrent writes to the print buffer being written to.
66 *
67 * 2) tipc_dump() and tipc_log_XXX() leverage the aforementioned
68 * use of 'print_lock' to protect against all types of concurrent operations
69 * on their associated print buffer (not just write operations).
70 *
71 * Note: All routines of the form tipc_printbuf_XXX() are lock-free, and rely
72 * on the caller to prevent simultaneous use of the print buffer(s) being
73 * manipulated.
74 */
75
76static char print_string[TIPC_PB_MAX_STR];
77static DEFINE_SPINLOCK(print_lock);
78
stephen hemminger31e3c3f2010-10-13 13:20:35 +000079static void tipc_printbuf_reset(struct print_buf *pb);
80static int tipc_printbuf_empty(struct print_buf *pb);
81static void tipc_printbuf_move(struct print_buf *pb_to,
82 struct print_buf *pb_from);
Per Lidenb97bf3f2006-01-02 19:04:38 +010083
84#define FORMAT(PTR,LEN,FMT) \
85{\
86 va_list args;\
87 va_start(args, FMT);\
88 LEN = vsprintf(PTR, FMT, args);\
89 va_end(args);\
90 *(PTR + LEN) = '\0';\
91}
92
Per Lidenb97bf3f2006-01-02 19:04:38 +010093/**
Per Liden4323add2006-01-18 00:38:21 +010094 * tipc_printbuf_init - initialize print buffer to empty
Allan Stephens29ede242006-10-16 21:42:04 -070095 * @pb: pointer to print buffer structure
96 * @raw: pointer to character array used by print buffer
97 * @size: size of character array
98 *
Allan Stephensc8903982008-05-05 01:22:30 -070099 * Note: If the character array is too small (or absent), the print buffer
100 * becomes a null device that discards anything written to it.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100101 */
102
Allan Stephens29ede242006-10-16 21:42:04 -0700103void tipc_printbuf_init(struct print_buf *pb, char *raw, u32 size)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100104{
Allan Stephens29ede242006-10-16 21:42:04 -0700105 pb->buf = raw;
106 pb->crs = raw;
107 pb->size = size;
Allan Stephensc8903982008-05-05 01:22:30 -0700108 pb->echo = 0;
Allan Stephens29ede242006-10-16 21:42:04 -0700109
110 if (size < TIPC_PB_MIN_SIZE) {
111 pb->buf = NULL;
112 } else if (raw) {
113 pb->buf[0] = 0;
Allan Stephens7d3aa712008-05-05 01:22:59 -0700114 pb->buf[size - 1] = ~0;
Allan Stephens29ede242006-10-16 21:42:04 -0700115 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100116}
117
118/**
Per Liden4323add2006-01-18 00:38:21 +0100119 * tipc_printbuf_reset - reinitialize print buffer to empty state
Allan Stephens29ede242006-10-16 21:42:04 -0700120 * @pb: pointer to print buffer structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121 */
122
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000123static void tipc_printbuf_reset(struct print_buf *pb)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124{
Allan Stephens7d3aa712008-05-05 01:22:59 -0700125 if (pb->buf) {
Allan Stephensc8903982008-05-05 01:22:30 -0700126 pb->crs = pb->buf;
127 pb->buf[0] = 0;
128 pb->buf[pb->size - 1] = ~0;
129 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100130}
131
132/**
Per Liden4323add2006-01-18 00:38:21 +0100133 * tipc_printbuf_empty - test if print buffer is in empty state
Allan Stephens29ede242006-10-16 21:42:04 -0700134 * @pb: pointer to print buffer structure
135 *
136 * Returns non-zero if print buffer is empty.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100137 */
138
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000139static int tipc_printbuf_empty(struct print_buf *pb)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100140{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000141 return !pb->buf || (pb->crs == pb->buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100142}
143
144/**
Per Liden4323add2006-01-18 00:38:21 +0100145 * tipc_printbuf_validate - check for print buffer overflow
Allan Stephens29ede242006-10-16 21:42:04 -0700146 * @pb: pointer to print buffer structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900147 *
148 * Verifies that a print buffer has captured all data written to it.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100149 * If data has been lost, linearize buffer and prepend an error message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900150 *
Allan Stephens29ede242006-10-16 21:42:04 -0700151 * Returns length of print buffer data string (including trailing NUL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100152 */
153
Per Liden4323add2006-01-18 00:38:21 +0100154int tipc_printbuf_validate(struct print_buf *pb)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100155{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900156 char *err = "\n\n*** PRINT BUFFER OVERFLOW ***\n\n";
157 char *cp_buf;
158 struct print_buf cb;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100159
Allan Stephens29ede242006-10-16 21:42:04 -0700160 if (!pb->buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100161 return 0;
162
Allan Stephens29ede242006-10-16 21:42:04 -0700163 if (pb->buf[pb->size - 1] == 0) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900164 cp_buf = kmalloc(pb->size, GFP_ATOMIC);
Allan Stephens7d3aa712008-05-05 01:22:59 -0700165 if (cp_buf) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900166 tipc_printbuf_init(&cb, cp_buf, pb->size);
167 tipc_printbuf_move(&cb, pb);
168 tipc_printbuf_move(pb, &cb);
169 kfree(cp_buf);
170 memcpy(pb->buf, err, strlen(err));
171 } else {
172 tipc_printbuf_reset(pb);
173 tipc_printf(pb, err);
174 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175 }
Eric Dumazeta02cec22010-09-22 20:43:57 +0000176 return pb->crs - pb->buf + 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177}
178
179/**
Per Liden4323add2006-01-18 00:38:21 +0100180 * tipc_printbuf_move - move print buffer contents to another print buffer
Allan Stephens29ede242006-10-16 21:42:04 -0700181 * @pb_to: pointer to destination print buffer structure
182 * @pb_from: pointer to source print buffer structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900183 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100184 * Current contents of destination print buffer (if any) are discarded.
185 * Source print buffer becomes empty if a successful move occurs.
186 */
187
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000188static void tipc_printbuf_move(struct print_buf *pb_to,
189 struct print_buf *pb_from)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100190{
191 int len;
192
193 /* Handle the cases where contents can't be moved */
194
Allan Stephens29ede242006-10-16 21:42:04 -0700195 if (!pb_to->buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100196 return;
197
Allan Stephens29ede242006-10-16 21:42:04 -0700198 if (!pb_from->buf) {
Per Liden4323add2006-01-18 00:38:21 +0100199 tipc_printbuf_reset(pb_to);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100200 return;
201 }
202
203 if (pb_to->size < pb_from->size) {
Allan Stephens93758c32008-05-05 01:21:12 -0700204 strcpy(pb_to->buf, "*** PRINT BUFFER MOVE ERROR ***");
205 pb_to->buf[pb_to->size - 1] = ~0;
206 pb_to->crs = strchr(pb_to->buf, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100207 return;
208 }
209
210 /* Copy data from char after cursor to end (if used) */
Allan Stephens29ede242006-10-16 21:42:04 -0700211
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212 len = pb_from->buf + pb_from->size - pb_from->crs - 2;
Allan Stephens7d3aa712008-05-05 01:22:59 -0700213 if ((pb_from->buf[pb_from->size - 1] == 0) && (len > 0)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100214 strcpy(pb_to->buf, pb_from->crs + 1);
215 pb_to->crs = pb_to->buf + len;
216 } else
217 pb_to->crs = pb_to->buf;
218
219 /* Copy data from start to cursor (always) */
Allan Stephens29ede242006-10-16 21:42:04 -0700220
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221 len = pb_from->crs - pb_from->buf;
222 strcpy(pb_to->crs, pb_from->buf);
223 pb_to->crs += len;
224
Per Liden4323add2006-01-18 00:38:21 +0100225 tipc_printbuf_reset(pb_from);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226}
227
228/**
Allan Stephensc8903982008-05-05 01:22:30 -0700229 * tipc_printf - append formatted output to print buffer
230 * @pb: pointer to print buffer
Allan Stephens29ede242006-10-16 21:42:04 -0700231 * @fmt: formatted info to be printed
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 */
233
234void tipc_printf(struct print_buf *pb, const char *fmt, ...)
235{
236 int chars_to_add;
237 int chars_left;
238 char save_char;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239
240 spin_lock_bh(&print_lock);
Allan Stephensc8903982008-05-05 01:22:30 -0700241
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 FORMAT(print_string, chars_to_add, fmt);
Allan Stephens29ede242006-10-16 21:42:04 -0700243 if (chars_to_add >= TIPC_PB_MAX_STR)
244 strcpy(print_string, "*** PRINT BUFFER STRING TOO LONG ***");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245
Allan Stephensc8903982008-05-05 01:22:30 -0700246 if (pb->buf) {
247 chars_left = pb->buf + pb->size - pb->crs - 1;
248 if (chars_to_add <= chars_left) {
249 strcpy(pb->crs, print_string);
250 pb->crs += chars_to_add;
251 } else if (chars_to_add >= (pb->size - 1)) {
252 strcpy(pb->buf, print_string + chars_to_add + 1
253 - pb->size);
254 pb->crs = pb->buf + pb->size - 1;
255 } else {
256 strcpy(pb->buf, print_string + chars_left);
257 save_char = print_string[chars_left];
258 print_string[chars_left] = 0;
259 strcpy(pb->crs, print_string);
260 print_string[chars_left] = save_char;
261 pb->crs = pb->buf + chars_to_add - chars_left;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900262 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100263 }
Allan Stephensc8903982008-05-05 01:22:30 -0700264
265 if (pb->echo)
Stephen Hemminger4b704d52009-03-18 19:11:29 -0700266 printk("%s", print_string);
Allan Stephensc8903982008-05-05 01:22:30 -0700267
Per Lidenb97bf3f2006-01-02 19:04:38 +0100268 spin_unlock_bh(&print_lock);
269}
270
Allan Stephens48c97132008-05-05 01:24:06 -0700271#ifdef CONFIG_TIPC_DEBUG
272
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100274 * print_to_console - write string of bytes to console in multiple chunks
275 */
276
277static void print_to_console(char *crs, int len)
278{
279 int rest = len;
280
281 while (rest > 0) {
Allan Stephens29ede242006-10-16 21:42:04 -0700282 int sz = rest < TIPC_PB_MAX_STR ? rest : TIPC_PB_MAX_STR;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100283 char c = crs[sz];
284
285 crs[sz] = 0;
286 printk((const char *)crs);
287 crs[sz] = c;
288 rest -= sz;
289 crs += sz;
290 }
291}
292
293/**
294 * printbuf_dump - write print buffer contents to console
295 */
296
297static void printbuf_dump(struct print_buf *pb)
298{
299 int len;
300
Allan Stephens29ede242006-10-16 21:42:04 -0700301 if (!pb->buf) {
302 printk("*** PRINT BUFFER NOT ALLOCATED ***");
303 return;
304 }
305
Per Lidenb97bf3f2006-01-02 19:04:38 +0100306 /* Dump print buffer from char after cursor to end (if used) */
Allan Stephens29ede242006-10-16 21:42:04 -0700307
Per Lidenb97bf3f2006-01-02 19:04:38 +0100308 len = pb->buf + pb->size - pb->crs - 2;
309 if ((pb->buf[pb->size - 1] == 0) && (len > 0))
310 print_to_console(pb->crs + 1, len);
311
312 /* Dump print buffer from start to cursor (always) */
Allan Stephens29ede242006-10-16 21:42:04 -0700313
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314 len = pb->crs - pb->buf;
315 print_to_console(pb->buf, len);
316}
317
318/**
Allan Stephens48c97132008-05-05 01:24:06 -0700319 * tipc_dump_dbg - dump (non-console) print buffer to console
Allan Stephensc8903982008-05-05 01:22:30 -0700320 * @pb: pointer to print buffer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321 */
322
Allan Stephens48c97132008-05-05 01:24:06 -0700323void tipc_dump_dbg(struct print_buf *pb, const char *fmt, ...)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324{
325 int len;
326
Allan Stephensc8903982008-05-05 01:22:30 -0700327 if (pb == TIPC_CONS)
328 return;
329
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330 spin_lock_bh(&print_lock);
Allan Stephensc8903982008-05-05 01:22:30 -0700331
Allan Stephens29ede242006-10-16 21:42:04 -0700332 FORMAT(print_string, len, fmt);
333 printk(print_string);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334
Allan Stephensc8903982008-05-05 01:22:30 -0700335 printk("\n---- Start of %s log dump ----\n\n",
336 (pb == TIPC_LOG) ? "global" : "local");
337 printbuf_dump(pb);
338 tipc_printbuf_reset(pb);
339 printk("\n---- End of dump ----\n");
340
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341 spin_unlock_bh(&print_lock);
342}
343
Allan Stephens48c97132008-05-05 01:24:06 -0700344#endif
345
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346/**
Allan Stephens025adbe2008-05-05 01:20:04 -0700347 * tipc_log_resize - change the size of the TIPC log buffer
348 * @log_size: print buffer size to use
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 */
350
Allan Stephensfb98ec72008-05-05 01:20:42 -0700351int tipc_log_resize(int log_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352{
Allan Stephensfb98ec72008-05-05 01:20:42 -0700353 int res = 0;
354
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355 spin_lock_bh(&print_lock);
Per Liden4323add2006-01-18 00:38:21 +0100356 if (TIPC_LOG->buf) {
357 kfree(TIPC_LOG->buf);
358 TIPC_LOG->buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359 }
Allan Stephens025adbe2008-05-05 01:20:04 -0700360 if (log_size) {
361 if (log_size < TIPC_PB_MIN_SIZE)
362 log_size = TIPC_PB_MIN_SIZE;
Allan Stephensc8903982008-05-05 01:22:30 -0700363 res = TIPC_LOG->echo;
Allan Stephens025adbe2008-05-05 01:20:04 -0700364 tipc_printbuf_init(TIPC_LOG, kmalloc(log_size, GFP_ATOMIC),
365 log_size);
Allan Stephensc8903982008-05-05 01:22:30 -0700366 TIPC_LOG->echo = res;
Allan Stephensfb98ec72008-05-05 01:20:42 -0700367 res = !TIPC_LOG->buf;
Allan Stephens025adbe2008-05-05 01:20:04 -0700368 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100369 spin_unlock_bh(&print_lock);
Allan Stephensfb98ec72008-05-05 01:20:42 -0700370
371 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372}
373
374/**
Allan Stephens025adbe2008-05-05 01:20:04 -0700375 * tipc_log_resize_cmd - reconfigure size of TIPC log buffer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376 */
377
Allan Stephens025adbe2008-05-05 01:20:04 -0700378struct sk_buff *tipc_log_resize_cmd(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379{
380 u32 value;
381
382 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
Per Liden4323add2006-01-18 00:38:21 +0100383 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384
Al Viro3e6c8cd2006-11-08 00:19:09 -0800385 value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386 if (value != delimit(value, 0, 32768))
Per Liden4323add2006-01-18 00:38:21 +0100387 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
388 " (log size must be 0-32768)");
Allan Stephensfb98ec72008-05-05 01:20:42 -0700389 if (tipc_log_resize(value))
390 return tipc_cfg_reply_error_string(
391 "unable to create specified log (log size is now 0)");
Per Liden4323add2006-01-18 00:38:21 +0100392 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393}
394
395/**
Per Liden4323add2006-01-18 00:38:21 +0100396 * tipc_log_dump - capture TIPC log buffer contents in configuration message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100397 */
398
Per Liden4323add2006-01-18 00:38:21 +0100399struct sk_buff *tipc_log_dump(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400{
401 struct sk_buff *reply;
402
403 spin_lock_bh(&print_lock);
Allan Stephens93758c32008-05-05 01:21:12 -0700404 if (!TIPC_LOG->buf) {
405 spin_unlock_bh(&print_lock);
Per Liden4323add2006-01-18 00:38:21 +0100406 reply = tipc_cfg_reply_ultra_string("log not activated\n");
Allan Stephens93758c32008-05-05 01:21:12 -0700407 } else if (tipc_printbuf_empty(TIPC_LOG)) {
408 spin_unlock_bh(&print_lock);
Per Liden4323add2006-01-18 00:38:21 +0100409 reply = tipc_cfg_reply_ultra_string("log is empty\n");
Allan Stephens93758c32008-05-05 01:21:12 -0700410 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411 else {
412 struct tlv_desc *rep_tlv;
413 struct print_buf pb;
414 int str_len;
415
Per Liden4323add2006-01-18 00:38:21 +0100416 str_len = min(TIPC_LOG->size, 32768u);
Allan Stephens93758c32008-05-05 01:21:12 -0700417 spin_unlock_bh(&print_lock);
Per Liden4323add2006-01-18 00:38:21 +0100418 reply = tipc_cfg_reply_alloc(TLV_SPACE(str_len));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100419 if (reply) {
420 rep_tlv = (struct tlv_desc *)reply->data;
Per Liden4323add2006-01-18 00:38:21 +0100421 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), str_len);
Allan Stephens93758c32008-05-05 01:21:12 -0700422 spin_lock_bh(&print_lock);
Per Liden4323add2006-01-18 00:38:21 +0100423 tipc_printbuf_move(&pb, TIPC_LOG);
Allan Stephens93758c32008-05-05 01:21:12 -0700424 spin_unlock_bh(&print_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100425 str_len = strlen(TLV_DATA(rep_tlv)) + 1;
426 skb_put(reply, TLV_SPACE(str_len));
427 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
428 }
429 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430 return reply;
431}
432