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