Back to home page

OSCL-LXR

 
 

    


0001 #
0002 # gdb helper commands and functions for Linux kernel debugging
0003 #
0004 #  kernel log buffer dump
0005 #
0006 # Copyright (c) Siemens AG, 2011, 2012
0007 #
0008 # Authors:
0009 #  Jan Kiszka <jan.kiszka@siemens.com>
0010 #
0011 # This work is licensed under the terms of the GNU GPL version 2.
0012 #
0013 
0014 import gdb
0015 import sys
0016 
0017 from linux import utils
0018 
0019 printk_info_type = utils.CachedType("struct printk_info")
0020 prb_data_blk_lpos_type = utils.CachedType("struct prb_data_blk_lpos")
0021 prb_desc_type = utils.CachedType("struct prb_desc")
0022 prb_desc_ring_type = utils.CachedType("struct prb_desc_ring")
0023 prb_data_ring_type = utils.CachedType("struct prb_data_ring")
0024 printk_ringbuffer_type = utils.CachedType("struct printk_ringbuffer")
0025 
0026 class LxDmesg(gdb.Command):
0027     """Print Linux kernel log buffer."""
0028 
0029     def __init__(self):
0030         super(LxDmesg, self).__init__("lx-dmesg", gdb.COMMAND_DATA)
0031 
0032     def invoke(self, arg, from_tty):
0033         inf = gdb.inferiors()[0]
0034 
0035         # read in prb structure
0036         prb_addr = int(str(gdb.parse_and_eval("(void *)'printk.c'::prb")).split()[0], 16)
0037         sz = printk_ringbuffer_type.get_type().sizeof
0038         prb = utils.read_memoryview(inf, prb_addr, sz).tobytes()
0039 
0040         # read in descriptor ring structure
0041         off = printk_ringbuffer_type.get_type()['desc_ring'].bitpos // 8
0042         addr = prb_addr + off
0043         sz = prb_desc_ring_type.get_type().sizeof
0044         desc_ring = utils.read_memoryview(inf, addr, sz).tobytes()
0045 
0046         # read in descriptor count, size, and address
0047         off = prb_desc_ring_type.get_type()['count_bits'].bitpos // 8
0048         desc_ring_count = 1 << utils.read_u32(desc_ring, off)
0049         desc_sz = prb_desc_type.get_type().sizeof
0050         off = prb_desc_ring_type.get_type()['descs'].bitpos // 8
0051         desc_addr = utils.read_ulong(desc_ring, off)
0052 
0053         # read in info size and address
0054         info_sz = printk_info_type.get_type().sizeof
0055         off = prb_desc_ring_type.get_type()['infos'].bitpos // 8
0056         info_addr = utils.read_ulong(desc_ring, off)
0057 
0058         # read in text data ring structure
0059         off = printk_ringbuffer_type.get_type()['text_data_ring'].bitpos // 8
0060         addr = prb_addr + off
0061         sz = prb_data_ring_type.get_type().sizeof
0062         text_data_ring = utils.read_memoryview(inf, addr, sz).tobytes()
0063 
0064         # read in text data size and address
0065         off = prb_data_ring_type.get_type()['size_bits'].bitpos // 8
0066         text_data_sz = 1 << utils.read_u32(text_data_ring, off)
0067         off = prb_data_ring_type.get_type()['data'].bitpos // 8
0068         text_data_addr = utils.read_ulong(text_data_ring, off)
0069 
0070         sv_off = prb_desc_type.get_type()['state_var'].bitpos // 8
0071 
0072         off = prb_desc_type.get_type()['text_blk_lpos'].bitpos // 8
0073         begin_off = off + (prb_data_blk_lpos_type.get_type()['begin'].bitpos // 8)
0074         next_off = off + (prb_data_blk_lpos_type.get_type()['next'].bitpos // 8)
0075 
0076         ts_off = printk_info_type.get_type()['ts_nsec'].bitpos // 8
0077         len_off = printk_info_type.get_type()['text_len'].bitpos // 8
0078 
0079         # definitions from kernel/printk/printk_ringbuffer.h
0080         desc_committed = 1
0081         desc_finalized = 2
0082         desc_sv_bits = utils.get_long_type().sizeof * 8
0083         desc_flags_shift = desc_sv_bits - 2
0084         desc_flags_mask = 3 << desc_flags_shift
0085         desc_id_mask = ~desc_flags_mask
0086 
0087         # read in tail and head descriptor ids
0088         off = prb_desc_ring_type.get_type()['tail_id'].bitpos // 8
0089         tail_id = utils.read_atomic_long(desc_ring, off)
0090         off = prb_desc_ring_type.get_type()['head_id'].bitpos // 8
0091         head_id = utils.read_atomic_long(desc_ring, off)
0092 
0093         did = tail_id
0094         while True:
0095             ind = did % desc_ring_count
0096             desc_off = desc_sz * ind
0097             info_off = info_sz * ind
0098 
0099             desc = utils.read_memoryview(inf, desc_addr + desc_off, desc_sz).tobytes()
0100 
0101             # skip non-committed record
0102             state = 3 & (utils.read_atomic_long(desc, sv_off) >> desc_flags_shift)
0103             if state != desc_committed and state != desc_finalized:
0104                 if did == head_id:
0105                     break
0106                 did = (did + 1) & desc_id_mask
0107                 continue
0108 
0109             begin = utils.read_ulong(desc, begin_off) % text_data_sz
0110             end = utils.read_ulong(desc, next_off) % text_data_sz
0111 
0112             info = utils.read_memoryview(inf, info_addr + info_off, info_sz).tobytes()
0113 
0114             # handle data-less record
0115             if begin & 1 == 1:
0116                 text = ""
0117             else:
0118                 # handle wrapping data block
0119                 if begin > end:
0120                     begin = 0
0121 
0122                 # skip over descriptor id
0123                 text_start = begin + utils.get_long_type().sizeof
0124 
0125                 text_len = utils.read_u16(info, len_off)
0126 
0127                 # handle truncated message
0128                 if end - text_start < text_len:
0129                     text_len = end - text_start
0130 
0131                 text_data = utils.read_memoryview(inf, text_data_addr + text_start,
0132                                                   text_len).tobytes()
0133                 text = text_data[0:text_len].decode(encoding='utf8', errors='replace')
0134 
0135             time_stamp = utils.read_u64(info, ts_off)
0136 
0137             for line in text.splitlines():
0138                 msg = u"[{time:12.6f}] {line}\n".format(
0139                     time=time_stamp / 1000000000.0,
0140                     line=line)
0141                 # With python2 gdb.write will attempt to convert unicode to
0142                 # ascii and might fail so pass an utf8-encoded str instead.
0143                 if sys.hexversion < 0x03000000:
0144                     msg = msg.encode(encoding='utf8', errors='replace')
0145                 gdb.write(msg)
0146 
0147             if did == head_id:
0148                 break
0149             did = (did + 1) & desc_id_mask
0150 
0151 
0152 LxDmesg()