dbg.c 11 KB

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