xref: /linux/include/linux/debugfs.h (revision 22c5696e3fe029f4fc2decbe7cc6663b5d281223)
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 <greg@kroah.com>
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_XSIGNED(__fops, __get, __set, __fmt, __is_signed)	\
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	 = (__is_signed) ? debugfs_attr_write_signed : debugfs_attr_write,	\
60 }
61 
62 #define DEFINE_DEBUGFS_ATTRIBUTE(__fops, __get, __set, __fmt)		\
63 	DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, false)
64 
65 #define DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(__fops, __get, __set, __fmt)	\
66 	DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, true)
67 
68 typedef struct vfsmount *(*debugfs_automount_t)(struct dentry *, void *);
69 
70 struct debugfs_short_fops {
71 	ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
72 	ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
73 	loff_t (*llseek) (struct file *, loff_t, int);
74 };
75 
76 #if defined(CONFIG_DEBUG_FS)
77 
78 struct dentry *debugfs_lookup(const char *name, struct dentry *parent);
79 
80 struct dentry *debugfs_create_file_full(const char *name, umode_t mode,
81 					struct dentry *parent, void *data,
82 					const void *aux,
83 					const struct file_operations *fops);
84 struct dentry *debugfs_create_file_short(const char *name, umode_t mode,
85 					 struct dentry *parent, void *data,
86 					 const void *aux,
87 					 const struct debugfs_short_fops *fops);
88 
89 /**
90  * debugfs_create_file - create a file in the debugfs filesystem
91  * @name: a pointer to a string containing the name of the file to create.
92  * @mode: the permission that the file should have.
93  * @parent: a pointer to the parent dentry for this file.  This should be a
94  *          directory dentry if set.  If this parameter is NULL, then the
95  *          file will be created in the root of the debugfs filesystem.
96  * @data: a pointer to something that the caller will want to get to later
97  *        on.  The inode.i_private pointer will point to this value on
98  *        the open() call.
99  * @fops: a pointer to a struct file_operations or struct debugfs_short_fops that
100  *        should be used for this file.
101  *
102  * This is the basic "create a file" function for debugfs.  It allows for a
103  * wide range of flexibility in creating a file, or a directory (if you want
104  * to create a directory, the debugfs_create_dir() function is
105  * recommended to be used instead.)
106  *
107  * This function will return a pointer to a dentry if it succeeds.  This
108  * pointer must be passed to the debugfs_remove() function when the file is
109  * to be removed (no automatic cleanup happens if your module is unloaded,
110  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
111  * returned.
112  *
113  * If debugfs is not enabled in the kernel, the value -%ENODEV will be
114  * returned.
115  *
116  * If fops points to a struct debugfs_short_fops, then simple_open() will be
117  * used for the open, and only read/write/llseek are supported and are proxied,
118  * so no module reference or release are needed.
119  *
120  * NOTE: it's expected that most callers should _ignore_ the errors returned
121  * by this function. Other debugfs functions handle the fact that the "dentry"
122  * passed to them could be an error and they don't crash in that case.
123  * Drivers should generally work fine even if debugfs fails to init anyway.
124  */
125 #define debugfs_create_file(name, mode, parent, data, fops)			\
126 	_Generic(fops,								\
127 		 const struct file_operations *: debugfs_create_file_full,	\
128 		 const struct debugfs_short_fops *: debugfs_create_file_short,	\
129 		 struct file_operations *: debugfs_create_file_full,		\
130 		 struct debugfs_short_fops *: debugfs_create_file_short)	\
131 		(name, mode, parent, data, NULL, fops)
132 
133 #define debugfs_create_file_aux(name, mode, parent, data, aux, fops)		\
134 	_Generic(fops,								\
135 		 const struct file_operations *: debugfs_create_file_full,	\
136 		 const struct debugfs_short_fops *: debugfs_create_file_short,	\
137 		 struct file_operations *: debugfs_create_file_full,		\
138 		 struct debugfs_short_fops *: debugfs_create_file_short)	\
139 		(name, mode, parent, data, aux, fops)
140 
141 struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
142 				   struct dentry *parent, void *data,
143 				   const struct file_operations *fops);
144 
145 void debugfs_create_file_size(const char *name, umode_t mode,
146 			      struct dentry *parent, void *data,
147 			      const struct file_operations *fops,
148 			      loff_t file_size);
149 
150 struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
151 
152 struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
153 				      const char *dest);
154 
155 struct dentry *debugfs_create_automount(const char *name,
156 					struct dentry *parent,
157 					debugfs_automount_t f,
158 					void *data);
159 
160 void debugfs_remove(struct dentry *dentry);
161 #define debugfs_remove_recursive debugfs_remove
162 
163 void debugfs_lookup_and_remove(const char *name, struct dentry *parent);
164 
165 void *debugfs_get_aux(const struct file *file);
166 
167 int debugfs_file_get(struct dentry *dentry);
168 void debugfs_file_put(struct dentry *dentry);
169 
170 ssize_t debugfs_attr_read(struct file *file, char __user *buf,
171 			size_t len, loff_t *ppos);
172 ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
173 			size_t len, loff_t *ppos);
174 ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf,
175 			size_t len, loff_t *ppos);
176 
177 int debugfs_change_name(struct dentry *dentry, const char *fmt, ...) __printf(2, 3);
178 
179 void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
180 		       u8 *value);
181 void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
182 			u16 *value);
183 void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
184 			u32 *value);
185 void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
186 			u64 *value);
187 void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent,
188 			  unsigned long *value);
189 void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
190 		       u8 *value);
191 void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
192 			u16 *value);
193 void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
194 			u32 *value);
195 void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
196 			u64 *value);
197 void debugfs_create_size_t(const char *name, umode_t mode,
198 			   struct dentry *parent, size_t *value);
199 void debugfs_create_atomic_t(const char *name, umode_t mode,
200 			     struct dentry *parent, atomic_t *value);
201 void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent,
202 			 bool *value);
203 void debugfs_create_str(const char *name, umode_t mode,
204 			struct dentry *parent, char **value);
205 
206 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
207 				  struct dentry *parent,
208 				  struct debugfs_blob_wrapper *blob);
209 
210 void debugfs_create_regset32(const char *name, umode_t mode,
211 			     struct dentry *parent,
212 			     struct debugfs_regset32 *regset);
213 
214 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
215 			  int nregs, void __iomem *base, char *prefix);
216 
217 void debugfs_create_u32_array(const char *name, umode_t mode,
218 			      struct dentry *parent,
219 			      struct debugfs_u32_array *array);
220 
221 void debugfs_create_devm_seqfile(struct device *dev, const char *name,
222 				 struct dentry *parent,
223 				 int (*read_fn)(struct seq_file *s, void *data));
224 
225 bool debugfs_initialized(void);
226 
227 ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
228 			       size_t count, loff_t *ppos);
229 
230 ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
231 				size_t count, loff_t *ppos);
232 
233 ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf,
234 			      size_t count, loff_t *ppos);
235 
236 /**
237  * struct debugfs_cancellation - cancellation data
238  * @list: internal, for keeping track
239  * @cancel: callback to call
240  * @cancel_data: extra data for the callback to call
241  */
242 struct debugfs_cancellation {
243 	struct list_head list;
244 	void (*cancel)(struct dentry *, void *);
245 	void *cancel_data;
246 };
247 
248 void __acquires(cancellation)
249 debugfs_enter_cancellation(struct file *file,
250 			   struct debugfs_cancellation *cancellation);
251 void __releases(cancellation)
252 debugfs_leave_cancellation(struct file *file,
253 			   struct debugfs_cancellation *cancellation);
254 
255 #else
256 
257 #include <linux/err.h>
258 
259 /*
260  * We do not return NULL from these functions if CONFIG_DEBUG_FS is not enabled
261  * so users have a chance to detect if there was a real error or not.  We don't
262  * want to duplicate the design decision mistakes of procfs and devfs again.
263  */
264 
debugfs_lookup(const char * name,struct dentry * parent)265 static inline struct dentry *debugfs_lookup(const char *name,
266 					    struct dentry *parent)
267 {
268 	return ERR_PTR(-ENODEV);
269 }
270 
debugfs_create_file_aux(const char * name,umode_t mode,struct dentry * parent,void * data,void * aux,const void * fops)271 static inline struct dentry *debugfs_create_file_aux(const char *name,
272 					umode_t mode, struct dentry *parent,
273 					void *data, void *aux,
274 					const void *fops)
275 {
276 	return ERR_PTR(-ENODEV);
277 }
278 
debugfs_create_file(const char * name,umode_t mode,struct dentry * parent,void * data,const void * fops)279 static inline struct dentry *debugfs_create_file(const char *name, umode_t mode,
280 					struct dentry *parent, void *data,
281 					const void *fops)
282 {
283 	return ERR_PTR(-ENODEV);
284 }
285 
debugfs_create_file_unsafe(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops)286 static inline struct dentry *debugfs_create_file_unsafe(const char *name,
287 					umode_t mode, struct dentry *parent,
288 					void *data,
289 					const struct file_operations *fops)
290 {
291 	return ERR_PTR(-ENODEV);
292 }
293 
debugfs_create_file_size(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops,loff_t file_size)294 static inline void debugfs_create_file_size(const char *name, umode_t mode,
295 					    struct dentry *parent, void *data,
296 					    const struct file_operations *fops,
297 					    loff_t file_size)
298 { }
299 
debugfs_create_dir(const char * name,struct dentry * parent)300 static inline struct dentry *debugfs_create_dir(const char *name,
301 						struct dentry *parent)
302 {
303 	return ERR_PTR(-ENODEV);
304 }
305 
debugfs_create_symlink(const char * name,struct dentry * parent,const char * dest)306 static inline struct dentry *debugfs_create_symlink(const char *name,
307 						    struct dentry *parent,
308 						    const char *dest)
309 {
310 	return ERR_PTR(-ENODEV);
311 }
312 
debugfs_create_automount(const char * name,struct dentry * parent,debugfs_automount_t f,void * data)313 static inline struct dentry *debugfs_create_automount(const char *name,
314 					struct dentry *parent,
315 					debugfs_automount_t f,
316 					void *data)
317 {
318 	return ERR_PTR(-ENODEV);
319 }
320 
debugfs_remove(struct dentry * dentry)321 static inline void debugfs_remove(struct dentry *dentry)
322 { }
323 
debugfs_remove_recursive(struct dentry * dentry)324 static inline void debugfs_remove_recursive(struct dentry *dentry)
325 { }
326 
debugfs_lookup_and_remove(const char * name,struct dentry * parent)327 static inline void debugfs_lookup_and_remove(const char *name,
328 					     struct dentry *parent)
329 { }
330 
331 void *debugfs_get_aux(const struct file *file);
332 
debugfs_file_get(struct dentry * dentry)333 static inline int debugfs_file_get(struct dentry *dentry)
334 {
335 	return 0;
336 }
337 
debugfs_file_put(struct dentry * dentry)338 static inline void debugfs_file_put(struct dentry *dentry)
339 { }
340 
debugfs_attr_read(struct file * file,char __user * buf,size_t len,loff_t * ppos)341 static inline ssize_t debugfs_attr_read(struct file *file, char __user *buf,
342 					size_t len, loff_t *ppos)
343 {
344 	return -ENODEV;
345 }
346 
debugfs_attr_write(struct file * file,const char __user * buf,size_t len,loff_t * ppos)347 static inline ssize_t debugfs_attr_write(struct file *file,
348 					const char __user *buf,
349 					size_t len, loff_t *ppos)
350 {
351 	return -ENODEV;
352 }
353 
debugfs_attr_write_signed(struct file * file,const char __user * buf,size_t len,loff_t * ppos)354 static inline ssize_t debugfs_attr_write_signed(struct file *file,
355 					const char __user *buf,
356 					size_t len, loff_t *ppos)
357 {
358 	return -ENODEV;
359 }
360 
debugfs_change_name(struct dentry * dentry,const char * fmt,...)361 static inline int __printf(2, 3) debugfs_change_name(struct dentry *dentry,
362 					const char *fmt, ...)
363 {
364 	return -ENODEV;
365 }
366 
debugfs_create_u8(const char * name,umode_t mode,struct dentry * parent,u8 * value)367 static inline void debugfs_create_u8(const char *name, umode_t mode,
368 				     struct dentry *parent, u8 *value) { }
369 
debugfs_create_u16(const char * name,umode_t mode,struct dentry * parent,u16 * value)370 static inline void debugfs_create_u16(const char *name, umode_t mode,
371 				      struct dentry *parent, u16 *value) { }
372 
debugfs_create_u32(const char * name,umode_t mode,struct dentry * parent,u32 * value)373 static inline void debugfs_create_u32(const char *name, umode_t mode,
374 				      struct dentry *parent, u32 *value) { }
375 
debugfs_create_u64(const char * name,umode_t mode,struct dentry * parent,u64 * value)376 static inline void debugfs_create_u64(const char *name, umode_t mode,
377 				      struct dentry *parent, u64 *value) { }
378 
debugfs_create_ulong(const char * name,umode_t mode,struct dentry * parent,unsigned long * value)379 static inline void debugfs_create_ulong(const char *name, umode_t mode,
380 					struct dentry *parent,
381 					unsigned long *value) { }
382 
debugfs_create_x8(const char * name,umode_t mode,struct dentry * parent,u8 * value)383 static inline void debugfs_create_x8(const char *name, umode_t mode,
384 				     struct dentry *parent, u8 *value) { }
385 
debugfs_create_x16(const char * name,umode_t mode,struct dentry * parent,u16 * value)386 static inline void debugfs_create_x16(const char *name, umode_t mode,
387 				      struct dentry *parent, u16 *value) { }
388 
debugfs_create_x32(const char * name,umode_t mode,struct dentry * parent,u32 * value)389 static inline void debugfs_create_x32(const char *name, umode_t mode,
390 				      struct dentry *parent, u32 *value) { }
391 
debugfs_create_x64(const char * name,umode_t mode,struct dentry * parent,u64 * value)392 static inline void debugfs_create_x64(const char *name, umode_t mode,
393 				      struct dentry *parent, u64 *value) { }
394 
debugfs_create_size_t(const char * name,umode_t mode,struct dentry * parent,size_t * value)395 static inline void debugfs_create_size_t(const char *name, umode_t mode,
396 					 struct dentry *parent, size_t *value)
397 { }
398 
debugfs_create_atomic_t(const char * name,umode_t mode,struct dentry * parent,atomic_t * value)399 static inline void debugfs_create_atomic_t(const char *name, umode_t mode,
400 					   struct dentry *parent,
401 					   atomic_t *value)
402 { }
403 
debugfs_create_bool(const char * name,umode_t mode,struct dentry * parent,bool * value)404 static inline void debugfs_create_bool(const char *name, umode_t mode,
405 				       struct dentry *parent, bool *value) { }
406 
debugfs_create_str(const char * name,umode_t mode,struct dentry * parent,char ** value)407 static inline void debugfs_create_str(const char *name, umode_t mode,
408 				      struct dentry *parent,
409 				      char **value)
410 { }
411 
debugfs_create_blob(const char * name,umode_t mode,struct dentry * parent,struct debugfs_blob_wrapper * blob)412 static inline struct dentry *debugfs_create_blob(const char *name, umode_t mode,
413 				  struct dentry *parent,
414 				  struct debugfs_blob_wrapper *blob)
415 {
416 	return ERR_PTR(-ENODEV);
417 }
418 
debugfs_create_regset32(const char * name,umode_t mode,struct dentry * parent,struct debugfs_regset32 * regset)419 static inline void debugfs_create_regset32(const char *name, umode_t mode,
420 					   struct dentry *parent,
421 					   struct debugfs_regset32 *regset)
422 {
423 }
424 
debugfs_print_regs32(struct seq_file * s,const struct debugfs_reg32 * regs,int nregs,void __iomem * base,char * prefix)425 static inline void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
426 			 int nregs, void __iomem *base, char *prefix)
427 {
428 }
429 
debugfs_initialized(void)430 static inline bool debugfs_initialized(void)
431 {
432 	return false;
433 }
434 
debugfs_create_u32_array(const char * name,umode_t mode,struct dentry * parent,struct debugfs_u32_array * array)435 static inline void debugfs_create_u32_array(const char *name, umode_t mode,
436 					    struct dentry *parent,
437 					    struct debugfs_u32_array *array)
438 {
439 }
440 
debugfs_create_devm_seqfile(struct device * dev,const char * name,struct dentry * parent,int (* read_fn)(struct seq_file * s,void * data))441 static inline void debugfs_create_devm_seqfile(struct device *dev,
442 					       const char *name,
443 					       struct dentry *parent,
444 					       int (*read_fn)(struct seq_file *s,
445 							      void *data))
446 {
447 }
448 
debugfs_read_file_bool(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)449 static inline ssize_t debugfs_read_file_bool(struct file *file,
450 					     char __user *user_buf,
451 					     size_t count, loff_t *ppos)
452 {
453 	return -ENODEV;
454 }
455 
debugfs_write_file_bool(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)456 static inline ssize_t debugfs_write_file_bool(struct file *file,
457 					      const char __user *user_buf,
458 					      size_t count, loff_t *ppos)
459 {
460 	return -ENODEV;
461 }
462 
debugfs_read_file_str(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)463 static inline ssize_t debugfs_read_file_str(struct file *file,
464 					    char __user *user_buf,
465 					    size_t count, loff_t *ppos)
466 {
467 	return -ENODEV;
468 }
469 
470 #endif
471 
472 #define debugfs_create_file_aux_num(name, mode, parent, data, n, fops) \
473 	debugfs_create_file_aux(name, mode, parent, data, \
474 				(void *)(unsigned long)n, fops)
475 #define debugfs_get_aux_num(f) (unsigned long)debugfs_get_aux(f)
476 
477 /**
478  * debugfs_create_xul - create a debugfs file that is used to read and write an
479  * unsigned long value, formatted in hexadecimal
480  * @name: a pointer to a string containing the name of the file to create.
481  * @mode: the permission that the file should have
482  * @parent: a pointer to the parent dentry for this file.  This should be a
483  *          directory dentry if set.  If this parameter is %NULL, then the
484  *          file will be created in the root of the debugfs filesystem.
485  * @value: a pointer to the variable that the file should read to and write
486  *         from.
487  */
debugfs_create_xul(const char * name,umode_t mode,struct dentry * parent,unsigned long * value)488 static inline void debugfs_create_xul(const char *name, umode_t mode,
489 				      struct dentry *parent,
490 				      unsigned long *value)
491 {
492 	if (sizeof(*value) == sizeof(u32))
493 		debugfs_create_x32(name, mode, parent, (u32 *)value);
494 	else
495 		debugfs_create_x64(name, mode, parent, (u64 *)value);
496 }
497 
498 #endif
499