kmsg_dump.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. static void kmsg_dumper_stdout(struct kmsg_dumper *dumper,
  10. enum kmsg_dump_reason reason)
  11. {
  12. static struct kmsg_dump_iter iter;
  13. static DEFINE_SPINLOCK(lock);
  14. static char line[1024];
  15. struct console *con;
  16. unsigned long flags;
  17. size_t len = 0;
  18. /* only dump kmsg when no console is available */
  19. if (!console_trylock())
  20. return;
  21. for_each_console(con) {
  22. if(strcmp(con->name, "tty") == 0 &&
  23. (con->flags & (CON_ENABLED | CON_CONSDEV)) != 0) {
  24. break;
  25. }
  26. }
  27. console_unlock();
  28. if (con)
  29. return;
  30. if (!spin_trylock_irqsave(&lock, flags))
  31. return;
  32. kmsg_dump_rewind(&iter);
  33. printf("kmsg_dump:\n");
  34. while (kmsg_dump_get_line(&iter, true, line, sizeof(line), &len)) {
  35. line[len] = '\0';
  36. printf("%s", line);
  37. }
  38. spin_unlock_irqrestore(&lock, flags);
  39. }
  40. static struct kmsg_dumper kmsg_dumper = {
  41. .dump = kmsg_dumper_stdout
  42. };
  43. int __init kmsg_dumper_stdout_init(void)
  44. {
  45. return kmsg_dump_register(&kmsg_dumper);
  46. }
  47. __uml_postsetup(kmsg_dumper_stdout_init);