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