dbg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * net/tipc/dbg.c: TIPC print buffer routines for debugging
  3. *
  4. * Copyright (c) 1996-2006, Ericsson AB
  5. * Copyright (c) 2005-2007, 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. strcpy(pb_to->buf, "*** PRINT BUFFER MOVE ERROR ***");
  158. pb_to->buf[pb_to->size - 1] = ~0;
  159. pb_to->crs = strchr(pb_to->buf, 0);
  160. pb_to->next = NULL;
  161. return;
  162. }
  163. /* Copy data from char after cursor to end (if used) */
  164. len = pb_from->buf + pb_from->size - pb_from->crs - 2;
  165. if ((pb_from->buf[pb_from->size-1] == 0) && (len > 0)) {
  166. strcpy(pb_to->buf, pb_from->crs + 1);
  167. pb_to->crs = pb_to->buf + len;
  168. } else
  169. pb_to->crs = pb_to->buf;
  170. /* Copy data from start to cursor (always) */
  171. len = pb_from->crs - pb_from->buf;
  172. strcpy(pb_to->crs, pb_from->buf);
  173. pb_to->crs += len;
  174. tipc_printbuf_reset(pb_from);
  175. }
  176. /**
  177. * tipc_printf - append formatted output to print buffer chain
  178. * @pb: pointer to chain of print buffers (may be NULL)
  179. * @fmt: formatted info to be printed
  180. */
  181. void tipc_printf(struct print_buf *pb, const char *fmt, ...)
  182. {
  183. int chars_to_add;
  184. int chars_left;
  185. char save_char;
  186. struct print_buf *pb_next;
  187. spin_lock_bh(&print_lock);
  188. FORMAT(print_string, chars_to_add, fmt);
  189. if (chars_to_add >= TIPC_PB_MAX_STR)
  190. strcpy(print_string, "*** PRINT BUFFER STRING TOO LONG ***");
  191. while (pb) {
  192. if (pb == TIPC_CONS)
  193. printk(print_string);
  194. else if (pb->buf) {
  195. chars_left = pb->buf + pb->size - pb->crs - 1;
  196. if (chars_to_add <= chars_left) {
  197. strcpy(pb->crs, print_string);
  198. pb->crs += chars_to_add;
  199. } else if (chars_to_add >= (pb->size - 1)) {
  200. strcpy(pb->buf, print_string + chars_to_add + 1
  201. - pb->size);
  202. pb->crs = pb->buf + pb->size - 1;
  203. } else {
  204. strcpy(pb->buf, print_string + chars_left);
  205. save_char = print_string[chars_left];
  206. print_string[chars_left] = 0;
  207. strcpy(pb->crs, print_string);
  208. print_string[chars_left] = save_char;
  209. pb->crs = pb->buf + chars_to_add - chars_left;
  210. }
  211. }
  212. pb_next = pb->next;
  213. pb->next = NULL;
  214. pb = pb_next;
  215. }
  216. spin_unlock_bh(&print_lock);
  217. }
  218. /**
  219. * TIPC_TEE - perform next output operation on both print buffers
  220. * @b0: pointer to chain of print buffers (may be NULL)
  221. * @b1: pointer to print buffer to add to chain
  222. *
  223. * Returns pointer to print buffer chain.
  224. */
  225. struct print_buf *TIPC_TEE(struct print_buf *b0, struct print_buf *b1)
  226. {
  227. struct print_buf *pb = b0;
  228. if (!b0 || (b0 == b1))
  229. return b1;
  230. spin_lock_bh(&print_lock);
  231. while (pb->next) {
  232. if ((pb->next == b1) || (pb->next == b0))
  233. pb->next = pb->next->next;
  234. else
  235. pb = pb->next;
  236. }
  237. pb->next = b1;
  238. spin_unlock_bh(&print_lock);
  239. return b0;
  240. }
  241. /**
  242. * print_to_console - write string of bytes to console in multiple chunks
  243. */
  244. static void print_to_console(char *crs, int len)
  245. {
  246. int rest = len;
  247. while (rest > 0) {
  248. int sz = rest < TIPC_PB_MAX_STR ? rest : TIPC_PB_MAX_STR;
  249. char c = crs[sz];
  250. crs[sz] = 0;
  251. printk((const char *)crs);
  252. crs[sz] = c;
  253. rest -= sz;
  254. crs += sz;
  255. }
  256. }
  257. /**
  258. * printbuf_dump - write print buffer contents to console
  259. */
  260. static void printbuf_dump(struct print_buf *pb)
  261. {
  262. int len;
  263. if (!pb->buf) {
  264. printk("*** PRINT BUFFER NOT ALLOCATED ***");
  265. return;
  266. }
  267. /* Dump print buffer from char after cursor to end (if used) */
  268. len = pb->buf + pb->size - pb->crs - 2;
  269. if ((pb->buf[pb->size - 1] == 0) && (len > 0))
  270. print_to_console(pb->crs + 1, len);
  271. /* Dump print buffer from start to cursor (always) */
  272. len = pb->crs - pb->buf;
  273. print_to_console(pb->buf, len);
  274. }
  275. /**
  276. * tipc_dump - dump non-console print buffer(s) to console
  277. * @pb: pointer to chain of print buffers
  278. */
  279. void tipc_dump(struct print_buf *pb, const char *fmt, ...)
  280. {
  281. struct print_buf *pb_next;
  282. int len;
  283. spin_lock_bh(&print_lock);
  284. FORMAT(print_string, len, fmt);
  285. printk(print_string);
  286. for (; pb; pb = pb->next) {
  287. if (pb != TIPC_CONS) {
  288. printk("\n---- Start of %s log dump ----\n\n",
  289. (pb == TIPC_LOG) ? "global" : "local");
  290. printbuf_dump(pb);
  291. tipc_printbuf_reset(pb);
  292. printk("\n---- End of dump ----\n");
  293. }
  294. pb_next = pb->next;
  295. pb->next = NULL;
  296. pb = pb_next;
  297. }
  298. spin_unlock_bh(&print_lock);
  299. }
  300. /**
  301. * tipc_log_resize - change the size of the TIPC log buffer
  302. * @log_size: print buffer size to use
  303. */
  304. int tipc_log_resize(int log_size)
  305. {
  306. int res = 0;
  307. spin_lock_bh(&print_lock);
  308. if (TIPC_LOG->buf) {
  309. kfree(TIPC_LOG->buf);
  310. TIPC_LOG->buf = NULL;
  311. }
  312. if (log_size) {
  313. if (log_size < TIPC_PB_MIN_SIZE)
  314. log_size = TIPC_PB_MIN_SIZE;
  315. tipc_printbuf_init(TIPC_LOG, kmalloc(log_size, GFP_ATOMIC),
  316. log_size);
  317. res = !TIPC_LOG->buf;
  318. }
  319. spin_unlock_bh(&print_lock);
  320. return res;
  321. }
  322. /**
  323. * tipc_log_resize_cmd - reconfigure size of TIPC log buffer
  324. */
  325. struct sk_buff *tipc_log_resize_cmd(const void *req_tlv_area, int req_tlv_space)
  326. {
  327. u32 value;
  328. if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
  329. return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
  330. value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
  331. if (value != delimit(value, 0, 32768))
  332. return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
  333. " (log size must be 0-32768)");
  334. if (tipc_log_resize(value))
  335. return tipc_cfg_reply_error_string(
  336. "unable to create specified log (log size is now 0)");
  337. return tipc_cfg_reply_none();
  338. }
  339. /**
  340. * tipc_log_dump - capture TIPC log buffer contents in configuration message
  341. */
  342. struct sk_buff *tipc_log_dump(void)
  343. {
  344. struct sk_buff *reply;
  345. spin_lock_bh(&print_lock);
  346. if (!TIPC_LOG->buf) {
  347. spin_unlock_bh(&print_lock);
  348. reply = tipc_cfg_reply_ultra_string("log not activated\n");
  349. } else if (tipc_printbuf_empty(TIPC_LOG)) {
  350. spin_unlock_bh(&print_lock);
  351. reply = tipc_cfg_reply_ultra_string("log is empty\n");
  352. }
  353. else {
  354. struct tlv_desc *rep_tlv;
  355. struct print_buf pb;
  356. int str_len;
  357. str_len = min(TIPC_LOG->size, 32768u);
  358. spin_unlock_bh(&print_lock);
  359. reply = tipc_cfg_reply_alloc(TLV_SPACE(str_len));
  360. if (reply) {
  361. rep_tlv = (struct tlv_desc *)reply->data;
  362. tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), str_len);
  363. spin_lock_bh(&print_lock);
  364. tipc_printbuf_move(&pb, TIPC_LOG);
  365. spin_unlock_bh(&print_lock);
  366. str_len = strlen(TLV_DATA(rep_tlv)) + 1;
  367. skb_put(reply, TLV_SPACE(str_len));
  368. TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
  369. }
  370. }
  371. return reply;
  372. }