scripts/gdb: Add command to check list consistency
PATCH 1/1 scripts/gdb: Add command to check list consistency
Add a gdb script to verify the consistency of lists.
--- scripts/gdb/linux/lists.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++ scripts/gdb/vmlinux-gdb.py | 1 + 2 files changed, 79 insertions(+) create mode 100644 scripts/gdb/linux/lists.py
scripts/gdb/linux/lists.py new file +78 -0
@@ -0,0 +1,78 @@ +# +# gdb helper commands and functions for Linux kernel debugging +# +# list tools +# +# Authors: +# Thiebaud Weksteen <[email protected]> +# +# This work is licensed under the terms of the GNU GPL version 2. +# + +import gdb + +from linux import utils + +list_head = utils.CachedType("struct list_head") + +def check_list(head): + nb = 0 + list_head_ptr_type = list_head.get_type().pointer() + c = head.cast(list_head_ptr_type) + try: + gdb.write("Starting with: {} {}\n".format(c, c.dereference())) + except gdb.MemoryError: + gdb.write('head is not accessible\n') + return + while True: + p = c['prev'] + n = c['next'] + try: + if p['next'] != c: + gdb.write('prev.next != current: current={current} ' + 'prev={p} prev.next={pnext}\n'.format( + current=c, + p=p, + pnext=p['next'] + )) + return + except gdb.MemoryError: + gdb.write('prev is not accessible: current={current} ' + 'current.prev={p}\n'.format( + current=c, + p=p + )) + return + try: + if n['prev'] != c: + gdb.write('next.prev != current: current={current} ' + 'next={n}, next.prev={nprev}\n'.format( + current=c, + n=n, + nprev=n['prev'] + )) + return + except gdb.MemoryError: + gdb.write('next is not accessible: current={current} ' + 'current.next={n}\n'.format( + current=c, + n=n + )) + return + c = n + nb += 1 + if c == head: + gdb.write("list is consistent: {} node(s)\n".format(nb)) + return + +class LxChkList(gdb.Command): + """Verify a list consistency""" + + def __init__(self): + super(LxChkList, self).__init__("lx-check-list", gdb.COMMAND_DATA) + + def invoke(self, arg, from_tty): + argv = gdb.string_to_argv(arg) + check_list(gdb.parse_and_eval(argv[0])) + +LxChkList()
scripts/gdb/vmlinux-gdb.py +1 -0
@@ -28,3 +28,4 @@ else: import linux.dmesg import linux.tasks import linux.cpus + import linux.lists
Signature · 1 line
2.1.0
Jan Kiszka
maintainerQuoted 15 lines · Thiébaud Weksteen
Add a gdb script to verify the consistency of lists.
Signed-off-by Thiébaud Weksteen <[email protected]>--- scripts/gdb/linux/lists.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++ scripts/gdb/vmlinux-gdb.py | 1 + 2 files changed, 79 insertions(+) create mode 100644 scripts/gdb/linux/lists.pyscripts/gdb/linux/lists.py new file +0
-0
scripts/gdb/linux/lists.py line 7
Quoted 7 lines
scripts/gdb/linux/lists.py new file +7
-0@@ -0,0 +1,78 @@ +# +# gdb helper commands and functions for Linux kernel debugging +# +# list tools +# +# Authors: +# Thiebaud Weksteen <[email protected]>
Who is the copyright holder, also you, your employer etc.? Please clarify, ideally by just having a copyright line like in the other scripts.
scripts/gdb/linux/lists.py line 18
Quoted 11 lines
scripts/gdb/linux/lists.py new file +11
-0+# +# This work is licensed under the terms of the GNU GPL version 2. +# + +import gdb + +from linux import utils + +list_head = utils.CachedType("struct list_head") + +def check_list(head):
In general - applies to internal naming as well as the command itself - I would prefer "list check". That would create a namespace under which we may add further list helpers later on.
scripts/gdb/linux/lists.py line 21
Quoted 3 lines
scripts/gdb/linux/lists.py new file +3
-0+ nb = 0 + list_head_ptr_type = list_head.get_type().pointer() + c = head.cast(list_head_ptr_type)
Why casting? Is it a common use case to not have the list head as a variable at hand? The user could still do
lx_list_check (struct list_head *)0x...
when needed.
Quoted 6 lines
scripts/gdb/linux/lists.py new file +6
-0+ try: + gdb.write("Starting with: {} {}\n".format(c, c.dereference())) + except gdb.MemoryError: + gdb.write('head is not accessible\n') + return + while True:
scripts/gdb/linux/lists.py line 37
Quoted 10 lines
scripts/gdb/linux/lists.py new file +10
-0+ p = c['prev'] + n = c['next'] + try: + if p['next'] != c: + gdb.write('prev.next != current: current={current} ' + 'prev={p} prev.next={pnext}\n'.format( + current=c, + p=p, + pnext=p['next'] + ))
This is not pep8-conforming. Please fix (even if the exiting scripts have some issues as well - just noticed...).
Quoted 47 lines
scripts/gdb/linux/lists.py new file +41
-0+ return + except gdb.MemoryError: + gdb.write('prev is not accessible: current={current} ' + 'current.prev={p}\n'.format( + current=c, + p=p + )) + return + try: + if n['prev'] != c: + gdb.write('next.prev != current: current={current} ' + 'next={n}, next.prev={nprev}\n'.format( + current=c, + n=n, + nprev=n['prev'] + )) + return + except gdb.MemoryError: + gdb.write('next is not accessible: current={current} ' + 'current.next={n}\n'.format( + current=c, + n=n + )) + return + c = n + nb += 1 + if c == head: + gdb.write("list is consistent: {} node(s)\n".format(nb)) + return + +class LxChkList(gdb.Command): + """Verify a list consistency""" + + def __init__(self): + super(LxChkList, self).__init__("lx-check-list", gdb.COMMAND_DATA) + + def invoke(self, arg, from_tty): + argv = gdb.string_to_argv(arg) + check_list(gdb.parse_and_eval(argv[0])) + +LxChkList()scripts/gdb/vmlinux-gdb.py +0
-0
scripts/gdb/vmlinux-gdb.py else: · line 31
Quoted 4 lines
scripts/gdb/vmlinux-gdb.py +1
-0@@ -28,3 +28,4 @@ else: import linux.dmesg import linux.tasks import linux.cpus + import linux.lists
Looks useful! As indicated, I could imagine extending this module by further helpers, for the other list types or for walking a list, executing some gdb command for each element.
Thanks,
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SES-DE Corporate Competence Center Embedded Linux
Jan Kiszka
maintainerQuoted 11 lines · Jan Kiszka
scripts/gdb/linux/lists.py line 21
Quoted 3 lines · Thiébaud Weksteen
scripts/gdb/linux/lists.py new file +3
-0+ nb = 0 + list_head_ptr_type = list_head.get_type().pointer() + c = head.cast(list_head_ptr_type)Why casting? Is it a common use case to not have the list head as a variable at hand? The user could still do
lx_list_check(structlist_head*)0x...when needed.
Specifically, this fails in a non-obvious way right now:
(gdb) lx-check-list workqueues
If you look at lx_thread_info, e.g., it accepts both objects and pointers.
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT RTC ITP SES-DE Corporate Competence Center Embedded Linux