dbg.c 11.3 KB
Newer Older
P
Per Liden 已提交
1
/*
2
 * net/tipc/dbg.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 39 40
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "core.h"
#include "config.h"
#include "dbg.h"

41
static char print_string[TIPC_PB_MAX_STR];
I
Ingo Molnar 已提交
42
static DEFINE_SPINLOCK(print_lock);
P
Per Liden 已提交
43

44 45 46
static struct print_buf null_buf = { NULL, 0, NULL, NULL };
struct print_buf *TIPC_NULL = &null_buf;

P
Per Liden 已提交
47
static struct print_buf cons_buf = { NULL, 0, NULL, NULL };
48
struct print_buf *TIPC_CONS = &cons_buf;
P
Per Liden 已提交
49 50

static struct print_buf log_buf = { NULL, 0, NULL, NULL };
51
struct print_buf *TIPC_LOG = &log_buf;
P
Per Liden 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65


#define FORMAT(PTR,LEN,FMT) \
{\
       va_list args;\
       va_start(args, FMT);\
       LEN = vsprintf(PTR, FMT, args);\
       va_end(args);\
       *(PTR + LEN) = '\0';\
}

/*
 * Locking policy when using print buffers.
 *
66 67 68 69 70 71 72 73
 * The following routines use 'print_lock' for protection:
 * 1) tipc_printf()  - to protect its print buffer(s) and 'print_string'
 * 2) TIPC_TEE()     - to protect its print buffer(s)
 * 3) tipc_dump()    - to protect its print buffer(s) and 'print_string'
 * 4) tipc_log_XXX() - to protect TIPC_LOG
 *
 * All routines of the form tipc_printbuf_XXX() rely on the caller to prevent
 * simultaneous use of the print buffer(s) being manipulated.
P
Per Liden 已提交
74 75 76
 */

/**
77
 * tipc_printbuf_init - initialize print buffer to empty
78 79 80 81 82 83
 * @pb: pointer to print buffer structure
 * @raw: pointer to character array used by print buffer
 * @size: size of character array
 *
 * Makes the print buffer a null device that discards anything written to it
 * if the character array is too small (or absent).
P
Per Liden 已提交
84 85
 */

86
void tipc_printbuf_init(struct print_buf *pb, char *raw, u32 size)
P
Per Liden 已提交
87
{
88 89 90
	pb->buf = raw;
	pb->crs = raw;
	pb->size = size;
91
	pb->next = NULL;
92 93 94 95 96 97 98

	if (size < TIPC_PB_MIN_SIZE) {
		pb->buf = NULL;
	} else if (raw) {
		pb->buf[0] = 0;
		pb->buf[size-1] = ~0;
	}
P
Per Liden 已提交
99 100 101
}

/**
102
 * tipc_printbuf_reset - reinitialize print buffer to empty state
103
 * @pb: pointer to print buffer structure
P
Per Liden 已提交
104 105
 */

106
void tipc_printbuf_reset(struct print_buf *pb)
P
Per Liden 已提交
107
{
108
	tipc_printbuf_init(pb, pb->buf, pb->size);
P
Per Liden 已提交
109 110 111
}

/**
112
 * tipc_printbuf_empty - test if print buffer is in empty state
113 114 115
 * @pb: pointer to print buffer structure
 *
 * Returns non-zero if print buffer is empty.
P
Per Liden 已提交
116 117
 */

118
int tipc_printbuf_empty(struct print_buf *pb)
P
Per Liden 已提交
119
{
120
	return (!pb->buf || (pb->crs == pb->buf));
P
Per Liden 已提交
121 122 123
}

/**
124
 * tipc_printbuf_validate - check for print buffer overflow
125
 * @pb: pointer to print buffer structure
126 127
 *
 * Verifies that a print buffer has captured all data written to it.
P
Per Liden 已提交
128
 * If data has been lost, linearize buffer and prepend an error message
129
 *
130
 * Returns length of print buffer data string (including trailing NUL)
P
Per Liden 已提交
131 132
 */

133
int tipc_printbuf_validate(struct print_buf *pb)
P
Per Liden 已提交
134
{
135 136 137
	char *err = "\n\n*** PRINT BUFFER OVERFLOW ***\n\n";
	char *cp_buf;
	struct print_buf cb;
P
Per Liden 已提交
138

139
	if (!pb->buf)
P
Per Liden 已提交
140 141
		return 0;

142
	if (pb->buf[pb->size - 1] == 0) {
143 144 145 146 147 148 149 150 151 152 153
		cp_buf = kmalloc(pb->size, GFP_ATOMIC);
		if (cp_buf != NULL){
			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 已提交
154 155 156 157 158
	}
	return (pb->crs - pb->buf + 1);
}

/**
159
 * tipc_printbuf_move - move print buffer contents to another print buffer
160 161
 * @pb_to: pointer to destination print buffer structure
 * @pb_from: pointer to source print buffer structure
162
 *
P
Per Liden 已提交
163 164 165 166
 * Current contents of destination print buffer (if any) are discarded.
 * Source print buffer becomes empty if a successful move occurs.
 */

167
void tipc_printbuf_move(struct print_buf *pb_to, struct print_buf *pb_from)
P
Per Liden 已提交
168 169 170 171 172
{
	int len;

	/* Handle the cases where contents can't be moved */

173
	if (!pb_to->buf)
P
Per Liden 已提交
174 175
		return;

176
	if (!pb_from->buf) {
177
		tipc_printbuf_reset(pb_to);
P
Per Liden 已提交
178 179 180 181
		return;
	}

	if (pb_to->size < pb_from->size) {
182 183 184 185
		strcpy(pb_to->buf, "*** PRINT BUFFER MOVE ERROR ***");
		pb_to->buf[pb_to->size - 1] = ~0;
		pb_to->crs = strchr(pb_to->buf, 0);
		pb_to->next = NULL;
P
Per Liden 已提交
186 187 188 189
		return;
	}

	/* Copy data from char after cursor to end (if used) */
190

P
Per Liden 已提交
191 192 193 194 195 196 197 198
	len = pb_from->buf + pb_from->size - pb_from->crs - 2;
	if ((pb_from->buf[pb_from->size-1] == 0) && (len > 0)) {
		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) */
199

P
Per Liden 已提交
200 201 202 203
	len = pb_from->crs - pb_from->buf;
	strcpy(pb_to->crs, pb_from->buf);
	pb_to->crs += len;

204
	tipc_printbuf_reset(pb_from);
P
Per Liden 已提交
205 206 207 208
}

/**
 * tipc_printf - append formatted output to print buffer chain
209 210
 * @pb: pointer to chain of print buffers (may be NULL)
 * @fmt: formatted info to be printed
P
Per Liden 已提交
211 212 213 214 215 216 217 218 219 220 221
 */

void tipc_printf(struct print_buf *pb, const char *fmt, ...)
{
	int chars_to_add;
	int chars_left;
	char save_char;
	struct print_buf *pb_next;

	spin_lock_bh(&print_lock);
	FORMAT(print_string, chars_to_add, fmt);
222 223
	if (chars_to_add >= TIPC_PB_MAX_STR)
		strcpy(print_string, "*** PRINT BUFFER STRING TOO LONG ***");
P
Per Liden 已提交
224 225

	while (pb) {
226
		if (pb == TIPC_CONS)
P
Per Liden 已提交
227 228 229 230 231 232
			printk(print_string);
		else 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;
233 234 235 236
			} 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;
P
Per Liden 已提交
237 238
			} else {
				strcpy(pb->buf, print_string + chars_left);
239 240 241 242 243 244 245
				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;
			}
		}
P
Per Liden 已提交
246
		pb_next = pb->next;
247
		pb->next = NULL;
P
Per Liden 已提交
248 249 250 251 252 253
		pb = pb_next;
	}
	spin_unlock_bh(&print_lock);
}

/**
254
 * TIPC_TEE - perform next output operation on both print buffers
255 256 257 258
 * @b0: pointer to chain of print buffers (may be NULL)
 * @b1: pointer to print buffer to add to chain
 *
 * Returns pointer to print buffer chain.
P
Per Liden 已提交
259 260
 */

261
struct print_buf *TIPC_TEE(struct print_buf *b0, struct print_buf *b1)
P
Per Liden 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
{
	struct print_buf *pb = b0;

	if (!b0 || (b0 == b1))
		return b1;

	spin_lock_bh(&print_lock);
	while (pb->next) {
		if ((pb->next == b1) || (pb->next == b0))
			pb->next = pb->next->next;
		else
			pb = pb->next;
	}
	pb->next = b1;
	spin_unlock_bh(&print_lock);
	return b0;
}

/**
 * print_to_console - write string of bytes to console in multiple chunks
 */

static void print_to_console(char *crs, int len)
{
	int rest = len;

	while (rest > 0) {
289
		int sz = rest < TIPC_PB_MAX_STR ? rest : TIPC_PB_MAX_STR;
P
Per Liden 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
		char c = crs[sz];

		crs[sz] = 0;
		printk((const char *)crs);
		crs[sz] = c;
		rest -= sz;
		crs += sz;
	}
}

/**
 * printbuf_dump - write print buffer contents to console
 */

static void printbuf_dump(struct print_buf *pb)
{
	int len;

308 309 310 311 312
	if (!pb->buf) {
		printk("*** PRINT BUFFER NOT ALLOCATED ***");
		return;
	}

P
Per Liden 已提交
313
	/* Dump print buffer from char after cursor to end (if used) */
314

P
Per Liden 已提交
315 316 317 318 319
	len = pb->buf + pb->size - pb->crs - 2;
	if ((pb->buf[pb->size - 1] == 0) && (len > 0))
		print_to_console(pb->crs + 1, len);

	/* Dump print buffer from start to cursor (always) */
320

P
Per Liden 已提交
321 322 323 324 325 326
	len = pb->crs - pb->buf;
	print_to_console(pb->buf, len);
}

/**
 * tipc_dump - dump non-console print buffer(s) to console
327
 * @pb: pointer to chain of print buffers
P
Per Liden 已提交
328 329 330 331
 */

void tipc_dump(struct print_buf *pb, const char *fmt, ...)
{
332
	struct print_buf *pb_next;
P
Per Liden 已提交
333 334 335
	int len;

	spin_lock_bh(&print_lock);
336 337
	FORMAT(print_string, len, fmt);
	printk(print_string);
P
Per Liden 已提交
338 339

	for (; pb; pb = pb->next) {
340 341 342 343 344 345 346 347 348 349
		if (pb != TIPC_CONS) {
			printk("\n---- Start of %s log dump ----\n\n",
			       (pb == TIPC_LOG) ? "global" : "local");
			printbuf_dump(pb);
			tipc_printbuf_reset(pb);
			printk("\n---- End of dump ----\n");
		}
		pb_next = pb->next;
		pb->next = NULL;
		pb = pb_next;
P
Per Liden 已提交
350 351 352 353 354
	}
	spin_unlock_bh(&print_lock);
}

/**
355 356
 * tipc_log_resize - change the size of the TIPC log buffer
 * @log_size: print buffer size to use
P
Per Liden 已提交
357 358
 */

359
int tipc_log_resize(int log_size)
P
Per Liden 已提交
360
{
361 362
	int res = 0;

P
Per Liden 已提交
363
	spin_lock_bh(&print_lock);
364 365 366
	if (TIPC_LOG->buf) {
		kfree(TIPC_LOG->buf);
		TIPC_LOG->buf = NULL;
P
Per Liden 已提交
367 368
	}
	if (log_size) {
369 370 371 372
		if (log_size < TIPC_PB_MIN_SIZE)
			log_size = TIPC_PB_MIN_SIZE;
		tipc_printbuf_init(TIPC_LOG, kmalloc(log_size, GFP_ATOMIC),
				   log_size);
373
		res = !TIPC_LOG->buf;
P
Per Liden 已提交
374
	}
375
	spin_unlock_bh(&print_lock);
376 377

	return res;
P
Per Liden 已提交
378 379 380
}

/**
381
 * tipc_log_resize_cmd - reconfigure size of TIPC log buffer
P
Per Liden 已提交
382 383
 */

384
struct sk_buff *tipc_log_resize_cmd(const void *req_tlv_area, int req_tlv_space)
P
Per Liden 已提交
385 386 387 388
{
	u32 value;

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

A
Al Viro 已提交
391
	value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
P
Per Liden 已提交
392
	if (value != delimit(value, 0, 32768))
393 394
		return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
						   " (log size must be 0-32768)");
395 396 397
	if (tipc_log_resize(value))
		return tipc_cfg_reply_error_string(
			"unable to create specified log (log size is now 0)");
398
	return tipc_cfg_reply_none();
P
Per Liden 已提交
399 400 401
}

/**
402
 * tipc_log_dump - capture TIPC log buffer contents in configuration message
P
Per Liden 已提交
403 404
 */

405
struct sk_buff *tipc_log_dump(void)
P
Per Liden 已提交
406 407 408 409
{
	struct sk_buff *reply;

	spin_lock_bh(&print_lock);
410 411
	if (!TIPC_LOG->buf) {
		spin_unlock_bh(&print_lock);
412
		reply = tipc_cfg_reply_ultra_string("log not activated\n");
413 414
	} else if (tipc_printbuf_empty(TIPC_LOG)) {
		spin_unlock_bh(&print_lock);
415
		reply = tipc_cfg_reply_ultra_string("log is empty\n");
416
	}
P
Per Liden 已提交
417 418 419 420 421
	else {
		struct tlv_desc *rep_tlv;
		struct print_buf pb;
		int str_len;

422
		str_len = min(TIPC_LOG->size, 32768u);
423
		spin_unlock_bh(&print_lock);
424
		reply = tipc_cfg_reply_alloc(TLV_SPACE(str_len));
P
Per Liden 已提交
425 426
		if (reply) {
			rep_tlv = (struct tlv_desc *)reply->data;
427
			tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), str_len);
428
			spin_lock_bh(&print_lock);
429
			tipc_printbuf_move(&pb, TIPC_LOG);
430
			spin_unlock_bh(&print_lock);
P
Per Liden 已提交
431 432 433 434 435 436 437 438
			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;
}