dmesg.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <[email protected]>
  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. printk_info_type = utils.CachedType("struct printk_info")
  17. prb_data_blk_lpos_type = utils.CachedType("struct prb_data_blk_lpos")
  18. prb_desc_type = utils.CachedType("struct prb_desc")
  19. prb_desc_ring_type = utils.CachedType("struct prb_desc_ring")
  20. prb_data_ring_type = utils.CachedType("struct prb_data_ring")
  21. printk_ringbuffer_type = utils.CachedType("struct printk_ringbuffer")
  22. class LxDmesg(gdb.Command):
  23. """Print Linux kernel log buffer."""
  24. def __init__(self):
  25. super(LxDmesg, self).__init__("lx-dmesg", gdb.COMMAND_DATA)
  26. def invoke(self, arg, from_tty):
  27. inf = gdb.inferiors()[0]
  28. # read in prb structure
  29. prb_addr = int(str(gdb.parse_and_eval("(void *)'printk.c'::prb")).split()[0], 16)
  30. sz = printk_ringbuffer_type.get_type().sizeof
  31. prb = utils.read_memoryview(inf, prb_addr, sz).tobytes()
  32. # read in descriptor ring structure
  33. off = printk_ringbuffer_type.get_type()['desc_ring'].bitpos // 8
  34. addr = prb_addr + off
  35. sz = prb_desc_ring_type.get_type().sizeof
  36. desc_ring = utils.read_memoryview(inf, addr, sz).tobytes()
  37. # read in descriptor count, size, and address
  38. off = prb_desc_ring_type.get_type()['count_bits'].bitpos // 8
  39. desc_ring_count = 1 << utils.read_u32(desc_ring, off)
  40. desc_sz = prb_desc_type.get_type().sizeof
  41. off = prb_desc_ring_type.get_type()['descs'].bitpos // 8
  42. desc_addr = utils.read_ulong(desc_ring, off)
  43. # read in info size and address
  44. info_sz = printk_info_type.get_type().sizeof
  45. off = prb_desc_ring_type.get_type()['infos'].bitpos // 8
  46. info_addr = utils.read_ulong(desc_ring, off)
  47. # read in text data ring structure
  48. off = printk_ringbuffer_type.get_type()['text_data_ring'].bitpos // 8
  49. addr = prb_addr + off
  50. sz = prb_data_ring_type.get_type().sizeof
  51. text_data_ring = utils.read_memoryview(inf, addr, sz).tobytes()
  52. # read in text data size and address
  53. off = prb_data_ring_type.get_type()['size_bits'].bitpos // 8
  54. text_data_sz = 1 << utils.read_u32(text_data_ring, off)
  55. off = prb_data_ring_type.get_type()['data'].bitpos // 8
  56. text_data_addr = utils.read_ulong(text_data_ring, off)
  57. sv_off = prb_desc_type.get_type()['state_var'].bitpos // 8
  58. off = prb_desc_type.get_type()['text_blk_lpos'].bitpos // 8
  59. begin_off = off + (prb_data_blk_lpos_type.get_type()['begin'].bitpos // 8)
  60. next_off = off + (prb_data_blk_lpos_type.get_type()['next'].bitpos // 8)
  61. ts_off = printk_info_type.get_type()['ts_nsec'].bitpos // 8
  62. len_off = printk_info_type.get_type()['text_len'].bitpos // 8
  63. # definitions from kernel/printk/printk_ringbuffer.h
  64. desc_committed = 1
  65. desc_finalized = 2
  66. desc_sv_bits = utils.get_long_type().sizeof * 8
  67. desc_flags_shift = desc_sv_bits - 2
  68. desc_flags_mask = 3 << desc_flags_shift
  69. desc_id_mask = ~desc_flags_mask
  70. # read in tail and head descriptor ids
  71. off = prb_desc_ring_type.get_type()['tail_id'].bitpos // 8
  72. tail_id = utils.read_atomic_long(desc_ring, off)
  73. off = prb_desc_ring_type.get_type()['head_id'].bitpos // 8
  74. head_id = utils.read_atomic_long(desc_ring, off)
  75. did = tail_id
  76. while True:
  77. ind = did % desc_ring_count
  78. desc_off = desc_sz * ind
  79. info_off = info_sz * ind
  80. desc = utils.read_memoryview(inf, desc_addr + desc_off, desc_sz).tobytes()
  81. # skip non-committed record
  82. state = 3 & (utils.read_atomic_long(desc, sv_off) >> desc_flags_shift)
  83. if state != desc_committed and state != desc_finalized:
  84. if did == head_id:
  85. break
  86. did = (did + 1) & desc_id_mask
  87. continue
  88. begin = utils.read_ulong(desc, begin_off) % text_data_sz
  89. end = utils.read_ulong(desc, next_off) % text_data_sz
  90. info = utils.read_memoryview(inf, info_addr + info_off, info_sz).tobytes()
  91. # handle data-less record
  92. if begin & 1 == 1:
  93. text = ""
  94. else:
  95. # handle wrapping data block
  96. if begin > end:
  97. begin = 0
  98. # skip over descriptor id
  99. text_start = begin + utils.get_long_type().sizeof
  100. text_len = utils.read_u16(info, len_off)
  101. # handle truncated message
  102. if end - text_start < text_len:
  103. text_len = end - text_start
  104. text_data = utils.read_memoryview(inf, text_data_addr + text_start,
  105. text_len).tobytes()
  106. text = text_data[0:text_len].decode(encoding='utf8', errors='replace')
  107. time_stamp = utils.read_u64(info, ts_off)
  108. for line in text.splitlines():
  109. msg = u"[{time:12.6f}] {line}\n".format(
  110. time=time_stamp / 1000000000.0,
  111. line=line)
  112. # With python2 gdb.write will attempt to convert unicode to
  113. # ascii and might fail so pass an utf8-encoded str instead.
  114. if sys.hexversion < 0x03000000:
  115. msg = msg.encode(encoding='utf8', errors='replace')
  116. gdb.write(msg)
  117. if did == head_id:
  118. break
  119. did = (did + 1) & desc_id_mask
  120. LxDmesg()