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_log_type = utils.CachedType("struct printk_log") 20 prb_data_blk_lpos_type = utils.CachedType("str !! 20 21 prb_desc_type = utils.CachedType("struct prb_d << 22 prb_desc_ring_type = utils.CachedType("struct << 23 prb_data_ring_type = utils.CachedType("struct << 24 printk_ringbuffer_type = utils.CachedType("str << 25 21 26 class LxDmesg(gdb.Command): 22 class LxDmesg(gdb.Command): 27 """Print Linux kernel log buffer.""" 23 """Print Linux kernel log buffer.""" 28 24 29 def __init__(self): 25 def __init__(self): 30 super(LxDmesg, self).__init__("lx-dmes 26 super(LxDmesg, self).__init__("lx-dmesg", gdb.COMMAND_DATA) 31 27 32 def invoke(self, arg, from_tty): 28 def invoke(self, arg, from_tty): 33 inf = gdb.inferiors()[0] !! 29 log_buf_addr = int(str(gdb.parse_and_eval( >> 30 "(void *)'printk.c'::log_buf")).split()[0], 16) >> 31 log_first_idx = int(gdb.parse_and_eval("'printk.c'::log_first_idx")) >> 32 log_next_idx = int(gdb.parse_and_eval("'printk.c'::log_next_idx")) >> 33 log_buf_len = int(gdb.parse_and_eval("'printk.c'::log_buf_len")) 34 34 35 # read in prb structure !! 35 inf = gdb.inferiors()[0] 36 prb_addr = int(str(gdb.parse_and_eval( !! 36 start = log_buf_addr + log_first_idx 37 sz = printk_ringbuffer_type.get_type() !! 37 if log_first_idx < log_next_idx: 38 prb = utils.read_memoryview(inf, prb_a !! 38 log_buf_2nd_half = -1 39 !! 39 length = log_next_idx - log_first_idx 40 # read in descriptor ring structure !! 40 log_buf = utils.read_memoryview(inf, start, length).tobytes() 41 off = printk_ringbuffer_type.get_type( !! 41 else: 42 addr = prb_addr + off !! 42 log_buf_2nd_half = log_buf_len - log_first_idx 43 sz = prb_desc_ring_type.get_type().siz !! 43 a = utils.read_memoryview(inf, start, log_buf_2nd_half) 44 desc_ring = utils.read_memoryview(inf, !! 44 b = utils.read_memoryview(inf, log_buf_addr, log_next_idx) 45 !! 45 log_buf = a.tobytes() + b.tobytes() 46 # read in descriptor count, size, and !! 46 47 off = prb_desc_ring_type.get_type()['c !! 47 length_offset = printk_log_type.get_type()['len'].bitpos // 8 48 desc_ring_count = 1 << utils.read_u32( !! 48 text_len_offset = printk_log_type.get_type()['text_len'].bitpos // 8 49 desc_sz = prb_desc_type.get_type().siz !! 49 time_stamp_offset = printk_log_type.get_type()['ts_nsec'].bitpos // 8 50 off = prb_desc_ring_type.get_type()['d !! 50 text_offset = printk_log_type.get_type().sizeof 51 desc_addr = utils.read_ulong(desc_ring !! 51 52 !! 52 pos = 0 53 # read in info size and address !! 53 while pos < log_buf.__len__(): 54 info_sz = printk_info_type.get_type(). !! 54 length = utils.read_u16(log_buf, pos + length_offset) 55 off = prb_desc_ring_type.get_type()['i !! 55 if length == 0: 56 info_addr = utils.read_ulong(desc_ring !! 56 if log_buf_2nd_half == -1: 57 !! 57 gdb.write("Corrupted log buffer!\n") 58 # read in text data ring structure << 59 off = printk_ringbuffer_type.get_type( << 60 addr = prb_addr + off << 61 sz = prb_data_ring_type.get_type().siz << 62 text_data_ring = utils.read_memoryview << 63 << 64 # read in text data size and address << 65 off = prb_data_ring_type.get_type()['s << 66 text_data_sz = 1 << utils.read_u32(tex << 67 off = prb_data_ring_type.get_type()['d << 68 text_data_addr = utils.read_ulong(text << 69 << 70 sv_off = prb_desc_type.get_type()['sta << 71 << 72 off = prb_desc_type.get_type()['text_b << 73 begin_off = off + (prb_data_blk_lpos_t << 74 next_off = off + (prb_data_blk_lpos_ty << 75 << 76 ts_off = printk_info_type.get_type()[' << 77 len_off = printk_info_type.get_type()[ << 78 << 79 # definitions from kernel/printk/print << 80 desc_committed = 1 << 81 desc_finalized = 2 << 82 desc_sv_bits = utils.get_long_type().s << 83 desc_flags_shift = desc_sv_bits - 2 << 84 desc_flags_mask = 3 << desc_flags_shif << 85 desc_id_mask = ~desc_flags_mask << 86 << 87 # read in tail and head descriptor ids << 88 off = prb_desc_ring_type.get_type()['t << 89 tail_id = utils.read_atomic_long(desc_ << 90 off = prb_desc_ring_type.get_type()['h << 91 head_id = utils.read_atomic_long(desc_ << 92 << 93 did = tail_id << 94 while True: << 95 ind = did % desc_ring_count << 96 desc_off = desc_sz * ind << 97 info_off = info_sz * ind << 98 << 99 desc = utils.read_memoryview(inf, << 100 << 101 # skip non-committed record << 102 state = 3 & (utils.read_atomic_lon << 103 if state != desc_committed and sta << 104 if did == head_id: << 105 break 58 break 106 did = (did + 1) & desc_id_mask !! 59 pos = log_buf_2nd_half 107 continue 60 continue 108 61 109 begin = utils.read_ulong(desc, beg !! 62 text_len = utils.read_u16(log_buf, pos + text_len_offset) 110 end = utils.read_ulong(desc, next_ !! 63 text_start = pos + text_offset 111 !! 64 text = log_buf[text_start:text_start + text_len].decode( 112 info = utils.read_memoryview(inf, !! 65 encoding='utf8', errors='replace') 113 !! 66 time_stamp = utils.read_u64(log_buf, pos + time_stamp_offset) 114 # handle data-less record << 115 if begin & 1 == 1: << 116 text = "" << 117 else: << 118 # handle wrapping data block << 119 if begin > end: << 120 begin = 0 << 121 << 122 # skip over descriptor id << 123 text_start = begin + utils.get << 124 << 125 text_len = utils.read_u16(info << 126 << 127 # handle truncated message << 128 if end - text_start < text_len << 129 text_len = end - text_star << 130 << 131 text_data = utils.read_memoryv << 132 << 133 text = text_data[0:text_len].d << 134 << 135 time_stamp = utils.read_u64(info, << 136 67 137 for line in text.splitlines(): 68 for line in text.splitlines(): 138 msg = u"[{time:12.6f}] {line}\ 69 msg = u"[{time:12.6f}] {line}\n".format( 139 time=time_stamp / 10000000 70 time=time_stamp / 1000000000.0, 140 line=line) 71 line=line) 141 # With python2 gdb.write will 72 # With python2 gdb.write will attempt to convert unicode to 142 # ascii and might fail so pass 73 # ascii and might fail so pass an utf8-encoded str instead. 143 if sys.hexversion < 0x03000000 74 if sys.hexversion < 0x03000000: 144 msg = msg.encode(encoding= 75 msg = msg.encode(encoding='utf8', errors='replace') 145 gdb.write(msg) 76 gdb.write(msg) 146 77 147 if did == head_id: !! 78 pos += length 148 break << 149 did = (did + 1) & desc_id_mask << 150 79 151 80 152 LxDmesg() 81 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.