debugfs.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2020, Oracle and/or its affiliates.
  4. * Author: Alan Maguire <alan.maguire@oracle.com>
  5. */
  6. #include <linux/debugfs.h>
  7. #include <linux/module.h>
  8. #include <kunit/test.h>
  9. #include "string-stream.h"
  10. #define KUNIT_DEBUGFS_ROOT "kunit"
  11. #define KUNIT_DEBUGFS_RESULTS "results"
  12. /*
  13. * Create a debugfs representation of test suites:
  14. *
  15. * Path Semantics
  16. * /sys/kernel/debug/kunit/<testsuite>/results Show results of last run for
  17. * testsuite
  18. *
  19. */
  20. static struct dentry *debugfs_rootdir;
  21. void kunit_debugfs_cleanup(void)
  22. {
  23. debugfs_remove_recursive(debugfs_rootdir);
  24. }
  25. void kunit_debugfs_init(void)
  26. {
  27. if (!debugfs_rootdir)
  28. debugfs_rootdir = debugfs_create_dir(KUNIT_DEBUGFS_ROOT, NULL);
  29. }
  30. static void debugfs_print_result(struct seq_file *seq,
  31. struct kunit_suite *suite,
  32. struct kunit_case *test_case)
  33. {
  34. if (!test_case || !test_case->log)
  35. return;
  36. seq_printf(seq, "%s", test_case->log);
  37. }
  38. /*
  39. * /sys/kernel/debug/kunit/<testsuite>/results shows all results for testsuite.
  40. */
  41. static int debugfs_print_results(struct seq_file *seq, void *v)
  42. {
  43. struct kunit_suite *suite = (struct kunit_suite *)seq->private;
  44. bool success = kunit_suite_has_succeeded(suite);
  45. struct kunit_case *test_case;
  46. if (!suite || !suite->log)
  47. return 0;
  48. seq_printf(seq, "%s", suite->log);
  49. kunit_suite_for_each_test_case(suite, test_case)
  50. debugfs_print_result(seq, suite, test_case);
  51. seq_printf(seq, "%s %d - %s\n",
  52. kunit_status_to_string(success), 1, suite->name);
  53. return 0;
  54. }
  55. static int debugfs_release(struct inode *inode, struct file *file)
  56. {
  57. return single_release(inode, file);
  58. }
  59. static int debugfs_results_open(struct inode *inode, struct file *file)
  60. {
  61. struct kunit_suite *suite;
  62. suite = (struct kunit_suite *)inode->i_private;
  63. return single_open(file, debugfs_print_results, suite);
  64. }
  65. static const struct file_operations debugfs_results_fops = {
  66. .open = debugfs_results_open,
  67. .read = seq_read,
  68. .llseek = seq_lseek,
  69. .release = debugfs_release,
  70. };
  71. void kunit_debugfs_create_suite(struct kunit_suite *suite)
  72. {
  73. struct kunit_case *test_case;
  74. /* Allocate logs before creating debugfs representation. */
  75. suite->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL);
  76. kunit_suite_for_each_test_case(suite, test_case)
  77. test_case->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL);
  78. suite->debugfs = debugfs_create_dir(suite->name, debugfs_rootdir);
  79. debugfs_create_file(KUNIT_DEBUGFS_RESULTS, S_IFREG | 0444,
  80. suite->debugfs,
  81. suite, &debugfs_results_fops);
  82. }
  83. void kunit_debugfs_destroy_suite(struct kunit_suite *suite)
  84. {
  85. struct kunit_case *test_case;
  86. debugfs_remove_recursive(suite->debugfs);
  87. kfree(suite->log);
  88. kunit_suite_for_each_test_case(suite, test_case)
  89. kfree(test_case->log);
  90. }