log.c 9.6 KB
Newer Older
P
Per Liden 已提交
1
/*
2
 * net/tipc/log.c: TIPC print buffer routines for debugging
3
 *
P
Per Liden 已提交
4
 * Copyright (c) 1996-2006, Ericsson AB
5
 * Copyright (c) 2005-2007, Wind River Systems
P
Per Liden 已提交
6 7
 * All rights reserved.
 *
P
Per Liden 已提交
8
 * Redistribution and use in source and binary forms, with or without
P
Per Liden 已提交
9 10
 * modification, are permitted provided that the following conditions are met:
 *
P
Per Liden 已提交
11 12 13 14 15 16 17 18
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the names of the copyright holders nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
P
Per Liden 已提交
19
 *
P
Per Liden 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
P
Per Liden 已提交
34 35 36 37 38
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "core.h"
#include "config.h"
39
#include "log.h"
P
Per Liden 已提交
40

41 42 43 44 45 46 47 48 49 50
/*
 * TIPC pre-defines the following print buffers:
 *
 * TIPC_NULL : null buffer (i.e. print nowhere)
 * TIPC_CONS : system console
 * TIPC_LOG  : TIPC log buffer
 *
 * Additional user-defined print buffers are also permitted.
 */
static struct print_buf null_buf = { NULL, 0, NULL, 0 };
51
struct print_buf *const TIPC_NULL = &null_buf;
52

53
static struct print_buf cons_buf = { NULL, 0, NULL, 1 };
54
struct print_buf *const TIPC_CONS = &cons_buf;
P
Per Liden 已提交
55

56
static struct print_buf log_buf = { NULL, 0, NULL, 1 };
57
struct print_buf *const TIPC_LOG = &log_buf;
P
Per Liden 已提交
58

59 60 61 62 63 64 65
/*
 * Locking policy when using print buffers.
 *
 * 1) tipc_printf() uses 'print_lock' to protect against concurrent access to
 * 'print_string' when writing to a print buffer. This also protects against
 * concurrent writes to the print buffer being written to.
 *
66 67 68
 * 2) tipc_log_XXX() leverages the aforementioned use of 'print_lock' to
 * protect against all types of concurrent operations on their associated
 * print buffer (not just write operations).
69 70 71 72 73 74 75 76
 *
 * Note: All routines of the form tipc_printbuf_XXX() are lock-free, and rely
 * on the caller to prevent simultaneous use of the print buffer(s) being
 * manipulated.
 */
static char print_string[TIPC_PB_MAX_STR];
static DEFINE_SPINLOCK(print_lock);

77 78
static void tipc_printbuf_move(struct print_buf *pb_to,
			       struct print_buf *pb_from);
P
Per Liden 已提交
79

80
#define FORMAT(PTR, LEN, FMT) \
P
Per Liden 已提交
81
{\
82 83 84 85 86
	va_list args;\
	va_start(args, FMT);\
	LEN = vsprintf(PTR, FMT, args);\
	va_end(args);\
	*(PTR + LEN) = '\0';\
P
Per Liden 已提交
87 88 89
}

/**
90
 * tipc_printbuf_init - initialize print buffer to empty
91 92 93 94
 * @pb: pointer to print buffer structure
 * @raw: pointer to character array used by print buffer
 * @size: size of character array
 *
95 96
 * Note: If the character array is too small (or absent), the print buffer
 * becomes a null device that discards anything written to it.
P
Per Liden 已提交
97
 */
98
void tipc_printbuf_init(struct print_buf *pb, char *raw, u32 size)
P
Per Liden 已提交
99
{
100 101 102
	pb->buf = raw;
	pb->crs = raw;
	pb->size = size;
103
	pb->echo = 0;
104 105 106 107 108

	if (size < TIPC_PB_MIN_SIZE) {
		pb->buf = NULL;
	} else if (raw) {
		pb->buf[0] = 0;
109
		pb->buf[size - 1] = ~0;
110
	}
P
Per Liden 已提交
111 112 113
}

/**
114
 * tipc_printbuf_reset - reinitialize print buffer to empty state
115
 * @pb: pointer to print buffer structure
P
Per Liden 已提交
116
 */
117
static void tipc_printbuf_reset(struct print_buf *pb)
P
Per Liden 已提交
118
{
119
	if (pb->buf) {
120 121 122 123
		pb->crs = pb->buf;
		pb->buf[0] = 0;
		pb->buf[pb->size - 1] = ~0;
	}
P
Per Liden 已提交
124 125 126
}

/**
127
 * tipc_printbuf_empty - test if print buffer is in empty state
128 129 130
 * @pb: pointer to print buffer structure
 *
 * Returns non-zero if print buffer is empty.
P
Per Liden 已提交
131
 */
132
static int tipc_printbuf_empty(struct print_buf *pb)
P
Per Liden 已提交
133
{
E
Eric Dumazet 已提交
134
	return !pb->buf || (pb->crs == pb->buf);
P
Per Liden 已提交
135 136 137
}

/**
138
 * tipc_printbuf_validate - check for print buffer overflow
139
 * @pb: pointer to print buffer structure
140 141
 *
 * Verifies that a print buffer has captured all data written to it.
P
Per Liden 已提交
142
 * If data has been lost, linearize buffer and prepend an error message
143
 *
144
 * Returns length of print buffer data string (including trailing NUL)
P
Per Liden 已提交
145
 */
146
int tipc_printbuf_validate(struct print_buf *pb)
P
Per Liden 已提交
147
{
148 149 150
	char *err = "\n\n*** PRINT BUFFER OVERFLOW ***\n\n";
	char *cp_buf;
	struct print_buf cb;
P
Per Liden 已提交
151

152
	if (!pb->buf)
P
Per Liden 已提交
153 154
		return 0;

155
	if (pb->buf[pb->size - 1] == 0) {
156
		cp_buf = kmalloc(pb->size, GFP_ATOMIC);
157
		if (cp_buf) {
158 159 160 161 162 163 164 165 166
			tipc_printbuf_init(&cb, cp_buf, pb->size);
			tipc_printbuf_move(&cb, pb);
			tipc_printbuf_move(pb, &cb);
			kfree(cp_buf);
			memcpy(pb->buf, err, strlen(err));
		} else {
			tipc_printbuf_reset(pb);
			tipc_printf(pb, err);
		}
P
Per Liden 已提交
167
	}
E
Eric Dumazet 已提交
168
	return pb->crs - pb->buf + 1;
P
Per Liden 已提交
169 170 171
}

/**
172
 * tipc_printbuf_move - move print buffer contents to another print buffer
173 174
 * @pb_to: pointer to destination print buffer structure
 * @pb_from: pointer to source print buffer structure
175
 *
P
Per Liden 已提交
176 177 178
 * Current contents of destination print buffer (if any) are discarded.
 * Source print buffer becomes empty if a successful move occurs.
 */
179 180
static void tipc_printbuf_move(struct print_buf *pb_to,
			       struct print_buf *pb_from)
P
Per Liden 已提交
181 182 183 184
{
	int len;

	/* Handle the cases where contents can't be moved */
185
	if (!pb_to->buf)
P
Per Liden 已提交
186 187
		return;

188
	if (!pb_from->buf) {
189
		tipc_printbuf_reset(pb_to);
P
Per Liden 已提交
190 191 192 193
		return;
	}

	if (pb_to->size < pb_from->size) {
194 195 196
		strcpy(pb_to->buf, "*** PRINT BUFFER MOVE ERROR ***");
		pb_to->buf[pb_to->size - 1] = ~0;
		pb_to->crs = strchr(pb_to->buf, 0);
P
Per Liden 已提交
197 198 199 200 201
		return;
	}

	/* Copy data from char after cursor to end (if used) */
	len = pb_from->buf + pb_from->size - pb_from->crs - 2;
202
	if ((pb_from->buf[pb_from->size - 1] == 0) && (len > 0)) {
P
Per Liden 已提交
203 204 205 206 207 208 209 210 211 212
		strcpy(pb_to->buf, pb_from->crs + 1);
		pb_to->crs = pb_to->buf + len;
	} else
		pb_to->crs = pb_to->buf;

	/* Copy data from start to cursor (always) */
	len = pb_from->crs - pb_from->buf;
	strcpy(pb_to->crs, pb_from->buf);
	pb_to->crs += len;

213
	tipc_printbuf_reset(pb_from);
P
Per Liden 已提交
214 215 216
}

/**
217 218
 * tipc_printf - append formatted output to print buffer
 * @pb: pointer to print buffer
219
 * @fmt: formatted info to be printed
P
Per Liden 已提交
220 221 222 223 224 225 226 227
 */
void tipc_printf(struct print_buf *pb, const char *fmt, ...)
{
	int chars_to_add;
	int chars_left;
	char save_char;

	spin_lock_bh(&print_lock);
228

P
Per Liden 已提交
229
	FORMAT(print_string, chars_to_add, fmt);
230 231
	if (chars_to_add >= TIPC_PB_MAX_STR)
		strcpy(print_string, "*** PRINT BUFFER STRING TOO LONG ***");
P
Per Liden 已提交
232

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
	if (pb->buf) {
		chars_left = pb->buf + pb->size - pb->crs - 1;
		if (chars_to_add <= chars_left) {
			strcpy(pb->crs, print_string);
			pb->crs += chars_to_add;
		} else if (chars_to_add >= (pb->size - 1)) {
			strcpy(pb->buf, print_string + chars_to_add + 1
			       - pb->size);
			pb->crs = pb->buf + pb->size - 1;
		} else {
			strcpy(pb->buf, print_string + chars_left);
			save_char = print_string[chars_left];
			print_string[chars_left] = 0;
			strcpy(pb->crs, print_string);
			print_string[chars_left] = save_char;
			pb->crs = pb->buf + chars_to_add - chars_left;
249
		}
P
Per Liden 已提交
250 251
	}

252
	if (pb->echo)
253
		printk("%s", print_string);
P
Per Liden 已提交
254 255 256 257 258

	spin_unlock_bh(&print_lock);
}

/**
259 260
 * tipc_log_resize - change the size of the TIPC log buffer
 * @log_size: print buffer size to use
P
Per Liden 已提交
261
 */
262
int tipc_log_resize(int log_size)
P
Per Liden 已提交
263
{
264 265
	int res = 0;

P
Per Liden 已提交
266
	spin_lock_bh(&print_lock);
267 268
	kfree(TIPC_LOG->buf);
	TIPC_LOG->buf = NULL;
P
Per Liden 已提交
269
	if (log_size) {
270 271
		if (log_size < TIPC_PB_MIN_SIZE)
			log_size = TIPC_PB_MIN_SIZE;
272
		res = TIPC_LOG->echo;
273 274
		tipc_printbuf_init(TIPC_LOG, kmalloc(log_size, GFP_ATOMIC),
				   log_size);
275
		TIPC_LOG->echo = res;
276
		res = !TIPC_LOG->buf;
P
Per Liden 已提交
277
	}
278
	spin_unlock_bh(&print_lock);
279 280

	return res;
P
Per Liden 已提交
281 282 283
}

/**
284
 * tipc_log_resize_cmd - reconfigure size of TIPC log buffer
P
Per Liden 已提交
285
 */
286
struct sk_buff *tipc_log_resize_cmd(const void *req_tlv_area, int req_tlv_space)
P
Per Liden 已提交
287 288 289 290
{
	u32 value;

	if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
291
		return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
P
Per Liden 已提交
292

A
Al Viro 已提交
293
	value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
294
	if (value > 32768)
295 296
		return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
						   " (log size must be 0-32768)");
297 298 299
	if (tipc_log_resize(value))
		return tipc_cfg_reply_error_string(
			"unable to create specified log (log size is now 0)");
300
	return tipc_cfg_reply_none();
P
Per Liden 已提交
301 302 303
}

/**
304
 * tipc_log_dump - capture TIPC log buffer contents in configuration message
P
Per Liden 已提交
305
 */
306
struct sk_buff *tipc_log_dump(void)
P
Per Liden 已提交
307 308 309 310
{
	struct sk_buff *reply;

	spin_lock_bh(&print_lock);
311 312
	if (!TIPC_LOG->buf) {
		spin_unlock_bh(&print_lock);
313
		reply = tipc_cfg_reply_ultra_string("log not activated\n");
314 315
	} else if (tipc_printbuf_empty(TIPC_LOG)) {
		spin_unlock_bh(&print_lock);
316
		reply = tipc_cfg_reply_ultra_string("log is empty\n");
317
	} else {
P
Per Liden 已提交
318 319 320 321
		struct tlv_desc *rep_tlv;
		struct print_buf pb;
		int str_len;

322
		str_len = min(TIPC_LOG->size, 32768u);
323
		spin_unlock_bh(&print_lock);
324
		reply = tipc_cfg_reply_alloc(TLV_SPACE(str_len));
P
Per Liden 已提交
325 326
		if (reply) {
			rep_tlv = (struct tlv_desc *)reply->data;
327
			tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), str_len);
328
			spin_lock_bh(&print_lock);
329
			tipc_printbuf_move(&pb, TIPC_LOG);
330
			spin_unlock_bh(&print_lock);
P
Per Liden 已提交
331 332 333 334 335 336 337
			str_len = strlen(TLV_DATA(rep_tlv)) + 1;
			skb_put(reply, TLV_SPACE(str_len));
			TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
		}
	}
	return reply;
}