66 lines
1.4 KiB
C
Raw Normal View History

2026-01-21 18:59:54 +08:00
// SPDX-License-Identifier: GPL-2.0
#include <linux/kmsg_dump.h>
2026-01-29 22:25:33 +08:00
#include <linux/spinlock.h>
2026-01-21 18:59:54 +08:00
#include <linux/console.h>
2026-01-29 22:25:33 +08:00
#include <linux/string.h>
2026-01-21 18:59:54 +08:00
#include <shared/init.h>
#include <shared/kern.h>
#include <os.h>
static void kmsg_dumper_stdout(struct kmsg_dumper *dumper,
enum kmsg_dump_reason reason)
{
2026-01-29 22:25:33 +08:00
static struct kmsg_dump_iter iter;
static DEFINE_SPINLOCK(lock);
2026-01-21 18:59:54 +08:00
static char line[1024];
struct console *con;
2026-01-29 22:25:33 +08:00
unsigned long flags;
2026-01-21 18:59:54 +08:00
size_t len = 0;
2026-01-29 22:25:33 +08:00
int cookie;
2026-01-21 18:59:54 +08:00
2026-01-29 22:25:33 +08:00
/*
* If no consoles are available to output crash information, dump
* the kmsg buffer to stdout.
*/
2026-01-21 18:59:54 +08:00
2026-01-29 22:25:33 +08:00
cookie = console_srcu_read_lock();
for_each_console_srcu(con) {
/*
* The ttynull console and disabled consoles are ignored
* since they cannot output. All other consoles are
* expected to output the crash information.
*/
if (strcmp(con->name, "ttynull") != 0 &&
(console_srcu_read_flags(con) & CON_ENABLED)) {
break;
}
}
console_srcu_read_unlock(cookie);
2026-01-21 18:59:54 +08:00
if (con)
return;
2026-01-29 22:25:33 +08:00
if (!spin_trylock_irqsave(&lock, flags))
return;
kmsg_dump_rewind(&iter);
2026-01-21 18:59:54 +08:00
printf("kmsg_dump:\n");
2026-01-29 22:25:33 +08:00
while (kmsg_dump_get_line(&iter, true, line, sizeof(line), &len)) {
2026-01-21 18:59:54 +08:00
line[len] = '\0';
printf("%s", line);
}
2026-01-29 22:25:33 +08:00
spin_unlock_irqrestore(&lock, flags);
2026-01-21 18:59:54 +08:00
}
static struct kmsg_dumper kmsg_dumper = {
.dump = kmsg_dumper_stdout
};
int __init kmsg_dumper_stdout_init(void)
{
return kmsg_dump_register(&kmsg_dumper);
}
__uml_postsetup(kmsg_dumper_stdout_init);