blob: bce14de5f6109c9c0da84aff7ec2223f67512351 [file] [log] [blame]
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -08001#
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 <jan.kiszka@siemens.com>
10#
11# This work is licensed under the terms of the GNU GPL version 2.
12#
13
14import gdb
Leonard Crestez46d10a02017-07-12 14:34:19 -070015import sys
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -080016
17from linux import utils
18
John Ognesse6076832020-08-14 23:31:25 +020619printk_info_type = utils.CachedType("struct printk_info")
20prb_data_blk_lpos_type = utils.CachedType("struct prb_data_blk_lpos")
21prb_desc_type = utils.CachedType("struct prb_desc")
22prb_desc_ring_type = utils.CachedType("struct prb_desc_ring")
23prb_data_ring_type = utils.CachedType("struct prb_data_ring")
24printk_ringbuffer_type = utils.CachedType("struct printk_ringbuffer")
25atomic_long_type = utils.CachedType("atomic_long_t")
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -080026
27class LxDmesg(gdb.Command):
28 """Print Linux kernel log buffer."""
29
30 def __init__(self):
31 super(LxDmesg, self).__init__("lx-dmesg", gdb.COMMAND_DATA)
32
33 def invoke(self, arg, from_tty):
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -080034 inf = gdb.inferiors()[0]
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -080035
John Ognesse6076832020-08-14 23:31:25 +020636 # 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()
Joel Colledgeca210ba2019-10-18 20:19:26 -070040
John Ognesse6076832020-08-14 23:31:25 +020641 # 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 text data ring structure
56 off = printk_ringbuffer_type.get_type()['text_data_ring'].bitpos // 8
57 addr = prb_addr + off
58 sz = prb_data_ring_type.get_type().sizeof
59 text_data_ring = utils.read_memoryview(inf, addr, sz).tobytes()
60
61 # read in text data
62 off = prb_data_ring_type.get_type()['size_bits'].bitpos // 8
63 text_data_sz = 1 << utils.read_u32(text_data_ring, off)
64 off = prb_data_ring_type.get_type()['data'].bitpos // 8
65 addr = utils.read_ulong(text_data_ring, off)
66 text_data = utils.read_memoryview(inf, addr, text_data_sz).tobytes()
67
68 counter_off = atomic_long_type.get_type()['counter'].bitpos // 8
69
70 sv_off = prb_desc_type.get_type()['state_var'].bitpos // 8
71
72 off = prb_desc_type.get_type()['text_blk_lpos'].bitpos // 8
73 begin_off = off + (prb_data_blk_lpos_type.get_type()['begin'].bitpos // 8)
74 next_off = off + (prb_data_blk_lpos_type.get_type()['next'].bitpos // 8)
75
76 off = prb_desc_type.get_type()['info'].bitpos // 8
77 ts_off = off + printk_info_type.get_type()['ts_nsec'].bitpos // 8
78 len_off = off + printk_info_type.get_type()['text_len'].bitpos // 8
79
80 # definitions from kernel/printk/printk_ringbuffer.h
John Ogness10dcb062020-09-14 14:39:52 +020681 desc_committed = 1
John Ogness4cfc7252020-09-14 14:39:53 +020682 desc_finalized = 2
John Ognesse6076832020-08-14 23:31:25 +020683 desc_sv_bits = utils.get_long_type().sizeof * 8
John Ogness10dcb062020-09-14 14:39:52 +020684 desc_flags_shift = desc_sv_bits - 2
85 desc_flags_mask = 3 << desc_flags_shift
John Ognesse6076832020-08-14 23:31:25 +020686 desc_id_mask = ~desc_flags_mask
87
88 # read in tail and head descriptor ids
89 off = prb_desc_ring_type.get_type()['tail_id'].bitpos // 8
90 tail_id = utils.read_u64(desc_ring, off + counter_off)
91 off = prb_desc_ring_type.get_type()['head_id'].bitpos // 8
92 head_id = utils.read_u64(desc_ring, off + counter_off)
93
94 did = tail_id
95 while True:
96 ind = did % desc_ring_count
97 desc_off = desc_sz * ind
98
99 # skip non-committed record
John Ogness10dcb062020-09-14 14:39:52 +0206100 state = 3 & (utils.read_u64(descs, desc_off + sv_off +
101 counter_off) >> desc_flags_shift)
John Ogness4cfc7252020-09-14 14:39:53 +0206102 if state != desc_committed and state != desc_finalized:
John Ognesse6076832020-08-14 23:31:25 +0206103 if did == head_id:
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -0800104 break
John Ognesse6076832020-08-14 23:31:25 +0206105 did = (did + 1) & desc_id_mask
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -0800106 continue
107
John Ognesse6076832020-08-14 23:31:25 +0206108 begin = utils.read_ulong(descs, desc_off + begin_off) % text_data_sz
109 end = utils.read_ulong(descs, desc_off + next_off) % text_data_sz
110
111 # handle data-less record
112 if begin & 1 == 1:
113 text = ""
114 else:
115 # handle wrapping data block
116 if begin > end:
117 begin = 0
118
119 # skip over descriptor id
120 text_start = begin + utils.get_long_type().sizeof
121
122 text_len = utils.read_u16(descs, desc_off + len_off)
123
124 # handle truncated message
125 if end - text_start < text_len:
126 text_len = end - text_start
127
128 text = text_data[text_start:text_start + text_len].decode(
129 encoding='utf8', errors='replace')
130
131 time_stamp = utils.read_u64(descs, desc_off + ts_off)
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -0800132
Kieran Binghamb3b08422016-05-23 16:25:21 -0700133 for line in text.splitlines():
Leonard Crestez46d10a02017-07-12 14:34:19 -0700134 msg = u"[{time:12.6f}] {line}\n".format(
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -0800135 time=time_stamp / 1000000000.0,
Leonard Crestez46d10a02017-07-12 14:34:19 -0700136 line=line)
137 # With python2 gdb.write will attempt to convert unicode to
138 # ascii and might fail so pass an utf8-encoded str instead.
139 if sys.hexversion < 0x03000000:
140 msg = msg.encode(encoding='utf8', errors='replace')
141 gdb.write(msg)
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -0800142
John Ognesse6076832020-08-14 23:31:25 +0206143 if did == head_id:
144 break
145 did = (did + 1) & desc_id_mask
Jan Kiszkaae7dbaa2015-02-17 13:47:04 -0800146
147
148LxDmesg()