2 * net/tipc/log.c: TIPC print buffer routines for debugging
4 * Copyright (c) 1996-2006, Ericsson AB
5 * Copyright (c) 2005-2007, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
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
34 * POSSIBILITY OF SUCH DAMAGE.
42 * TIPC pre-defines the following print buffers:
44 * TIPC_NULL : null buffer (i.e. print nowhere)
45 * TIPC_CONS : system console
46 * TIPC_LOG : TIPC log buffer
48 * Additional user-defined print buffers are also permitted.
51 static struct print_buf null_buf
= { NULL
, 0, NULL
, 0 };
52 struct print_buf
*const TIPC_NULL
= &null_buf
;
54 static struct print_buf cons_buf
= { NULL
, 0, NULL
, 1 };
55 struct print_buf
*const TIPC_CONS
= &cons_buf
;
57 static struct print_buf log_buf
= { NULL
, 0, NULL
, 1 };
58 struct print_buf
*const TIPC_LOG
= &log_buf
;
61 * Locking policy when using print buffers.
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.
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).
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
76 static char print_string
[TIPC_PB_MAX_STR
];
77 static DEFINE_SPINLOCK(print_lock
);
79 static void tipc_printbuf_move(struct print_buf
*pb_to
,
80 struct print_buf
*pb_from
);
82 #define FORMAT(PTR, LEN, FMT) \
86 LEN = vsprintf(PTR, FMT, args);\
92 * tipc_printbuf_init - initialize print buffer to empty
93 * @pb: pointer to print buffer structure
94 * @raw: pointer to character array used by print buffer
95 * @size: size of character array
97 * Note: If the character array is too small (or absent), the print buffer
98 * becomes a null device that discards anything written to it.
101 void tipc_printbuf_init(struct print_buf
*pb
, char *raw
, u32 size
)
108 if (size
< TIPC_PB_MIN_SIZE
) {
112 pb
->buf
[size
- 1] = ~0;
117 * tipc_printbuf_reset - reinitialize print buffer to empty state
118 * @pb: pointer to print buffer structure
121 static void tipc_printbuf_reset(struct print_buf
*pb
)
126 pb
->buf
[pb
->size
- 1] = ~0;
131 * tipc_printbuf_empty - test if print buffer is in empty state
132 * @pb: pointer to print buffer structure
134 * Returns non-zero if print buffer is empty.
137 static int tipc_printbuf_empty(struct print_buf
*pb
)
139 return !pb
->buf
|| (pb
->crs
== pb
->buf
);
143 * tipc_printbuf_validate - check for print buffer overflow
144 * @pb: pointer to print buffer structure
146 * Verifies that a print buffer has captured all data written to it.
147 * If data has been lost, linearize buffer and prepend an error message
149 * Returns length of print buffer data string (including trailing NUL)
152 int tipc_printbuf_validate(struct print_buf
*pb
)
154 char *err
= "\n\n*** PRINT BUFFER OVERFLOW ***\n\n";
161 if (pb
->buf
[pb
->size
- 1] == 0) {
162 cp_buf
= kmalloc(pb
->size
, GFP_ATOMIC
);
164 tipc_printbuf_init(&cb
, cp_buf
, pb
->size
);
165 tipc_printbuf_move(&cb
, pb
);
166 tipc_printbuf_move(pb
, &cb
);
168 memcpy(pb
->buf
, err
, strlen(err
));
170 tipc_printbuf_reset(pb
);
171 tipc_printf(pb
, err
);
174 return pb
->crs
- pb
->buf
+ 1;
178 * tipc_printbuf_move - move print buffer contents to another print buffer
179 * @pb_to: pointer to destination print buffer structure
180 * @pb_from: pointer to source print buffer structure
182 * Current contents of destination print buffer (if any) are discarded.
183 * Source print buffer becomes empty if a successful move occurs.
186 static void tipc_printbuf_move(struct print_buf
*pb_to
,
187 struct print_buf
*pb_from
)
191 /* Handle the cases where contents can't be moved */
197 tipc_printbuf_reset(pb_to
);
201 if (pb_to
->size
< pb_from
->size
) {
202 strcpy(pb_to
->buf
, "*** PRINT BUFFER MOVE ERROR ***");
203 pb_to
->buf
[pb_to
->size
- 1] = ~0;
204 pb_to
->crs
= strchr(pb_to
->buf
, 0);
208 /* Copy data from char after cursor to end (if used) */
210 len
= pb_from
->buf
+ pb_from
->size
- pb_from
->crs
- 2;
211 if ((pb_from
->buf
[pb_from
->size
- 1] == 0) && (len
> 0)) {
212 strcpy(pb_to
->buf
, pb_from
->crs
+ 1);
213 pb_to
->crs
= pb_to
->buf
+ len
;
215 pb_to
->crs
= pb_to
->buf
;
217 /* Copy data from start to cursor (always) */
219 len
= pb_from
->crs
- pb_from
->buf
;
220 strcpy(pb_to
->crs
, pb_from
->buf
);
223 tipc_printbuf_reset(pb_from
);
227 * tipc_printf - append formatted output to print buffer
228 * @pb: pointer to print buffer
229 * @fmt: formatted info to be printed
232 void tipc_printf(struct print_buf
*pb
, const char *fmt
, ...)
238 spin_lock_bh(&print_lock
);
240 FORMAT(print_string
, chars_to_add
, fmt
);
241 if (chars_to_add
>= TIPC_PB_MAX_STR
)
242 strcpy(print_string
, "*** PRINT BUFFER STRING TOO LONG ***");
245 chars_left
= pb
->buf
+ pb
->size
- pb
->crs
- 1;
246 if (chars_to_add
<= chars_left
) {
247 strcpy(pb
->crs
, print_string
);
248 pb
->crs
+= chars_to_add
;
249 } else if (chars_to_add
>= (pb
->size
- 1)) {
250 strcpy(pb
->buf
, print_string
+ chars_to_add
+ 1
252 pb
->crs
= pb
->buf
+ pb
->size
- 1;
254 strcpy(pb
->buf
, print_string
+ chars_left
);
255 save_char
= print_string
[chars_left
];
256 print_string
[chars_left
] = 0;
257 strcpy(pb
->crs
, print_string
);
258 print_string
[chars_left
] = save_char
;
259 pb
->crs
= pb
->buf
+ chars_to_add
- chars_left
;
264 printk("%s", print_string
);
266 spin_unlock_bh(&print_lock
);
270 * tipc_log_resize - change the size of the TIPC log buffer
271 * @log_size: print buffer size to use
274 int tipc_log_resize(int log_size
)
278 spin_lock_bh(&print_lock
);
279 kfree(TIPC_LOG
->buf
);
280 TIPC_LOG
->buf
= NULL
;
282 if (log_size
< TIPC_PB_MIN_SIZE
)
283 log_size
= TIPC_PB_MIN_SIZE
;
284 res
= TIPC_LOG
->echo
;
285 tipc_printbuf_init(TIPC_LOG
, kmalloc(log_size
, GFP_ATOMIC
),
287 TIPC_LOG
->echo
= res
;
288 res
= !TIPC_LOG
->buf
;
290 spin_unlock_bh(&print_lock
);
296 * tipc_log_resize_cmd - reconfigure size of TIPC log buffer
299 struct sk_buff
*tipc_log_resize_cmd(const void *req_tlv_area
, int req_tlv_space
)
303 if (!TLV_CHECK(req_tlv_area
, req_tlv_space
, TIPC_TLV_UNSIGNED
))
304 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR
);
306 value
= ntohl(*(__be32
*)TLV_DATA(req_tlv_area
));
307 if (value
!= delimit(value
, 0, 32768))
308 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
309 " (log size must be 0-32768)");
310 if (tipc_log_resize(value
))
311 return tipc_cfg_reply_error_string(
312 "unable to create specified log (log size is now 0)");
313 return tipc_cfg_reply_none();
317 * tipc_log_dump - capture TIPC log buffer contents in configuration message
320 struct sk_buff
*tipc_log_dump(void)
322 struct sk_buff
*reply
;
324 spin_lock_bh(&print_lock
);
325 if (!TIPC_LOG
->buf
) {
326 spin_unlock_bh(&print_lock
);
327 reply
= tipc_cfg_reply_ultra_string("log not activated\n");
328 } else if (tipc_printbuf_empty(TIPC_LOG
)) {
329 spin_unlock_bh(&print_lock
);
330 reply
= tipc_cfg_reply_ultra_string("log is empty\n");
332 struct tlv_desc
*rep_tlv
;
336 str_len
= min(TIPC_LOG
->size
, 32768u);
337 spin_unlock_bh(&print_lock
);
338 reply
= tipc_cfg_reply_alloc(TLV_SPACE(str_len
));
340 rep_tlv
= (struct tlv_desc
*)reply
->data
;
341 tipc_printbuf_init(&pb
, TLV_DATA(rep_tlv
), str_len
);
342 spin_lock_bh(&print_lock
);
343 tipc_printbuf_move(&pb
, TIPC_LOG
);
344 spin_unlock_bh(&print_lock
);
345 str_len
= strlen(TLV_DATA(rep_tlv
)) + 1;
346 skb_put(reply
, TLV_SPACE(str_len
));
347 TLV_SET(rep_tlv
, TIPC_TLV_ULTRA_STRING
, NULL
, str_len
);