1 // SPDX-License-Identifier: GPL-2.0
3 * debugfs.h - a tiny little debug file system
5 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
6 * Copyright (C) 2004 IBM Inc.
8 * debugfs is for people to use instead of /proc or /sys.
9 * See Documentation/filesystems/ for more details.
16 #include <linux/seq_file.h>
18 #include <linux/types.h>
19 #include <linux/compiler.h>
22 struct file_operations;
24 struct debugfs_blob_wrapper {
29 struct debugfs_reg32 {
34 struct debugfs_regset32 {
35 const struct debugfs_reg32 *regs;
38 struct device *dev; /* Optional device for Runtime PM */
41 struct debugfs_u32_array {
46 extern struct dentry *arch_debugfs_dir;
48 #define DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, __is_signed) \
49 static int __fops ## _open(struct inode *inode, struct file *file) \
51 __simple_attr_check_format(__fmt, 0ull); \
52 return simple_attr_open(inode, file, __get, __set, __fmt); \
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 .llseek = no_llseek, \
63 #define DEFINE_DEBUGFS_ATTRIBUTE(__fops, __get, __set, __fmt) \
64 DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, false)
66 #define DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(__fops, __get, __set, __fmt) \
67 DEFINE_DEBUGFS_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, true)
69 typedef struct vfsmount *(*debugfs_automount_t)(struct dentry *, void *);
71 #if defined(CONFIG_DEBUG_FS)
73 struct dentry *debugfs_lookup(const char *name, struct dentry *parent);
75 struct dentry *debugfs_create_file(const char *name, umode_t mode,
76 struct dentry *parent, void *data,
77 const struct file_operations *fops);
78 struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
79 struct dentry *parent, void *data,
80 const struct file_operations *fops);
82 void debugfs_create_file_size(const char *name, umode_t mode,
83 struct dentry *parent, void *data,
84 const struct file_operations *fops,
87 struct dentry *debugfs_create_dir(const char *name, struct dentry *parent);
89 struct dentry *debugfs_create_symlink(const char *name, struct dentry *parent,
92 struct dentry *debugfs_create_automount(const char *name,
93 struct dentry *parent,
94 debugfs_automount_t f,
97 void debugfs_remove(struct dentry *dentry);
98 #define debugfs_remove_recursive debugfs_remove
100 void debugfs_lookup_and_remove(const char *name, struct dentry *parent);
102 const struct file_operations *debugfs_real_fops(const struct file *filp);
104 int debugfs_file_get(struct dentry *dentry);
105 void debugfs_file_put(struct dentry *dentry);
107 ssize_t debugfs_attr_read(struct file *file, char __user *buf,
108 size_t len, loff_t *ppos);
109 ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
110 size_t len, loff_t *ppos);
111 ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf,
112 size_t len, loff_t *ppos);
114 struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
115 struct dentry *new_dir, const char *new_name);
117 void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
119 void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
121 void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
123 void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
125 struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
126 struct dentry *parent, unsigned long *value);
127 void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
129 void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
131 void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
133 void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
135 void debugfs_create_size_t(const char *name, umode_t mode,
136 struct dentry *parent, size_t *value);
137 void debugfs_create_atomic_t(const char *name, umode_t mode,
138 struct dentry *parent, atomic_t *value);
139 struct dentry *debugfs_create_bool(const char *name, umode_t mode,
140 struct dentry *parent, bool *value);
142 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
143 struct dentry *parent,
144 struct debugfs_blob_wrapper *blob);
146 void debugfs_create_regset32(const char *name, umode_t mode,
147 struct dentry *parent,
148 struct debugfs_regset32 *regset);
150 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
151 int nregs, void __iomem *base, char *prefix);
153 void debugfs_create_u32_array(const char *name, umode_t mode,
154 struct dentry *parent,
155 struct debugfs_u32_array *array);
157 void debugfs_create_devm_seqfile(struct device *dev, const char *name,
158 struct dentry *parent,
159 int (*read_fn)(struct seq_file *s, void *data));
161 bool debugfs_initialized(void);
163 ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
164 size_t count, loff_t *ppos);
166 ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
167 size_t count, loff_t *ppos);
171 #include <linux/err.h>
174 * We do not return NULL from these functions if CONFIG_DEBUG_FS is not enabled
175 * so users have a chance to detect if there was a real error or not. We don't
176 * want to duplicate the design decision mistakes of procfs and devfs again.
179 static inline struct dentry *debugfs_lookup(const char *name,
180 struct dentry *parent)
182 return ERR_PTR(-ENODEV);
185 static inline struct dentry *debugfs_create_file(const char *name, umode_t mode,
186 struct dentry *parent, void *data,
187 const struct file_operations *fops)
189 return ERR_PTR(-ENODEV);
192 static inline struct dentry *debugfs_create_file_unsafe(const char *name,
193 umode_t mode, struct dentry *parent,
195 const struct file_operations *fops)
197 return ERR_PTR(-ENODEV);
200 static inline void debugfs_create_file_size(const char *name, umode_t mode,
201 struct dentry *parent, void *data,
202 const struct file_operations *fops,
206 static inline struct dentry *debugfs_create_dir(const char *name,
207 struct dentry *parent)
209 return ERR_PTR(-ENODEV);
212 static inline struct dentry *debugfs_create_symlink(const char *name,
213 struct dentry *parent,
216 return ERR_PTR(-ENODEV);
219 static inline struct dentry *debugfs_create_automount(const char *name,
220 struct dentry *parent,
221 debugfs_automount_t f,
224 return ERR_PTR(-ENODEV);
227 static inline void debugfs_remove(struct dentry *dentry)
230 static inline void debugfs_remove_recursive(struct dentry *dentry)
233 static inline void debugfs_lookup_and_remove(const char *name,
234 struct dentry *parent)
237 const struct file_operations *debugfs_real_fops(const struct file *filp);
239 static inline int debugfs_file_get(struct dentry *dentry)
244 static inline void debugfs_file_put(struct dentry *dentry)
247 static inline ssize_t debugfs_attr_read(struct file *file, char __user *buf,
248 size_t len, loff_t *ppos)
253 static inline ssize_t debugfs_attr_write(struct file *file,
254 const char __user *buf,
255 size_t len, loff_t *ppos)
260 static inline ssize_t debugfs_attr_write_signed(struct file *file,
261 const char __user *buf,
262 size_t len, loff_t *ppos)
267 static inline struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
268 struct dentry *new_dir, char *new_name)
270 return ERR_PTR(-ENODEV);
273 static inline void debugfs_create_u8(const char *name, umode_t mode,
274 struct dentry *parent, u8 *value) { }
276 static inline void debugfs_create_u16(const char *name, umode_t mode,
277 struct dentry *parent, u16 *value) { }
279 static inline void debugfs_create_u32(const char *name, umode_t mode,
280 struct dentry *parent, u32 *value) { }
282 static inline void debugfs_create_u64(const char *name, umode_t mode,
283 struct dentry *parent, u64 *value) { }
285 static inline struct dentry *debugfs_create_ulong(const char *name,
287 struct dentry *parent,
288 unsigned long *value)
290 return ERR_PTR(-ENODEV);
293 static inline void debugfs_create_x8(const char *name, umode_t mode,
294 struct dentry *parent, u8 *value) { }
296 static inline void debugfs_create_x16(const char *name, umode_t mode,
297 struct dentry *parent, u16 *value) { }
299 static inline void debugfs_create_x32(const char *name, umode_t mode,
300 struct dentry *parent, u32 *value) { }
302 static inline void debugfs_create_x64(const char *name, umode_t mode,
303 struct dentry *parent, u64 *value) { }
305 static inline void debugfs_create_size_t(const char *name, umode_t mode,
306 struct dentry *parent, size_t *value)
309 static inline void debugfs_create_atomic_t(const char *name, umode_t mode,
310 struct dentry *parent,
314 static inline struct dentry *debugfs_create_bool(const char *name, umode_t mode,
315 struct dentry *parent,
318 return ERR_PTR(-ENODEV);
321 static inline struct dentry *debugfs_create_blob(const char *name, umode_t mode,
322 struct dentry *parent,
323 struct debugfs_blob_wrapper *blob)
325 return ERR_PTR(-ENODEV);
328 static inline void debugfs_create_regset32(const char *name, umode_t mode,
329 struct dentry *parent,
330 struct debugfs_regset32 *regset)
334 static inline void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
335 int nregs, void __iomem *base, char *prefix)
339 static inline bool debugfs_initialized(void)
344 static inline void debugfs_create_u32_array(const char *name, umode_t mode,
345 struct dentry *parent,
346 struct debugfs_u32_array *array)
350 static inline void debugfs_create_devm_seqfile(struct device *dev,
352 struct dentry *parent,
353 int (*read_fn)(struct seq_file *s,
358 static inline ssize_t debugfs_read_file_bool(struct file *file,
359 char __user *user_buf,
360 size_t count, loff_t *ppos)
365 static inline ssize_t debugfs_write_file_bool(struct file *file,
366 const char __user *user_buf,
367 size_t count, loff_t *ppos)
375 * debugfs_create_xul - create a debugfs file that is used to read and write an
376 * unsigned long value, formatted in hexadecimal
377 * @name: a pointer to a string containing the name of the file to create.
378 * @mode: the permission that the file should have
379 * @parent: a pointer to the parent dentry for this file. This should be a
380 * directory dentry if set. If this parameter is %NULL, then the
381 * file will be created in the root of the debugfs filesystem.
382 * @value: a pointer to the variable that the file should read to and write
385 static inline void debugfs_create_xul(const char *name, umode_t mode,
386 struct dentry *parent,
387 unsigned long *value)
389 if (sizeof(*value) == sizeof(u32))
390 debugfs_create_x32(name, mode, parent, (u32 *)value);
392 debugfs_create_x64(name, mode, parent, (u64 *)value);