dmesg.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. import sys
  15. from linux import utils
  16. class LxDmesg(gdb.Command):
  17. """Print Linux kernel log buffer."""
  18. def __init__(self):
  19. super(LxDmesg, self).__init__("lx-dmesg", gdb.COMMAND_DATA)
  20. def invoke(self, arg, from_tty):
  21. log_buf_addr = int(str(gdb.parse_and_eval(
  22. "(void *)'printk.c'::log_buf")).split()[0], 16)
  23. log_first_idx = int(gdb.parse_and_eval("'printk.c'::log_first_idx"))
  24. log_next_idx = int(gdb.parse_and_eval("'printk.c'::log_next_idx"))
  25. log_buf_len = int(gdb.parse_and_eval("'printk.c'::log_buf_len"))
  26. inf = gdb.inferiors()[0]
  27. start = log_buf_addr + log_first_idx
  28. if log_first_idx < log_next_idx:
  29. log_buf_2nd_half = -1
  30. length = log_next_idx - log_first_idx
  31. log_buf = utils.read_memoryview(inf, start, length).tobytes()
  32. else:
  33. log_buf_2nd_half = log_buf_len - log_first_idx
  34. a = utils.read_memoryview(inf, start, log_buf_2nd_half)
  35. b = utils.read_memoryview(inf, log_buf_addr, log_next_idx)
  36. log_buf = a.tobytes() + b.tobytes()
  37. pos = 0
  38. while pos < log_buf.__len__():
  39. length = utils.read_u16(log_buf[pos + 8:pos + 10])
  40. if length == 0:
  41. if log_buf_2nd_half == -1:
  42. gdb.write("Corrupted log buffer!\n")
  43. break
  44. pos = log_buf_2nd_half
  45. continue
  46. text_len = utils.read_u16(log_buf[pos + 10:pos + 12])
  47. text = log_buf[pos + 16:pos + 16 + text_len].decode(
  48. encoding='utf8', errors='replace')
  49. time_stamp = utils.read_u64(log_buf[pos:pos + 8])
  50. for line in text.splitlines():
  51. msg = u"[{time:12.6f}] {line}\n".format(
  52. time=time_stamp / 1000000000.0,
  53. line=line)
  54. # With python2 gdb.write will attempt to convert unicode to
  55. # ascii and might fail so pass an utf8-encoded str instead.
  56. if sys.hexversion < 0x03000000:
  57. msg = msg.encode(encoding='utf8', errors='replace')
  58. gdb.write(msg)
  59. pos += length
  60. LxDmesg()