dbg.c 11 KB

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