xref: /linux-6.15/lib/kunit/debugfs.c (revision 34dfd5bb)
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 
7 #include <linux/debugfs.h>
8 #include <linux/module.h>
9 
10 #include <kunit/test.h>
11 
12 #include "string-stream.h"
13 #include "debugfs.h"
14 
15 #define KUNIT_DEBUGFS_ROOT             "kunit"
16 #define KUNIT_DEBUGFS_RESULTS          "results"
17 
18 /*
19  * Create a debugfs representation of test suites:
20  *
21  * Path						Semantics
22  * /sys/kernel/debug/kunit/<testsuite>/results	Show results of last run for
23  *						testsuite
24  *
25  */
26 
27 static struct dentry *debugfs_rootdir;
28 
29 void kunit_debugfs_cleanup(void)
30 {
31 	debugfs_remove_recursive(debugfs_rootdir);
32 }
33 
34 void kunit_debugfs_init(void)
35 {
36 	if (!debugfs_rootdir)
37 		debugfs_rootdir = debugfs_create_dir(KUNIT_DEBUGFS_ROOT, NULL);
38 }
39 
40 static void debugfs_print_result(struct seq_file *seq, struct string_stream *log)
41 {
42 	struct string_stream_fragment *frag_container;
43 
44 	if (!log)
45 		return;
46 
47 	/*
48 	 * Walk the fragments so we don't need to allocate a temporary
49 	 * buffer to hold the entire string.
50 	 */
51 	spin_lock(&log->lock);
52 	list_for_each_entry(frag_container, &log->fragments, node)
53 		seq_printf(seq, "%s", frag_container->fragment);
54 	spin_unlock(&log->lock);
55 }
56 
57 /*
58  * /sys/kernel/debug/kunit/<testsuite>/results shows all results for testsuite.
59  */
60 static int debugfs_print_results(struct seq_file *seq, void *v)
61 {
62 	struct kunit_suite *suite = (struct kunit_suite *)seq->private;
63 	enum kunit_status success;
64 	struct kunit_case *test_case;
65 
66 	if (!suite)
67 		return 0;
68 
69 	success = kunit_suite_has_succeeded(suite);
70 
71 	/* Print KTAP header so the debugfs log can be parsed as valid KTAP. */
72 	seq_puts(seq, "KTAP version 1\n");
73 	seq_puts(seq, "1..1\n");
74 
75 	/* Print suite header because it is not stored in the test logs. */
76 	seq_puts(seq, KUNIT_SUBTEST_INDENT "KTAP version 1\n");
77 	seq_printf(seq, KUNIT_SUBTEST_INDENT "# Subtest: %s\n", suite->name);
78 	seq_printf(seq, KUNIT_SUBTEST_INDENT "1..%zd\n", kunit_suite_num_test_cases(suite));
79 
80 	kunit_suite_for_each_test_case(suite, test_case)
81 		debugfs_print_result(seq, test_case->log);
82 
83 	debugfs_print_result(seq, suite->log);
84 
85 	seq_printf(seq, "%s %d %s\n",
86 		   kunit_status_to_ok_not_ok(success), 1, suite->name);
87 	return 0;
88 }
89 
90 static int debugfs_release(struct inode *inode, struct file *file)
91 {
92 	return single_release(inode, file);
93 }
94 
95 static int debugfs_results_open(struct inode *inode, struct file *file)
96 {
97 	struct kunit_suite *suite;
98 
99 	suite = (struct kunit_suite *)inode->i_private;
100 
101 	return single_open(file, debugfs_print_results, suite);
102 }
103 
104 static const struct file_operations debugfs_results_fops = {
105 	.open = debugfs_results_open,
106 	.read = seq_read,
107 	.llseek = seq_lseek,
108 	.release = debugfs_release,
109 };
110 
111 void kunit_debugfs_create_suite(struct kunit_suite *suite)
112 {
113 	struct kunit_case *test_case;
114 
115 	/* Allocate logs before creating debugfs representation. */
116 	suite->log = alloc_string_stream(GFP_KERNEL);
117 	string_stream_set_append_newlines(suite->log, true);
118 
119 	kunit_suite_for_each_test_case(suite, test_case) {
120 		test_case->log = alloc_string_stream(GFP_KERNEL);
121 		string_stream_set_append_newlines(test_case->log, true);
122 	}
123 
124 	suite->debugfs = debugfs_create_dir(suite->name, debugfs_rootdir);
125 
126 	debugfs_create_file(KUNIT_DEBUGFS_RESULTS, S_IFREG | 0444,
127 			    suite->debugfs,
128 			    suite, &debugfs_results_fops);
129 }
130 
131 void kunit_debugfs_destroy_suite(struct kunit_suite *suite)
132 {
133 	struct kunit_case *test_case;
134 
135 	debugfs_remove_recursive(suite->debugfs);
136 	string_stream_destroy(suite->log);
137 	kunit_suite_for_each_test_case(suite, test_case)
138 		string_stream_destroy(test_case->log);
139 }
140