dmesg.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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("log_buf")).split()[0], 16)
  21. log_first_idx = int(gdb.parse_and_eval("log_first_idx"))
  22. log_next_idx = int(gdb.parse_and_eval("log_next_idx"))
  23. log_buf_len = int(gdb.parse_and_eval("log_buf_len"))
  24. inf = gdb.inferiors()[0]
  25. start = log_buf_addr + log_first_idx
  26. if log_first_idx < log_next_idx:
  27. log_buf_2nd_half = -1
  28. length = log_next_idx - log_first_idx
  29. log_buf = inf.read_memory(start, length)
  30. else:
  31. log_buf_2nd_half = log_buf_len - log_first_idx
  32. log_buf = inf.read_memory(start, log_buf_2nd_half) + \
  33. inf.read_memory(log_buf_addr, log_next_idx)
  34. pos = 0
  35. while pos < log_buf.__len__():
  36. length = utils.read_u16(log_buf[pos + 8:pos + 10])
  37. if length == 0:
  38. if log_buf_2nd_half == -1:
  39. gdb.write("Corrupted log buffer!\n")
  40. break
  41. pos = log_buf_2nd_half
  42. continue
  43. text_len = utils.read_u16(log_buf[pos + 10:pos + 12])
  44. text = log_buf[pos + 16:pos + 16 + text_len]
  45. time_stamp = utils.read_u64(log_buf[pos:pos + 8])
  46. for line in memoryview(text).tobytes().splitlines():
  47. gdb.write("[{time:12.6f}] {line}\n".format(
  48. time=time_stamp / 1000000000.0,
  49. line=line))
  50. pos += length
  51. LxDmesg()