Skip to content

Commit e3c8d33

Browse files
AntonioBorneopmladek
authored andcommitted
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: e607683 ("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
1 parent 9a3bfa0 commit e3c8d33

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

scripts/gdb/linux/dmesg.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
prb_desc_ring_type = utils.CachedType("struct prb_desc_ring")
2323
prb_data_ring_type = utils.CachedType("struct prb_data_ring")
2424
printk_ringbuffer_type = utils.CachedType("struct printk_ringbuffer")
25-
atomic_long_type = utils.CachedType("atomic_long_t")
2625

2726
class LxDmesg(gdb.Command):
2827
"""Print Linux kernel log buffer."""
@@ -68,8 +67,6 @@ def invoke(self, arg, from_tty):
6867
off = prb_data_ring_type.get_type()['data'].bitpos // 8
6968
text_data_addr = utils.read_ulong(text_data_ring, off)
7069

71-
counter_off = atomic_long_type.get_type()['counter'].bitpos // 8
72-
7370
sv_off = prb_desc_type.get_type()['state_var'].bitpos // 8
7471

7572
off = prb_desc_type.get_type()['text_blk_lpos'].bitpos // 8
@@ -89,9 +86,9 @@ def invoke(self, arg, from_tty):
8986

9087
# read in tail and head descriptor ids
9188
off = prb_desc_ring_type.get_type()['tail_id'].bitpos // 8
92-
tail_id = utils.read_u64(desc_ring, off + counter_off)
89+
tail_id = utils.read_atomic_long(desc_ring, off)
9390
off = prb_desc_ring_type.get_type()['head_id'].bitpos // 8
94-
head_id = utils.read_u64(desc_ring, off + counter_off)
91+
head_id = utils.read_atomic_long(desc_ring, off)
9592

9693
did = tail_id
9794
while True:
@@ -102,7 +99,7 @@ def invoke(self, arg, from_tty):
10299
desc = utils.read_memoryview(inf, desc_addr + desc_off, desc_sz).tobytes()
103100

104101
# skip non-committed record
105-
state = 3 & (utils.read_u64(desc, sv_off + counter_off) >> desc_flags_shift)
102+
state = 3 & (utils.read_atomic_long(desc, sv_off) >> desc_flags_shift)
106103
if state != desc_committed and state != desc_finalized:
107104
if did == head_id:
108105
break

scripts/gdb/linux/utils.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,12 @@ def get_type(self):
3535

3636

3737
long_type = CachedType("long")
38-
38+
atomic_long_type = CachedType("atomic_long_t")
3939

4040
def get_long_type():
4141
global long_type
4242
return long_type.get_type()
4343

44-
4544
def offset_of(typeobj, field):
4645
element = gdb.Value(0).cast(typeobj)
4746
return int(str(element[field].address).split()[0], 16)
@@ -129,6 +128,17 @@ def read_ulong(buffer, offset):
129128
else:
130129
return read_u32(buffer, offset)
131130

131+
atomic_long_counter_offset = atomic_long_type.get_type()['counter'].bitpos
132+
atomic_long_counter_sizeof = atomic_long_type.get_type()['counter'].type.sizeof
133+
134+
def read_atomic_long(buffer, offset):
135+
global atomic_long_counter_offset
136+
global atomic_long_counter_sizeof
137+
138+
if atomic_long_counter_sizeof == 8:
139+
return read_u64(buffer, offset + atomic_long_counter_offset)
140+
else:
141+
return read_u32(buffer, offset + atomic_long_counter_offset)
132142

133143
target_arch = None
134144

0 commit comments

Comments
 (0)