Skip to contentSkip to conversation

gdb script for kconfig and timer list

PATCH New Stephen Boyd 1 participant 6 messages Mar 29, 2019 GDB kernel debugging helper scripts

lore
Share & tools
lore
#0 lore raw

v2 cover gdb script for kconfig and timer list

This is a handful of changes to the kernel's gdb scripts to do some more debugging with kgdb. The first patch allows the vmlinux to be reloaded from where it was specified on the command line so that this set of scripts can be used from anywhere. The second patch adds a script to dump the config.gz to a file on the host debugging machine. The third patch adds some rb tree utilities and the last patch uses those rb tree walking utilities to dump out the contents of /proc/timer_list from a system under debug.

Andrew picked these patches up into -mm. I'm resending for full review instead of just sending the diff.

Changes from v1:
* Fixed config dumping script off-by-one error on builtin config size
* Silenced pep8 style warnings and errors
* Added a new patch at the end to fix existing warnings
* Fixed some TODOs in timerlist printing
* cpumask printing
* jiffies printing uses jiffies_64 now to avoid conversion problems

Cc Douglas Anderson <[email protected]>
Cc Nikolay Borisov <[email protected]>
Cc Kieran Bingham <[email protected]>
Cc Jan Kiszka <[email protected]>
Cc Jackie Liu <[email protected]>

Stephen Boyd (5):
scripts/gdb: Find vmlinux where it was before
scripts/gdb: Add kernel config dumping command
scripts/gdb: Add rb tree iterating utilities
scripts/gdb: Add a timer list command
scripts/gdb: Silence pep8 checks

 scripts/gdb/linux/config.py       |  44 ++++++
 scripts/gdb/linux/constants.py.in |  13 ++
 scripts/gdb/linux/cpus.py         |   1 +
 scripts/gdb/linux/lists.py        |   1 +
 scripts/gdb/linux/proc.py         |  10 +-
 scripts/gdb/linux/rbtree.py       | 177 ++++++++++++++++++++++++
 scripts/gdb/linux/symbols.py      |   6 +-
 scripts/gdb/linux/tasks.py        |   2 +
 scripts/gdb/linux/timerlist.py    | 219 ++++++++++++++++++++++++++++++
 scripts/gdb/linux/utils.py        |   7 +-
 scripts/gdb/vmlinux-gdb.py        |   3 +
 11 files changed, 477 insertions(+), 6 deletions(-)
 create mode 100644 scripts/gdb/linux/config.py
 create mode 100644 scripts/gdb/linux/rbtree.py
 create mode 100644 scripts/gdb/linux/timerlist.py

base-commit: 9e98c678c2d6ae3a17cb2de55d17f69dddaa231b

Signature · 1 line
Sent by a computer through tubes
#1 lore raw

PATCH v2 1/5 scripts/gdb: Find vmlinux where it was before

If I run 'gdb <path/to/vmlinux>' and there's the vmlinux-gdb.py file there I can properly see symbols and use the lx commands provided by the GDB scripts. But once I run 'lx-symbols' at the command prompt, gdb reloads the vmlinux symbols assuming that this script was run from the directory that has vmlinux at the root. That isn't always true, but we could just look and see what symbols were already loaded and use that instead. Let's do that so this can work by being invoked anywhere.

Cc Douglas Anderson <[email protected]>
Cc Nikolay Borisov <[email protected]>
Cc Kieran Bingham <[email protected]>
Cc Jan Kiszka <[email protected]>
Cc Jackie Liu <[email protected]>
---
 scripts/gdb/linux/symbols.py | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
scripts/gdb/linux/symbols.py +5 -1
index 004b0ac7fa72..2f5b95f09fa0 100644
@@ -139,8 +139,12 @@ lx-symbols command."""
                 saved_states.append({'breakpoint': bp, 'enabled': bp.enabled})
 
         # drop all current symbols and reload vmlinux
+        orig_vmlinux = 'vmlinux'
+        for obj in gdb.objfiles():
+            if obj.filename.endswith('vmlinux'):
+                orig_vmlinux = obj.filename
         gdb.execute("symbol-file", to_string=True)
-        gdb.execute("symbol-file vmlinux")
+        gdb.execute("symbol-file {0}".format(orig_vmlinux))
 
         self.loaded_modules = []
         module_list = modules.module_list()
Signature · 1 line
Sent by a computer through tubes
#2 lore raw

PATCH v2 2/5 scripts/gdb: Add kernel config dumping command

In reply to Stephen Boyd

lx-configdump <file> dumps the contents of the gzipped .config to a text file when the config is included in the kernel with CONFIG_IKCONFIG. By default, the file written is called config.txt, but it can be any user supplied filename as well. If the kernel config is in a module (configs.ko), then it can be loaded along with symbols for the module loaded with 'lx-symbols' and then this command will still work.

Obviously if you have the whole vmlinux then this can also be achieved with scripts/extract-ikconfig, but this gdb script can be useful to confirm that the memory contents of the config in memory and the vmlinux contents on disk match what is expected.

Cc Douglas Anderson <[email protected]>
Cc Nikolay Borisov <[email protected]>
Cc Kieran Bingham <[email protected]>
Cc Jan Kiszka <[email protected]>
Cc Jackie Liu <[email protected]>
---
 scripts/gdb/linux/config.py | 44 +++++++++++++++++++++++++++++++++++++
 scripts/gdb/vmlinux-gdb.py  |  1 +
 2 files changed, 45 insertions(+)
 create mode 100644 scripts/gdb/linux/config.py
scripts/gdb/linux/config.py new file +44 -0
new file mode 100644
index 000000000000..90e1565b1967
@@ -0,0 +1,44 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Copyright 2019 Google LLC.
+
+import gdb
+import zlib
+
+from linux import utils
+
+
+class LxConfigDump(gdb.Command):
+    """Output kernel config to the filename specified as the command
+       argument. Equivalent to 'zcat /proc/config.gz > config.txt' on
+       a running target"""
+
+    def __init__(self):
+        super(LxConfigDump, self).__init__("lx-configdump", gdb.COMMAND_DATA,
+                                           gdb.COMPLETE_FILENAME)
+
+    def invoke(self, arg, from_tty):
+        if len(arg) == 0:
+            filename = "config.txt"
+        else:
+            filename = arg
+
+        try:
+            py_config_ptr = gdb.parse_and_eval("kernel_config_data + 8")
+            py_config_size = gdb.parse_and_eval(
+                    "sizeof(kernel_config_data) - 1 - 8 * 2")
+        except gdb.error as e:
+            raise gdb.GdbError("Can't find config, enable CONFIG_IKCONFIG?")
+
+        inf = gdb.inferiors()[0]
+        zconfig_buf = utils.read_memoryview(inf, py_config_ptr,
+                                            py_config_size).tobytes()
+
+        config_buf = zlib.decompress(zconfig_buf, 16)
+        with open(filename, 'wb') as f:
+            f.write(config_buf)
+
+        gdb.write("Dumped config to " + filename + "\n")
+
+
+LxConfigDump()
scripts/gdb/vmlinux-gdb.py +1 -0
index 6e0b0afd888a..be0efb5dda5b 100644
@@ -27,6 +27,7 @@ else:
     import linux.modules
     import linux.dmesg
     import linux.tasks
+    import linux.config
     import linux.cpus
     import linux.lists
     import linux.proc
Signature · 1 line
Sent by a computer through tubes
#3 lore raw

PATCH v2 3/5 scripts/gdb: Add rb tree iterating utilities

In reply to Stephen Boyd

Implement gdb functions for rb_first(), rb_last(), rb_next(), and rb_prev(). These can be useful to iterate through the kernel's red-black trees.

Cc Douglas Anderson <[email protected]>
Cc Nikolay Borisov <[email protected]>
Cc Kieran Bingham <[email protected]>
Cc Jan Kiszka <[email protected]>
Cc Jackie Liu <[email protected]>
---
 scripts/gdb/linux/rbtree.py | 177 ++++++++++++++++++++++++++++++++++++
 scripts/gdb/vmlinux-gdb.py  |   1 +
 2 files changed, 178 insertions(+)
 create mode 100644 scripts/gdb/linux/rbtree.py
scripts/gdb/linux/rbtree.py new file +177 -0
new file mode 100644
index 000000000000..39db889b874c
@@ -0,0 +1,177 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Copyright 2019 Google LLC.
+
+import gdb
+
+from linux import utils
+
+rb_root_type = utils.CachedType("struct rb_root")
+rb_node_type = utils.CachedType("struct rb_node")
+
+
+def rb_first(root):
+    if root.type == rb_root_type.get_type():
+        node = node.address.cast(rb_root_type.get_type().pointer())
+    elif root.type != rb_root_type.get_type().pointer():
+        raise gdb.GdbError("Must be struct rb_root not {}".format(root.type))
+
+    node = root['rb_node']
+    if node is 0:
+        return None
+
+    while node['rb_left']:
+        node = node['rb_left']
+
+    return node
+
+
+def rb_last(root):
+    if root.type == rb_root_type.get_type():
+        node = node.address.cast(rb_root_type.get_type().pointer())
+    elif root.type != rb_root_type.get_type().pointer():
+        raise gdb.GdbError("Must be struct rb_root not {}".format(root.type))
+
+    node = root['rb_node']
+    if node is 0:
+        return None
+
+    while node['rb_right']:
+        node = node['rb_right']
+
+    return node
+
+
+def rb_parent(node):
+    parent = gdb.Value(node['__rb_parent_color'] & ~3)
+    return parent.cast(rb_node_type.get_type().pointer())
+
+
+def rb_empty_node(node):
+    return node['__rb_parent_color'] == node.address
+
+
+def rb_next(node):
+    if node.type == rb_node_type.get_type():
+        node = node.address.cast(rb_node_type.get_type().pointer())
+    elif node.type != rb_node_type.get_type().pointer():
+        raise gdb.GdbError("Must be struct rb_node not {}".format(node.type))
+
+    if rb_empty_node(node):
+        return None
+
+    if node['rb_right']:
+        node = node['rb_right']
+        while node['rb_left']:
+            node = node['rb_left']
+        return node
+
+    parent = rb_parent(node)
+    while parent and node == parent['rb_right']:
+            node = parent
+            parent = rb_parent(node)
+
+    return parent
+
+
+def rb_prev(node):
+    if node.type == rb_node_type.get_type():
+        node = node.address.cast(rb_node_type.get_type().pointer())
+    elif node.type != rb_node_type.get_type().pointer():
+        raise gdb.GdbError("Must be struct rb_node not {}".format(node.type))
+
+    if rb_empty_node(node):
+        return None
+
+    if node['rb_left']:
+        node = node['rb_left']
+        while node['rb_right']:
+            node = node['rb_right']
+        return node.dereference()
+
+    parent = rb_parent(node)
+    while parent and node == parent['rb_left'].dereference():
+            node = parent
+            parent = rb_parent(node)
+
+    return parent
+
+
+class LxRbFirst(gdb.Function):
+    """Lookup and return a node from an RBTree
+
+$lx_rb_first(root): Return the node at the given index.
+If index is omitted, the root node is dereferenced and returned."""
+
+    def __init__(self):
+        super(LxRbFirst, self).__init__("lx_rb_first")
+
+    def invoke(self, root):
+        result = rb_first(root)
+        if result is None:
+            raise gdb.GdbError("No entry in tree")
+
+        return result
+
+
+LxRbFirst()
+
+
+class LxRbLast(gdb.Function):
+    """Lookup and return a node from an RBTree.
+
+$lx_rb_last(root): Return the node at the given index.
+If index is omitted, the root node is dereferenced and returned."""
+
+    def __init__(self):
+        super(LxRbLast, self).__init__("lx_rb_last")
+
+    def invoke(self, root):
+        result = rb_last(root)
+        if result is None:
+            raise gdb.GdbError("No entry in tree")
+
+        return result
+
+
+LxRbLast()
+
+
+class LxRbNext(gdb.Function):
+    """Lookup and return a node from an RBTree.
+
+$lx_rb_next(node): Return the node at the given index.
+If index is omitted, the root node is dereferenced and returned."""
+
+    def __init__(self):
+        super(LxRbNext, self).__init__("lx_rb_next")
+
+    def invoke(self, node):
+        result = rb_next(node)
+        if result is None:
+            raise gdb.GdbError("No entry in tree")
+
+        return result
+
+
+LxRbNext()
+
+
+class LxRbPrev(gdb.Function):
+    """Lookup and return a node from an RBTree.
+
+$lx_rb_prev(node): Return the node at the given index.
+If index is omitted, the root node is dereferenced and returned."""
+
+    def __init__(self):
+        super(LxRbPrev, self).__init__("lx_rb_prev")
+
+    def invoke(self, node):
+        result = rb_prev(node)
+        if result is None:
+            raise gdb.GdbError("No entry in tree")
+
+        return result
+
+
+LxRbPrev()
scripts/gdb/vmlinux-gdb.py +1 -0
index be0efb5dda5b..89e4aa4f8966 100644
@@ -30,5 +30,6 @@ else:
     import linux.config
     import linux.cpus
     import linux.lists
+    import linux.rbtree
     import linux.proc
     import linux.constants
Signature · 1 line
Sent by a computer through tubes
#4 lore raw

PATCH v2 4/5 scripts/gdb: Add a timer list command

In reply to Stephen Boyd

Implement a command to print the timer list, much like how /proc/timer_list is implemented. This can be used to look at the pending timers on a crashed system.

Cc Douglas Anderson <[email protected]>
Cc Nikolay Borisov <[email protected]>
Cc Kieran Bingham <[email protected]>
Cc Jan Kiszka <[email protected]>
Cc Jackie Liu <[email protected]>
---
 scripts/gdb/linux/constants.py.in |  13 ++
 scripts/gdb/linux/timerlist.py    | 219 ++++++++++++++++++++++++++++++
 scripts/gdb/vmlinux-gdb.py        |   1 +
 3 files changed, 233 insertions(+)
 create mode 100644 scripts/gdb/linux/timerlist.py
scripts/gdb/linux/constants.py.in +13 -0
index d3319a80788a..76f46f427b96 100644
@@ -13,8 +13,10 @@
  */
 
 #include <linux/fs.h>
+#include <linux/hrtimer.h>
 #include <linux/mount.h>
 #include <linux/of_fdt.h>
+#include <linux/threads.h>
 
 /* We need to stringify expanded macros so that they can be parsed */
 
@@ -44,6 +46,9 @@ LX_VALUE(SB_DIRSYNC)
 LX_VALUE(SB_NOATIME)
 LX_VALUE(SB_NODIRATIME)
 
+/* linux/htimer.h */
+LX_GDBPARSED(hrtimer_resolution)
+
 /* linux/mount.h */
 LX_VALUE(MNT_NOSUID)
 LX_VALUE(MNT_NODEV)
@@ -52,8 +57,16 @@ LX_VALUE(MNT_NOATIME)
 LX_VALUE(MNT_NODIRATIME)
 LX_VALUE(MNT_RELATIME)
 
+/* linux/threads.h */
+LX_VALUE(NR_CPUS)
+
 /* linux/of_fdt.h> */
 LX_VALUE(OF_DT_HEADER)
 
 /* Kernel Configs */
+LX_CONFIG(CONFIG_GENERIC_CLOCKEVENTS)
+LX_CONFIG(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST)
+LX_CONFIG(CONFIG_HIGH_RES_TIMERS)
+LX_CONFIG(CONFIG_NR_CPUS)
 LX_CONFIG(CONFIG_OF)
+LX_CONFIG(CONFIG_TICK_ONESHOT)
scripts/gdb/linux/timerlist.py new file +219 -0

219 lines not on this page · raw message

scripts/gdb/vmlinux-gdb.py +1 -0
index 89e4aa4f8966..033578cc4cd7 100644
@@ -33,3 +33,4 @@ else:
     import linux.rbtree
     import linux.proc
     import linux.constants
+    import linux.timerlist
Signature · 1 line
Sent by a computer through tubes
#5 lore raw

PATCH v2 5/5 scripts/gdb: Silence pep8 checks

In reply to Stephen Boyd

These scripts have some pep8 style warnings. Fix them up so that this directory is all pep8 clean.

Cc Douglas Anderson <[email protected]>
Cc Nikolay Borisov <[email protected]>
Cc Kieran Bingham <[email protected]>
Cc Jan Kiszka <[email protected]>
Cc Jackie Liu <[email protected]>
---
 scripts/gdb/linux/cpus.py  |  1 +
 scripts/gdb/linux/lists.py |  1 +
 scripts/gdb/linux/proc.py  | 10 ++++++++--
 scripts/gdb/linux/tasks.py |  2 ++
 scripts/gdb/linux/utils.py |  7 ++++---
 5 files changed, 16 insertions(+), 5 deletions(-)
scripts/gdb/linux/cpus.py +1 -0
index ca11e8df31b6..008e62f3190d 100644
@@ -135,6 +135,7 @@ and can help identify the state of hotplugged CPUs"""
         gdb.write("Online CPUs   : {}\n".format(list(each_online_cpu())))
         gdb.write("Active CPUs   : {}\n".format(list(each_active_cpu())))
 
+
 LxCpus()
 
 
scripts/gdb/linux/lists.py +1 -0
index 2f335fbd86fd..1987d756b36b 100644
@@ -110,4 +110,5 @@ class LxListChk(gdb.Command):
             raise gdb.GdbError("lx-list-check takes one argument")
         list_check(gdb.parse_and_eval(argv[0]))
 
+
 LxListChk()
scripts/gdb/linux/proc.py +8 -2
index 2f01a958eb22..6a56bba233a9 100644
@@ -29,6 +29,7 @@ class LxCmdLine(gdb.Command):
     def invoke(self, arg, from_tty):
         gdb.write(gdb.parse_and_eval("saved_command_line").string() + "\n")
 
+
 LxCmdLine()
 
 
@@ -43,6 +44,7 @@ class LxVersion(gdb.Command):
         # linux_banner should contain a newline
         gdb.write(gdb.parse_and_eval("(char *)linux_banner").string())
 
+
 LxVersion()
 
 
@@ -86,6 +88,7 @@ Equivalent to cat /proc/iomem on a running target"""
     def invoke(self, arg, from_tty):
         return show_lx_resources("iomem_resource")
 
+
 LxIOMem()
 
 
@@ -100,6 +103,7 @@ Equivalent to cat /proc/ioports on a running target"""
     def invoke(self, arg, from_tty):
         return show_lx_resources("ioport_resource")
 
+
 LxIOPorts()
 
 
@@ -149,7 +153,7 @@ values of that process namespace"""
         if len(argv) >= 1:
             try:
                 pid = int(argv[0])
-            except:
+            except gdb.error:
                 raise gdb.GdbError("Provide a PID as integer value")
         else:
             pid = 1
@@ -195,6 +199,7 @@ values of that process namespace"""
                         info_opts(FS_INFO, s_flags),
                         info_opts(MNT_INFO, m_flags)))
 
+
 LxMounts()
 
 
@@ -259,7 +264,7 @@ class LxFdtDump(gdb.Command):
 
         try:
             f = open(filename, 'wb')
-        except:
+        except gdb.error:
             raise gdb.GdbError("Could not open file to dump fdt")
 
         f.write(fdt_buf)
@@ -267,4 +272,5 @@ class LxFdtDump(gdb.Command):
 
         gdb.write("Dumped fdt blob to " + filename + "\n")
 
+
 LxFdtDump()
scripts/gdb/linux/tasks.py +2 -0
index f6ab3ccf698f..0301dc1e0138 100644
@@ -79,6 +79,7 @@ class LxPs(gdb.Command):
                 pid=task["pid"],
                 comm=task["comm"].string()))
 
+
 LxPs()
 
 
@@ -134,4 +135,5 @@ variable."""
         else:
             raise gdb.GdbError("No task of PID " + str(pid))
 
+
 LxThreadInfoByPidFunc()
scripts/gdb/linux/utils.py +4 -3
index 50805874cfc3..bc67126118c4 100644
@@ -66,6 +66,7 @@ Note that TYPE and ELEMENT have to be quoted as strings."""
         return container_of(ptr, gdb.lookup_type(typename.string()).pointer(),
                             elementname.string())
 
+
 ContainerOf()
 
 
@@ -148,14 +149,14 @@ def get_gdbserver_type():
     def probe_qemu():
         try:
             return gdb.execute("monitor info version", to_string=True) != ""
-        except:
+        except gdb.error:
             return False
 
     def probe_kgdb():
         try:
             thread_info = gdb.execute("info thread 2", to_string=True)
             return "shadowCPU0" in thread_info
-        except:
+        except gdb.error:
             return False
 
     global gdbserver_type
@@ -172,7 +173,7 @@ def get_gdbserver_type():
 def gdb_eval_or_none(expresssion):
     try:
         return gdb.parse_and_eval(expresssion)
-    except:
+    except gdb.error:
         return None
 
 
Signature · 1 line
Sent by a computer through tubes