debugfs.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include <linux/init.h>
  2. #include <linux/debugfs.h>
  3. #include <linux/module.h>
  4. #include "debugfs.h"
  5. static struct dentry *d_xen_debug;
  6. struct dentry * __init xen_init_debugfs(void)
  7. {
  8. if (!d_xen_debug) {
  9. d_xen_debug = debugfs_create_dir("xen", NULL);
  10. if (!d_xen_debug)
  11. pr_warning("Could not create 'xen' debugfs directory\n");
  12. }
  13. return d_xen_debug;
  14. }
  15. struct array_data
  16. {
  17. void *array;
  18. unsigned elements;
  19. };
  20. static int u32_array_open(struct inode *inode, struct file *file)
  21. {
  22. file->private_data = NULL;
  23. return nonseekable_open(inode, file);
  24. }
  25. static size_t format_array(char *buf, size_t bufsize, const char *fmt,
  26. u32 *array, unsigned array_size)
  27. {
  28. size_t ret = 0;
  29. unsigned i;
  30. for(i = 0; i < array_size; i++) {
  31. size_t len;
  32. len = snprintf(buf, bufsize, fmt, array[i]);
  33. len++; /* ' ' or '\n' */
  34. ret += len;
  35. if (buf) {
  36. buf += len;
  37. bufsize -= len;
  38. buf[-1] = (i == array_size-1) ? '\n' : ' ';
  39. }
  40. }
  41. ret++; /* \0 */
  42. if (buf)
  43. *buf = '\0';
  44. return ret;
  45. }
  46. static char *format_array_alloc(const char *fmt, u32 *array, unsigned array_size)
  47. {
  48. size_t len = format_array(NULL, 0, fmt, array, array_size);
  49. char *ret;
  50. ret = kmalloc(len, GFP_KERNEL);
  51. if (ret == NULL)
  52. return NULL;
  53. format_array(ret, len, fmt, array, array_size);
  54. return ret;
  55. }
  56. static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
  57. loff_t *ppos)
  58. {
  59. struct inode *inode = file->f_path.dentry->d_inode;
  60. struct array_data *data = inode->i_private;
  61. size_t size;
  62. if (*ppos == 0) {
  63. if (file->private_data) {
  64. kfree(file->private_data);
  65. file->private_data = NULL;
  66. }
  67. file->private_data = format_array_alloc("%u", data->array, data->elements);
  68. }
  69. size = 0;
  70. if (file->private_data)
  71. size = strlen(file->private_data);
  72. return simple_read_from_buffer(buf, len, ppos, file->private_data, size);
  73. }
  74. static int xen_array_release(struct inode *inode, struct file *file)
  75. {
  76. kfree(file->private_data);
  77. return 0;
  78. }
  79. static struct file_operations u32_array_fops = {
  80. .owner = THIS_MODULE,
  81. .open = u32_array_open,
  82. .release= xen_array_release,
  83. .read = u32_array_read,
  84. };
  85. struct dentry *xen_debugfs_create_u32_array(const char *name, mode_t mode,
  86. struct dentry *parent,
  87. u32 *array, unsigned elements)
  88. {
  89. struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
  90. if (data == NULL)
  91. return NULL;
  92. data->array = array;
  93. data->elements = elements;
  94. return debugfs_create_file(name, mode, parent, data, &u32_array_fops);
  95. }