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