| .. | .. |
|---|
| 16 | 16 | |
|---|
| 17 | 17 | from linux import utils |
|---|
| 18 | 18 | |
|---|
| 19 | +printk_info_type = utils.CachedType("struct printk_info") |
|---|
| 20 | +prb_data_blk_lpos_type = utils.CachedType("struct prb_data_blk_lpos") |
|---|
| 21 | +prb_desc_type = utils.CachedType("struct prb_desc") |
|---|
| 22 | +prb_desc_ring_type = utils.CachedType("struct prb_desc_ring") |
|---|
| 23 | +prb_data_ring_type = utils.CachedType("struct prb_data_ring") |
|---|
| 24 | +printk_ringbuffer_type = utils.CachedType("struct printk_ringbuffer") |
|---|
| 25 | +atomic_long_type = utils.CachedType("atomic_long_t") |
|---|
| 19 | 26 | |
|---|
| 20 | 27 | class LxDmesg(gdb.Command): |
|---|
| 21 | 28 | """Print Linux kernel log buffer.""" |
|---|
| .. | .. |
|---|
| 24 | 31 | super(LxDmesg, self).__init__("lx-dmesg", gdb.COMMAND_DATA) |
|---|
| 25 | 32 | |
|---|
| 26 | 33 | def invoke(self, arg, from_tty): |
|---|
| 27 | | - log_buf_addr = int(str(gdb.parse_and_eval( |
|---|
| 28 | | - "(void *)'printk.c'::log_buf")).split()[0], 16) |
|---|
| 29 | | - log_first_idx = int(gdb.parse_and_eval("'printk.c'::log_first_idx")) |
|---|
| 30 | | - log_next_idx = int(gdb.parse_and_eval("'printk.c'::log_next_idx")) |
|---|
| 31 | | - log_buf_len = int(gdb.parse_and_eval("'printk.c'::log_buf_len")) |
|---|
| 32 | | - |
|---|
| 33 | 34 | inf = gdb.inferiors()[0] |
|---|
| 34 | | - start = log_buf_addr + log_first_idx |
|---|
| 35 | | - if log_first_idx < log_next_idx: |
|---|
| 36 | | - log_buf_2nd_half = -1 |
|---|
| 37 | | - length = log_next_idx - log_first_idx |
|---|
| 38 | | - log_buf = utils.read_memoryview(inf, start, length).tobytes() |
|---|
| 39 | | - else: |
|---|
| 40 | | - log_buf_2nd_half = log_buf_len - log_first_idx |
|---|
| 41 | | - a = utils.read_memoryview(inf, start, log_buf_2nd_half) |
|---|
| 42 | | - b = utils.read_memoryview(inf, log_buf_addr, log_next_idx) |
|---|
| 43 | | - log_buf = a.tobytes() + b.tobytes() |
|---|
| 44 | 35 | |
|---|
| 45 | | - pos = 0 |
|---|
| 46 | | - while pos < log_buf.__len__(): |
|---|
| 47 | | - length = utils.read_u16(log_buf[pos + 8:pos + 10]) |
|---|
| 48 | | - if length == 0: |
|---|
| 49 | | - if log_buf_2nd_half == -1: |
|---|
| 50 | | - gdb.write("Corrupted log buffer!\n") |
|---|
| 36 | + # read in prb structure |
|---|
| 37 | + prb_addr = int(str(gdb.parse_and_eval("(void *)'printk.c'::prb")).split()[0], 16) |
|---|
| 38 | + sz = printk_ringbuffer_type.get_type().sizeof |
|---|
| 39 | + prb = utils.read_memoryview(inf, prb_addr, sz).tobytes() |
|---|
| 40 | + |
|---|
| 41 | + # read in descriptor ring structure |
|---|
| 42 | + off = printk_ringbuffer_type.get_type()['desc_ring'].bitpos // 8 |
|---|
| 43 | + addr = prb_addr + off |
|---|
| 44 | + sz = prb_desc_ring_type.get_type().sizeof |
|---|
| 45 | + desc_ring = utils.read_memoryview(inf, addr, sz).tobytes() |
|---|
| 46 | + |
|---|
| 47 | + # read in descriptor array |
|---|
| 48 | + off = prb_desc_ring_type.get_type()['count_bits'].bitpos // 8 |
|---|
| 49 | + desc_ring_count = 1 << utils.read_u32(desc_ring, off) |
|---|
| 50 | + desc_sz = prb_desc_type.get_type().sizeof |
|---|
| 51 | + off = prb_desc_ring_type.get_type()['descs'].bitpos // 8 |
|---|
| 52 | + addr = utils.read_ulong(desc_ring, off) |
|---|
| 53 | + descs = utils.read_memoryview(inf, addr, desc_sz * desc_ring_count).tobytes() |
|---|
| 54 | + |
|---|
| 55 | + # read in info array |
|---|
| 56 | + info_sz = printk_info_type.get_type().sizeof |
|---|
| 57 | + off = prb_desc_ring_type.get_type()['infos'].bitpos // 8 |
|---|
| 58 | + addr = utils.read_ulong(desc_ring, off) |
|---|
| 59 | + infos = utils.read_memoryview(inf, addr, info_sz * desc_ring_count).tobytes() |
|---|
| 60 | + |
|---|
| 61 | + # read in text data ring structure |
|---|
| 62 | + off = printk_ringbuffer_type.get_type()['text_data_ring'].bitpos // 8 |
|---|
| 63 | + addr = prb_addr + off |
|---|
| 64 | + sz = prb_data_ring_type.get_type().sizeof |
|---|
| 65 | + text_data_ring = utils.read_memoryview(inf, addr, sz).tobytes() |
|---|
| 66 | + |
|---|
| 67 | + # read in text data |
|---|
| 68 | + off = prb_data_ring_type.get_type()['size_bits'].bitpos // 8 |
|---|
| 69 | + text_data_sz = 1 << utils.read_u32(text_data_ring, off) |
|---|
| 70 | + off = prb_data_ring_type.get_type()['data'].bitpos // 8 |
|---|
| 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 |
|---|
| 75 | + |
|---|
| 76 | + sv_off = prb_desc_type.get_type()['state_var'].bitpos // 8 |
|---|
| 77 | + |
|---|
| 78 | + off = prb_desc_type.get_type()['text_blk_lpos'].bitpos // 8 |
|---|
| 79 | + begin_off = off + (prb_data_blk_lpos_type.get_type()['begin'].bitpos // 8) |
|---|
| 80 | + next_off = off + (prb_data_blk_lpos_type.get_type()['next'].bitpos // 8) |
|---|
| 81 | + |
|---|
| 82 | + ts_off = printk_info_type.get_type()['ts_nsec'].bitpos // 8 |
|---|
| 83 | + len_off = printk_info_type.get_type()['text_len'].bitpos // 8 |
|---|
| 84 | + |
|---|
| 85 | + # definitions from kernel/printk/printk_ringbuffer.h |
|---|
| 86 | + desc_committed = 1 |
|---|
| 87 | + desc_finalized = 2 |
|---|
| 88 | + desc_sv_bits = utils.get_long_type().sizeof * 8 |
|---|
| 89 | + desc_flags_shift = desc_sv_bits - 2 |
|---|
| 90 | + desc_flags_mask = 3 << desc_flags_shift |
|---|
| 91 | + desc_id_mask = ~desc_flags_mask |
|---|
| 92 | + |
|---|
| 93 | + # read in tail and head descriptor ids |
|---|
| 94 | + off = prb_desc_ring_type.get_type()['tail_id'].bitpos // 8 |
|---|
| 95 | + tail_id = utils.read_u64(desc_ring, off + counter_off) |
|---|
| 96 | + off = prb_desc_ring_type.get_type()['head_id'].bitpos // 8 |
|---|
| 97 | + head_id = utils.read_u64(desc_ring, off + counter_off) |
|---|
| 98 | + |
|---|
| 99 | + did = tail_id |
|---|
| 100 | + while True: |
|---|
| 101 | + ind = did % desc_ring_count |
|---|
| 102 | + desc_off = desc_sz * ind |
|---|
| 103 | + info_off = info_sz * ind |
|---|
| 104 | + |
|---|
| 105 | + # skip non-committed record |
|---|
| 106 | + state = 3 & (utils.read_u64(descs, desc_off + sv_off + |
|---|
| 107 | + counter_off) >> desc_flags_shift) |
|---|
| 108 | + if state != desc_committed and state != desc_finalized: |
|---|
| 109 | + if did == head_id: |
|---|
| 51 | 110 | break |
|---|
| 52 | | - pos = log_buf_2nd_half |
|---|
| 111 | + did = (did + 1) & desc_id_mask |
|---|
| 53 | 112 | continue |
|---|
| 54 | 113 | |
|---|
| 55 | | - text_len = utils.read_u16(log_buf[pos + 10:pos + 12]) |
|---|
| 56 | | - text = log_buf[pos + 16:pos + 16 + text_len].decode( |
|---|
| 57 | | - encoding='utf8', errors='replace') |
|---|
| 58 | | - time_stamp = utils.read_u64(log_buf[pos:pos + 8]) |
|---|
| 114 | + begin = utils.read_ulong(descs, desc_off + begin_off) % text_data_sz |
|---|
| 115 | + end = utils.read_ulong(descs, desc_off + next_off) % text_data_sz |
|---|
| 116 | + |
|---|
| 117 | + # handle data-less record |
|---|
| 118 | + if begin & 1 == 1: |
|---|
| 119 | + text = "" |
|---|
| 120 | + else: |
|---|
| 121 | + # handle wrapping data block |
|---|
| 122 | + if begin > end: |
|---|
| 123 | + begin = 0 |
|---|
| 124 | + |
|---|
| 125 | + # skip over descriptor id |
|---|
| 126 | + text_start = begin + utils.get_long_type().sizeof |
|---|
| 127 | + |
|---|
| 128 | + text_len = utils.read_u16(infos, info_off + len_off) |
|---|
| 129 | + |
|---|
| 130 | + # handle truncated message |
|---|
| 131 | + if end - text_start < text_len: |
|---|
| 132 | + text_len = end - text_start |
|---|
| 133 | + |
|---|
| 134 | + text = text_data[text_start:text_start + text_len].decode( |
|---|
| 135 | + encoding='utf8', errors='replace') |
|---|
| 136 | + |
|---|
| 137 | + time_stamp = utils.read_u64(infos, info_off + ts_off) |
|---|
| 59 | 138 | |
|---|
| 60 | 139 | for line in text.splitlines(): |
|---|
| 61 | 140 | msg = u"[{time:12.6f}] {line}\n".format( |
|---|
| .. | .. |
|---|
| 67 | 146 | msg = msg.encode(encoding='utf8', errors='replace') |
|---|
| 68 | 147 | gdb.write(msg) |
|---|
| 69 | 148 | |
|---|
| 70 | | - pos += length |
|---|
| 149 | + if did == head_id: |
|---|
| 150 | + break |
|---|
| 151 | + did = (did + 1) & desc_id_mask |
|---|
| 71 | 152 | |
|---|
| 72 | 153 | |
|---|
| 73 | 154 | LxDmesg() |
|---|