debugfs.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 <kunit/test-bug.h>
  10. #include "string-stream.h"
  11. #include "debugfs.h"
  12. #define KUNIT_DEBUGFS_ROOT "kunit"
  13. #define KUNIT_DEBUGFS_RESULTS "results"
  14. #define KUNIT_DEBUGFS_RUN "run"
  15. /*
  16. * Create a debugfs representation of test suites:
  17. *
  18. * Path Semantics
  19. * /sys/kernel/debug/kunit/<testsuite>/results Show results of last run for
  20. * testsuite
  21. * /sys/kernel/debug/kunit/<testsuite>/run Write to this file to trigger
  22. * testsuite to run
  23. *
  24. */
  25. static struct dentry *debugfs_rootdir;
  26. void kunit_debugfs_cleanup(void)
  27. {
  28. debugfs_remove_recursive(debugfs_rootdir);
  29. }
  30. void kunit_debugfs_init(void)
  31. {
  32. if (!debugfs_rootdir)
  33. debugfs_rootdir = debugfs_create_dir(KUNIT_DEBUGFS_ROOT, NULL);
  34. }
  35. static void debugfs_print_result(struct seq_file *seq, struct string_stream *log)
  36. {
  37. struct string_stream_fragment *frag_container;
  38. if (!log)
  39. return;
  40. /*
  41. * Walk the fragments so we don't need to allocate a temporary
  42. * buffer to hold the entire string.
  43. */
  44. spin_lock(&log->lock);
  45. list_for_each_entry(frag_container, &log->fragments, node)
  46. seq_printf(seq, "%s", frag_container->fragment);
  47. spin_unlock(&log->lock);
  48. }
  49. /*
  50. * /sys/kernel/debug/kunit/<testsuite>/results shows all results for testsuite.
  51. */
  52. static int debugfs_print_results(struct seq_file *seq, void *v)
  53. {
  54. struct kunit_suite *suite = (struct kunit_suite *)seq->private;
  55. enum kunit_status success;
  56. struct kunit_case *test_case;
  57. if (!suite)
  58. return 0;
  59. success = kunit_suite_has_succeeded(suite);
  60. /* Print KTAP header so the debugfs log can be parsed as valid KTAP. */
  61. seq_puts(seq, "KTAP version 1\n");
  62. seq_puts(seq, "1..1\n");
  63. /* Print suite header because it is not stored in the test logs. */
  64. seq_puts(seq, KUNIT_SUBTEST_INDENT "KTAP version 1\n");
  65. seq_printf(seq, KUNIT_SUBTEST_INDENT "# Subtest: %s\n", suite->name);
  66. seq_printf(seq, KUNIT_SUBTEST_INDENT "1..%zd\n", kunit_suite_num_test_cases(suite));
  67. kunit_suite_for_each_test_case(suite, test_case)
  68. debugfs_print_result(seq, test_case->log);
  69. debugfs_print_result(seq, suite->log);
  70. seq_printf(seq, "%s %d %s\n",
  71. kunit_status_to_ok_not_ok(success), 1, suite->name);
  72. return 0;
  73. }
  74. static int debugfs_release(struct inode *inode, struct file *file)
  75. {
  76. return single_release(inode, file);
  77. }
  78. static int debugfs_results_open(struct inode *inode, struct file *file)
  79. {
  80. struct kunit_suite *suite;
  81. suite = (struct kunit_suite *)inode->i_private;
  82. return single_open(file, debugfs_print_results, suite);
  83. }
  84. /*
  85. * Print a usage message to the debugfs "run" file
  86. * (/sys/kernel/debug/kunit/<testsuite>/run) if opened.
  87. */
  88. static int debugfs_print_run(struct seq_file *seq, void *v)
  89. {
  90. struct kunit_suite *suite = (struct kunit_suite *)seq->private;
  91. seq_puts(seq, "Write to this file to trigger the test suite to run.\n");
  92. seq_printf(seq, "usage: echo \"any string\" > /sys/kernel/debugfs/kunit/%s/run\n",
  93. suite->name);
  94. return 0;
  95. }
  96. /*
  97. * The debugfs "run" file (/sys/kernel/debug/kunit/<testsuite>/run)
  98. * contains no information. Write to the file to trigger the test suite
  99. * to run.
  100. */
  101. static int debugfs_run_open(struct inode *inode, struct file *file)
  102. {
  103. struct kunit_suite *suite;
  104. suite = (struct kunit_suite *)inode->i_private;
  105. return single_open(file, debugfs_print_run, suite);
  106. }
  107. /*
  108. * Trigger a test suite to run by writing to the suite's "run" debugfs
  109. * file found at: /sys/kernel/debug/kunit/<testsuite>/run
  110. *
  111. * Note: what is written to this file will not be saved.
  112. */
  113. static ssize_t debugfs_run(struct file *file,
  114. const char __user *buf, size_t count, loff_t *ppos)
  115. {
  116. struct inode *f_inode = file->f_inode;
  117. struct kunit_suite *suite = (struct kunit_suite *) f_inode->i_private;
  118. __kunit_test_suites_init(&suite, 1, true);
  119. return count;
  120. }
  121. static const struct file_operations debugfs_results_fops = {
  122. .open = debugfs_results_open,
  123. .read = seq_read,
  124. .llseek = seq_lseek,
  125. .release = debugfs_release,
  126. };
  127. static const struct file_operations debugfs_run_fops = {
  128. .open = debugfs_run_open,
  129. .read = seq_read,
  130. .write = debugfs_run,
  131. .llseek = seq_lseek,
  132. .release = debugfs_release,
  133. };
  134. void kunit_debugfs_create_suite(struct kunit_suite *suite)
  135. {
  136. struct kunit_case *test_case;
  137. struct string_stream *stream;
  138. /* If suite log already allocated, do not create new debugfs files. */
  139. if (suite->log)
  140. return;
  141. /*
  142. * Allocate logs before creating debugfs representation.
  143. * The suite->log and test_case->log pointer are expected to be NULL
  144. * if there isn't a log, so only set it if the log stream was created
  145. * successfully.
  146. */
  147. stream = alloc_string_stream(GFP_KERNEL);
  148. if (IS_ERR(stream))
  149. return;
  150. string_stream_set_append_newlines(stream, true);
  151. suite->log = stream;
  152. kunit_suite_for_each_test_case(suite, test_case) {
  153. stream = alloc_string_stream(GFP_KERNEL);
  154. if (IS_ERR(stream))
  155. goto err;
  156. string_stream_set_append_newlines(stream, true);
  157. test_case->log = stream;
  158. }
  159. suite->debugfs = debugfs_create_dir(suite->name, debugfs_rootdir);
  160. debugfs_create_file(KUNIT_DEBUGFS_RESULTS, S_IFREG | 0444,
  161. suite->debugfs,
  162. suite, &debugfs_results_fops);
  163. /* Do not create file to re-run test if test runs on init */
  164. if (!suite->is_init) {
  165. debugfs_create_file(KUNIT_DEBUGFS_RUN, S_IFREG | 0644,
  166. suite->debugfs,
  167. suite, &debugfs_run_fops);
  168. }
  169. return;
  170. err:
  171. string_stream_destroy(suite->log);
  172. suite->log = NULL;
  173. kunit_suite_for_each_test_case(suite, test_case) {
  174. string_stream_destroy(test_case->log);
  175. test_case->log = NULL;
  176. }
  177. }
  178. void kunit_debugfs_destroy_suite(struct kunit_suite *suite)
  179. {
  180. struct kunit_case *test_case;
  181. debugfs_remove_recursive(suite->debugfs);
  182. string_stream_destroy(suite->log);
  183. kunit_suite_for_each_test_case(suite, test_case)
  184. string_stream_destroy(test_case->log);
  185. }