trace_seq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. * Returns 1 if we successfully written all the contents to
  67. * the buffer.
  68. * Returns 0 if we the length to write is bigger than the
  69. * reserved buffer space. In this case, nothing gets written.
  70. */
  71. int trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
  72. {
  73. unsigned int len = TRACE_SEQ_BUF_LEFT(s);
  74. va_list ap;
  75. int ret;
  76. if (s->full || !len)
  77. return 0;
  78. va_start(ap, fmt);
  79. ret = vsnprintf(s->buffer + s->len, len, fmt, ap);
  80. va_end(ap);
  81. /* If we can't write it all, don't bother writing anything */
  82. if (ret >= len) {
  83. s->full = 1;
  84. return 0;
  85. }
  86. s->len += ret;
  87. return 1;
  88. }
  89. EXPORT_SYMBOL_GPL(trace_seq_printf);
  90. /**
  91. * trace_seq_bitmask - write a bitmask array in its ASCII representation
  92. * @s: trace sequence descriptor
  93. * @maskp: points to an array of unsigned longs that represent a bitmask
  94. * @nmaskbits: The number of bits that are valid in @maskp
  95. *
  96. * Writes a ASCII representation of a bitmask string into @s.
  97. *
  98. * Returns 1 if we successfully written all the contents to
  99. * the buffer.
  100. * Returns 0 if we the length to write is bigger than the
  101. * reserved buffer space. In this case, nothing gets written.
  102. */
  103. int trace_seq_bitmask(struct trace_seq *s, const unsigned long *maskp,
  104. int nmaskbits)
  105. {
  106. unsigned int len = TRACE_SEQ_BUF_LEFT(s);
  107. int ret;
  108. if (s->full || !len)
  109. return 0;
  110. ret = bitmap_scnprintf(s->buffer, len, maskp, nmaskbits);
  111. s->len += ret;
  112. return 1;
  113. }
  114. EXPORT_SYMBOL_GPL(trace_seq_bitmask);
  115. /**
  116. * trace_seq_vprintf - sequence printing of trace information
  117. * @s: trace sequence descriptor
  118. * @fmt: printf format string
  119. *
  120. * The tracer may use either sequence operations or its own
  121. * copy to user routines. To simplify formating of a trace
  122. * trace_seq_printf is used to store strings into a special
  123. * buffer (@s). Then the output may be either used by
  124. * the sequencer or pulled into another buffer.
  125. *
  126. * Returns how much it wrote to the buffer.
  127. */
  128. int trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args)
  129. {
  130. unsigned int len = TRACE_SEQ_BUF_LEFT(s);
  131. int ret;
  132. if (s->full || !len)
  133. return 0;
  134. ret = vsnprintf(s->buffer + s->len, len, fmt, args);
  135. /* If we can't write it all, don't bother writing anything */
  136. if (ret >= len) {
  137. s->full = 1;
  138. return 0;
  139. }
  140. s->len += ret;
  141. return len;
  142. }
  143. EXPORT_SYMBOL_GPL(trace_seq_vprintf);
  144. /**
  145. * trace_seq_bprintf - Write the printf string from binary arguments
  146. * @s: trace sequence descriptor
  147. * @fmt: The format string for the @binary arguments
  148. * @binary: The binary arguments for @fmt.
  149. *
  150. * When recording in a fast path, a printf may be recorded with just
  151. * saving the format and the arguments as they were passed to the
  152. * function, instead of wasting cycles converting the arguments into
  153. * ASCII characters. Instead, the arguments are saved in a 32 bit
  154. * word array that is defined by the format string constraints.
  155. *
  156. * This function will take the format and the binary array and finish
  157. * the conversion into the ASCII string within the buffer.
  158. *
  159. * Returns how much it wrote to the buffer.
  160. */
  161. int trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary)
  162. {
  163. unsigned int len = TRACE_SEQ_BUF_LEFT(s);
  164. int ret;
  165. if (s->full || !len)
  166. return 0;
  167. ret = bstr_printf(s->buffer + s->len, len, fmt, binary);
  168. /* If we can't write it all, don't bother writing anything */
  169. if (ret >= len) {
  170. s->full = 1;
  171. return 0;
  172. }
  173. s->len += ret;
  174. return len;
  175. }
  176. EXPORT_SYMBOL_GPL(trace_seq_bprintf);
  177. /**
  178. * trace_seq_puts - trace sequence printing of simple string
  179. * @s: trace sequence descriptor
  180. * @str: simple string to record
  181. *
  182. * The tracer may use either the sequence operations or its own
  183. * copy to user routines. This function records a simple string
  184. * into a special buffer (@s) for later retrieval by a sequencer
  185. * or other mechanism.
  186. *
  187. * Returns how much it wrote to the buffer.
  188. */
  189. int trace_seq_puts(struct trace_seq *s, const char *str)
  190. {
  191. unsigned int len = strlen(str);
  192. if (s->full)
  193. return 0;
  194. if (len > TRACE_SEQ_BUF_LEFT(s)) {
  195. s->full = 1;
  196. return 0;
  197. }
  198. memcpy(s->buffer + s->len, str, len);
  199. s->len += len;
  200. return len;
  201. }
  202. EXPORT_SYMBOL_GPL(trace_seq_puts);
  203. /**
  204. * trace_seq_putc - trace sequence printing of simple character
  205. * @s: trace sequence descriptor
  206. * @c: simple character to record
  207. *
  208. * The tracer may use either the sequence operations or its own
  209. * copy to user routines. This function records a simple charater
  210. * into a special buffer (@s) for later retrieval by a sequencer
  211. * or other mechanism.
  212. *
  213. * Returns how much it wrote to the buffer.
  214. */
  215. int trace_seq_putc(struct trace_seq *s, unsigned char c)
  216. {
  217. if (s->full)
  218. return 0;
  219. if (TRACE_SEQ_BUF_LEFT(s) < 1) {
  220. s->full = 1;
  221. return 0;
  222. }
  223. s->buffer[s->len++] = c;
  224. return 1;
  225. }
  226. EXPORT_SYMBOL_GPL(trace_seq_putc);
  227. /**
  228. * trace_seq_putmem - write raw data into the trace_seq buffer
  229. * @s: trace sequence descriptor
  230. * @mem: The raw memory to copy into the buffer
  231. * @len: The length of the raw memory to copy (in bytes)
  232. *
  233. * There may be cases where raw memory needs to be written into the
  234. * buffer and a strcpy() would not work. Using this function allows
  235. * for such cases.
  236. *
  237. * Returns how much it wrote to the buffer.
  238. */
  239. int trace_seq_putmem(struct trace_seq *s, const void *mem, unsigned int len)
  240. {
  241. if (s->full)
  242. return 0;
  243. if (len > TRACE_SEQ_BUF_LEFT(s)) {
  244. s->full = 1;
  245. return 0;
  246. }
  247. memcpy(s->buffer + s->len, mem, len);
  248. s->len += len;
  249. return len;
  250. }
  251. EXPORT_SYMBOL_GPL(trace_seq_putmem);
  252. #define MAX_MEMHEX_BYTES 8U
  253. #define HEX_CHARS (MAX_MEMHEX_BYTES*2 + 1)
  254. /**
  255. * trace_seq_putmem_hex - write raw memory into the buffer in ASCII hex
  256. * @s: trace sequence descriptor
  257. * @mem: The raw memory to write its hex ASCII representation of
  258. * @len: The length of the raw memory to copy (in bytes)
  259. *
  260. * This is similar to trace_seq_putmem() except instead of just copying the
  261. * raw memory into the buffer it writes its ASCII representation of it
  262. * in hex characters.
  263. *
  264. * Returns how much it wrote to the buffer.
  265. */
  266. int trace_seq_putmem_hex(struct trace_seq *s, const void *mem,
  267. unsigned int len)
  268. {
  269. unsigned char hex[HEX_CHARS];
  270. const unsigned char *data = mem;
  271. unsigned int start_len;
  272. int i, j;
  273. int cnt = 0;
  274. if (s->full)
  275. return 0;
  276. while (len) {
  277. start_len = min(len, HEX_CHARS - 1);
  278. #ifdef __BIG_ENDIAN
  279. for (i = 0, j = 0; i < start_len; i++) {
  280. #else
  281. for (i = start_len-1, j = 0; i >= 0; i--) {
  282. #endif
  283. hex[j++] = hex_asc_hi(data[i]);
  284. hex[j++] = hex_asc_lo(data[i]);
  285. }
  286. if (WARN_ON_ONCE(j == 0 || j/2 > len))
  287. break;
  288. /* j increments twice per loop */
  289. len -= j / 2;
  290. hex[j++] = ' ';
  291. cnt += trace_seq_putmem(s, hex, j);
  292. }
  293. return cnt;
  294. }
  295. EXPORT_SYMBOL_GPL(trace_seq_putmem_hex);
  296. /**
  297. * trace_seq_path - copy a path into the sequence buffer
  298. * @s: trace sequence descriptor
  299. * @path: path to write into the sequence buffer.
  300. *
  301. * Write a path name into the sequence buffer.
  302. *
  303. * Returns 1 if we successfully written all the contents to
  304. * the buffer.
  305. * Returns 0 if we the length to write is bigger than the
  306. * reserved buffer space. In this case, nothing gets written.
  307. */
  308. int trace_seq_path(struct trace_seq *s, const struct path *path)
  309. {
  310. unsigned char *p;
  311. if (s->full)
  312. return 0;
  313. if (TRACE_SEQ_BUF_LEFT(s) < 1) {
  314. s->full = 1;
  315. return 0;
  316. }
  317. p = d_path(path, s->buffer + s->len, PAGE_SIZE - s->len);
  318. if (!IS_ERR(p)) {
  319. p = mangle_path(s->buffer + s->len, p, "\n");
  320. if (p) {
  321. s->len = p - s->buffer;
  322. return 1;
  323. }
  324. } else {
  325. s->buffer[s->len++] = '?';
  326. return 1;
  327. }
  328. s->full = 1;
  329. return 0;
  330. }
  331. EXPORT_SYMBOL_GPL(trace_seq_path);
  332. /**
  333. * trace_seq_to_user - copy the squence buffer to user space
  334. * @s: trace sequence descriptor
  335. * @ubuf: The userspace memory location to copy to
  336. * @cnt: The amount to copy
  337. *
  338. * Copies the sequence buffer into the userspace memory pointed to
  339. * by @ubuf. It starts from the last read position (@s->readpos)
  340. * and writes up to @cnt characters or till it reaches the end of
  341. * the content in the buffer (@s->len), which ever comes first.
  342. *
  343. * On success, it returns a positive number of the number of bytes
  344. * it copied.
  345. *
  346. * On failure it returns -EBUSY if all of the content in the
  347. * sequence has been already read, which includes nothing in the
  348. * sequenc (@s->len == @s->readpos).
  349. *
  350. * Returns -EFAULT if the copy to userspace fails.
  351. */
  352. int trace_seq_to_user(struct trace_seq *s, char __user *ubuf, int cnt)
  353. {
  354. int len;
  355. int ret;
  356. if (!cnt)
  357. return 0;
  358. if (s->len <= s->readpos)
  359. return -EBUSY;
  360. len = s->len - s->readpos;
  361. if (cnt > len)
  362. cnt = len;
  363. ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
  364. if (ret == cnt)
  365. return -EFAULT;
  366. cnt -= ret;
  367. s->readpos += cnt;
  368. return cnt;
  369. }
  370. EXPORT_SYMBOL_GPL(trace_seq_to_user);