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