trace_seq.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_TRACE_SEQ_H
  3. #define _LINUX_TRACE_SEQ_H
  4. #include <linux/seq_buf.h>
  5. #include <asm/page.h>
  6. /*
  7. * Trace sequences are used to allow a function to call several other functions
  8. * to create a string of data to use (up to a max of PAGE_SIZE).
  9. */
  10. struct trace_seq {
  11. char buffer[PAGE_SIZE];
  12. struct seq_buf seq;
  13. int full;
  14. };
  15. static inline void
  16. trace_seq_init(struct trace_seq *s)
  17. {
  18. seq_buf_init(&s->seq, s->buffer, PAGE_SIZE);
  19. s->full = 0;
  20. }
  21. /**
  22. * trace_seq_used - amount of actual data written to buffer
  23. * @s: trace sequence descriptor
  24. *
  25. * Returns the amount of data written to the buffer.
  26. *
  27. * IMPORTANT!
  28. *
  29. * Use this instead of @s->seq.len if you need to pass the amount
  30. * of data from the buffer to another buffer (userspace, or what not).
  31. * The @s->seq.len on overflow is bigger than the buffer size and
  32. * using it can cause access to undefined memory.
  33. */
  34. static inline int trace_seq_used(struct trace_seq *s)
  35. {
  36. return seq_buf_used(&s->seq);
  37. }
  38. /**
  39. * trace_seq_buffer_ptr - return pointer to next location in buffer
  40. * @s: trace sequence descriptor
  41. *
  42. * Returns the pointer to the buffer where the next write to
  43. * the buffer will happen. This is useful to save the location
  44. * that is about to be written to and then return the result
  45. * of that write.
  46. */
  47. static inline char *
  48. trace_seq_buffer_ptr(struct trace_seq *s)
  49. {
  50. return s->buffer + seq_buf_used(&s->seq);
  51. }
  52. /**
  53. * trace_seq_has_overflowed - return true if the trace_seq took too much
  54. * @s: trace sequence descriptor
  55. *
  56. * Returns true if too much data was added to the trace_seq and it is
  57. * now full and will not take anymore.
  58. */
  59. static inline bool trace_seq_has_overflowed(struct trace_seq *s)
  60. {
  61. return s->full || seq_buf_has_overflowed(&s->seq);
  62. }
  63. /*
  64. * Currently only defined when tracing is enabled.
  65. */
  66. #ifdef CONFIG_TRACING
  67. extern __printf(2, 3)
  68. void trace_seq_printf(struct trace_seq *s, const char *fmt, ...);
  69. extern __printf(2, 0)
  70. void trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args);
  71. extern void
  72. trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary);
  73. extern int trace_print_seq(struct seq_file *m, struct trace_seq *s);
  74. extern int trace_seq_to_user(struct trace_seq *s, char __user *ubuf,
  75. int cnt);
  76. extern void trace_seq_puts(struct trace_seq *s, const char *str);
  77. extern void trace_seq_putc(struct trace_seq *s, unsigned char c);
  78. extern void trace_seq_putmem(struct trace_seq *s, const void *mem, unsigned int len);
  79. extern void trace_seq_putmem_hex(struct trace_seq *s, const void *mem,
  80. unsigned int len);
  81. extern int trace_seq_path(struct trace_seq *s, const struct path *path);
  82. extern void trace_seq_bitmask(struct trace_seq *s, const unsigned long *maskp,
  83. int nmaskbits);
  84. extern int trace_seq_hex_dump(struct trace_seq *s, const char *prefix_str,
  85. int prefix_type, int rowsize, int groupsize,
  86. const void *buf, size_t len, bool ascii);
  87. #else /* CONFIG_TRACING */
  88. static inline void trace_seq_printf(struct trace_seq *s, const char *fmt, ...)
  89. {
  90. }
  91. static inline void
  92. trace_seq_bprintf(struct trace_seq *s, const char *fmt, const u32 *binary)
  93. {
  94. }
  95. static inline void
  96. trace_seq_bitmask(struct trace_seq *s, const unsigned long *maskp,
  97. int nmaskbits)
  98. {
  99. }
  100. static inline int trace_print_seq(struct seq_file *m, struct trace_seq *s)
  101. {
  102. return 0;
  103. }
  104. static inline int trace_seq_to_user(struct trace_seq *s, char __user *ubuf,
  105. int cnt)
  106. {
  107. return 0;
  108. }
  109. static inline void trace_seq_puts(struct trace_seq *s, const char *str)
  110. {
  111. }
  112. static inline void trace_seq_putc(struct trace_seq *s, unsigned char c)
  113. {
  114. }
  115. static inline void
  116. trace_seq_putmem(struct trace_seq *s, const void *mem, unsigned int len)
  117. {
  118. }
  119. static inline void trace_seq_putmem_hex(struct trace_seq *s, const void *mem,
  120. unsigned int len)
  121. {
  122. }
  123. static inline int trace_seq_path(struct trace_seq *s, const struct path *path)
  124. {
  125. return 0;
  126. }
  127. #endif /* CONFIG_TRACING */
  128. #endif /* _LINUX_TRACE_SEQ_H */