xref: /linux-6.15/include/linux/debugfs.h (revision c098564d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  debugfs.h - a tiny little debug file system
4  *
5  *  Copyright (C) 2004 Greg Kroah-Hartman <[email protected]>
6  *  Copyright (C) 2004 IBM Inc.
7  *
8  *  debugfs is for people to use instead of /proc or /sys.
9  *  See Documentation/filesystems/ for more details.
10  */
11 
12 #ifndef _DEBUGFS_H_
13 #define _DEBUGFS_H_
14 
15 #include <linux/fs.h>
16 #include <linux/seq_file.h>
17 
18 #include <linux/types.h>
19 #include <linux/compiler.h>
20 
21 struct device;
22 struct file_operations;
23 
24 struct debugfs_blob_wrapper {
25 	void *data;
26 	unsigned long size;
27 };
28 
29 struct debugfs_reg32 {
30 	char *name;
31 	unsigned long offset;
32 };
33 
34 struct debugfs_regset32 {
35 	const struct debugfs_reg32 *regs;
36 	int nregs;
37 	void __iomem *base;
38 	struct device *dev;	/* Optional device for Runtime PM */
39 };
40 
41 struct debugfs_u32_array {
42 	u32 *array;
43 	u32 n_elements;
44 };
45 
46 extern struct dentry *arch_debugfs_dir;
47 
48 #define DEFINE_DEBUGFS_ATTRIBUTE(__fops, __get, __set, __fmt)		\
49 static int __fops ## _open(struct inode *inode, struct file *file)	\
50 {									\
51 	__simple_attr_check_format(__fmt, 0ull);			\
52 	return simple_attr_open(inode, file, __get, __set, __fmt);	\
53 }									\
54 static const struct file_operations __fops = {				\
55 	.owner	 = THIS_MODULE,						\
56 	.open	 = __fops ## _open,					\
57 	.release = simple_attr_release,					\
58 	.read	 = debugfs_attr_read,					\
59 	.write	 = debugfs_attr_write,					\
60 	.llseek  = no_llseek,						\
61 }
62 
63 typedef struct vfsmount *(*debugfs_automount_t)(struct dentry *, void *);
64 
65 #if defined(CONFIG_DEBUG_FS)
66 
67 struct dentry *debugfs_lookup(const char *name, struct dentry *parent);
68 
69 struct dentry *debugfs_create_file(const char *name, umode_t mode,
70 				   struct dentry *parent, void *data,
71 				   const struct file_operations *fops);
72 struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
73 				   struct dentry *parent, void *data,
74 				   const struct file_operations *fops);
75 
76 void debugfs_create_file_size(const char *name, umode_t mode,
77 			      struct dentry *parent, void *data,
78 			      const struct file_operations *fops,
79 			      loff_t file_size);
80 
81 struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
82 
83 struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
84 				      const char *dest);
85 
86 struct dentry *debugfs_create_automount(const char *name,
87 					struct dentry *parent,
88 					debugfs_automount_t f,
89 					void *data);
90 
91 void debugfs_remove(struct dentry *dentry);
92 #define debugfs_remove_recursive debugfs_remove
93 
94 const struct file_operations *debugfs_real_fops(const struct file *filp);
95 
96 int debugfs_file_get(struct dentry *dentry);
97 void debugfs_file_put(struct dentry *dentry);
98 
99 ssize_t debugfs_attr_read(struct file *file, char __user *buf,
100 			size_t len, loff_t *ppos);
101 ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
102 			size_t len, loff_t *ppos);
103 
104 struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
105                 struct dentry *new_dir, const char *new_name);
106 
107 void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
108 		       u8 *value);
109 void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
110 			u16 *value);
111 void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
112 			u32 *value);
113 void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
114 			u64 *value);
115 struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
116 				    struct dentry *parent, unsigned long *value);
117 void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
118 		       u8 *value);
119 void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
120 			u16 *value);
121 void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
122 			u32 *value);
123 void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
124 			u64 *value);
125 void debugfs_create_size_t(const char *name, umode_t mode,
126 			   struct dentry *parent, size_t *value);
127 void debugfs_create_atomic_t(const char *name, umode_t mode,
128 			     struct dentry *parent, atomic_t *value);
129 struct dentry *debugfs_create_bool(const char *name, umode_t mode,
130 				  struct dentry *parent, bool *value);
131 void debugfs_create_str(const char *name, umode_t mode,
132 			struct dentry *parent, char **value);
133 
134 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
135 				  struct dentry *parent,
136 				  struct debugfs_blob_wrapper *blob);
137 
138 void debugfs_create_regset32(const char *name, umode_t mode,
139 			     struct dentry *parent,
140 			     struct debugfs_regset32 *regset);
141 
142 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
143 			  int nregs, void __iomem *base, char *prefix);
144 
145 void debugfs_create_u32_array(const char *name, umode_t mode,
146 			      struct dentry *parent,
147 			      struct debugfs_u32_array *array);
148 
149 void debugfs_create_devm_seqfile(struct device *dev, const char *name,
150 				 struct dentry *parent,
151 				 int (*read_fn)(struct seq_file *s, void *data));
152 
153 bool debugfs_initialized(void);
154 
155 ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
156 			       size_t count, loff_t *ppos);
157 
158 ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
159 				size_t count, loff_t *ppos);
160 
161 ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf,
162 			      size_t count, loff_t *ppos);
163 
164 #else
165 
166 #include <linux/err.h>
167 
168 /*
169  * We do not return NULL from these functions if CONFIG_DEBUG_FS is not enabled
170  * so users have a chance to detect if there was a real error or not.  We don't
171  * want to duplicate the design decision mistakes of procfs and devfs again.
172  */
173 
174 static inline struct dentry *debugfs_lookup(const char *name,
175 					    struct dentry *parent)
176 {
177 	return ERR_PTR(-ENODEV);
178 }
179 
180 static inline struct dentry *debugfs_create_file(const char *name, umode_t mode,
181 					struct dentry *parent, void *data,
182 					const struct file_operations *fops)
183 {
184 	return ERR_PTR(-ENODEV);
185 }
186 
187 static inline struct dentry *debugfs_create_file_unsafe(const char *name,
188 					umode_t mode, struct dentry *parent,
189 					void *data,
190 					const struct file_operations *fops)
191 {
192 	return ERR_PTR(-ENODEV);
193 }
194 
195 static inline void debugfs_create_file_size(const char *name, umode_t mode,
196 					    struct dentry *parent, void *data,
197 					    const struct file_operations *fops,
198 					    loff_t file_size)
199 { }
200 
201 static inline struct dentry *debugfs_create_dir(const char *name,
202 						struct dentry *parent)
203 {
204 	return ERR_PTR(-ENODEV);
205 }
206 
207 static inline struct dentry *debugfs_create_symlink(const char *name,
208 						    struct dentry *parent,
209 						    const char *dest)
210 {
211 	return ERR_PTR(-ENODEV);
212 }
213 
214 static inline struct dentry *debugfs_create_automount(const char *name,
215 					struct dentry *parent,
216 					debugfs_automount_t f,
217 					void *data)
218 {
219 	return ERR_PTR(-ENODEV);
220 }
221 
222 static inline void debugfs_remove(struct dentry *dentry)
223 { }
224 
225 static inline void debugfs_remove_recursive(struct dentry *dentry)
226 { }
227 
228 const struct file_operations *debugfs_real_fops(const struct file *filp);
229 
230 static inline int debugfs_file_get(struct dentry *dentry)
231 {
232 	return 0;
233 }
234 
235 static inline void debugfs_file_put(struct dentry *dentry)
236 { }
237 
238 static inline ssize_t debugfs_attr_read(struct file *file, char __user *buf,
239 					size_t len, loff_t *ppos)
240 {
241 	return -ENODEV;
242 }
243 
244 static inline ssize_t debugfs_attr_write(struct file *file,
245 					const char __user *buf,
246 					size_t len, loff_t *ppos)
247 {
248 	return -ENODEV;
249 }
250 
251 static inline struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
252                 struct dentry *new_dir, char *new_name)
253 {
254 	return ERR_PTR(-ENODEV);
255 }
256 
257 static inline void debugfs_create_u8(const char *name, umode_t mode,
258 				     struct dentry *parent, u8 *value) { }
259 
260 static inline void debugfs_create_u16(const char *name, umode_t mode,
261 				      struct dentry *parent, u16 *value) { }
262 
263 static inline void debugfs_create_u32(const char *name, umode_t mode,
264 				      struct dentry *parent, u32 *value) { }
265 
266 static inline void debugfs_create_u64(const char *name, umode_t mode,
267 				      struct dentry *parent, u64 *value) { }
268 
269 static inline struct dentry *debugfs_create_ulong(const char *name,
270 						umode_t mode,
271 						struct dentry *parent,
272 						unsigned long *value)
273 {
274 	return ERR_PTR(-ENODEV);
275 }
276 
277 static inline void debugfs_create_x8(const char *name, umode_t mode,
278 				     struct dentry *parent, u8 *value) { }
279 
280 static inline void debugfs_create_x16(const char *name, umode_t mode,
281 				      struct dentry *parent, u16 *value) { }
282 
283 static inline void debugfs_create_x32(const char *name, umode_t mode,
284 				      struct dentry *parent, u32 *value) { }
285 
286 static inline void debugfs_create_x64(const char *name, umode_t mode,
287 				      struct dentry *parent, u64 *value) { }
288 
289 static inline void debugfs_create_size_t(const char *name, umode_t mode,
290 					 struct dentry *parent, size_t *value)
291 { }
292 
293 static inline void debugfs_create_atomic_t(const char *name, umode_t mode,
294 					   struct dentry *parent,
295 					   atomic_t *value)
296 { }
297 
298 static inline struct dentry *debugfs_create_bool(const char *name, umode_t mode,
299 						 struct dentry *parent,
300 						 bool *value)
301 {
302 	return ERR_PTR(-ENODEV);
303 }
304 
305 static inline void debugfs_create_str(const char *name, umode_t mode,
306 				      struct dentry *parent,
307 				      char **value)
308 { }
309 
310 static inline struct dentry *debugfs_create_blob(const char *name, umode_t mode,
311 				  struct dentry *parent,
312 				  struct debugfs_blob_wrapper *blob)
313 {
314 	return ERR_PTR(-ENODEV);
315 }
316 
317 static inline void debugfs_create_regset32(const char *name, umode_t mode,
318 					   struct dentry *parent,
319 					   struct debugfs_regset32 *regset)
320 {
321 }
322 
323 static inline void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
324 			 int nregs, void __iomem *base, char *prefix)
325 {
326 }
327 
328 static inline bool debugfs_initialized(void)
329 {
330 	return false;
331 }
332 
333 static inline void debugfs_create_u32_array(const char *name, umode_t mode,
334 					    struct dentry *parent,
335 					    struct debugfs_u32_array *array)
336 {
337 }
338 
339 static inline void debugfs_create_devm_seqfile(struct device *dev,
340 					       const char *name,
341 					       struct dentry *parent,
342 					       int (*read_fn)(struct seq_file *s,
343 							      void *data))
344 {
345 }
346 
347 static inline ssize_t debugfs_read_file_bool(struct file *file,
348 					     char __user *user_buf,
349 					     size_t count, loff_t *ppos)
350 {
351 	return -ENODEV;
352 }
353 
354 static inline ssize_t debugfs_write_file_bool(struct file *file,
355 					      const char __user *user_buf,
356 					      size_t count, loff_t *ppos)
357 {
358 	return -ENODEV;
359 }
360 
361 static inline ssize_t debugfs_read_file_str(struct file *file,
362 					    char __user *user_buf,
363 					    size_t count, loff_t *ppos)
364 {
365 	return -ENODEV;
366 }
367 
368 #endif
369 
370 /**
371  * debugfs_create_xul - create a debugfs file that is used to read and write an
372  * unsigned long value, formatted in hexadecimal
373  * @name: a pointer to a string containing the name of the file to create.
374  * @mode: the permission that the file should have
375  * @parent: a pointer to the parent dentry for this file.  This should be a
376  *          directory dentry if set.  If this parameter is %NULL, then the
377  *          file will be created in the root of the debugfs filesystem.
378  * @value: a pointer to the variable that the file should read to and write
379  *         from.
380  */
381 static inline void debugfs_create_xul(const char *name, umode_t mode,
382 				      struct dentry *parent,
383 				      unsigned long *value)
384 {
385 	if (sizeof(*value) == sizeof(u32))
386 		debugfs_create_x32(name, mode, parent, (u32 *)value);
387 	else
388 		debugfs_create_x64(name, mode, parent, (u64 *)value);
389 }
390 
391 #endif
392