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