dmesg.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #
  2. # gdb helper commands and functions for Linux kernel debugging
  3. #
  4. # kernel log buffer dump
  5. #
  6. # Copyright (c) Siemens AG, 2011, 2012
  7. #
  8. # Authors:
  9. # Jan Kiszka <jan.kiszka@siemens.com>
  10. #
  11. # This work is licensed under the terms of the GNU GPL version 2.
  12. #
  13. import gdb
  14. from linux import utils
  15. class LxDmesg(gdb.Command):
  16. """Print Linux kernel log buffer."""
  17. def __init__(self):
  18. super(LxDmesg, self).__init__("lx-dmesg", gdb.COMMAND_DATA)
  19. def invoke(self, arg, from_tty):
  20. log_buf_addr = int(str(gdb.parse_and_eval(
  21. "'printk.c'::log_buf")).split()[0], 16)
  22. log_first_idx = int(gdb.parse_and_eval("'printk.c'::log_first_idx"))
  23. log_next_idx = int(gdb.parse_and_eval("'printk.c'::log_next_idx"))
  24. log_buf_len = int(gdb.parse_and_eval("'printk.c'::log_buf_len"))
  25. inf = gdb.inferiors()[0]
  26. start = log_buf_addr + log_first_idx
  27. if log_first_idx < log_next_idx:
  28. log_buf_2nd_half = -1
  29. length = log_next_idx - log_first_idx
  30. log_buf = utils.read_memoryview(inf, start, length).tobytes()
  31. else:
  32. log_buf_2nd_half = log_buf_len - log_first_idx
  33. a = utils.read_memoryview(inf, start, log_buf_2nd_half)
  34. b = utils.read_memoryview(inf, log_buf_addr, log_next_idx)
  35. log_buf = a.tobytes() + b.tobytes()
  36. pos = 0
  37. while pos < log_buf.__len__():
  38. length = utils.read_u16(log_buf[pos + 8:pos + 10])
  39. if length == 0:
  40. if log_buf_2nd_half == -1:
  41. gdb.write("Corrupted log buffer!\n")
  42. break
  43. pos = log_buf_2nd_half
  44. continue
  45. text_len = utils.read_u16(log_buf[pos + 10:pos + 12])
  46. text = log_buf[pos + 16:pos + 16 + text_len].decode()
  47. time_stamp = utils.read_u64(log_buf[pos:pos + 8])
  48. for line in text.splitlines():
  49. gdb.write("[{time:12.6f}] {line}\n".format(
  50. time=time_stamp / 1000000000.0,
  51. line=line))
  52. pos += length
  53. LxDmesg()