trace_seq.c 10 KB

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