trace_seq.c 9.9 KB

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