debugfs.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2020, Oracle and/or its affiliates.
  4. * Author: Alan Maguire <[email protected]>
  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. enum kunit_status success = kunit_suite_has_succeeded(suite);
  45. struct kunit_case *test_case;
  46. if (!suite)
  47. return 0;
  48. /* Print KTAP header so the debugfs log can be parsed as valid KTAP. */
  49. seq_puts(seq, "KTAP version 1\n");
  50. seq_puts(seq, "1..1\n");
  51. /* Print suite header because it is not stored in the test logs. */
  52. seq_puts(seq, KUNIT_SUBTEST_INDENT "KTAP version 1\n");
  53. seq_printf(seq, KUNIT_SUBTEST_INDENT "# Subtest: %s\n", suite->name);
  54. seq_printf(seq, KUNIT_SUBTEST_INDENT "1..%zd\n", kunit_suite_num_test_cases(suite));
  55. kunit_suite_for_each_test_case(suite, test_case)
  56. debugfs_print_result(seq, suite, test_case);
  57. if (suite->log)
  58. seq_printf(seq, "%s", suite->log);
  59. seq_printf(seq, "%s %d %s\n",
  60. kunit_status_to_ok_not_ok(success), 1, suite->name);
  61. return 0;
  62. }
  63. static int debugfs_release(struct inode *inode, struct file *file)
  64. {
  65. return single_release(inode, file);
  66. }
  67. static int debugfs_results_open(struct inode *inode, struct file *file)
  68. {
  69. struct kunit_suite *suite;
  70. suite = (struct kunit_suite *)inode->i_private;
  71. return single_open(file, debugfs_print_results, suite);
  72. }
  73. static const struct file_operations debugfs_results_fops = {
  74. .open = debugfs_results_open,
  75. .read = seq_read,
  76. .llseek = seq_lseek,
  77. .release = debugfs_release,
  78. };
  79. void kunit_debugfs_create_suite(struct kunit_suite *suite)
  80. {
  81. struct kunit_case *test_case;
  82. /* Allocate logs before creating debugfs representation. */
  83. suite->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL);
  84. kunit_suite_for_each_test_case(suite, test_case)
  85. test_case->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL);
  86. suite->debugfs = debugfs_create_dir(suite->name, debugfs_rootdir);
  87. debugfs_create_file(KUNIT_DEBUGFS_RESULTS, S_IFREG | 0444,
  88. suite->debugfs,
  89. suite, &debugfs_results_fops);
  90. }
  91. void kunit_debugfs_destroy_suite(struct kunit_suite *suite)
  92. {
  93. struct kunit_case *test_case;
  94. debugfs_remove_recursive(suite->debugfs);
  95. kfree(suite->log);
  96. kunit_suite_for_each_test_case(suite, test_case)
  97. kfree(test_case->log);
  98. }