trace_seq.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * trace_seq.c
  3. *
  4. * Copyright (C) 2008-2014 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
  5. *
  6. * The trace_seq is a handy tool that allows you to pass a descriptor around
  7. * to a buffer that other functions can write to. It is similar to the
  8. * seq_file functionality but has some differences.
  9. *
  10. * To use it, the trace_seq must be initialized with trace_seq_init().
  11. * This will set up the counters within the descriptor. You can call
  12. * trace_seq_init() more than once to reset the trace_seq to start
  13. * from scratch.
  14. *
  15. * The buffer size is currently PAGE_SIZE, although it may become dynamic
  16. * in the future.
  17. *
  18. * A write to the buffer will either succed or fail. That is, unlike
  19. * sprintf() there will not be a partial write (well it may write into
  20. * the buffer but it wont update the pointers). This allows users to
  21. * try to write something into the trace_seq buffer and if it fails
  22. * they can flush it and try again.
  23. *
  24. */
  25. #include <linux/uaccess.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/trace_seq.h>
  28. /* How much buffer is left on the trace_seq? */
  29. #define TRACE_SEQ_BUF_LEFT(s) ((PAGE_SIZE - 1) - (s)->len)
  30. /* How much buffer is written? */
  31. #define TRACE_SEQ_BUF_USED(s) min((s)->len, (unsigned int)(PAGE_SIZE - 1))
  32. /**
  33. * trace_print_seq - move the contents of trace_seq into a seq_file
  34. * @m: the seq_file descriptor that is the destination
  35. * @s: the trace_seq descriptor that is the source.
  36. *
  37. * Returns 0 on success and non zero on error. If it succeeds to
  38. * write to the seq_file it will reset the trace_seq, otherwise
  39. * it does not modify the trace_seq to let the caller try again.
  40. */
  41. int trace_print_seq(struct seq_file *m, struct trace_seq *s)
  42. {
  43. unsigned int len = TRACE_SEQ_BUF_USED(s);
  44. int ret;
  45. ret = seq_write(m, s->buffer, len);
  46. /*
  47. * Only reset this buffer if we successfully wrote to the
  48. * seq_file buffer. This lets the caller try again or
  49. * do something else with the contents.
  50. */
  51. if (!ret)
  52. trace_seq_init(s);
  53. return ret;
  54. }
  55. /**
  56. * trace_seq_printf - sequence printing of trace information
  57. * @s: trace sequence descriptor
  58. * @fmt: printf format string
  59. *
  60. * The tracer may use either sequence operations or its own
  61. * copy to user routines. To simplify formating of a trace
  62. * trace_seq_printf() is used to store strings into a special
  63. * buffer (@s). Then the output may be either used by
  64. * the sequencer or pulled into another buffer.
  65. */
  66. void trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
  67. {
  68. unsigned int len = TRACE_SEQ_BUF_LEFT(s);
  69. va_list ap;
  70. int ret;
  71. if (s->full || !len)
  72. return;
  73. va_start(ap, fmt);
  74. ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
  75. va_end(ap);
  76. /* If we can't write it all, don't bother writing anything */
  77. if (ret >= len) {
  78. s->full = 1;
  79. return;
  80. }
  81. s->len += ret;
  82. }
  83. EXPORT_SYMBOL_GPL(trace_seq_printf);
  84. /**
  85. * trace_seq_bitmask - write a bitmask array in its ASCII representation
  86. * @s: trace sequence descriptor
  87. * @maskp: points to an array of unsigned longs that represent a bitmask
  88. * @nmaskbits: The number of bits that are valid in @maskp
  89. *
  90. * Writes a ASCII representation of a bitmask string into @s.
  91. */
  92. void trace_seq_bitmask(struct trace_seq *s, const unsigned long *maskp,
  93. int nmaskbits)
  94. {
  95. unsigned int len = TRACE_SEQ_BUF_LEFT(s);
  96. int ret;
  97. if (s->full || !len)
  98. return;
  99. ret = bitmap_scnprintf(s->buffer + s->len, len, maskp, nmaskbits);
  100. s->len += ret;
  101. }
  102. EXPORT_SYMBOL_GPL(trace_seq_bitmask);
  103. /**
  104. * trace_seq_vprintf - sequence printing of trace information
  105. * @s: trace sequence descriptor
  106. * @fmt: printf format string
  107. *
  108. * The tracer may use either sequence operations or its own
  109. * copy to user routines. To simplify formating of a trace
  110. * trace_seq_printf is used to store strings into a special
  111. * buffer (@s). Then the output may be either used by
  112. * the sequencer or pulled into another buffer.
  113. */
  114. void trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args)
  115. {
  116. unsigned int len = TRACE_SEQ_BUF_LEFT(s);
  117. int ret;
  118. if (s->full || !len)
  119. return;
  120. ret = vsnprintf(s->buffer + s->len, len, fmt, args);
  121. /* If we can't write it all, don't bother writing anything */
  122. if (ret >= len) {
  123. s->full = 1;
  124. return;
  125. }
  126. s->len += ret;
  127. }
  128. EXPORT_SYMBOL_GPL(trace_seq_vprintf);
  129. /**
  130. * trace_seq_bprintf - Write the printf string from binary arguments
  131. * @s: trace sequence descriptor
  132. * @fmt: The format string for the @binary arguments
  133. * @binary: The binary arguments for @fmt.
  134. *
  135. * When recording in a fast path, a printf may be recorded with just
  136. * saving the format and the arguments as they were passed to the
  137. * function, instead of wasting cycles converting the arguments into
  138. * ASCII characters. Instead, the arguments are saved in a 32 bit
  139. * word array that is defined by the format string constraints.
  140. *
  141. * This function will take the format and the binary array and finish
  142. * the conversion into the ASCII string within the buffer.
  143. */
  144. void trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary)
  145. {
  146. unsigned int len = TRACE_SEQ_BUF_LEFT(s);
  147. int ret;
  148. if (s->full || !len)
  149. return;
  150. ret = bstr_printf(s->buffer + s->len, len, fmt, binary);
  151. /* If we can't write it all, don't bother writing anything */
  152. if (ret >= len) {
  153. s->full = 1;
  154. return;
  155. }
  156. s->len += ret;
  157. }
  158. EXPORT_SYMBOL_GPL(trace_seq_bprintf);
  159. /**
  160. * trace_seq_puts - trace sequence printing of simple string
  161. * @s: trace sequence descriptor
  162. * @str: simple string to record
  163. *
  164. * The tracer may use either the sequence operations or its own
  165. * copy to user routines. This function records a simple string
  166. * into a special buffer (@s) for later retrieval by a sequencer
  167. * or other mechanism.
  168. */
  169. void trace_seq_puts(struct trace_seq *s, const char *str)
  170. {
  171. unsigned int len = strlen(str);
  172. if (s->full)
  173. return;
  174. if (len > TRACE_SEQ_BUF_LEFT(s)) {
  175. s->full = 1;
  176. return;
  177. }
  178. memcpy(s->buffer + s->len, str, len);
  179. s->len += len;
  180. }
  181. EXPORT_SYMBOL_GPL(trace_seq_puts);
  182. /**
  183. * trace_seq_putc - trace sequence printing of simple character
  184. * @s: trace sequence descriptor
  185. * @c: simple character to record
  186. *
  187. * The tracer may use either the sequence operations or its own
  188. * copy to user routines. This function records a simple charater
  189. * into a special buffer (@s) for later retrieval by a sequencer
  190. * or other mechanism.
  191. */
  192. void trace_seq_putc(struct trace_seq *s, unsigned char c)
  193. {
  194. if (s->full)
  195. return;
  196. if (TRACE_SEQ_BUF_LEFT(s) < 1) {
  197. s->full = 1;
  198. return;
  199. }
  200. s->buffer[s->len++] = c;
  201. }
  202. EXPORT_SYMBOL_GPL(trace_seq_putc);
  203. /**
  204. * trace_seq_putmem - write raw data into the trace_seq buffer
  205. * @s: trace sequence descriptor
  206. * @mem: The raw memory to copy into the buffer
  207. * @len: The length of the raw memory to copy (in bytes)
  208. *
  209. * There may be cases where raw memory needs to be written into the
  210. * buffer and a strcpy() would not work. Using this function allows
  211. * for such cases.
  212. */
  213. void trace_seq_putmem(struct trace_seq *s, const void *mem, unsigned int len)
  214. {
  215. if (s->full)
  216. return;
  217. if (len > TRACE_SEQ_BUF_LEFT(s)) {
  218. s->full = 1;
  219. return;
  220. }
  221. memcpy(s->buffer + s->len, mem, len);
  222. s->len += len;
  223. }
  224. EXPORT_SYMBOL_GPL(trace_seq_putmem);
  225. #define MAX_MEMHEX_BYTES 8U
  226. #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
  227. /**
  228. * trace_seq_putmem_hex - write raw memory into the buffer in ASCII hex
  229. * @s: trace sequence descriptor
  230. * @mem: The raw memory to write its hex ASCII representation of
  231. * @len: The length of the raw memory to copy (in bytes)
  232. *
  233. * This is similar to trace_seq_putmem() except instead of just copying the
  234. * raw memory into the buffer it writes its ASCII representation of it
  235. * in hex characters.
  236. */
  237. void trace_seq_putmem_hex(struct trace_seq *s, const void *mem,
  238. unsigned int len)
  239. {
  240. unsigned char hex[HEX_CHARS];
  241. const unsigned char *data = mem;
  242. unsigned int start_len;
  243. int i, j;
  244. if (s->full)
  245. return;
  246. while (len) {
  247. start_len = min(len, HEX_CHARS - 1);
  248. #ifdef __BIG_ENDIAN
  249. for (i = 0, j = 0; i < start_len; i++) {
  250. #else
  251. for (i = start_len-1, j = 0; i >= 0; i--) {
  252. #endif
  253. hex[j++] = hex_asc_hi(data[i]);
  254. hex[j++] = hex_asc_lo(data[i]);
  255. }
  256. if (WARN_ON_ONCE(j == 0 || j/2 > len))
  257. break;
  258. /* j increments twice per loop */
  259. len -= j / 2;
  260. hex[j++] = ' ';
  261. trace_seq_putmem(s, hex, j);
  262. }
  263. }
  264. EXPORT_SYMBOL_GPL(trace_seq_putmem_hex);
  265. /**
  266. * trace_seq_path - copy a path into the sequence buffer
  267. * @s: trace sequence descriptor
  268. * @path: path to write into the sequence buffer.
  269. *
  270. * Write a path name into the sequence buffer.
  271. *
  272. * Returns 1 if we successfully written all the contents to
  273. * the buffer.
  274. * Returns 0 if we the length to write is bigger than the
  275. * reserved buffer space. In this case, nothing gets written.
  276. */
  277. int trace_seq_path(struct trace_seq *s, const struct path *path)
  278. {
  279. unsigned char *p;
  280. if (s->full)
  281. return 0;
  282. if (TRACE_SEQ_BUF_LEFT(s) < 1) {
  283. s->full = 1;
  284. return 0;
  285. }
  286. p = d_path(path, s->buffer + s->len, PAGE_SIZE - s->len);
  287. if (!IS_ERR(p)) {
  288. p = mangle_path(s->buffer + s->len, p, "\n");
  289. if (p) {
  290. s->len = p - s->buffer;
  291. return 1;
  292. }
  293. } else {
  294. s->buffer[s->len++] = '?';
  295. return 1;
  296. }
  297. s->full = 1;
  298. return 0;
  299. }
  300. EXPORT_SYMBOL_GPL(trace_seq_path);
  301. /**
  302. * trace_seq_to_user - copy the squence buffer to user space
  303. * @s: trace sequence descriptor
  304. * @ubuf: The userspace memory location to copy to
  305. * @cnt: The amount to copy
  306. *
  307. * Copies the sequence buffer into the userspace memory pointed to
  308. * by @ubuf. It starts from the last read position (@s->readpos)
  309. * and writes up to @cnt characters or till it reaches the end of
  310. * the content in the buffer (@s->len), which ever comes first.
  311. *
  312. * On success, it returns a positive number of the number of bytes
  313. * it copied.
  314. *
  315. * On failure it returns -EBUSY if all of the content in the
  316. * sequence has been already read, which includes nothing in the
  317. * sequenc (@s->len == @s->readpos).
  318. *
  319. * Returns -EFAULT if the copy to userspace fails.
  320. */
  321. int trace_seq_to_user(struct trace_seq *s, char __user *ubuf, int cnt)
  322. {
  323. int len;
  324. int ret;
  325. if (!cnt)
  326. return 0;
  327. if (s->len <= s->readpos)
  328. return -EBUSY;
  329. len = s->len - s->readpos;
  330. if (cnt > len)
  331. cnt = len;
  332. ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
  333. if (ret == cnt)
  334. return -EFAULT;
  335. cnt -= ret;
  336. s->readpos += cnt;
  337. return cnt;
  338. }
  339. EXPORT_SYMBOL_GPL(trace_seq_to_user);