xref: /openbmc/linux/arch/um/kernel/kmsg_dump.c (revision 0bf49ffb)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kmsg_dump.h>
3 #include <linux/console.h>
4 #include <linux/string.h>
5 #include <shared/init.h>
6 #include <shared/kern.h>
7 #include <os.h>
8 
9 static void kmsg_dumper_stdout(struct kmsg_dumper *dumper,
10 				enum kmsg_dump_reason reason)
11 {
12 	static char line[1024];
13 	struct console *con;
14 	size_t len = 0;
15 
16 	/* only dump kmsg when no console is available */
17 	if (!console_trylock())
18 		return;
19 
20 	for_each_console(con) {
21 		if(strcmp(con->name, "tty") == 0 &&
22 		   (con->flags & (CON_ENABLED | CON_CONSDEV)) != 0) {
23 			break;
24 		}
25 	}
26 
27 	console_unlock();
28 
29 	if (con)
30 		return;
31 
32 	printf("kmsg_dump:\n");
33 	while (kmsg_dump_get_line(dumper, true, line, sizeof(line), &len)) {
34 		line[len] = '\0';
35 		printf("%s", line);
36 	}
37 }
38 
39 static struct kmsg_dumper kmsg_dumper = {
40 	.dump = kmsg_dumper_stdout
41 };
42 
43 int __init kmsg_dumper_stdout_init(void)
44 {
45 	return kmsg_dump_register(&kmsg_dumper);
46 }
47 
48 __uml_postsetup(kmsg_dumper_stdout_init);
49