lib/kunit/debugfs.c

Source file repositories/reference/linux-study-clean/lib/kunit/debugfs.c

File Facts

System
Linux kernel
Corpus path
lib/kunit/debugfs.c
Extension
.c
Size
6020 bytes
Lines
231
Domain
Kernel Services
Bucket
lib
Inferred role
Kernel Services: operation-table or driver-model contract
Status
pattern implementation candidate

Why This File Exists

Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.

Dependency Surface

Detected Declarations

Annotated Snippet

static const struct file_operations debugfs_results_fops = {
	.open = debugfs_results_open,
	.read = seq_read,
	.llseek = seq_lseek,
	.release = debugfs_release,
};

static const struct file_operations debugfs_run_fops = {
	.open = debugfs_run_open,
	.read = seq_read,
	.write = debugfs_run,
	.llseek = seq_lseek,
	.release = debugfs_release,
};

void kunit_debugfs_create_suite(struct kunit_suite *suite)
{
	struct kunit_case *test_case;
	struct string_stream *stream;

	/* If suite log already allocated, do not create new debugfs files. */
	if (suite->log)
		return;

	/*
	 * Allocate logs before creating debugfs representation.
	 * The suite->log and test_case->log pointer are expected to be NULL
	 * if there isn't a log, so only set it if the log stream was created
	 * successfully.
	 */
	stream = alloc_string_stream(GFP_KERNEL);
	if (IS_ERR(stream))
		return;

	string_stream_set_append_newlines(stream, true);
	suite->log = stream;

	kunit_suite_for_each_test_case(suite, test_case) {
		stream = alloc_string_stream(GFP_KERNEL);
		if (IS_ERR(stream))
			goto err;

		string_stream_set_append_newlines(stream, true);
		test_case->log = stream;
	}

	suite->debugfs = debugfs_create_dir(suite->name, debugfs_rootdir);

	debugfs_create_file(KUNIT_DEBUGFS_RESULTS, S_IFREG | 0444,
			    suite->debugfs,
			    suite, &debugfs_results_fops);

	/* Do not create file to re-run test if test runs on init */
	if (!suite->is_init) {
		debugfs_create_file(KUNIT_DEBUGFS_RUN, S_IFREG | 0644,
				    suite->debugfs,
				    suite, &debugfs_run_fops);
	}
	return;

err:
	string_stream_destroy(suite->log);
	suite->log = NULL;
	kunit_suite_for_each_test_case(suite, test_case) {
		string_stream_destroy(test_case->log);
		test_case->log = NULL;
	}
}

void kunit_debugfs_destroy_suite(struct kunit_suite *suite)
{
	struct kunit_case *test_case;

	debugfs_remove_recursive(suite->debugfs);
	string_stream_destroy(suite->log);
	kunit_suite_for_each_test_case(suite, test_case)
		string_stream_destroy(test_case->log);
}

Annotation

Implementation Notes