Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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