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