WIP FPC-III support
[linux/fpc-iii.git] / lib / kunit / debugfs.c
blob9214c493d8b76135af6511d04530d5189b6b58c0
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 */
7 #include <linux/debugfs.h>
8 #include <linux/module.h>
10 #include <kunit/test.h>
12 #include "string-stream.h"
14 #define KUNIT_DEBUGFS_ROOT "kunit"
15 #define KUNIT_DEBUGFS_RESULTS "results"
18 * Create a debugfs representation of test suites:
20 * Path Semantics
21 * /sys/kernel/debug/kunit/<testsuite>/results Show results of last run for
22 * testsuite
26 static struct dentry *debugfs_rootdir;
28 void kunit_debugfs_cleanup(void)
30 debugfs_remove_recursive(debugfs_rootdir);
33 void kunit_debugfs_init(void)
35 if (!debugfs_rootdir)
36 debugfs_rootdir = debugfs_create_dir(KUNIT_DEBUGFS_ROOT, NULL);
39 static void debugfs_print_result(struct seq_file *seq,
40 struct kunit_suite *suite,
41 struct kunit_case *test_case)
43 if (!test_case || !test_case->log)
44 return;
46 seq_printf(seq, "%s", test_case->log);
50 * /sys/kernel/debug/kunit/<testsuite>/results shows all results for testsuite.
52 static int debugfs_print_results(struct seq_file *seq, void *v)
54 struct kunit_suite *suite = (struct kunit_suite *)seq->private;
55 bool success = kunit_suite_has_succeeded(suite);
56 struct kunit_case *test_case;
58 if (!suite || !suite->log)
59 return 0;
61 seq_printf(seq, "%s", suite->log);
63 kunit_suite_for_each_test_case(suite, test_case)
64 debugfs_print_result(seq, suite, test_case);
66 seq_printf(seq, "%s %d - %s\n",
67 kunit_status_to_string(success), 1, suite->name);
68 return 0;
71 static int debugfs_release(struct inode *inode, struct file *file)
73 return single_release(inode, file);
76 static int debugfs_results_open(struct inode *inode, struct file *file)
78 struct kunit_suite *suite;
80 suite = (struct kunit_suite *)inode->i_private;
82 return single_open(file, debugfs_print_results, suite);
85 static const struct file_operations debugfs_results_fops = {
86 .open = debugfs_results_open,
87 .read = seq_read,
88 .llseek = seq_lseek,
89 .release = debugfs_release,
92 void kunit_debugfs_create_suite(struct kunit_suite *suite)
94 struct kunit_case *test_case;
96 /* Allocate logs before creating debugfs representation. */
97 suite->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL);
98 kunit_suite_for_each_test_case(suite, test_case)
99 test_case->log = kzalloc(KUNIT_LOG_SIZE, GFP_KERNEL);
101 suite->debugfs = debugfs_create_dir(suite->name, debugfs_rootdir);
103 debugfs_create_file(KUNIT_DEBUGFS_RESULTS, S_IFREG | 0444,
104 suite->debugfs,
105 suite, &debugfs_results_fops);
108 void kunit_debugfs_destroy_suite(struct kunit_suite *suite)
110 struct kunit_case *test_case;
112 debugfs_remove_recursive(suite->debugfs);
113 kfree(suite->log);
114 kunit_suite_for_each_test_case(suite, test_case)
115 kfree(test_case->log);