dbg.c 11 KB

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