[PATCH] scripts/gdb: fix 'lx-timerlist' on newer kernel

From: Lin Ma
Date: Mon Jan 02 2023 - 02:47:01 EST


After commit 511885d7061e ("lib/timerqueue: Rely on rbtree semantics for
next timer"), the struct timerqueue_head changes internal field hence
causes the old lx-timerlist command keeps reporting errors.

This fix addes a simple version comparison and necessary code for
traversing timerqueue on a newer kernel. Moreover, it fixes some
python 3.X compatibility bugs.

Test with python 2.7 and python 3.6
Test with
* commit 442284a89a65 ("scripts/gdb: add a timer list command") and
* commit 88603b6dc419 ("Linux 6.2-rc2") kernel

Signed-off-by: Lin Ma <linma@xxxxxxxxxx>
---
scripts/gdb/linux/timerlist.py | 41 ++++++++++++++++++++++------------
1 file changed, 27 insertions(+), 14 deletions(-)

diff --git a/scripts/gdb/linux/timerlist.py b/scripts/gdb/linux/timerlist.py
index 071d0dd5a634..65d035df253d 100644
--- a/scripts/gdb/linux/timerlist.py
+++ b/scripts/gdb/linux/timerlist.py
@@ -4,6 +4,7 @@

import binascii
import gdb
+import re

from linux import constants
from linux import cpus
@@ -42,9 +43,15 @@ def print_timer(rb_node, idx):
return text


-def print_active_timers(base):
- curr = base['active']['next']['node']
- curr = curr.address.cast(rbtree.rb_node_type.get_type().pointer())
+def print_active_timers(base, oldver):
+ # 511885d7061e ("lib/timerqueue: Rely on rbtree semantics for next timer")
+ if oldver:
+ curr = base['active']['next']['node']
+ curr = curr.address.cast(rbtree.rb_node_type.get_type().pointer())
+ else:
+ leftmost = base['active']['rb_root']['rb_leftmost']
+ curr = leftmost.cast(rbtree.rb_node_type.get_type().pointer())
+
idx = 0
while curr:
yield print_timer(curr, idx)
@@ -52,7 +59,7 @@ def print_active_timers(base):
idx += 1


-def print_base(base):
+def print_base(base, oldver):
text = " .base: {}\n".format(base.address)
text += " .index: {}\n".format(base['index'])

@@ -62,21 +69,20 @@ def print_base(base):
if constants.LX_CONFIG_HIGH_RES_TIMERS:
text += " .offset: {} nsecs\n".format(base['offset'])
text += "active timers:\n"
- text += "".join([x for x in print_active_timers(base)])
+ text += "".join([x for x in print_active_timers(base, oldver)])
return text


-def print_cpu(hrtimer_bases, cpu, max_clock_bases):
+def print_cpu(hrtimer_bases, cpu, max_clock_bases, oldver):
cpu_base = cpus.per_cpu(hrtimer_bases, cpu)
jiffies = gdb.parse_and_eval("jiffies_64")
tick_sched_ptr = gdb.parse_and_eval("&tick_cpu_sched")
ts = cpus.per_cpu(tick_sched_ptr, cpu)

text = "cpu: {}\n".format(cpu)
- for i in xrange(max_clock_bases):
+ for i in range(max_clock_bases):
text += " clock {}:\n".format(i)
- text += print_base(cpu_base['clock_base'][i])
-
+ text += print_base(cpu_base['clock_base'][i], oldver)
if constants.LX_CONFIG_HIGH_RES_TIMERS:
fmts = [(" .{} : {} nsecs", 'expires_next'),
(" .{} : {}", 'hres_active'),
@@ -165,7 +171,7 @@ def pr_cpumask(mask):
i -= 1
start = i * 2
end = start + 2
- chunks.append(buf[start:end])
+ chunks.append(str(buf[start:end]))
if i != 0 and i % 4 == 0:
chunks.append(',')

@@ -184,14 +190,21 @@ class LxTimerList(gdb.Command):

def invoke(self, arg, from_tty):
hrtimer_bases = gdb.parse_and_eval("&hrtimer_bases")
- max_clock_bases = gdb.parse_and_eval("HRTIMER_MAX_CLOCK_BASES")
-
- text = "Timer List Version: gdb scripts\n"
+ max_clock_bases = gdb.parse_and_eval("(int)HRTIMER_MAX_CLOCK_BASES")
+ banner = gdb.parse_and_eval("(char *)linux_banner").string()
+ r = re.match(r"^Linux version (.+) \(.*@.*$", banner)
+ if not r:
+ gdb.write("failed to load image version")
+ return
+ lver = r.group(1)
+ oldver = lver < '5.3.0-rc1'
+
+ text = "Timer List Version: gdb scripts, old version: {}\n".format(oldver)
text += "HRTIMER_MAX_CLOCK_BASES: {}\n".format(max_clock_bases)
text += "now at {} nsecs\n".format(ktime_get())

for cpu in cpus.each_online_cpu():
- text += print_cpu(hrtimer_bases, cpu, max_clock_bases)
+ text += print_cpu(hrtimer_bases, cpu, max_clock_bases, oldver)

if constants.LX_CONFIG_GENERIC_CLOCKEVENTS:
if constants.LX_CONFIG_GENERIC_CLOCKEVENTS_BROADCAST:
--
2.17.1