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