Skip to contentSkip to conversation

scripts/gdb: Updates for 4.7

PATCH Applied Jan Kiszka 1 participant 19 messages May 10, 2016

Subsystems (3)
lore
Share & tools
lore

Jan Kiszka

author maintainer
#0 lore raw

cover scripts/gdb: Updates for 4.7

Hi Andrew,

please include the following enhancements and fixed for the gdb scripts in your queue.

This adds a number of new commands and helper functions, contributed by Kieran, and fixes lx-dmesg for Python 3 as well as corrects the output of lx-lsmod.

Kieran stepped up to support me significantly with the maintenance of the script. Andrew, it may very well be that you will receive the next merge request from him already.

Jan

CC Dom Cote <[email protected]>
CC Jeff Mahoney <[email protected]>
Cc Jonathan Corbet <[email protected]>
CC Kieran Bingham <[email protected]>
CC Kieran Bingham <[email protected]>

Dom Cote (2):
scripts/gdb: Improve types abstraction for gdb python scripts scripts/gdb: Fix issue with dmesg.py and python 3.X

Jan Kiszka (2):
scripts/gdb: Adjust module reference counter reported by lx-lsmod scripts/gdb: Cast CPU numbers to integer

Kieran Bingham (14):
scripts/gdb: Provide linux constants scripts/gdb: Provide kernel list item generators scripts/gdb: Convert modules usage to lists functions scripts/gdb: Provide exception catching parser scripts/gdb: Support !CONFIG_MODULES gracefully scripts/gdb: Provide a dentry_name VFS path helper scripts/gdb: Add io resource readers scripts/gdb: Add mount point list command scripts/gdb: Add cpu iterators
scripts/gdb: Add a Radix Tree Parser scripts/gdb: Add documentation example for radix tree scripts/gdb: Add lx_thread_info_by_pid helper scripts/gdb: decode bytestream on dmesg for Python3
MAINTAINERS: Add co-maintainer for scripts/gdb

 Documentation/gdb-kernel-debugging.txt |  21 +++++
 Kbuild                                 |  10 +++
 MAINTAINERS                            |   1 +
 scripts/gdb/linux/Makefile             |  12 ++-
 scripts/gdb/linux/constants.py.in      |  59 +++++++++++++
 scripts/gdb/linux/cpus.py              |  38 ++++++++
 scripts/gdb/linux/dmesg.py             |  11 +--
 scripts/gdb/linux/lists.py             |  21 +++++
 scripts/gdb/linux/modules.py           |  24 +++--
 scripts/gdb/linux/proc.py              | 156 +++++++++++++++++++++++++++++++++
 scripts/gdb/linux/radixtree.py         |  97 ++++++++++++++++++++
 scripts/gdb/linux/tasks.py             |  19 ++++
 scripts/gdb/linux/utils.py             |  32 ++++++-
 scripts/gdb/vmlinux-gdb.py             |   2 +
 14 files changed, 482 insertions(+), 21 deletions(-)
 create mode 100644 scripts/gdb/linux/constants.py.in
 create mode 100644 scripts/gdb/linux/radixtree.py
Signature · 1 line
2.1.4
author maintainer
#1 lore raw

PATCH 1/18 scripts/gdb: Adjust module reference counter reported by lx-lsmod

This takes the MODULE_REF_BASE into account.

---
 scripts/gdb/linux/modules.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/gdb/linux/modules.py +1 -1
index 0a35d6d..38f5d17 100644
@@ -78,7 +78,7 @@ class LxLsmod(gdb.Command):
                 address=str(layout['base']).split()[0],
                 name=module['name'].string(),
                 size=str(layout['size']),
-                ref=str(module['refcnt']['counter'])))
+                ref=str(module['refcnt']['counter'] - 1)))
 
             source_list = module['source_list']
             t = self._module_use_type.get_type().pointer()
Signature · 1 line
2.1.4
author maintainer
#2 lore raw

PATCH 2/18 scripts/gdb: Provide linux constants

In reply to Jan Kiszka

From: Kieran Bingham <[email protected]>

Some macro's and defines are needed when parsing memory, and without compiling the kernel as -g3 they are not available in the debug-symbols.

We use the pre-processor here to extract constants to a dedicated module for the linux debugger extensions

Top level Kbuild is used to call in and generate the constants file, while maintaining dependencies on autogenerated files in include/generated

---
 Kbuild                            | 10 ++++++++++
 scripts/gdb/linux/Makefile        | 12 +++++++++++-
 scripts/gdb/linux/constants.py.in | 32 ++++++++++++++++++++++++++++++++
 scripts/gdb/vmlinux-gdb.py        |  1 +
 4 files changed, 54 insertions(+), 1 deletion(-)
 create mode 100644 scripts/gdb/linux/constants.py.in
Kbuild +10 -0
index f55cefd..3d0ae15 100644
@@ -5,6 +5,7 @@
 # 2) Generate timeconst.h
 # 3) Generate asm-offsets.h (may need bounds.h and timeconst.h)
 # 4) Check for missing system calls
+# 5) Generate constants.py (may need bounds.h)
 
 # Default sed regexp - multiline due to syntax constraints
 define sed-y
@@ -96,5 +97,14 @@ quiet_cmd_syscalls = CALL    $<
 missing-syscalls: scripts/checksyscalls.sh $(offsets-file) FORCE
 	$(call cmd,syscalls)
 
+#####
+# 5) Generate constants for Python GDB integration
+#
+
+extra-$(CONFIG_GDB_SCRIPTS) += build_constants_py
+
+build_constants_py: $(obj)/$(timeconst-file) $(obj)/$(bounds-file)
+	@$(MAKE) $(build)=scripts/gdb/linux $@
+
 # Keep these three files during make clean
 no-clean-files := $(bounds-file) $(offsets-file) $(timeconst-file)
scripts/gdb/linux/Makefile +11 -1
index 6cf1ecf..cd129e6 100644
@@ -8,4 +8,14 @@ ifneq ($(KBUILD_SRC),)
 endif
 	@:
 
-clean-files := *.pyc *.pyo $(if $(KBUILD_SRC),*.py)
+quiet_cmd_gen_constants_py = GEN     $@
+      cmd_gen_constants_py = \
+	$(CPP) -E -x c -P $(c_flags) $< > $@ ;\
+	sed -i '1,/<!-- end-c-headers -->/d;' $@
+
+$(obj)/constants.py: $(SRCTREE)/$(obj)/constants.py.in
+	$(call if_changed,gen_constants_py)
+
+build_constants_py: $(obj)/constants.py
+
+clean-files := *.pyc *.pyo $(if $(KBUILD_SRC),*.py) $(obj)/constants.py
scripts/gdb/linux/constants.py.in new file +32 -0
new file mode 100644
index 0000000..79d9d00
@@ -0,0 +1,32 @@
+/*
+ * gdb helper commands and functions for Linux kernel debugging
+ *
+ *  Kernel constants derived from include files.
+ *
+ * Copyright (c) 2016 Linaro Ltd
+ *
+ * Authors:
+ *  Kieran Bingham <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2.
+ *
+ */
+
+/* We need to stringify expanded macros so that they can be parsed */
+#define STRING(x) #x
+#define XSTRING(x) STRING(x)
+
+#define LX_VALUE(x) LX_##x = x
+#define LX_GDBPARSED(x) LX_##x = gdb.parse_and_eval(XSTRING(x))
+
+/*
+ * IS_ENABLED generates (a || b) which is not compatible with python
+ * We can only switch on configuration items we know are available
+ * Therefore - IS_BUILTIN() is more appropriate
+ */
+#define LX_CONFIG(x) LX_##x = IS_BUILTIN(x)
+
+/* The build system will take care of deleting everything above this marker */
+<!-- end-c-headers -->
+
+import gdb
scripts/gdb/vmlinux-gdb.py +1 -0
index d5943ec..6e0b0af 100644
@@ -30,3 +30,4 @@ else:
     import linux.cpus
     import linux.lists
     import linux.proc
+    import linux.constants
Signature · 1 line
2.1.4
author maintainer
#3 lore raw

PATCH 3/18 scripts/gdb: Provide kernel list item generators

In reply to Jan Kiszka

From: Kieran Bingham <[email protected]>

Facilitate linked-list items by providing a generator to return the dereferenced, and type-cast objects from a kernel linked list

CC Jeff Mahoney <[email protected]>
---
 scripts/gdb/linux/lists.py | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
scripts/gdb/linux/lists.py +21 -0
index 3a3775b..2f335fb 100644
@@ -18,6 +18,27 @@ from linux import utils
 list_head = utils.CachedType("struct list_head")
 
 
+def list_for_each(head):
+    if head.type == list_head.get_type().pointer():
+        head = head.dereference()
+    elif head.type != list_head.get_type():
+        raise gdb.GdbError("Must be struct list_head not {}"
+                           .format(head.type))
+
+    node = head['next'].dereference()
+    while node.address != head.address:
+        yield node.address
+        node = node['next'].dereference()
+
+
+def list_for_each_entry(head, gdbtype, member):
+    for node in list_for_each(head):
+        if node.type != list_head.get_type().pointer():
+            raise TypeError("Type {} found. Expected struct list_head *."
+                            .format(node.type))
+        yield utils.container_of(node, gdbtype, member)
+
+
 def list_check(head):
     nb = 0
     if (head.type == list_head.get_type().pointer()):
Signature · 1 line
2.1.4
author maintainer
#4 lore raw

PATCH 4/18 scripts/gdb: Convert modules usage to lists functions

In reply to Jan Kiszka

From: Kieran Bingham <[email protected]>

Simplify the module list functions with the new list_for_each_entry abstractions

---
 scripts/gdb/linux/modules.py | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
scripts/gdb/linux/modules.py +6 -11
index 38f5d17..62557dd 100644
@@ -13,7 +13,7 @@
 
 import gdb
 
-from linux import cpus, utils
+from linux import cpus, utils, lists
 
 
 module_type = utils.CachedType("struct module")
@@ -23,12 +23,9 @@ def module_list():
     global module_type
     module_ptr_type = module_type.get_type().pointer()
     modules = gdb.parse_and_eval("modules")
-    entry = modules['next']
-    end_of_list = modules.address
 
-    while entry != end_of_list:
-        yield utils.container_of(entry, module_ptr_type, "list")
-        entry = entry['next']
+    for module in lists.list_for_each_entry(modules, module_ptr_type, "list"):
+        yield module
 
 
 def find_module_by_name(name):
@@ -80,17 +77,15 @@ class LxLsmod(gdb.Command):
                 size=str(layout['size']),
                 ref=str(module['refcnt']['counter'] - 1)))
 
-            source_list = module['source_list']
             t = self._module_use_type.get_type().pointer()
-            entry = source_list['next']
             first = True
-            while entry != source_list.address:
-                use = utils.container_of(entry, t, "source_list")
+            sources = module['source_list']
+            for use in lists.list_for_each_entry(sources, t, "source_list"):
                 gdb.write("{separator}{name}".format(
                     separator=" " if first else ",",
                     name=use['source']['name'].string()))
                 first = False
-                entry = entry['next']
+
             gdb.write("\n")
 
 
Signature · 1 line
2.1.4
author maintainer
#5 lore raw

PATCH 5/18 scripts/gdb: Provide exception catching parser

In reply to Jan Kiszka

From: Kieran Bingham <[email protected]>

If we attempt to read a value that is not available to GDB, an exception is raised. Most of the time, this is a good thing; however on occasion we will want to be able to determine if a symbol is available.

By catching the exception to simply return None, we can determine if we tried to read an invalid value, without the exception taking our execution context away from us

---
 scripts/gdb/linux/utils.py | 7 +++++++
 1 file changed, 7 insertions(+)
scripts/gdb/linux/utils.py +7 -0
index 0893b32..dbe2ad7 100644
@@ -154,3 +154,10 @@ def get_gdbserver_type():
         if gdbserver_type is not None and hasattr(gdb, 'events'):
             gdb.events.exited.connect(exit_handler)
     return gdbserver_type
+
+
+def gdb_eval_or_none(expresssion):
+    try:
+        return gdb.parse_and_eval(expresssion)
+    except:
+        return None
Signature · 1 line
2.1.4
author maintainer
#6 lore raw

PATCH 6/18 scripts/gdb: Support !CONFIG_MODULES gracefully

In reply to Jan Kiszka

From: Kieran Bingham <[email protected]>

If CONFIG_MODULES is not enabled, lx-lsmod tries to find a non-existent symbol and generates an unfriendly traceback:

(gdb) lx-lsmod
Address Module Size Used by
Traceback (most recent call last):
File "scripts/gdb/linux/modules.py", line 75, in invoke
for module in module_list():
File "scripts/gdb/linux/modules.py", line 24, in module_list
module_ptr_type = module_type.get_type().pointer()
File "scripts/gdb/linux/utils.py", line 28, in get_type
self._type = gdb.lookup_type(self._name)
gdb.error: No struct type named module.
Error occurred in Python command: No struct type named module.

Catch the error and return an empty module_list() for a clean command output as follows:

(gdb) lx-lsmod
Address Module Size Used by
(gdb)

---
 scripts/gdb/linux/modules.py | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
scripts/gdb/linux/modules.py +4 -1
index 62557dd..441b2323 100644
@@ -21,8 +21,11 @@ module_type = utils.CachedType("struct module")
 
 def module_list():
     global module_type
+    modules = utils.gdb_eval_or_none("modules")
+    if modules is None:
+        return
+
     module_ptr_type = module_type.get_type().pointer()
-    modules = gdb.parse_and_eval("modules")
 
     for module in lists.list_for_each_entry(modules, module_ptr_type, "list"):
         yield module
Signature · 1 line
2.1.4
author maintainer
#7 lore raw

PATCH 7/18 scripts/gdb: Provide a dentry_name VFS path helper

In reply to Jan Kiszka

From: Kieran Bingham <[email protected]>

Walk the VFS entries, pre-pending the iname strings to generate a full VFS path name from a dentry.

---
 scripts/gdb/linux/utils.py | 8 ++++++++
 1 file changed, 8 insertions(+)
scripts/gdb/linux/utils.py +8 -0
index dbe2ad7..de03a6b 100644
@@ -161,3 +161,11 @@ def gdb_eval_or_none(expresssion):
         return gdb.parse_and_eval(expresssion)
     except:
         return None
+
+
+def dentry_name(d):
+    parent = d['d_parent']
+    if parent == d or parent == 0:
+        return ""
+    p = dentry_name(d['d_parent']) + "/"
+    return p + d['d_iname'].string()
Signature · 1 line
2.1.4
author maintainer
#8 lore raw

PATCH 8/18 scripts/gdb: Add io resource readers

In reply to Jan Kiszka

From: Kieran Bingham <[email protected]>

Provide iomem_resource and ioports_resource printers and command hooks

It can be quite interesting to halt the kernel as it's booting and check to see this list as it is being populated.

It should be useful in the event that a kernel is not booting, you can identify what memory resources have been registered

---
 scripts/gdb/linux/proc.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
scripts/gdb/linux/proc.py +57 -0
index 6e6709c..d855b2f 100644
@@ -39,3 +39,60 @@ class LxVersion(gdb.Command):
         gdb.write(gdb.parse_and_eval("linux_banner").string())
 
 LxVersion()
+
+
+# Resource Structure Printers
+#  /proc/iomem
+#  /proc/ioports
+
+def get_resources(resource, depth):
+    while resource:
+        yield resource, depth
+
+        child = resource['child']
+        if child:
+            for res, deep in get_resources(child, depth + 1):
+                yield res, deep
+
+        resource = resource['sibling']
+
+
+def show_lx_resources(resource_str):
+        resource = gdb.parse_and_eval(resource_str)
+        width = 4 if resource['end'] < 0x10000 else 8
+        # Iterate straight to the first child
+        for res, depth in get_resources(resource['child'], 0):
+            start = int(res['start'])
+            end = int(res['end'])
+            gdb.write(" " * depth * 2 +
+                      "{0:0{1}x}-".format(start, width) +
+                      "{0:0{1}x} : ".format(end, width) +
+                      res['name'].string() + "\n")
+
+
+class LxIOMem(gdb.Command):
+    """Identify the IO memory resource locations defined by the kernel
+
+Equivalent to cat /proc/iomem on a running target"""
+
+    def __init__(self):
+        super(LxIOMem, self).__init__("lx-iomem", gdb.COMMAND_DATA)
+
+    def invoke(self, arg, from_tty):
+        return show_lx_resources("iomem_resource")
+
+LxIOMem()
+
+
+class LxIOPorts(gdb.Command):
+    """Identify the IO port resource locations defined by the kernel
+
+Equivalent to cat /proc/ioports on a running target"""
+
+    def __init__(self):
+        super(LxIOPorts, self).__init__("lx-ioports", gdb.COMMAND_DATA)
+
+    def invoke(self, arg, from_tty):
+        return show_lx_resources("ioport_resource")
+
+LxIOPorts()
Signature · 1 line
2.1.4
author maintainer
#9 lore raw

PATCH 9/18 scripts/gdb: Add mount point list command

In reply to Jan Kiszka

From: Kieran Bingham <[email protected]>

lx-mounts will identify current mount points based on the 'init_task' namespace by default, as we do not yet have a kernel thread list implementation to select the current running thread.

Optionally, a user can specify a PID to list from that process' namespace

---
 scripts/gdb/linux/constants.py.in | 20 ++++++++
 scripts/gdb/linux/proc.py         | 99 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 119 insertions(+)
scripts/gdb/linux/constants.py.in +20 -0
index 79d9d00..7986f4e 100644
@@ -12,7 +12,11 @@
  *
  */
 
+#include <linux/fs.h>
+#include <linux/mount.h>
+
 /* We need to stringify expanded macros so that they can be parsed */
+
 #define STRING(x) #x
 #define XSTRING(x) STRING(x)
 
@@ -30,3 +34,19 @@
 <!-- end-c-headers -->
 
 import gdb
+
+/* linux/fs.h */
+LX_VALUE(MS_RDONLY)
+LX_VALUE(MS_SYNCHRONOUS)
+LX_VALUE(MS_MANDLOCK)
+LX_VALUE(MS_DIRSYNC)
+LX_VALUE(MS_NOATIME)
+LX_VALUE(MS_NODIRATIME)
+
+/* linux/mount.h */
+LX_VALUE(MNT_NOSUID)
+LX_VALUE(MNT_NODEV)
+LX_VALUE(MNT_NOEXEC)
+LX_VALUE(MNT_NOATIME)
+LX_VALUE(MNT_NODIRATIME)
+LX_VALUE(MNT_RELATIME)
scripts/gdb/linux/proc.py +99 -0

108 lines not on this page · raw message

Signature · 1 line
2.1.4
author maintainer
#10 lore raw

PATCH 10/18 scripts/gdb: Add cpu iterators

In reply to Jan Kiszka

From: Kieran Bingham <[email protected]>

The linux kernel provides macro's for iterating against values from the cpu_list masks. By providing some commonly used masks, we can mirror the kernels helper macros with easy to use generators.

---
 scripts/gdb/linux/cpus.py | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
scripts/gdb/linux/cpus.py +38 -0
index 4297b83..fc316fb 100644
@@ -100,6 +100,44 @@ def cpu_list(mask_name):
         yield cpu
 
 
+def each_online_cpu():
+    for cpu in cpu_list("__cpu_online_mask"):
+        yield cpu
+
+
+def each_present_cpu():
+    for cpu in cpu_list("__cpu_present_mask"):
+        yield cpu
+
+
+def each_possible_cpu():
+    for cpu in cpu_list("__cpu_possible_mask"):
+        yield cpu
+
+
+def each_active_cpu():
+    for cpu in cpu_list("__cpu_active_mask"):
+        yield cpu
+
+
+class LxCpus(gdb.Command):
+    """List CPU status arrays
+
+Displays the known state of each CPU based on the kernel masks
+and can help identify the state of hotplugged CPUs"""
+
+    def __init__(self):
+        super(LxCpus, self).__init__("lx-cpus", gdb.COMMAND_DATA)
+
+    def invoke(self, arg, from_tty):
+        gdb.write("Possible CPUs : {}\n".format(list(each_possible_cpu())))
+        gdb.write("Present CPUs  : {}\n".format(list(each_present_cpu())))
+        gdb.write("Online CPUs   : {}\n".format(list(each_online_cpu())))
+        gdb.write("Active CPUs   : {}\n".format(list(each_active_cpu())))
+
+LxCpus()
+
+
 class PerCpu(gdb.Function):
     """Return per-cpu variable.
 
Signature · 1 line
2.1.4
author maintainer
#11 lore raw

PATCH 11/18 scripts/gdb: Cast CPU numbers to integer

In reply to Jan Kiszka

We won't see more than 2 billion CPUs any time soon, and having cpu_list return long makes the output of lx-cpus a bit ugly.

---
 scripts/gdb/linux/cpus.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/gdb/linux/cpus.py +1 -1
index fc316fb..ca11e8d 100644
@@ -97,7 +97,7 @@ def cpu_list(mask_name):
         bits >>= 1
         bit += 1
 
-        yield cpu
+        yield int(cpu)
 
 
 def each_online_cpu():
Signature · 1 line
2.1.4
author maintainer
#12 lore raw

PATCH 12/18 scripts/gdb: Add a Radix Tree Parser

In reply to Jan Kiszka

From: Kieran Bingham <[email protected]>

Linux makes use of the Radix Tree data structure to store pointers indexed by integer values. This structure is utilised across many structures in the kernel including the IRQ descriptor tables, and several filesystems.

This module provides a method to lookup values from a structure given its head node.

Usage:

The function lx_radix_tree_lookup, must be given a symbol of type struct radix_tree_root, and an index into that tree.

The object returned is a generic integer value, and must be cast correctly to the type based on the storage in the data structure.

For example, to print the irq descriptor in the sparse irq_desc_tree at index 18, try the following:

(gdb) print (struct irq_desc)$lx_radix_tree_lookup(irq_desc_tree, 18)

---
 scripts/gdb/linux/constants.py.in |  7 +++
 scripts/gdb/linux/radixtree.py    | 97 +++++++++++++++++++++++++++++++++++++++
 scripts/gdb/vmlinux-gdb.py        |  1 +
 3 files changed, 105 insertions(+)
 create mode 100644 scripts/gdb/linux/radixtree.py
scripts/gdb/linux/constants.py.in +7 -0
index 7986f4e..07e6c2b 100644
@@ -14,6 +14,7 @@
 
 #include <linux/fs.h>
 #include <linux/mount.h>
+#include <linux/radix-tree.h>
 
 /* We need to stringify expanded macros so that they can be parsed */
 
@@ -50,3 +51,9 @@ LX_VALUE(MNT_NOEXEC)
 LX_VALUE(MNT_NOATIME)
 LX_VALUE(MNT_NODIRATIME)
 LX_VALUE(MNT_RELATIME)
+
+/* linux/radix-tree.h */
+LX_VALUE(RADIX_TREE_INDIRECT_PTR)
+LX_GDBPARSED(RADIX_TREE_HEIGHT_MASK)
+LX_GDBPARSED(RADIX_TREE_MAP_SHIFT)
+LX_GDBPARSED(RADIX_TREE_MAP_MASK)
scripts/gdb/linux/radixtree.py new file +97 -0

97 lines not on this page · raw message

scripts/gdb/vmlinux-gdb.py +1 -0
index 6e0b0af..3a80ad6 100644
@@ -31,3 +31,4 @@ else:
     import linux.lists
     import linux.proc
     import linux.constants
+    import linux.radixtree
Signature · 1 line
2.1.4
author maintainer
#13 lore raw

PATCH 13/18 scripts/gdb: Add documentation example for radix tree

In reply to Jan Kiszka

From: Kieran Bingham <[email protected]>

Provide a worked example for utilising the lx_radix_tree_lookup function

Cc Jonathan Corbet <[email protected]>
---
 Documentation/gdb-kernel-debugging.txt | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
Documentation/gdb-kernel-debugging.txt +21 -0
index 7050ce8..4ab7d43 100644
@@ -139,6 +139,27 @@ Examples of using the Linux-provided gdb helpers
       start_comm = "swapper/2\000\000\000\000\000\000"
     }
 
+ o Dig into a radix tree data structure, such as the IRQ descriptors:
+    (gdb) print (struct irq_desc)$lx_radix_tree_lookup(irq_desc_tree, 18)
+    $6 = {
+      irq_common_data = {
+        state_use_accessors = 67584,
+        handler_data = 0x0 <__vectors_start>,
+        msi_desc = 0x0 <__vectors_start>,
+        affinity = {{
+            bits = {65535}
+          }}
+      },
+      irq_data = {
+        mask = 0,
+        irq = 18,
+        hwirq = 27,
+        common = 0xee803d80,
+        chip = 0xc0eb0854 <gic_data>,
+        domain = 0xee808000,
+        parent_data = 0x0 <__vectors_start>,
+        chip_data = 0xc0eb0854 <gic_data>
+      } <... trimmed ...>
 
 List of commands and functions
 ------------------------------
Signature · 1 line
2.1.4
author maintainer
#14 lore raw

PATCH 14/18 scripts/gdb: Add lx_thread_info_by_pid helper

In reply to Jan Kiszka

From: Kieran Bingham <[email protected]>

The tasks module already provides helpers to find the task struct by pid, and the thread_info by task struct; however this is cumbersome to utilise on the gdb commandline.

Wrap these two functionalities together in an extra single helper to allow exploring the thread info, from a PID value

---
 scripts/gdb/linux/tasks.py | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/gdb/linux/tasks.py +19 -0
index 862a4ae..1bf949c 100644
@@ -114,3 +114,22 @@ variable."""
 
 
 LxThreadInfoFunc()
+
+
+class LxThreadInfoByPidFunc (gdb.Function):
+    """Calculate Linux thread_info from task variable found by pid
+
+$lx_thread_info_by_pid(PID): Given PID, return the corresponding thread_info
+variable."""
+
+    def __init__(self):
+        super(LxThreadInfoByPidFunc, self).__init__("lx_thread_info_by_pid")
+
+    def invoke(self, pid):
+        task = get_task_by_pid(pid)
+        if task:
+            return get_thread_info(task.dereference())
+        else:
+            raise gdb.GdbError("No task of PID " + str(pid))
+
+LxThreadInfoByPidFunc()
Signature · 1 line
2.1.4
author maintainer
#15 lore raw

PATCH 15/18 scripts/gdb: Improve types abstraction for gdb python scripts

In reply to Jan Kiszka

From: Dom Cote <[email protected]>

Change the read_u16 function so it accepts both 'str' and 'byte' as type for the arguments.

When calling read_memory() from gdb API, depending on if it was built with 2.7 or 3.X, the format used to return the data will differ ( 'str' for 2.7, and 'byte' for 3.X ).

Add a function read_memoryview() to be able to get a 'memoryview' object back from read_memory() both with python 2.7 and 3.X .

Tested with python 3.4 and 2.7
Tested with gdb 7.7

---
 scripts/gdb/linux/utils.py | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
scripts/gdb/linux/utils.py +15 -2
index de03a6b..5080587 100644
@@ -87,11 +87,24 @@ def get_target_endianness():
     return target_endianness
 
 
+def read_memoryview(inf, start, length):
+    return memoryview(inf.read_memory(start, length))
+
+
 def read_u16(buffer):
+    value = [0, 0]
+
+    if type(buffer[0]) is str:
+        value[0] = ord(buffer[0])
+        value[1] = ord(buffer[1])
+    else:
+        value[0] = buffer[0]
+        value[1] = buffer[1]
+
     if get_target_endianness() == LITTLE_ENDIAN:
-        return ord(buffer[0]) + (ord(buffer[1]) << 8)
+        return value[0] + (value[1] << 8)
     else:
-        return ord(buffer[1]) + (ord(buffer[0]) << 8)
+        return value[1] + (value[0] << 8)
 
 
 def read_u32(buffer):
Signature · 1 line
2.1.4
author maintainer
#16 lore raw

PATCH 16/18 scripts/gdb: Fix issue with dmesg.py and python 3.X

In reply to Jan Kiszka

From: Dom Cote <[email protected]>

When built against Python 3, GDB differs in the return type for its read_memory function, causing the lx-dmesg command to fail.

Now that we have an improved read_16() we can use the new read_memoryview() abstraction to make lx-dmesg return valid data on both current Python APIs

Tested with python 3.4 and 2.7
Tested with gdb 7.7

[Kieran: Adjusted commit log to better reflect code changes]
---
 scripts/gdb/linux/dmesg.py | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
scripts/gdb/linux/dmesg.py +4 -3
index 927d0d2..04d6719 100644
@@ -33,11 +33,12 @@ class LxDmesg(gdb.Command):
         if log_first_idx < log_next_idx:
             log_buf_2nd_half = -1
             length = log_next_idx - log_first_idx
-            log_buf = inf.read_memory(start, length)
+            log_buf = utils.read_memoryview(inf, start, length).tobytes()
         else:
             log_buf_2nd_half = log_buf_len - log_first_idx
-            log_buf = inf.read_memory(start, log_buf_2nd_half) + \
-                inf.read_memory(log_buf_addr, log_next_idx)
+            a = utils.read_memoryview(inf, start, log_buf_2nd_half)
+            b = utils.read_memoryview(inf, log_buf_addr, log_next_idx)
+            log_buf = a.tobytes() + b.tobytes()
 
         pos = 0
         while pos < log_buf.__len__():
Signature · 1 line
2.1.4
author maintainer
#17 lore raw

PATCH 17/18 scripts/gdb: decode bytestream on dmesg for Python3

In reply to Jan Kiszka

From: Kieran Bingham <[email protected]>

The recent fixes to lx-dmesg, now allow the command to print successfully on Python3, however the python interpreter wraps the bytes for each line with a b'<text>' marker.

To remove this, we need to decode the line, where .decode() will default to 'UTF-8'

---
 scripts/gdb/linux/dmesg.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
scripts/gdb/linux/dmesg.py +2 -2
index 04d6719..f9b92ec 100644
@@ -51,10 +51,10 @@ class LxDmesg(gdb.Command):
                 continue
 
             text_len = utils.read_u16(log_buf[pos + 10:pos + 12])
-            text = log_buf[pos + 16:pos + 16 + text_len]
+            text = log_buf[pos + 16:pos + 16 + text_len].decode()
             time_stamp = utils.read_u64(log_buf[pos:pos + 8])
 
-            for line in memoryview(text).tobytes().splitlines():
+            for line in text.splitlines():
                 gdb.write("[{time:12.6f}] {line}\n".format(
                     time=time_stamp / 1000000000.0,
                     line=line))
Signature · 1 line
2.1.4
author maintainer
#18 lore raw

PATCH 18/18 MAINTAINERS: Add co-maintainer for scripts/gdb

In reply to Jan Kiszka

From: Kieran Bingham <[email protected]>

Add myself as a co-maintainer for scripts/gdb supporting Jan Kizka

---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
index a727d99..22c0d84 100644
@@ -4773,6 +4773,7 @@ F:	drivers/scsi/gdt*
 
 GDB KERNEL DEBUGGING HELPER SCRIPTS
 M:	Jan Kiszka <[email protected]>
+M:	Kieran Bingham <[email protected]>
 S:	Supported
 F:	scripts/gdb/
 
Signature · 1 line
2.1.4