Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

scripts/gdb: fix 'lx-dmesg' on 32 bits arch

The type atomic_long_t can have size 4 or 8 bytes, depending on
CONFIG_64BIT; it's only content, the field 'counter', is either an
int or a s64 value.

Current code incorrectly uses the fixed size utils.read_u64() to
read the field 'counter' inside atomic_long_t.

On 32 bits architectures reading the last element 'tail_id' of the
struct prb_desc_ring:
struct prb_desc_ring {
...
atomic_long_t tail_id;
};
causes the utils.read_u64() to access outside the boundary of the
struct and the gdb command 'lx-dmesg' exits with error:
Python Exception <class 'IndexError'>: index out of range
Error occurred in Python: index out of range

Query the really used atomic_long_t counter type size.

Link: https://lore.kernel.org/r/20220617143758.137307-1-antonio.borneo@foss.st.com
Fixes: e60768311af8 ("scripts/gdb: update for lockless printk ringbuffer")
Signed-off-by: Antonio Borneo <antonio.borneo@foss.st.com>
[pmladek@suse.com: Query the really used atomic_long_t counter type size]
Tested-by: Antonio Borneo <antonio.borneo@foss.st.com>
Reviewed-by: John Ogness <john.ogness@linutronix.de>
Signed-off-by: Petr Mladek <pmladek@suse.com>
Link: https://lore.kernel.org/r/20220719122831.19890-1-pmladek@suse.com

authored by

Antonio Borneo and committed by
Petr Mladek
e3c8d33e 9a3bfa01

+15 -8
+3 -6
scripts/gdb/linux/dmesg.py
··· 22 22 prb_desc_ring_type = utils.CachedType("struct prb_desc_ring") 23 23 prb_data_ring_type = utils.CachedType("struct prb_data_ring") 24 24 printk_ringbuffer_type = utils.CachedType("struct printk_ringbuffer") 25 - atomic_long_type = utils.CachedType("atomic_long_t") 26 25 27 26 class LxDmesg(gdb.Command): 28 27 """Print Linux kernel log buffer.""" ··· 67 68 off = prb_data_ring_type.get_type()['data'].bitpos // 8 68 69 text_data_addr = utils.read_ulong(text_data_ring, off) 69 70 70 - counter_off = atomic_long_type.get_type()['counter'].bitpos // 8 71 - 72 71 sv_off = prb_desc_type.get_type()['state_var'].bitpos // 8 73 72 74 73 off = prb_desc_type.get_type()['text_blk_lpos'].bitpos // 8 ··· 86 89 87 90 # read in tail and head descriptor ids 88 91 off = prb_desc_ring_type.get_type()['tail_id'].bitpos // 8 89 - tail_id = utils.read_u64(desc_ring, off + counter_off) 92 + tail_id = utils.read_atomic_long(desc_ring, off) 90 93 off = prb_desc_ring_type.get_type()['head_id'].bitpos // 8 91 - head_id = utils.read_u64(desc_ring, off + counter_off) 94 + head_id = utils.read_atomic_long(desc_ring, off) 92 95 93 96 did = tail_id 94 97 while True: ··· 99 102 desc = utils.read_memoryview(inf, desc_addr + desc_off, desc_sz).tobytes() 100 103 101 104 # skip non-committed record 102 - state = 3 & (utils.read_u64(desc, sv_off + counter_off) >> desc_flags_shift) 105 + state = 3 & (utils.read_atomic_long(desc, sv_off) >> desc_flags_shift) 103 106 if state != desc_committed and state != desc_finalized: 104 107 if did == head_id: 105 108 break
+12 -2
scripts/gdb/linux/utils.py
··· 35 35 36 36 37 37 long_type = CachedType("long") 38 - 38 + atomic_long_type = CachedType("atomic_long_t") 39 39 40 40 def get_long_type(): 41 41 global long_type 42 42 return long_type.get_type() 43 - 44 43 45 44 def offset_of(typeobj, field): 46 45 element = gdb.Value(0).cast(typeobj) ··· 128 129 else: 129 130 return read_u32(buffer, offset) 130 131 132 + atomic_long_counter_offset = atomic_long_type.get_type()['counter'].bitpos 133 + atomic_long_counter_sizeof = atomic_long_type.get_type()['counter'].type.sizeof 134 + 135 + def read_atomic_long(buffer, offset): 136 + global atomic_long_counter_offset 137 + global atomic_long_counter_sizeof 138 + 139 + if atomic_long_counter_sizeof == 8: 140 + return read_u64(buffer, offset + atomic_long_counter_offset) 141 + else: 142 + return read_u32(buffer, offset + atomic_long_counter_offset) 131 143 132 144 target_arch = None 133 145