trace_seq.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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) seq_buf_buffer_left(&(s)->seq)
  30. /* How much buffer is written? */
  31. #define TRACE_SEQ_BUF_USED(s) seq_buf_used(&(s)->seq)
  32. /*
  33. * trace_seq should work with being initialized with 0s.
  34. */
  35. static inline void __trace_seq_init(struct trace_seq *s)
  36. {
  37. if (unlikely(!s->seq.size))
  38. trace_seq_init(s);
  39. }
  40. /**
  41. * trace_print_seq - move the contents of trace_seq into a seq_file
  42. * @m: the seq_file descriptor that is the destination
  43. * @s: the trace_seq descriptor that is the source.
  44. *
  45. * Returns 0 on success and non zero on error. If it succeeds to
  46. * write to the seq_file it will reset the trace_seq, otherwise
  47. * it does not modify the trace_seq to let the caller try again.
  48. */
  49. int trace_print_seq(struct seq_file *m, struct trace_seq *s)
  50. {
  51. int ret;
  52. __trace_seq_init(s);
  53. ret = seq_buf_print_seq(m, &s->seq);
  54. /*
  55. * Only reset this buffer if we successfully wrote to the
  56. * seq_file buffer. This lets the caller try again or
  57. * do something else with the contents.
  58. */
  59. if (!ret)
  60. trace_seq_init(s);
  61. return ret;
  62. }
  63. /**
  64. * trace_seq_printf - sequence printing of trace information
  65. * @s: trace sequence descriptor
  66. * @fmt: printf format string
  67. *
  68. * The tracer may use either sequence operations or its own
  69. * copy to user routines. To simplify formating of a trace
  70. * trace_seq_printf() is used to store strings into a special
  71. * buffer (@s). Then the output may be either used by
  72. * the sequencer or pulled into another buffer.
  73. */
  74. void trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
  75. {
  76. unsigned int save_len = s->seq.len;
  77. va_list ap;
  78. if (s->full)
  79. return;
  80. __trace_seq_init(s);
  81. va_start(ap, fmt);
  82. seq_buf_vprintf(&s->seq, fmt, ap);
  83. va_end(ap);
  84. /* If we can't write it all, don't bother writing anything */
  85. if (unlikely(seq_buf_has_overflowed(&s->seq))) {
  86. s->seq.len = save_len;
  87. s->full = 1;
  88. }
  89. }
  90. EXPORT_SYMBOL_GPL(trace_seq_printf);
  91. /**
  92. * trace_seq_bitmask - write a bitmask array in its ASCII representation
  93. * @s: trace sequence descriptor
  94. * @maskp: points to an array of unsigned longs that represent a bitmask
  95. * @nmaskbits: The number of bits that are valid in @maskp
  96. *
  97. * Writes a ASCII representation of a bitmask string into @s.
  98. */
  99. void trace_seq_bitmask(struct trace_seq *s, const unsigned long *maskp,
  100. int nmaskbits)
  101. {
  102. unsigned int save_len = s->seq.len;
  103. if (s->full)
  104. return;
  105. __trace_seq_init(s);
  106. seq_buf_printf(&s->seq, "%*pb", nmaskbits, maskp);
  107. if (unlikely(seq_buf_has_overflowed(&s->seq))) {
  108. s->seq.len = save_len;
  109. s->full = 1;
  110. }
  111. }
  112. EXPORT_SYMBOL_GPL(trace_seq_bitmask);
  113. /**
  114. * trace_seq_vprintf - sequence printing of trace information
  115. * @s: trace sequence descriptor
  116. * @fmt: printf format string
  117. *
  118. * The tracer may use either sequence operations or its own
  119. * copy to user routines. To simplify formating of a trace
  120. * trace_seq_printf is used to store strings into a special
  121. * buffer (@s). Then the output may be either used by
  122. * the sequencer or pulled into another buffer.
  123. */
  124. void trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args)
  125. {
  126. unsigned int save_len = s->seq.len;
  127. if (s->full)
  128. return;
  129. __trace_seq_init(s);
  130. seq_buf_vprintf(&s->seq, fmt, args);
  131. /* If we can't write it all, don't bother writing anything */
  132. if (unlikely(seq_buf_has_overflowed(&s->seq))) {
  133. s->seq.len = save_len;
  134. s->full = 1;
  135. }
  136. }
  137. EXPORT_SYMBOL_GPL(trace_seq_vprintf);
  138. /**
  139. * trace_seq_bprintf - Write the printf string from binary arguments
  140. * @s: trace sequence descriptor
  141. * @fmt: The format string for the @binary arguments
  142. * @binary: The binary arguments for @fmt.
  143. *
  144. * When recording in a fast path, a printf may be recorded with just
  145. * saving the format and the arguments as they were passed to the
  146. * function, instead of wasting cycles converting the arguments into
  147. * ASCII characters. Instead, the arguments are saved in a 32 bit
  148. * word array that is defined by the format string constraints.
  149. *
  150. * This function will take the format and the binary array and finish
  151. * the conversion into the ASCII string within the buffer.
  152. */
  153. void trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary)
  154. {
  155. unsigned int save_len = s->seq.len;
  156. if (s->full)
  157. return;
  158. __trace_seq_init(s);
  159. seq_buf_bprintf(&s->seq, fmt, binary);
  160. /* If we can't write it all, don't bother writing anything */
  161. if (unlikely(seq_buf_has_overflowed(&s->seq))) {
  162. s->seq.len = save_len;
  163. s->full = 1;
  164. return;
  165. }
  166. }
  167. EXPORT_SYMBOL_GPL(trace_seq_bprintf);
  168. /**
  169. * trace_seq_puts - trace sequence printing of simple string
  170. * @s: trace sequence descriptor
  171. * @str: simple string to record
  172. *
  173. * The tracer may use either the sequence operations or its own
  174. * copy to user routines. This function records a simple string
  175. * into a special buffer (@s) for later retrieval by a sequencer
  176. * or other mechanism.
  177. */
  178. void trace_seq_puts(struct trace_seq *s, const char *str)
  179. {
  180. unsigned int len = strlen(str);
  181. if (s->full)
  182. return;
  183. __trace_seq_init(s);
  184. if (len > TRACE_SEQ_BUF_LEFT(s)) {
  185. s->full = 1;
  186. return;
  187. }
  188. seq_buf_putmem(&s->seq, str, len);
  189. }
  190. EXPORT_SYMBOL_GPL(trace_seq_puts);
  191. /**
  192. * trace_seq_putc - trace sequence printing of simple character
  193. * @s: trace sequence descriptor
  194. * @c: simple character to record
  195. *
  196. * The tracer may use either the sequence operations or its own
  197. * copy to user routines. This function records a simple charater
  198. * into a special buffer (@s) for later retrieval by a sequencer
  199. * or other mechanism.
  200. */
  201. void trace_seq_putc(struct trace_seq *s, unsigned char c)
  202. {
  203. if (s->full)
  204. return;
  205. __trace_seq_init(s);
  206. if (TRACE_SEQ_BUF_LEFT(s) < 1) {
  207. s->full = 1;
  208. return;
  209. }
  210. seq_buf_putc(&s->seq, c);
  211. }
  212. EXPORT_SYMBOL_GPL(trace_seq_putc);
  213. /**
  214. * trace_seq_putmem - write raw data into the trace_seq buffer
  215. * @s: trace sequence descriptor
  216. * @mem: The raw memory to copy into the buffer
  217. * @len: The length of the raw memory to copy (in bytes)
  218. *
  219. * There may be cases where raw memory needs to be written into the
  220. * buffer and a strcpy() would not work. Using this function allows
  221. * for such cases.
  222. */
  223. void trace_seq_putmem(struct trace_seq *s, const void *mem, unsigned int len)
  224. {
  225. if (s->full)
  226. return;
  227. __trace_seq_init(s);
  228. if (len > TRACE_SEQ_BUF_LEFT(s)) {
  229. s->full = 1;
  230. return;
  231. }
  232. seq_buf_putmem(&s->seq, mem, len);
  233. }
  234. EXPORT_SYMBOL_GPL(trace_seq_putmem);
  235. /**
  236. * trace_seq_putmem_hex - write raw memory into the buffer in ASCII hex
  237. * @s: trace sequence descriptor
  238. * @mem: The raw memory to write its hex ASCII representation of
  239. * @len: The length of the raw memory to copy (in bytes)
  240. *
  241. * This is similar to trace_seq_putmem() except instead of just copying the
  242. * raw memory into the buffer it writes its ASCII representation of it
  243. * in hex characters.
  244. */
  245. void trace_seq_putmem_hex(struct trace_seq *s, const void *mem,
  246. unsigned int len)
  247. {
  248. unsigned int save_len = s->seq.len;
  249. if (s->full)
  250. return;
  251. __trace_seq_init(s);
  252. /* Each byte is represented by two chars */
  253. if (len * 2 > TRACE_SEQ_BUF_LEFT(s)) {
  254. s->full = 1;
  255. return;
  256. }
  257. /* The added spaces can still cause an overflow */
  258. seq_buf_putmem_hex(&s->seq, mem, len);
  259. if (unlikely(seq_buf_has_overflowed(&s->seq))) {
  260. s->seq.len = save_len;
  261. s->full = 1;
  262. return;
  263. }
  264. }
  265. EXPORT_SYMBOL_GPL(trace_seq_putmem_hex);
  266. /**
  267. * trace_seq_path - copy a path into the sequence buffer
  268. * @s: trace sequence descriptor
  269. * @path: path to write into the sequence buffer.
  270. *
  271. * Write a path name into the sequence buffer.
  272. *
  273. * Returns 1 if we successfully written all the contents to
  274. * the buffer.
  275. * Returns 0 if we the length to write is bigger than the
  276. * reserved buffer space. In this case, nothing gets written.
  277. */
  278. int trace_seq_path(struct trace_seq *s, const struct path *path)
  279. {
  280. unsigned int save_len = s->seq.len;
  281. if (s->full)
  282. return 0;
  283. __trace_seq_init(s);
  284. if (TRACE_SEQ_BUF_LEFT(s) < 1) {
  285. s->full = 1;
  286. return 0;
  287. }
  288. seq_buf_path(&s->seq, path, "\n");
  289. if (unlikely(seq_buf_has_overflowed(&s->seq))) {
  290. s->seq.len = save_len;
  291. s->full = 1;
  292. return 0;
  293. }
  294. return 1;
  295. }
  296. EXPORT_SYMBOL_GPL(trace_seq_path);
  297. /**
  298. * trace_seq_to_user - copy the squence buffer to user space
  299. * @s: trace sequence descriptor
  300. * @ubuf: The userspace memory location to copy to
  301. * @cnt: The amount to copy
  302. *
  303. * Copies the sequence buffer into the userspace memory pointed to
  304. * by @ubuf. It starts from the last read position (@s->readpos)
  305. * and writes up to @cnt characters or till it reaches the end of
  306. * the content in the buffer (@s->len), which ever comes first.
  307. *
  308. * On success, it returns a positive number of the number of bytes
  309. * it copied.
  310. *
  311. * On failure it returns -EBUSY if all of the content in the
  312. * sequence has been already read, which includes nothing in the
  313. * sequenc (@s->len == @s->readpos).
  314. *
  315. * Returns -EFAULT if the copy to userspace fails.
  316. */
  317. int trace_seq_to_user(struct trace_seq *s, char __user *ubuf, int cnt)
  318. {
  319. __trace_seq_init(s);
  320. return seq_buf_to_user(&s->seq, ubuf, cnt);
  321. }
  322. EXPORT_SYMBOL_GPL(trace_seq_to_user);