trace_seq.c 11 KB

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