]>
Commit | Line | Data |
---|---|---|
b97bf3fd | 1 | /* |
29ede244 | 2 | * net/tipc/dbg.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" | |
39 | #include "dbg.h" | |
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 | 51 | static struct print_buf null_buf = { NULL, 0, NULL, 0 }; |
7d3aa712 | 52 | struct print_buf *const TIPC_NULL = &null_buf; |
29ede244 | 53 | |
c8903985 | 54 | static struct print_buf cons_buf = { NULL, 0, NULL, 1 }; |
7d3aa712 | 55 | struct print_buf *const TIPC_CONS = &cons_buf; |
b97bf3fd | 56 | |
c8903985 | 57 | static struct print_buf log_buf = { NULL, 0, NULL, 1 }; |
7d3aa712 | 58 | struct 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 | * | |
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 | ||
76 | static char print_string[TIPC_PB_MAX_STR]; | |
77 | static DEFINE_SPINLOCK(print_lock); | |
78 | ||
b97bf3fd PL |
79 | |
80 | #define FORMAT(PTR,LEN,FMT) \ | |
81 | {\ | |
82 | va_list args;\ | |
83 | va_start(args, FMT);\ | |
84 | LEN = vsprintf(PTR, FMT, args);\ | |
85 | va_end(args);\ | |
86 | *(PTR + LEN) = '\0';\ | |
87 | } | |
88 | ||
b97bf3fd | 89 | /** |
4323add6 | 90 | * tipc_printbuf_init - initialize print buffer to empty |
29ede244 AS |
91 | * @pb: pointer to print buffer structure |
92 | * @raw: pointer to character array used by print buffer | |
93 | * @size: size of character array | |
94 | * | |
c8903985 AS |
95 | * Note: If the character array is too small (or absent), the print buffer |
96 | * becomes a null device that discards anything written to it. | |
b97bf3fd PL |
97 | */ |
98 | ||
29ede244 | 99 | void tipc_printbuf_init(struct print_buf *pb, char *raw, u32 size) |
b97bf3fd | 100 | { |
29ede244 AS |
101 | pb->buf = raw; |
102 | pb->crs = raw; | |
103 | pb->size = size; | |
c8903985 | 104 | pb->echo = 0; |
29ede244 AS |
105 | |
106 | if (size < TIPC_PB_MIN_SIZE) { | |
107 | pb->buf = NULL; | |
108 | } else if (raw) { | |
109 | pb->buf[0] = 0; | |
7d3aa712 | 110 | pb->buf[size - 1] = ~0; |
29ede244 | 111 | } |
b97bf3fd PL |
112 | } |
113 | ||
114 | /** | |
4323add6 | 115 | * tipc_printbuf_reset - reinitialize print buffer to empty state |
29ede244 | 116 | * @pb: pointer to print buffer structure |
b97bf3fd PL |
117 | */ |
118 | ||
4323add6 | 119 | void tipc_printbuf_reset(struct print_buf *pb) |
b97bf3fd | 120 | { |
7d3aa712 | 121 | if (pb->buf) { |
c8903985 AS |
122 | pb->crs = pb->buf; |
123 | pb->buf[0] = 0; | |
124 | pb->buf[pb->size - 1] = ~0; | |
125 | } | |
b97bf3fd PL |
126 | } |
127 | ||
128 | /** | |
4323add6 | 129 | * tipc_printbuf_empty - test if print buffer is in empty state |
29ede244 AS |
130 | * @pb: pointer to print buffer structure |
131 | * | |
132 | * Returns non-zero if print buffer is empty. | |
b97bf3fd PL |
133 | */ |
134 | ||
4323add6 | 135 | int tipc_printbuf_empty(struct print_buf *pb) |
b97bf3fd | 136 | { |
29ede244 | 137 | return (!pb->buf || (pb->crs == pb->buf)); |
b97bf3fd PL |
138 | } |
139 | ||
140 | /** | |
4323add6 | 141 | * tipc_printbuf_validate - check for print buffer overflow |
29ede244 | 142 | * @pb: pointer to print buffer structure |
c4307285 YH |
143 | * |
144 | * Verifies that a print buffer has captured all data written to it. | |
b97bf3fd | 145 | * If data has been lost, linearize buffer and prepend an error message |
c4307285 | 146 | * |
29ede244 | 147 | * Returns length of print buffer data string (including trailing NUL) |
b97bf3fd PL |
148 | */ |
149 | ||
4323add6 | 150 | int tipc_printbuf_validate(struct print_buf *pb) |
b97bf3fd | 151 | { |
c4307285 YH |
152 | char *err = "\n\n*** PRINT BUFFER OVERFLOW ***\n\n"; |
153 | char *cp_buf; | |
154 | struct print_buf cb; | |
b97bf3fd | 155 | |
29ede244 | 156 | if (!pb->buf) |
b97bf3fd PL |
157 | return 0; |
158 | ||
29ede244 | 159 | if (pb->buf[pb->size - 1] == 0) { |
c4307285 | 160 | cp_buf = kmalloc(pb->size, GFP_ATOMIC); |
7d3aa712 | 161 | if (cp_buf) { |
c4307285 YH |
162 | tipc_printbuf_init(&cb, cp_buf, pb->size); |
163 | tipc_printbuf_move(&cb, pb); | |
164 | tipc_printbuf_move(pb, &cb); | |
165 | kfree(cp_buf); | |
166 | memcpy(pb->buf, err, strlen(err)); | |
167 | } else { | |
168 | tipc_printbuf_reset(pb); | |
169 | tipc_printf(pb, err); | |
170 | } | |
b97bf3fd PL |
171 | } |
172 | return (pb->crs - pb->buf + 1); | |
173 | } | |
174 | ||
175 | /** | |
4323add6 | 176 | * tipc_printbuf_move - move print buffer contents to another print buffer |
29ede244 AS |
177 | * @pb_to: pointer to destination print buffer structure |
178 | * @pb_from: pointer to source print buffer structure | |
c4307285 | 179 | * |
b97bf3fd PL |
180 | * Current contents of destination print buffer (if any) are discarded. |
181 | * Source print buffer becomes empty if a successful move occurs. | |
182 | */ | |
183 | ||
4323add6 | 184 | void tipc_printbuf_move(struct print_buf *pb_to, struct print_buf *pb_from) |
b97bf3fd PL |
185 | { |
186 | int len; | |
187 | ||
188 | /* Handle the cases where contents can't be moved */ | |
189 | ||
29ede244 | 190 | if (!pb_to->buf) |
b97bf3fd PL |
191 | return; |
192 | ||
29ede244 | 193 | if (!pb_from->buf) { |
4323add6 | 194 | tipc_printbuf_reset(pb_to); |
b97bf3fd PL |
195 | return; |
196 | } | |
197 | ||
198 | if (pb_to->size < pb_from->size) { | |
93758c3d AS |
199 | strcpy(pb_to->buf, "*** PRINT BUFFER MOVE ERROR ***"); |
200 | pb_to->buf[pb_to->size - 1] = ~0; | |
201 | pb_to->crs = strchr(pb_to->buf, 0); | |
b97bf3fd PL |
202 | return; |
203 | } | |
204 | ||
205 | /* Copy data from char after cursor to end (if used) */ | |
29ede244 | 206 | |
b97bf3fd | 207 | len = pb_from->buf + pb_from->size - pb_from->crs - 2; |
7d3aa712 | 208 | if ((pb_from->buf[pb_from->size - 1] == 0) && (len > 0)) { |
b97bf3fd PL |
209 | strcpy(pb_to->buf, pb_from->crs + 1); |
210 | pb_to->crs = pb_to->buf + len; | |
211 | } else | |
212 | pb_to->crs = pb_to->buf; | |
213 | ||
214 | /* Copy data from start to cursor (always) */ | |
29ede244 | 215 | |
b97bf3fd PL |
216 | len = pb_from->crs - pb_from->buf; |
217 | strcpy(pb_to->crs, pb_from->buf); | |
218 | pb_to->crs += len; | |
219 | ||
4323add6 | 220 | tipc_printbuf_reset(pb_from); |
b97bf3fd PL |
221 | } |
222 | ||
223 | /** | |
c8903985 AS |
224 | * tipc_printf - append formatted output to print buffer |
225 | * @pb: pointer to print buffer | |
29ede244 | 226 | * @fmt: formatted info to be printed |
b97bf3fd PL |
227 | */ |
228 | ||
229 | void tipc_printf(struct print_buf *pb, const char *fmt, ...) | |
230 | { | |
231 | int chars_to_add; | |
232 | int chars_left; | |
233 | char save_char; | |
b97bf3fd PL |
234 | |
235 | spin_lock_bh(&print_lock); | |
c8903985 | 236 | |
b97bf3fd | 237 | FORMAT(print_string, chars_to_add, fmt); |
29ede244 AS |
238 | if (chars_to_add >= TIPC_PB_MAX_STR) |
239 | strcpy(print_string, "*** PRINT BUFFER STRING TOO LONG ***"); | |
b97bf3fd | 240 | |
c8903985 AS |
241 | if (pb->buf) { |
242 | chars_left = pb->buf + pb->size - pb->crs - 1; | |
243 | if (chars_to_add <= chars_left) { | |
244 | strcpy(pb->crs, print_string); | |
245 | pb->crs += chars_to_add; | |
246 | } else if (chars_to_add >= (pb->size - 1)) { | |
247 | strcpy(pb->buf, print_string + chars_to_add + 1 | |
248 | - pb->size); | |
249 | pb->crs = pb->buf + pb->size - 1; | |
250 | } else { | |
251 | strcpy(pb->buf, print_string + chars_left); | |
252 | save_char = print_string[chars_left]; | |
253 | print_string[chars_left] = 0; | |
254 | strcpy(pb->crs, print_string); | |
255 | print_string[chars_left] = save_char; | |
256 | pb->crs = pb->buf + chars_to_add - chars_left; | |
c4307285 | 257 | } |
b97bf3fd | 258 | } |
b97bf3fd | 259 | |
c8903985 | 260 | if (pb->echo) |
4b704d59 | 261 | printk("%s", print_string); |
b97bf3fd | 262 | |
b97bf3fd | 263 | spin_unlock_bh(&print_lock); |
b97bf3fd PL |
264 | } |
265 | ||
48c97139 AS |
266 | #ifdef CONFIG_TIPC_DEBUG |
267 | ||
b97bf3fd PL |
268 | /** |
269 | * print_to_console - write string of bytes to console in multiple chunks | |
270 | */ | |
271 | ||
272 | static void print_to_console(char *crs, int len) | |
273 | { | |
274 | int rest = len; | |
275 | ||
276 | while (rest > 0) { | |
29ede244 | 277 | int sz = rest < TIPC_PB_MAX_STR ? rest : TIPC_PB_MAX_STR; |
b97bf3fd PL |
278 | char c = crs[sz]; |
279 | ||
280 | crs[sz] = 0; | |
281 | printk((const char *)crs); | |
282 | crs[sz] = c; | |
283 | rest -= sz; | |
284 | crs += sz; | |
285 | } | |
286 | } | |
287 | ||
288 | /** | |
289 | * printbuf_dump - write print buffer contents to console | |
290 | */ | |
291 | ||
292 | static void printbuf_dump(struct print_buf *pb) | |
293 | { | |
294 | int len; | |
295 | ||
29ede244 AS |
296 | if (!pb->buf) { |
297 | printk("*** PRINT BUFFER NOT ALLOCATED ***"); | |
298 | return; | |
299 | } | |
300 | ||
b97bf3fd | 301 | /* Dump print buffer from char after cursor to end (if used) */ |
29ede244 | 302 | |
b97bf3fd PL |
303 | len = pb->buf + pb->size - pb->crs - 2; |
304 | if ((pb->buf[pb->size - 1] == 0) && (len > 0)) | |
305 | print_to_console(pb->crs + 1, len); | |
306 | ||
307 | /* Dump print buffer from start to cursor (always) */ | |
29ede244 | 308 | |
b97bf3fd PL |
309 | len = pb->crs - pb->buf; |
310 | print_to_console(pb->buf, len); | |
311 | } | |
312 | ||
313 | /** | |
48c97139 | 314 | * tipc_dump_dbg - dump (non-console) print buffer to console |
c8903985 | 315 | * @pb: pointer to print buffer |
b97bf3fd PL |
316 | */ |
317 | ||
48c97139 | 318 | void tipc_dump_dbg(struct print_buf *pb, const char *fmt, ...) |
b97bf3fd PL |
319 | { |
320 | int len; | |
321 | ||
c8903985 AS |
322 | if (pb == TIPC_CONS) |
323 | return; | |
324 | ||
b97bf3fd | 325 | spin_lock_bh(&print_lock); |
c8903985 | 326 | |
29ede244 AS |
327 | FORMAT(print_string, len, fmt); |
328 | printk(print_string); | |
b97bf3fd | 329 | |
c8903985 AS |
330 | printk("\n---- Start of %s log dump ----\n\n", |
331 | (pb == TIPC_LOG) ? "global" : "local"); | |
332 | printbuf_dump(pb); | |
333 | tipc_printbuf_reset(pb); | |
334 | printk("\n---- End of dump ----\n"); | |
335 | ||
b97bf3fd PL |
336 | spin_unlock_bh(&print_lock); |
337 | } | |
338 | ||
48c97139 AS |
339 | #endif |
340 | ||
b97bf3fd | 341 | /** |
025adbe8 AS |
342 | * tipc_log_resize - change the size of the TIPC log buffer |
343 | * @log_size: print buffer size to use | |
b97bf3fd PL |
344 | */ |
345 | ||
fb98ec71 | 346 | int tipc_log_resize(int log_size) |
b97bf3fd | 347 | { |
fb98ec71 AS |
348 | int res = 0; |
349 | ||
b97bf3fd | 350 | spin_lock_bh(&print_lock); |
4323add6 PL |
351 | if (TIPC_LOG->buf) { |
352 | kfree(TIPC_LOG->buf); | |
353 | TIPC_LOG->buf = NULL; | |
b97bf3fd | 354 | } |
b97bf3fd | 355 | if (log_size) { |
29ede244 AS |
356 | if (log_size < TIPC_PB_MIN_SIZE) |
357 | log_size = TIPC_PB_MIN_SIZE; | |
c8903985 | 358 | res = TIPC_LOG->echo; |
29ede244 AS |
359 | tipc_printbuf_init(TIPC_LOG, kmalloc(log_size, GFP_ATOMIC), |
360 | log_size); | |
c8903985 | 361 | TIPC_LOG->echo = res; |
fb98ec71 | 362 | res = !TIPC_LOG->buf; |
b97bf3fd | 363 | } |
025adbe8 | 364 | spin_unlock_bh(&print_lock); |
fb98ec71 AS |
365 | |
366 | return res; | |
b97bf3fd PL |
367 | } |
368 | ||
369 | /** | |
025adbe8 | 370 | * tipc_log_resize_cmd - reconfigure size of TIPC log buffer |
b97bf3fd PL |
371 | */ |
372 | ||
025adbe8 | 373 | struct sk_buff *tipc_log_resize_cmd(const void *req_tlv_area, int req_tlv_space) |
b97bf3fd PL |
374 | { |
375 | u32 value; | |
376 | ||
377 | if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED)) | |
4323add6 | 378 | return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR); |
b97bf3fd | 379 | |
3e6c8cd5 | 380 | value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area)); |
b97bf3fd | 381 | if (value != delimit(value, 0, 32768)) |
4323add6 PL |
382 | return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE |
383 | " (log size must be 0-32768)"); | |
fb98ec71 AS |
384 | if (tipc_log_resize(value)) |
385 | return tipc_cfg_reply_error_string( | |
386 | "unable to create specified log (log size is now 0)"); | |
4323add6 | 387 | return tipc_cfg_reply_none(); |
b97bf3fd PL |
388 | } |
389 | ||
390 | /** | |
4323add6 | 391 | * tipc_log_dump - capture TIPC log buffer contents in configuration message |
b97bf3fd PL |
392 | */ |
393 | ||
4323add6 | 394 | struct sk_buff *tipc_log_dump(void) |
b97bf3fd PL |
395 | { |
396 | struct sk_buff *reply; | |
397 | ||
398 | spin_lock_bh(&print_lock); | |
93758c3d AS |
399 | if (!TIPC_LOG->buf) { |
400 | spin_unlock_bh(&print_lock); | |
4323add6 | 401 | reply = tipc_cfg_reply_ultra_string("log not activated\n"); |
93758c3d AS |
402 | } else if (tipc_printbuf_empty(TIPC_LOG)) { |
403 | spin_unlock_bh(&print_lock); | |
4323add6 | 404 | reply = tipc_cfg_reply_ultra_string("log is empty\n"); |
93758c3d | 405 | } |
b97bf3fd PL |
406 | else { |
407 | struct tlv_desc *rep_tlv; | |
408 | struct print_buf pb; | |
409 | int str_len; | |
410 | ||
4323add6 | 411 | str_len = min(TIPC_LOG->size, 32768u); |
93758c3d | 412 | spin_unlock_bh(&print_lock); |
4323add6 | 413 | reply = tipc_cfg_reply_alloc(TLV_SPACE(str_len)); |
b97bf3fd PL |
414 | if (reply) { |
415 | rep_tlv = (struct tlv_desc *)reply->data; | |
4323add6 | 416 | tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), str_len); |
93758c3d | 417 | spin_lock_bh(&print_lock); |
4323add6 | 418 | tipc_printbuf_move(&pb, TIPC_LOG); |
93758c3d | 419 | spin_unlock_bh(&print_lock); |
b97bf3fd PL |
420 | str_len = strlen(TLV_DATA(rep_tlv)) + 1; |
421 | skb_put(reply, TLV_SPACE(str_len)); | |
422 | TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len); | |
423 | } | |
424 | } | |
b97bf3fd PL |
425 | return reply; |
426 | } | |
427 |