Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[PATCH] scripts/gdb: Add command to check list consistency

38 views
Skip to first unread message

Thiébaud Weksteen

unread,
Apr 22, 2015, 6:00:06 AM4/22/15
to
Add a gdb script to verify the consistency of lists.

Signed-off-by: Thiébaud Weksteen <thie...@weksteen.fr>
---
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

diff --git a/scripts/gdb/linux/lists.py b/scripts/gdb/linux/lists.py
new file mode 100644
index 0000000..d512f62
--- /dev/null
+++ b/scripts/gdb/linux/lists.py
@@ -0,0 +1,78 @@
+#
+# gdb helper commands and functions for Linux kernel debugging
+#
+# list tools
+#
+# Authors:
+# Thiebaud Weksteen <thie...@weksteen.fr>
+#
+# 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()
diff --git a/scripts/gdb/vmlinux-gdb.py b/scripts/gdb/vmlinux-gdb.py
index 4848928..ce82bf5 100644
--- a/scripts/gdb/vmlinux-gdb.py
+++ b/scripts/gdb/vmlinux-gdb.py
@@ -28,3 +28,4 @@ else:
import linux.dmesg
import linux.tasks
import linux.cpus
+ import linux.lists
--
2.1.0
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majo...@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

Jan Kiszka

unread,
Apr 23, 2015, 8:20:07 AM4/23/15
to
On 2015-04-22 09:32, Thiébaud Weksteen wrote:
> Add a gdb script to verify the consistency of lists.
>
> Signed-off-by: Thiébaud Weksteen <thie...@weksteen.fr>
> ---
> 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
>
> diff --git a/scripts/gdb/linux/lists.py b/scripts/gdb/linux/lists.py
> new file mode 100644
> index 0000000..d512f62
> --- /dev/null
> +++ b/scripts/gdb/linux/lists.py
> @@ -0,0 +1,78 @@
> +#
> +# gdb helper commands and functions for Linux kernel debugging
> +#
> +# list tools
> +#
> +# Authors:
> +# Thiebaud Weksteen <thie...@weksteen.fr>

Who is the copyright holder, also you, your employer etc.? Please
clarify, ideally by just having a copyright line like in the other scripts.

> +#
> +# 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.

> + 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.

> + 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']
> + ))

This is not pep8-conforming. Please fix (even if the exiting scripts
have some issues as well - just noticed...).
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

--
Siemens AG, Corporate Technology, CT RTC ITP SES-DE
Corporate Competence Center Embedded Linux

Jan Kiszka

unread,
Apr 23, 2015, 8:30:06 AM4/23/15
to
On 2015-04-23 14:10, Jan Kiszka wrote:
> On 2015-04-22 09:32, Thiébaud Weksteen wrote:
>> + 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.

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.

Thiébaud Weksteen

unread,
Apr 23, 2015, 10:00:05 PM4/23/15
to
Add a gdb script to verify the consistency of lists.

Signed-off-by: Thiébaud Weksteen <thie...@weksteen.fr>
---
Implement suggestions from Jan.

Changes in v2:
- Add copyright line
- Rename check_list to list_check
- Remove casting and only accept (struct list_head) object
- Add error message if argument is missing
- Reformat error messages to include address of nodes

Example of use:
(gdb) lx-list-check workqueues
Starting with: {next = 0xf3aea5a8, prev = 0xf580e668}
list is consistent: 30 node(s)
(gdb) set *((int *) 0xf3934908) = 0x0
(gdb) lx-list-check workqueues
Starting with: {next = 0xf3aea5a8, prev = 0xf580e668}
next is not accessible: current@0xf3934908={next = 0x0, prev = 0xf3aea5a8}
(gdb) set *((int *) 0xf3934908) = 0xdeadbeef
(gdb) lx-list-check workqueues
Starting with: {next = 0xf3aea5a8, prev = 0xf580e668}
next.prev != current: current@0xf3934908={next = 0xdeadbeef, prev = 0xf3aea5a8}
next@0xdeadbeef={next = 0x0, prev = 0x0}
(gdb) set *((int *) 0xf3934908) = 0xf537f188
(gdb) lx-list-check workqueues
Starting with: {next = 0xf3aea5a8, prev = 0xf580e668}
list is consistent: 30 node(s)


scripts/gdb/linux/lists.py | 89 ++++++++++++++++++++++++++++++++++++++++++++++
scripts/gdb/vmlinux-gdb.py | 1 +
2 files changed, 90 insertions(+)
create mode 100644 scripts/gdb/linux/lists.py

diff --git a/scripts/gdb/linux/lists.py b/scripts/gdb/linux/lists.py
new file mode 100644
index 0000000..2a94b54
--- /dev/null
+++ b/scripts/gdb/linux/lists.py
@@ -0,0 +1,89 @@
+#
+# gdb helper commands and functions for Linux kernel debugging
+#
+# list tools
+#
+# Copyright (c) Thiebaud Weksteen, 2015
+#
+# Authors:
+# Thiebaud Weksteen <thie...@weksteen.fr>
+#
+# 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 list_check(head):
+ nb = 0
+ c = head
+ if (c.type != list_head.get_type()):
+ raise gdb.GdbError('The argument should be of type (struct list_head)')
+ try:
+ gdb.write("Starting with: {}\n".format(c))
+ except gdb.MemoryError:
+ gdb.write('head is not accessible\n')
+ return
+ while True:
+ p = c['prev'].dereference()
+ n = c['next'].dereference()
+ try:
+ if p['next'] != c.address:
+ gdb.write('prev.next != current: '
+ 'current@{current_addr}={current} '
+ 'prev@{p_addr}={p}\n'.format(
+ current_addr=c.address,
+ current=c,
+ p_addr=p.address,
+ p=p,
+ ))
+ return
+ except gdb.MemoryError:
+ gdb.write('prev is not accessible: '
+ 'current@{current_addr}={current}\n'.format(
+ current_addr=c.address,
+ current=c
+ ))
+ return
+ try:
+ if n['prev'] != c.address:
+ gdb.write('next.prev != current: '
+ 'current@{current_addr}={current} '
+ 'next@{n_addr}={n}\n'.format(
+ current_addr=c.address,
+ current=c,
+ n_addr=n.address,
+ n=n,
+ ))
+ return
+ except gdb.MemoryError:
+ gdb.write('next is not accessible: '
+ 'current@{current_addr}={current}\n'.format(
+ current_addr=c.address,
+ current=c
+ ))
+ return
+ c = n
+ nb += 1
+ if c == head:
+ gdb.write("list is consistent: {} node(s)\n".format(nb))
+ return
+
+
+class LxListChk(gdb.Command):
+ """Verify a list consistency"""
+
+ def __init__(self):
+ super(LxListChk, self).__init__("lx-list-check", gdb.COMMAND_DATA)
+
+ def invoke(self, arg, from_tty):
+ argv = gdb.string_to_argv(arg)
+ if len(argv) != 1:
+ raise gdb.GdbError("lx-list-check takes one argument")
+ list_check(gdb.parse_and_eval(argv[0]))
+
+LxListChk()
diff --git a/scripts/gdb/vmlinux-gdb.py b/scripts/gdb/vmlinux-gdb.py
index 4848928..ce82bf5 100644
--- a/scripts/gdb/vmlinux-gdb.py
+++ b/scripts/gdb/vmlinux-gdb.py
@@ -28,3 +28,4 @@ else:
import linux.dmesg
import linux.tasks
import linux.cpus
+ import linux.lists
--
2.1.0

Jan Kiszka

unread,
May 1, 2015, 3:00:05 AM5/1/15
to
On 2015-04-24 03:57, Thiébaud Weksteen wrote:
> Add a gdb script to verify the consistency of lists.
>
> Signed-off-by: Thiébaud Weksteen <thie...@weksteen.fr>
> ---
> Implement suggestions from Jan.
>
> Changes in v2:
> - Add copyright line
> - Rename check_list to list_check
> - Remove casting and only accept (struct list_head) object
> - Add error message if argument is missing
> - Reformat error messages to include address of nodes
>

Thanks! I've queued it up (git.kiszka.org/linux.git queues/gdb-scripts)
along with two small improvements (completion and support for list
pointers). Will push to Andrew for 4.2. I'm also still thinking about
lx-list-for-each...

Jan

signature.asc
0 new messages