tipc: Prune down link-specific debugging code
[deliverable/linux.git] / net / tipc / log.c
CommitLineData
b97bf3fd 1/*
f5e75269 2 * net/tipc/log.c: TIPC print buffer routines for debugging
c4307285 3 *
593a5f22 4 * Copyright (c) 1996-2006, Ericsson AB
fb98ec71 5 * Copyright (c) 2005-2007, Wind River Systems
b97bf3fd
PL
6 * All rights reserved.
7 *
9ea1fd3c 8 * Redistribution and use in source and binary forms, with or without
b97bf3fd
PL
9 * modification, are permitted provided that the following conditions are met:
10 *
9ea1fd3c
PL
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.
b97bf3fd 19 *
9ea1fd3c
PL
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
b97bf3fd
PL
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "config.h"
f5e75269 39#include "log.h"
b97bf3fd 40
c8903985
AS
41/*
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 */
b97bf3fd 50
c8903985 51static struct print_buf null_buf = { NULL, 0, NULL, 0 };
7d3aa712 52struct print_buf *const TIPC_NULL = &null_buf;
29ede244 53
c8903985 54static struct print_buf cons_buf = { NULL, 0, NULL, 1 };
31e3c3f6 55static struct print_buf *const TIPC_CONS = &cons_buf;
b97bf3fd 56
c8903985 57static struct print_buf log_buf = { NULL, 0, NULL, 1 };
7d3aa712 58struct print_buf *const TIPC_LOG = &log_buf;
b97bf3fd 59
c8903985
AS
60/*
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 *
7ced6890
AS
67 * 2) tipc_log_XXX() leverages the aforementioned use of 'print_lock' to
68 * protect against all types of concurrent operations on their associated
69 * print buffer (not just write operations).
c8903985
AS
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
31e3c3f6 79static 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);
b97bf3fd 83
f5e75269 84#define FORMAT(PTR, LEN, FMT) \
b97bf3fd 85{\
f5e75269
AS
86 va_list args;\
87 va_start(args, FMT);\
88 LEN = vsprintf(PTR, FMT, args);\
89 va_end(args);\
90 *(PTR + LEN) = '\0';\
b97bf3fd
PL
91}
92
b97bf3fd 93/**
4323add6 94 * tipc_printbuf_init - initialize print buffer to empty
29ede244
AS
95 * @pb: pointer to print buffer structure
96 * @raw: pointer to character array used by print buffer
97 * @size: size of character array
98 *
c8903985
AS
99 * Note: If the character array is too small (or absent), the print buffer
100 * becomes a null device that discards anything written to it.
b97bf3fd
PL
101 */
102
29ede244 103void tipc_printbuf_init(struct print_buf *pb, char *raw, u32 size)
b97bf3fd 104{
29ede244
AS
105 pb->buf = raw;
106 pb->crs = raw;
107 pb->size = size;
c8903985 108 pb->echo = 0;
29ede244
AS
109
110 if (size < TIPC_PB_MIN_SIZE) {
111 pb->buf = NULL;
112 } else if (raw) {
113 pb->buf[0] = 0;
7d3aa712 114 pb->buf[size - 1] = ~0;
29ede244 115 }
b97bf3fd
PL
116}
117
118/**
4323add6 119 * tipc_printbuf_reset - reinitialize print buffer to empty state
29ede244 120 * @pb: pointer to print buffer structure
b97bf3fd
PL
121 */
122
31e3c3f6 123static void tipc_printbuf_reset(struct print_buf *pb)
b97bf3fd 124{
7d3aa712 125 if (pb->buf) {
c8903985
AS
126 pb->crs = pb->buf;
127 pb->buf[0] = 0;
128 pb->buf[pb->size - 1] = ~0;
129 }
b97bf3fd
PL
130}
131
132/**
4323add6 133 * tipc_printbuf_empty - test if print buffer is in empty state
29ede244
AS
134 * @pb: pointer to print buffer structure
135 *
136 * Returns non-zero if print buffer is empty.
b97bf3fd
PL
137 */
138
31e3c3f6 139static int tipc_printbuf_empty(struct print_buf *pb)
b97bf3fd 140{
a02cec21 141 return !pb->buf || (pb->crs == pb->buf);
b97bf3fd
PL
142}
143
144/**
4323add6 145 * tipc_printbuf_validate - check for print buffer overflow
29ede244 146 * @pb: pointer to print buffer structure
c4307285
YH
147 *
148 * Verifies that a print buffer has captured all data written to it.
b97bf3fd 149 * If data has been lost, linearize buffer and prepend an error message
c4307285 150 *
29ede244 151 * Returns length of print buffer data string (including trailing NUL)
b97bf3fd
PL
152 */
153
4323add6 154int tipc_printbuf_validate(struct print_buf *pb)
b97bf3fd 155{
c4307285
YH
156 char *err = "\n\n*** PRINT BUFFER OVERFLOW ***\n\n";
157 char *cp_buf;
158 struct print_buf cb;
b97bf3fd 159
29ede244 160 if (!pb->buf)
b97bf3fd
PL
161 return 0;
162
29ede244 163 if (pb->buf[pb->size - 1] == 0) {
c4307285 164 cp_buf = kmalloc(pb->size, GFP_ATOMIC);
7d3aa712 165 if (cp_buf) {
c4307285
YH
166 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 }
b97bf3fd 175 }
a02cec21 176 return pb->crs - pb->buf + 1;
b97bf3fd
PL
177}
178
179/**
4323add6 180 * tipc_printbuf_move - move print buffer contents to another print buffer
29ede244
AS
181 * @pb_to: pointer to destination print buffer structure
182 * @pb_from: pointer to source print buffer structure
c4307285 183 *
b97bf3fd
PL
184 * Current contents of destination print buffer (if any) are discarded.
185 * Source print buffer becomes empty if a successful move occurs.
186 */
187
31e3c3f6 188static void tipc_printbuf_move(struct print_buf *pb_to,
189 struct print_buf *pb_from)
b97bf3fd
PL
190{
191 int len;
192
193 /* Handle the cases where contents can't be moved */
194
29ede244 195 if (!pb_to->buf)
b97bf3fd
PL
196 return;
197
29ede244 198 if (!pb_from->buf) {
4323add6 199 tipc_printbuf_reset(pb_to);
b97bf3fd
PL
200 return;
201 }
202
203 if (pb_to->size < pb_from->size) {
93758c3d
AS
204 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);
b97bf3fd
PL
207 return;
208 }
209
210 /* Copy data from char after cursor to end (if used) */
29ede244 211
b97bf3fd 212 len = pb_from->buf + pb_from->size - pb_from->crs - 2;
7d3aa712 213 if ((pb_from->buf[pb_from->size - 1] == 0) && (len > 0)) {
b97bf3fd
PL
214 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) */
29ede244 220
b97bf3fd
PL
221 len = pb_from->crs - pb_from->buf;
222 strcpy(pb_to->crs, pb_from->buf);
223 pb_to->crs += len;
224
4323add6 225 tipc_printbuf_reset(pb_from);
b97bf3fd
PL
226}
227
228/**
c8903985
AS
229 * tipc_printf - append formatted output to print buffer
230 * @pb: pointer to print buffer
29ede244 231 * @fmt: formatted info to be printed
b97bf3fd
PL
232 */
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;
b97bf3fd
PL
239
240 spin_lock_bh(&print_lock);
c8903985 241
b97bf3fd 242 FORMAT(print_string, chars_to_add, fmt);
29ede244
AS
243 if (chars_to_add >= TIPC_PB_MAX_STR)
244 strcpy(print_string, "*** PRINT BUFFER STRING TOO LONG ***");
b97bf3fd 245
c8903985
AS
246 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;
c4307285 262 }
b97bf3fd 263 }
b97bf3fd 264
c8903985 265 if (pb->echo)
4b704d59 266 printk("%s", print_string);
b97bf3fd 267
b97bf3fd 268 spin_unlock_bh(&print_lock);
b97bf3fd
PL
269}
270
b97bf3fd 271/**
025adbe8
AS
272 * tipc_log_resize - change the size of the TIPC log buffer
273 * @log_size: print buffer size to use
b97bf3fd
PL
274 */
275
fb98ec71 276int tipc_log_resize(int log_size)
b97bf3fd 277{
fb98ec71
AS
278 int res = 0;
279
b97bf3fd 280 spin_lock_bh(&print_lock);
f5e75269
AS
281 kfree(TIPC_LOG->buf);
282 TIPC_LOG->buf = NULL;
b97bf3fd 283 if (log_size) {
29ede244
AS
284 if (log_size < TIPC_PB_MIN_SIZE)
285 log_size = TIPC_PB_MIN_SIZE;
c8903985 286 res = TIPC_LOG->echo;
29ede244
AS
287 tipc_printbuf_init(TIPC_LOG, kmalloc(log_size, GFP_ATOMIC),
288 log_size);
c8903985 289 TIPC_LOG->echo = res;
fb98ec71 290 res = !TIPC_LOG->buf;
b97bf3fd 291 }
025adbe8 292 spin_unlock_bh(&print_lock);
fb98ec71
AS
293
294 return res;
b97bf3fd
PL
295}
296
297/**
025adbe8 298 * tipc_log_resize_cmd - reconfigure size of TIPC log buffer
b97bf3fd
PL
299 */
300
025adbe8 301struct sk_buff *tipc_log_resize_cmd(const void *req_tlv_area, int req_tlv_space)
b97bf3fd
PL
302{
303 u32 value;
304
305 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
4323add6 306 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
b97bf3fd 307
3e6c8cd5 308 value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
b97bf3fd 309 if (value != delimit(value, 0, 32768))
4323add6
PL
310 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
311 " (log size must be 0-32768)");
fb98ec71
AS
312 if (tipc_log_resize(value))
313 return tipc_cfg_reply_error_string(
314 "unable to create specified log (log size is now 0)");
4323add6 315 return tipc_cfg_reply_none();
b97bf3fd
PL
316}
317
318/**
4323add6 319 * tipc_log_dump - capture TIPC log buffer contents in configuration message
b97bf3fd
PL
320 */
321
4323add6 322struct sk_buff *tipc_log_dump(void)
b97bf3fd
PL
323{
324 struct sk_buff *reply;
325
326 spin_lock_bh(&print_lock);
93758c3d
AS
327 if (!TIPC_LOG->buf) {
328 spin_unlock_bh(&print_lock);
4323add6 329 reply = tipc_cfg_reply_ultra_string("log not activated\n");
93758c3d
AS
330 } else if (tipc_printbuf_empty(TIPC_LOG)) {
331 spin_unlock_bh(&print_lock);
4323add6 332 reply = tipc_cfg_reply_ultra_string("log is empty\n");
f5e75269 333 } else {
b97bf3fd
PL
334 struct tlv_desc *rep_tlv;
335 struct print_buf pb;
336 int str_len;
337
4323add6 338 str_len = min(TIPC_LOG->size, 32768u);
93758c3d 339 spin_unlock_bh(&print_lock);
4323add6 340 reply = tipc_cfg_reply_alloc(TLV_SPACE(str_len));
b97bf3fd
PL
341 if (reply) {
342 rep_tlv = (struct tlv_desc *)reply->data;
4323add6 343 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), str_len);
93758c3d 344 spin_lock_bh(&print_lock);
4323add6 345 tipc_printbuf_move(&pb, TIPC_LOG);
93758c3d 346 spin_unlock_bh(&print_lock);
b97bf3fd
PL
347 str_len = strlen(TLV_DATA(rep_tlv)) + 1;
348 skb_put(reply, TLV_SPACE(str_len));
349 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
350 }
351 }
b97bf3fd
PL
352 return reply;
353}
This page took 0.453128 seconds and 5 git commands to generate.