trace_seq.h 3.7 KB

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