1 # 1 # 2 # gdb helper commands and functions for Linux 2 # gdb helper commands and functions for Linux kernel debugging 3 # 3 # 4 # kernel log buffer dump 4 # kernel log buffer dump 5 # 5 # 6 # Copyright (c) Siemens AG, 2011, 2012 6 # Copyright (c) Siemens AG, 2011, 2012 7 # 7 # 8 # Authors: 8 # Authors: 9 # Jan Kiszka <jan.kiszka@siemens.com> 9 # Jan Kiszka <jan.kiszka@siemens.com> 10 # 10 # 11 # This work is licensed under the terms of the 11 # This work is licensed under the terms of the GNU GPL version 2. 12 # 12 # 13 13 14 import gdb 14 import gdb 15 import sys 15 import sys 16 16 17 from linux import utils 17 from linux import utils 18 18 19 printk_info_type = utils.CachedType("struct pr 19 printk_info_type = utils.CachedType("struct printk_info") 20 prb_data_blk_lpos_type = utils.CachedType("str 20 prb_data_blk_lpos_type = utils.CachedType("struct prb_data_blk_lpos") 21 prb_desc_type = utils.CachedType("struct prb_d 21 prb_desc_type = utils.CachedType("struct prb_desc") 22 prb_desc_ring_type = utils.CachedType("struct 22 prb_desc_ring_type = utils.CachedType("struct prb_desc_ring") 23 prb_data_ring_type = utils.CachedType("struct 23 prb_data_ring_type = utils.CachedType("struct prb_data_ring") 24 printk_ringbuffer_type = utils.CachedType("str 24 printk_ringbuffer_type = utils.CachedType("struct printk_ringbuffer") >> 25 atomic_long_type = utils.CachedType("atomic_long_t") 25 26 26 class LxDmesg(gdb.Command): 27 class LxDmesg(gdb.Command): 27 """Print Linux kernel log buffer.""" 28 """Print Linux kernel log buffer.""" 28 29 29 def __init__(self): 30 def __init__(self): 30 super(LxDmesg, self).__init__("lx-dmes 31 super(LxDmesg, self).__init__("lx-dmesg", gdb.COMMAND_DATA) 31 32 32 def invoke(self, arg, from_tty): 33 def invoke(self, arg, from_tty): 33 inf = gdb.inferiors()[0] 34 inf = gdb.inferiors()[0] 34 35 35 # read in prb structure 36 # read in prb structure 36 prb_addr = int(str(gdb.parse_and_eval( 37 prb_addr = int(str(gdb.parse_and_eval("(void *)'printk.c'::prb")).split()[0], 16) 37 sz = printk_ringbuffer_type.get_type() 38 sz = printk_ringbuffer_type.get_type().sizeof 38 prb = utils.read_memoryview(inf, prb_a 39 prb = utils.read_memoryview(inf, prb_addr, sz).tobytes() 39 40 40 # read in descriptor ring structure 41 # read in descriptor ring structure 41 off = printk_ringbuffer_type.get_type( 42 off = printk_ringbuffer_type.get_type()['desc_ring'].bitpos // 8 42 addr = prb_addr + off 43 addr = prb_addr + off 43 sz = prb_desc_ring_type.get_type().siz 44 sz = prb_desc_ring_type.get_type().sizeof 44 desc_ring = utils.read_memoryview(inf, 45 desc_ring = utils.read_memoryview(inf, addr, sz).tobytes() 45 46 46 # read in descriptor count, size, and !! 47 # read in descriptor array 47 off = prb_desc_ring_type.get_type()['c 48 off = prb_desc_ring_type.get_type()['count_bits'].bitpos // 8 48 desc_ring_count = 1 << utils.read_u32( 49 desc_ring_count = 1 << utils.read_u32(desc_ring, off) 49 desc_sz = prb_desc_type.get_type().siz 50 desc_sz = prb_desc_type.get_type().sizeof 50 off = prb_desc_ring_type.get_type()['d 51 off = prb_desc_ring_type.get_type()['descs'].bitpos // 8 51 desc_addr = utils.read_ulong(desc_ring !! 52 addr = utils.read_ulong(desc_ring, off) >> 53 descs = utils.read_memoryview(inf, addr, desc_sz * desc_ring_count).tobytes() 52 54 53 # read in info size and address !! 55 # read in info array 54 info_sz = printk_info_type.get_type(). 56 info_sz = printk_info_type.get_type().sizeof 55 off = prb_desc_ring_type.get_type()['i 57 off = prb_desc_ring_type.get_type()['infos'].bitpos // 8 56 info_addr = utils.read_ulong(desc_ring !! 58 addr = utils.read_ulong(desc_ring, off) >> 59 infos = utils.read_memoryview(inf, addr, info_sz * desc_ring_count).tobytes() 57 60 58 # read in text data ring structure 61 # read in text data ring structure 59 off = printk_ringbuffer_type.get_type( 62 off = printk_ringbuffer_type.get_type()['text_data_ring'].bitpos // 8 60 addr = prb_addr + off 63 addr = prb_addr + off 61 sz = prb_data_ring_type.get_type().siz 64 sz = prb_data_ring_type.get_type().sizeof 62 text_data_ring = utils.read_memoryview 65 text_data_ring = utils.read_memoryview(inf, addr, sz).tobytes() 63 66 64 # read in text data size and address !! 67 # read in text data 65 off = prb_data_ring_type.get_type()['s 68 off = prb_data_ring_type.get_type()['size_bits'].bitpos // 8 66 text_data_sz = 1 << utils.read_u32(tex 69 text_data_sz = 1 << utils.read_u32(text_data_ring, off) 67 off = prb_data_ring_type.get_type()['d 70 off = prb_data_ring_type.get_type()['data'].bitpos // 8 68 text_data_addr = utils.read_ulong(text !! 71 addr = utils.read_ulong(text_data_ring, off) >> 72 text_data = utils.read_memoryview(inf, addr, text_data_sz).tobytes() >> 73 >> 74 counter_off = atomic_long_type.get_type()['counter'].bitpos // 8 69 75 70 sv_off = prb_desc_type.get_type()['sta 76 sv_off = prb_desc_type.get_type()['state_var'].bitpos // 8 71 77 72 off = prb_desc_type.get_type()['text_b 78 off = prb_desc_type.get_type()['text_blk_lpos'].bitpos // 8 73 begin_off = off + (prb_data_blk_lpos_t 79 begin_off = off + (prb_data_blk_lpos_type.get_type()['begin'].bitpos // 8) 74 next_off = off + (prb_data_blk_lpos_ty 80 next_off = off + (prb_data_blk_lpos_type.get_type()['next'].bitpos // 8) 75 81 76 ts_off = printk_info_type.get_type()[' 82 ts_off = printk_info_type.get_type()['ts_nsec'].bitpos // 8 77 len_off = printk_info_type.get_type()[ 83 len_off = printk_info_type.get_type()['text_len'].bitpos // 8 78 84 79 # definitions from kernel/printk/print 85 # definitions from kernel/printk/printk_ringbuffer.h 80 desc_committed = 1 86 desc_committed = 1 81 desc_finalized = 2 87 desc_finalized = 2 82 desc_sv_bits = utils.get_long_type().s 88 desc_sv_bits = utils.get_long_type().sizeof * 8 83 desc_flags_shift = desc_sv_bits - 2 89 desc_flags_shift = desc_sv_bits - 2 84 desc_flags_mask = 3 << desc_flags_shif 90 desc_flags_mask = 3 << desc_flags_shift 85 desc_id_mask = ~desc_flags_mask 91 desc_id_mask = ~desc_flags_mask 86 92 87 # read in tail and head descriptor ids 93 # read in tail and head descriptor ids 88 off = prb_desc_ring_type.get_type()['t 94 off = prb_desc_ring_type.get_type()['tail_id'].bitpos // 8 89 tail_id = utils.read_atomic_long(desc_ !! 95 tail_id = utils.read_u64(desc_ring, off + counter_off) 90 off = prb_desc_ring_type.get_type()['h 96 off = prb_desc_ring_type.get_type()['head_id'].bitpos // 8 91 head_id = utils.read_atomic_long(desc_ !! 97 head_id = utils.read_u64(desc_ring, off + counter_off) 92 98 93 did = tail_id 99 did = tail_id 94 while True: 100 while True: 95 ind = did % desc_ring_count 101 ind = did % desc_ring_count 96 desc_off = desc_sz * ind 102 desc_off = desc_sz * ind 97 info_off = info_sz * ind 103 info_off = info_sz * ind 98 104 99 desc = utils.read_memoryview(inf, << 100 << 101 # skip non-committed record 105 # skip non-committed record 102 state = 3 & (utils.read_atomic_lon !! 106 state = 3 & (utils.read_u64(descs, desc_off + sv_off + >> 107 counter_off) >> desc_flags_shift) 103 if state != desc_committed and sta 108 if state != desc_committed and state != desc_finalized: 104 if did == head_id: 109 if did == head_id: 105 break 110 break 106 did = (did + 1) & desc_id_mask 111 did = (did + 1) & desc_id_mask 107 continue 112 continue 108 113 109 begin = utils.read_ulong(desc, beg !! 114 begin = utils.read_ulong(descs, desc_off + begin_off) % text_data_sz 110 end = utils.read_ulong(desc, next_ !! 115 end = utils.read_ulong(descs, desc_off + next_off) % text_data_sz 111 << 112 info = utils.read_memoryview(inf, << 113 116 114 # handle data-less record 117 # handle data-less record 115 if begin & 1 == 1: 118 if begin & 1 == 1: 116 text = "" 119 text = "" 117 else: 120 else: 118 # handle wrapping data block 121 # handle wrapping data block 119 if begin > end: 122 if begin > end: 120 begin = 0 123 begin = 0 121 124 122 # skip over descriptor id 125 # skip over descriptor id 123 text_start = begin + utils.get 126 text_start = begin + utils.get_long_type().sizeof 124 127 125 text_len = utils.read_u16(info !! 128 text_len = utils.read_u16(infos, info_off + len_off) 126 129 127 # handle truncated message 130 # handle truncated message 128 if end - text_start < text_len 131 if end - text_start < text_len: 129 text_len = end - text_star 132 text_len = end - text_start 130 133 131 text_data = utils.read_memoryv !! 134 text = text_data[text_start:text_start + text_len].decode( 132 !! 135 encoding='utf8', errors='replace') 133 text = text_data[0:text_len].d << 134 136 135 time_stamp = utils.read_u64(info, !! 137 time_stamp = utils.read_u64(infos, info_off + ts_off) 136 138 137 for line in text.splitlines(): 139 for line in text.splitlines(): 138 msg = u"[{time:12.6f}] {line}\ 140 msg = u"[{time:12.6f}] {line}\n".format( 139 time=time_stamp / 10000000 141 time=time_stamp / 1000000000.0, 140 line=line) 142 line=line) 141 # With python2 gdb.write will 143 # With python2 gdb.write will attempt to convert unicode to 142 # ascii and might fail so pass 144 # ascii and might fail so pass an utf8-encoded str instead. 143 if sys.hexversion < 0x03000000 145 if sys.hexversion < 0x03000000: 144 msg = msg.encode(encoding= 146 msg = msg.encode(encoding='utf8', errors='replace') 145 gdb.write(msg) 147 gdb.write(msg) 146 148 147 if did == head_id: 149 if did == head_id: 148 break 150 break 149 did = (did + 1) & desc_id_mask 151 did = (did + 1) & desc_id_mask 150 152 151 153 152 LxDmesg() 154 LxDmesg()
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.