log.c 9.7 KB

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