dbg.c 10 KB

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