Version 2 of this series is a rebase over 3.7-rc4 to resolve some minor
collision in the top-level makefile. Moreover, this implements automatic
symbol loading for kernel modules using silent breakpoints. See patch 3
for details.
Unless someone complains over this series or suggests a better workflow,
I'm planning to send a pull to Linus during the next merge window.
Here is the original intro for reference:
This adds the infrastructure and first tools that make kernel debugging
through gdb more comfortable. Since 7.0, gdb supports python scripting.
And this opens the doors to automate steps like the tedious loading of
module symbols at the right address, resolving per-cpu variables or even
retrieving the current kernel log without resuming an stopped target.
Many of the helpers naturally depend on the layout of structures or
internal mechanics of the kernel. So the best place to maintain such
things, keeping them consistent with the corresponding kernel is, well,
the kernel itself.
While these scripts have been originally developed for debugging via
QEMU/KVM, I've now also added the required bits for KGDB. Works fine,
but as QEMU/KVM tends to outperform KGDB it remains the recommendation
- when available.
There are two architecture dependencies so far, one regarding per-cpu,
the other regarding thread_info calculation. None of them I was able to
test on a target, so I'm counting on review/testing by the corresponding
communities.
This series should be considered the foundation of much more kernel
state exploration helper, e.g. around tasks, timers, locks, sockets -
I guess people will have even more ideas.
Hope it's useful!
PS: Also available via git://git.kiszka.org/linux.git queues/gdb-scripts
CC: "David S. Miller" <[email protected]>
CC: Fenghua Yu <[email protected]>
CC: Kay Sievers <[email protected]>
CC: [email protected]
CC: [email protected]
CC: Michal Marek <[email protected]>
CC: [email protected]
CC: Tony Luck <[email protected]>
Jan Kiszka (13):
scripts/gdb: Add infrastructure
scripts/gdb: Add container_of helper and convenience function
scripts/gdb: Add lx-symbols command
scripts/gdb: Add get_target_endianness helper
scripts/gdb: Add read_u16/32/64 helpers
scripts/gdb: Add lx-dmesg command
scripts/gdb: Add task iteration helper
scripts/gdb: Add helper and convenience function to look up tasks
scripts/gdb: Add is_target_arch helper
scripts/gdb: Add internal helper and convenience function to retrieve
thread_info
scripts/gdb: Add get_gdbserver_type helper
scripts/gdb: Add internal helper and convenience function for per-cpu
lookup
scripts/gdb: Add lx_current convenience function
Makefile | 5 +-
scripts/Makefile | 3 +-
scripts/gdb/Makefile | 9 +++
scripts/gdb/dmesg.py | 63 +++++++++++++++++++++
scripts/gdb/percpu.py | 76 +++++++++++++++++++++++++
scripts/gdb/symbols.py | 133 +++++++++++++++++++++++++++++++++++++++++++
scripts/gdb/task.py | 108 +++++++++++++++++++++++++++++++++++
scripts/gdb/utils.py | 134 ++++++++++++++++++++++++++++++++++++++++++++
scripts/gdb/vmlinux-gdb.py | 26 +++++++++
9 files changed, 555 insertions(+), 2 deletions(-)
create mode 100644 scripts/gdb/Makefile
create mode 100644 scripts/gdb/dmesg.py
create mode 100644 scripts/gdb/percpu.py
create mode 100644 scripts/gdb/symbols.py
create mode 100644 scripts/gdb/task.py
create mode 100644 scripts/gdb/utils.py
create mode 100644 scripts/gdb/vmlinux-gdb.py
--
1.7.3.4
This function allows to obtain a per-cpu variable, either of the current
or an explicitly specified CPU.
Note: sparc64 version is untested.
CC: "David S. Miller" <[email protected]>
CC: [email protected]
Signed-off-by: Jan Kiszka <[email protected]>
---
scripts/gdb/percpu.py | 61 ++++++++++++++++++++++++++++++++++++++++++++
scripts/gdb/vmlinux-gdb.py | 1 +
2 files changed, 62 insertions(+), 0 deletions(-)
create mode 100644 scripts/gdb/percpu.py
diff --git a/scripts/gdb/percpu.py b/scripts/gdb/percpu.py
new file mode 100644
index 0000000..6e24400
--- /dev/null
+++ b/scripts/gdb/percpu.py
@@ -0,0 +1,61 @@
+#
+# gdb helper commands and functions for Linux kernel debugging
+#
+# per-cpu tools
+#
+# Copyright (c) 2011, 2012 Siemens AG
+#
+# Authors:
+# Jan Kiszka <[email protected]>
+#
+# This work is licensed under the terms of the GNU GPL version 2.
+#
+
+import gdb
+
+from utils import *
+from task import *
+
+MAX_CPUS = 4096
+
+def get_current_cpu():
+ if get_gdbserver_type() == GDBSERVER_QEMU:
+ return gdb.selected_thread().num - 1
+ elif get_gdbserver_type() == GDBSERVER_KGDB:
+ tid = gdb.selected_thread().ptid[2]
+ if tid > (0x100000000 - MAX_CPUS - 2):
+ return 0x100000000 - tid - 2
+ else:
+ return get_thread_info(get_task_by_pid(tid))['cpu']
+ else:
+ raise gdb.GdbError("Sorry, obtaining the current CPU is "
+ "not yet supported with this gdb server.")
+
+def per_cpu(var_name, cpu):
+ var = gdb.parse_and_eval("&" + var_name)
+ if cpu == -1:
+ cpu = get_current_cpu()
+ if is_target_arch("sparc:v9"):
+ offset = gdb.parse_and_eval("trap_block[" + str(cpu) +
+ "].__per_cpu_base")
+ else:
+ offset = gdb.parse_and_eval("__per_cpu_offset[" + str(cpu) +
+ "]")
+ pointer = var.cast(get_long_type()) + offset
+ return pointer.cast(var.type).dereference()
+
+
+class PerCpu(gdb.Function):
+ __doc__ = "Return per-cpu variable.\n" \
+ "\n" \
+ "$lx_per_cpu(\"VAR\"[, CPU]): Return the per-cpu variable called VAR for the\n" \
+ "given CPU number. If CPU is omitted, the CPU of the current context is used.\n" \
+ "Note that VAR has to be quoted as string."
+
+ def __init__(self):
+ super(PerCpu, self).__init__("lx_per_cpu")
+
+ def invoke(self, var_name, cpu = -1):
+ return per_cpu(var_name.string(), cpu)
+
+PerCpu()
diff --git a/scripts/gdb/vmlinux-gdb.py b/scripts/gdb/vmlinux-gdb.py
index deaf652..1d952b5 100644
--- a/scripts/gdb/vmlinux-gdb.py
+++ b/scripts/gdb/vmlinux-gdb.py
@@ -23,3 +23,4 @@ else:
import symbols
import dmesg
import task
+ import percpu
--
1.7.3.4
This is probably the most useful helper when debugging kernel modules:
lx-symbols will first reload vmlinux. Then it searches recursively for
*.ko files in the specified paths and the current directory. Finally it
walks the kernel's module list, issuing the necessary add-symbol-file
command for each loaded module so that gdb know which module symbol
correspond to which address.
In addition, the command installs a silent breakpoint in the
sys_init_module function at the point where the module was loaded but
not yet initialized. The breakpoint handler will try to load symbols
from the module files found during lx-symbols execution. This way,
breakpoints can be set to module initialization functions, and there is
usually no need to explicitly call lx-symbols after (re-)loading a
module.
Signed-off-by: Jan Kiszka <[email protected]>
---
scripts/gdb/symbols.py | 133 ++++++++++++++++++++++++++++++++++++++++++++
scripts/gdb/vmlinux-gdb.py | 1 +
2 files changed, 134 insertions(+), 0 deletions(-)
create mode 100644 scripts/gdb/symbols.py
diff --git a/scripts/gdb/symbols.py b/scripts/gdb/symbols.py
new file mode 100644
index 0000000..ddba327
--- /dev/null
+++ b/scripts/gdb/symbols.py
@@ -0,0 +1,133 @@
+#
+# gdb helper commands and functions for Linux kernel debugging
+#
+# load kernel and module symbols
+#
+# Copyright (c) 2011, 2012 Siemens AG
+#
+# Authors:
+# Jan Kiszka <[email protected]>
+#
+# This work is licensed under the terms of the GNU GPL version 2.
+#
+
+import gdb
+import os
+import re
+import string
+
+from utils import *
+
+class LinuxSymbols(gdb.Command):
+ __doc__ = "(Re-)load symbols of Linux kernel and currently loaded modules.\n" \
+ "\n" \
+ "The kernel (vmlinux) is taken from the current working directly. Modules (.ko)\n" \
+ "are scanned recursively, starting in the same directory. Optionally, the module\n" \
+ "search path can be extended by a space separated list of paths passed to the\n" \
+ "lx-symbols command."
+
+ module_files = []
+ breakpoint = None
+
+ class _LoadModuleBreakpoint(gdb.Breakpoint):
+ def __init__(self, spec, gdb_command):
+ super(LinuxSymbols._LoadModuleBreakpoint,
+ self).__init__(spec, internal = True)
+ self.silent = True
+ self.gdb_command = gdb_command
+
+ def stop(self):
+ module = gdb.parse_and_eval("mod")
+ self.gdb_command._load_module_symbols(module)
+ return False
+
+ def __init__(self):
+ super(LinuxSymbols, self).__init__("lx-symbols",
+ gdb.COMMAND_FILES,
+ gdb.COMPLETE_FILENAME)
+
+ def _load_module_symbols(self, module):
+ module_name = module['name'].string()
+ module_addr = str(module['module_core'])
+ module_pattern = ".*/" + \
+ string.replace(module_name, "_", r"[_\-]") + r"\.ko$"
+ module_path = ""
+ for name in self.module_files:
+ if re.match(module_pattern, name):
+ module_path = name
+ break
+
+ if module_path:
+ print "loading @" + module_addr + ": " + module_path
+ if gdb.VERSION >= "7.2":
+ gdb.execute("add-symbol-file " + \
+ module_path + " " + module_addr,
+ to_string = True)
+ else:
+ gdb.execute("add-symbol-file " + \
+ module_path + " " + module_addr)
+ else:
+ print "no module object found for '" + \
+ module_name + "'"
+
+ def _find_breakpoint_location(self):
+ breakpoint_match = \
+ "^[0-9]*[\t]*blocking_notifier_call_chain\(" \
+ "&module_notify_list,$"
+
+ src = gdb.execute("list sys_init_module", to_string = True)
+ done = False
+ lineno = None
+ while not done:
+ src = gdb.execute("list", to_string = True)
+ for line in src.splitlines():
+ if re.match(breakpoint_match, line):
+ done = True
+ lineno = line.split()[0]
+ break
+ elif re.match("^[0-9]*\t}$", line):
+ done = True
+ break
+ return lineno
+
+ def invoke(self, arg, from_tty):
+ print "loading vmlinux"
+ gdb.execute("symbol-file vmlinux")
+
+ modules = gdb.parse_and_eval("modules")
+ entry = modules['next']
+ if entry == modules.address:
+ print "no modules found"
+ return
+
+ self.module_files = []
+ paths = arg.split()
+ paths.append(os.getcwd())
+ for path in paths:
+ print "scanning for modules in " + path
+ for root, dirs, files in os.walk(path):
+ for name in files:
+ if re.match(r".*\.ko$", name):
+ self.module_files.append(
+ root + "/" + name)
+
+ module_type = gdb.lookup_type("struct module").pointer()
+
+ while entry != modules.address:
+ module = container_of(entry, module_type, "list")
+ self._load_module_symbols(module)
+ entry = entry['next']
+
+ if gdb.VERSION < "7.2":
+ print "Note: symbol update on module loading not " \
+ "supported with this gdb version"
+ elif self.breakpoint == None:
+ lineno = self._find_breakpoint_location()
+ if lineno != None:
+ self.breakpoint = self._LoadModuleBreakpoint(
+ lineno, self)
+ else:
+ print "unable to detect breakpoint location " \
+ "of load module completion"
+
+LinuxSymbols()
diff --git a/scripts/gdb/vmlinux-gdb.py b/scripts/gdb/vmlinux-gdb.py
index 62c30b8..fa1d5e1 100644
--- a/scripts/gdb/vmlinux-gdb.py
+++ b/scripts/gdb/vmlinux-gdb.py
@@ -20,3 +20,4 @@ if not gdb.VERSION >= "7.1":
"to work."
else:
import utils
+ import symbols
--
1.7.3.4
This helper probes the type of the gdb server. Supported are QEMU and
KGDB so far. Knowledge about the gdb server is required e.g. to retrieve
the current CPU or current task.
Signed-off-by: Jan Kiszka <[email protected]>
---
scripts/gdb/utils.py | 35 +++++++++++++++++++++++++++++++++++
1 files changed, 35 insertions(+), 0 deletions(-)
diff --git a/scripts/gdb/utils.py b/scripts/gdb/utils.py
index 31aaf53..ca60187 100644
--- a/scripts/gdb/utils.py
+++ b/scripts/gdb/utils.py
@@ -97,3 +97,38 @@ def is_target_arch(arch):
if target_arch == None:
target_arch = gdb.execute("show architecture", False, True)
return target_arch.find(arch) >= 0
+
+
+GDBSERVER_QEMU = 0
+GDBSERVER_KGDB = 1
+gdbserver_type = None
+
+def get_gdbserver_type():
+ def exit_handler(event):
+ global gdbserver_type
+ gdbserver_type = None
+ gdb.events.exited.disconnect(exit_handler)
+
+ def probe_qemu():
+ try:
+ return gdb.execute("monitor info version", False,
+ True) != ""
+ except:
+ return False
+
+ def probe_kgdb():
+ try:
+ thread_info = gdb.execute("info thread 2", False, True)
+ return thread_info.find("shadowCPU0") >= 0
+ except:
+ return False
+
+ global gdbserver_type
+ if gdbserver_type == None:
+ if probe_qemu():
+ gdbserver_type = GDBSERVER_QEMU
+ elif probe_kgdb():
+ gdbserver_type = GDBSERVER_KGDB
+ if gdbserver_type != None:
+ gdb.events.exited.connect(exit_handler)
+ return gdbserver_type
--
1.7.3.4
Parse the target endianness from the output of "show endian" and cache
the result to return it via the new helper get_target_endiannes. We will
need it for reading integers from buffers that contain target memory.
Signed-off-by: Jan Kiszka <[email protected]>
---
scripts/gdb/utils.py | 17 +++++++++++++++++
1 files changed, 17 insertions(+), 0 deletions(-)
diff --git a/scripts/gdb/utils.py b/scripts/gdb/utils.py
index 74682c3..a4e6df2 100644
--- a/scripts/gdb/utils.py
+++ b/scripts/gdb/utils.py
@@ -53,3 +53,20 @@ class ContainerOf(gdb.Function):
elementname.string())
ContainerOf()
+
+
+BIG_ENDIAN = 0
+LITTLE_ENDIAN = 1
+target_endianness = None
+
+def get_target_endianness():
+ global target_endianness
+ if target_endianness == None:
+ endian = gdb.execute("show endian", False, True)
+ if endian.find("little endian") >= 0:
+ target_endianness = LITTLE_ENDIAN
+ elif endian.find("big endian") >= 0:
+ target_endianness = BIG_ENDIAN
+ else:
+ raise gdb.GdgError("unknown endianness '%s'" % endian)
+ return target_endianness
--
1.7.3.4
Add the helper task_by_pid that can look up a task by its PID. Also
export it as a convenience function.
Signed-off-by: Jan Kiszka <[email protected]>
---
scripts/gdb/task.py | 29 +++++++++++++++++++++++++++++
scripts/gdb/vmlinux-gdb.py | 1 +
2 files changed, 30 insertions(+), 0 deletions(-)
diff --git a/scripts/gdb/task.py b/scripts/gdb/task.py
index 1e4f61e..41d85cc 100644
--- a/scripts/gdb/task.py
+++ b/scripts/gdb/task.py
@@ -38,3 +38,32 @@ def for_each_task(func, arg = None):
get_task_ptr_type(), "thread_group")
if t == g:
break
+
+def get_task_by_pid(pid):
+ def match_pid(t, arg):
+ if int(t['pid']) == arg['pid']:
+ arg['task'] = t
+
+ arg = { 'pid': pid, 'task': None }
+ for_each_task(match_pid, arg)
+
+ return arg['task']
+
+
+class LxTaskByPidFunc(gdb.Function):
+ __doc__ = "Find Linux task by PID and return the task_struct variable.\n" \
+ "\n" \
+ "$lx_task_by_pid(PID): Given PID, iterate over all tasks of the target and\n" \
+ "return that task_struct variable which PID matches.\n"
+
+ def __init__(self):
+ super(LxTaskByPidFunc, self).__init__("lx_task_by_pid")
+
+ def invoke(self, pid):
+ task = get_task_by_pid(pid)
+ if task:
+ return task.dereference()
+ else:
+ raise gdb.GdbError("No task of PID " + str(pid))
+
+LxTaskByPidFunc()
diff --git a/scripts/gdb/vmlinux-gdb.py b/scripts/gdb/vmlinux-gdb.py
index 8b0422e..deaf652 100644
--- a/scripts/gdb/vmlinux-gdb.py
+++ b/scripts/gdb/vmlinux-gdb.py
@@ -22,3 +22,4 @@ else:
import utils
import symbols
import dmesg
+ import task
--
1.7.3.4
Provide an internal helper with container_of semantics. As type lookups
are very slow in gdb-python and we need a type "long" for this, cache
the reference to this type object. Then export the helper also as a
convenience function form use at the gdb command line.
Signed-off-by: Jan Kiszka <[email protected]>
---
scripts/gdb/utils.py | 55 ++++++++++++++++++++++++++++++++++++++++++++
scripts/gdb/vmlinux-gdb.py | 2 +
2 files changed, 57 insertions(+), 0 deletions(-)
create mode 100644 scripts/gdb/utils.py
diff --git a/scripts/gdb/utils.py b/scripts/gdb/utils.py
new file mode 100644
index 0000000..74682c3
--- /dev/null
+++ b/scripts/gdb/utils.py
@@ -0,0 +1,55 @@
+#
+# gdb helper commands and functions for Linux kernel debugging
+#
+# common utilities
+#
+# Copyright (c) 2011, 2012 Siemens AG
+#
+# Authors:
+# Jan Kiszka <[email protected]>
+#
+# This work is licensed under the terms of the GNU GPL version 2.
+#
+
+import gdb
+
+long_type = None
+
+def get_type(type_name):
+ t = gdb.lookup_type(type_name)
+ if t == None:
+ raise gdb.GdbError("cannot resolve type '%s'" % type_name)
+ return t
+
+def get_long_type():
+ global long_type
+ if long_type == None:
+ long_type = get_type("long")
+ return long_type
+
+
+def offset_of(typeobj, field):
+ element = gdb.Value(0).cast(typeobj)
+ return int(str(element[field].address).split()[0], 16)
+
+def container_of(ptr, typeobj, member):
+ return (ptr.cast(get_long_type()) -
+ offset_of(typeobj, member)).cast(typeobj)
+
+
+class ContainerOf(gdb.Function):
+ __doc__ = "Return pointer to containing data structure.\n" \
+ "\n" \
+ "$container_of(PTR, \"TYPE\", \"ELEMENT\"): Given PTR, return a pointer to the\n" \
+ "data structure of the type TYPE in which PTR is the address of ELEMENT.\n" \
+ "Note that TYPE and ELEMENT have to be quoted as strings."
+
+ def __init__(self):
+ super(ContainerOf, self).__init__("container_of")
+
+ def invoke(self, ptr, typename, elementname):
+ return container_of(ptr,
+ gdb.lookup_type(typename.string()).pointer(),
+ elementname.string())
+
+ContainerOf()
diff --git a/scripts/gdb/vmlinux-gdb.py b/scripts/gdb/vmlinux-gdb.py
index 00df617..62c30b8 100644
--- a/scripts/gdb/vmlinux-gdb.py
+++ b/scripts/gdb/vmlinux-gdb.py
@@ -18,3 +18,5 @@ sys.path = [ os.path.dirname(__file__) + "/scripts/gdb" ] + sys.path
if not gdb.VERSION >= "7.1":
print "NOTE: gdb 7.1 or later required for Linux helper scripts " \
"to work."
+else:
+ import utils
--
1.7.3.4
This provides the basic infrastructure to load kernel-specific python
helper scripts when debugging the kernel in gdb.
The loading mechanism is based on gdb loading for <objfile>-gdb.py when
opening <objfile>. Therefore, this places a corresponding link to the
main helper script into the output directory that contains vmlinux.
The main scripts will pull in submodules containing Linux specific gdb
commands and functions. To avoid polluting the source directory with
compiled python modules, we link to them from the object directory.
Due to gdb.parse_and_eval, we depend on gdb >= 7.1.
This feature depends on CONFIG_DEBUG_INFO.
CC: Michal Marek <[email protected]>
CC: [email protected]
Signed-off-by: Jan Kiszka <[email protected]>
---
Makefile | 5 ++++-
scripts/Makefile | 3 ++-
scripts/gdb/Makefile | 9 +++++++++
scripts/gdb/vmlinux-gdb.py | 20 ++++++++++++++++++++
4 files changed, 35 insertions(+), 2 deletions(-)
create mode 100644 scripts/gdb/Makefile
create mode 100644 scripts/gdb/vmlinux-gdb.py
diff --git a/Makefile b/Makefile
index a1ccf22..6af7c03 100644
--- a/Makefile
+++ b/Makefile
@@ -774,6 +774,9 @@ endif
ifdef CONFIG_BUILD_DOCSRC
$(Q)$(MAKE) $(build)=Documentation
endif
+ifdef CONFIG_DEBUG_INFO
+ $(Q)ln -fsn $(srctree)/scripts/gdb/vmlinux-gdb.py
+endif
+$(call if_changed,link-vmlinux)
# The actual objects are generated when descending,
@@ -1013,7 +1016,7 @@ MRPROPER_FILES += .config .config.old .version .old_version $(version_h) \
Module.symvers tags TAGS cscope* GPATH GTAGS GRTAGS GSYMS \
signing_key.priv signing_key.x509 x509.genkey \
extra_certificates signing_key.x509.keyid \
- signing_key.x509.signer
+ signing_key.x509.signer vmlinux-gdb.py
# clean - Delete most, but leave enough to build external modules
#
diff --git a/scripts/Makefile b/scripts/Makefile
index 01e7adb..3204b91 100644
--- a/scripts/Makefile
+++ b/scripts/Makefile
@@ -37,6 +37,7 @@ subdir-$(CONFIG_MODVERSIONS) += genksyms
subdir-y += mod
subdir-$(CONFIG_SECURITY_SELINUX) += selinux
subdir-$(CONFIG_DTC) += dtc
+subdir-$(CONFIG_DEBUG_INFO) += gdb
# Let clean descend into subdirs
-subdir- += basic kconfig package selinux
+subdir- += basic kconfig package selinux gdb
diff --git a/scripts/gdb/Makefile b/scripts/gdb/Makefile
new file mode 100644
index 0000000..34ccd06
--- /dev/null
+++ b/scripts/gdb/Makefile
@@ -0,0 +1,9 @@
+always := gdb-scripts
+
+$(obj)/gdb-scripts:
+ifneq ($(KBUILD_SRC),)
+ $(Q)ln -fsn $(srctree)/$(obj)/*.py $(objtree)/$(obj)
+endif
+ @:
+
+clean-files := *.pyc $(if $(KBUILD_SRC),*.py)
diff --git a/scripts/gdb/vmlinux-gdb.py b/scripts/gdb/vmlinux-gdb.py
new file mode 100644
index 0000000..00df617
--- /dev/null
+++ b/scripts/gdb/vmlinux-gdb.py
@@ -0,0 +1,20 @@
+#
+# gdb helper commands and functions for Linux kernel debugging
+#
+# loader module
+#
+# Copyright (c) 2012 Siemens AG
+#
+# Authors:
+# Jan Kiszka <[email protected]>
+#
+# This work is licensed under the terms of the GNU GPL version 2.
+#
+
+import os
+
+sys.path = [ os.path.dirname(__file__) + "/scripts/gdb" ] + sys.path
+
+if not gdb.VERSION >= "7.1":
+ print "NOTE: gdb 7.1 or later required for Linux helper scripts " \
+ "to work."
--
1.7.3.4
The internal helper for_each_task iterates over all tasks of the target,
calling the provided function on each. For performance reasons, we cache
a reference to the gdb type object of a task.
Signed-off-by: Jan Kiszka <[email protected]>
---
scripts/gdb/task.py | 40 ++++++++++++++++++++++++++++++++++++++++
1 files changed, 40 insertions(+), 0 deletions(-)
create mode 100644 scripts/gdb/task.py
diff --git a/scripts/gdb/task.py b/scripts/gdb/task.py
new file mode 100644
index 0000000..1e4f61e
--- /dev/null
+++ b/scripts/gdb/task.py
@@ -0,0 +1,40 @@
+#
+# gdb helper commands and functions for Linux kernel debugging
+#
+# task & thread tools
+#
+# Copyright (c) 2011, 2012 Siemens AG
+#
+# Authors:
+# Jan Kiszka <[email protected]>
+#
+# This work is licensed under the terms of the GNU GPL version 2.
+#
+
+import gdb
+
+from utils import *
+
+task_ptr_type = None
+
+def get_task_ptr_type():
+ global task_ptr_type
+ if task_ptr_type == None:
+ task_ptr_type = get_type("struct task_struct").pointer()
+ return task_ptr_type
+
+def for_each_task(func, arg = None):
+ init_task = gdb.parse_and_eval("init_task")
+ g = init_task.address
+ while True:
+ g = container_of(g['tasks']['next'], get_task_ptr_type(),
+ "tasks")
+ if g == init_task.address:
+ break;
+ t = g
+ while True:
+ func(t, arg)
+ t = container_of(t['thread_group']['next'],
+ get_task_ptr_type(), "thread_group")
+ if t == g:
+ break
--
1.7.3.4
This is a shorthand for *$lx_per_cpu("current_task"), i.e. a convenience
function to retrieve the currently running task of the active context.
Signed-off-by: Jan Kiszka <[email protected]>
---
scripts/gdb/percpu.py | 15 +++++++++++++++
1 files changed, 15 insertions(+), 0 deletions(-)
diff --git a/scripts/gdb/percpu.py b/scripts/gdb/percpu.py
index 6e24400..ecf72b6 100644
--- a/scripts/gdb/percpu.py
+++ b/scripts/gdb/percpu.py
@@ -59,3 +59,18 @@ class PerCpu(gdb.Function):
return per_cpu(var_name.string(), cpu)
PerCpu()
+
+
+class LxCurrentFunc(gdb.Function):
+ __doc__ = "Return current task.\n" \
+ "\n" \
+ "$lx_current([CPU]): Return the per-cpu task variable for the given CPU\n" \
+ "number. If CPU is omitted, the CPU of the current context is used."
+
+ def __init__(self):
+ super(LxCurrentFunc, self).__init__("lx_current")
+
+ def invoke(self, cpu = -1):
+ return per_cpu("current_task", cpu).dereference()
+
+LxCurrentFunc()
--
1.7.3.4
This pokes into the log buffer of the debugged kernel, dumping it to the
gdb console. Helping in case the target should or can no longer execute
dmesg itself.
CC: Kay Sievers <[email protected]>
Signed-off-by: Jan Kiszka <[email protected]>
---
scripts/gdb/dmesg.py | 63 ++++++++++++++++++++++++++++++++++++++++++++
scripts/gdb/vmlinux-gdb.py | 1 +
2 files changed, 64 insertions(+), 0 deletions(-)
create mode 100644 scripts/gdb/dmesg.py
diff --git a/scripts/gdb/dmesg.py b/scripts/gdb/dmesg.py
new file mode 100644
index 0000000..2f475bc
--- /dev/null
+++ b/scripts/gdb/dmesg.py
@@ -0,0 +1,63 @@
+#
+# gdb helper commands and functions for Linux kernel debugging
+#
+# kernel log buffer dump
+#
+# Copyright (c) 2011, 2012 Siemens AG
+#
+# Authors:
+# Jan Kiszka <[email protected]>
+#
+# This work is licensed under the terms of the GNU GPL version 2.
+#
+
+import gdb
+import string
+
+from utils import *
+
+class LinuxDmesg(gdb.Command):
+ __doc__ = "Print Linux kernel log buffer."
+
+ def __init__(self):
+ super(LinuxDmesg, self).__init__("lx-dmesg", gdb.COMMAND_DATA)
+
+ def invoke(self, arg, from_tty):
+ log_buf_addr = int(str(gdb.parse_and_eval("(void *)log_buf")),
+ 16)
+ log_first_idx = int(gdb.parse_and_eval("log_first_idx"))
+ log_next_idx = int(gdb.parse_and_eval("log_next_idx"))
+ log_buf_len = int(gdb.parse_and_eval("log_buf_len"))
+
+ inf = gdb.inferiors()[0]
+ start = log_buf_addr + log_first_idx
+ 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)
+ 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)
+
+ pos = 0
+ while pos < log_buf.__len__():
+ length = read_u16(log_buf[pos + 8 : pos + 10])
+ if length == 0:
+ if log_buf_2nd_half == -1:
+ print "Corrupted log buffer!"
+ break
+ pos = log_buf_2nd_half
+ continue
+
+ text_len = read_u16(log_buf[pos + 10 : pos + 12])
+ time_stamp = read_u64(log_buf[pos : pos + 8])
+
+ for line in log_buf[pos + 16 :
+ pos + 16 + text_len].splitlines():
+ print "[%13.6f] " % \
+ (time_stamp / 1000000000.0) + line
+
+ pos += length
+
+LinuxDmesg()
diff --git a/scripts/gdb/vmlinux-gdb.py b/scripts/gdb/vmlinux-gdb.py
index fa1d5e1..8b0422e 100644
--- a/scripts/gdb/vmlinux-gdb.py
+++ b/scripts/gdb/vmlinux-gdb.py
@@ -21,3 +21,4 @@ if not gdb.VERSION >= "7.1":
else:
import utils
import symbols
+ import dmesg
--
1.7.3.4
This helper caches to result of "show architecture" and matches the
provided arch (sub-)string against that output.
Signed-off-by: Jan Kiszka <[email protected]>
---
scripts/gdb/utils.py | 9 +++++++++
1 files changed, 9 insertions(+), 0 deletions(-)
diff --git a/scripts/gdb/utils.py b/scripts/gdb/utils.py
index 18e3bfc..31aaf53 100644
--- a/scripts/gdb/utils.py
+++ b/scripts/gdb/utils.py
@@ -88,3 +88,12 @@ def read_u64(buffer):
return read_u32(buffer[0:4]) + (read_u32(buffer[4:8]) << 32)
else:
return read_u32(buffer[4:8]) + (read_u32(buffer[0:4]) << 32)
+
+
+target_arch = None
+
+def is_target_arch(arch):
+ global target_arch
+ if target_arch == None:
+ target_arch = gdb.execute("show architecture", False, True)
+ return target_arch.find(arch) >= 0
--
1.7.3.4
Add helpers for reading integers from target memory buffers. Required
when caching the memory access is more efficient than reading individual
values via gdb.
Signed-off-by: Jan Kiszka <[email protected]>
---
scripts/gdb/utils.py | 18 ++++++++++++++++++
1 files changed, 18 insertions(+), 0 deletions(-)
diff --git a/scripts/gdb/utils.py b/scripts/gdb/utils.py
index a4e6df2..18e3bfc 100644
--- a/scripts/gdb/utils.py
+++ b/scripts/gdb/utils.py
@@ -70,3 +70,21 @@ def get_target_endianness():
else:
raise gdb.GdgError("unknown endianness '%s'" % endian)
return target_endianness
+
+def read_u16(buffer):
+ if get_target_endianness() == LITTLE_ENDIAN:
+ return ord(buffer[0]) + (ord(buffer[1]) << 8)
+ else:
+ return ord(buffer[1]) + (ord(buffer[0]) << 8)
+
+def read_u32(buffer):
+ if get_target_endianness() == LITTLE_ENDIAN:
+ return read_u16(buffer[0:2]) + (read_u16(buffer[2:4]) << 16)
+ else:
+ return read_u16(buffer[2:4]) + (read_u16(buffer[0:2]) << 16)
+
+def read_u64(buffer):
+ if get_target_endianness() == LITTLE_ENDIAN:
+ return read_u32(buffer[0:4]) + (read_u32(buffer[4:8]) << 32)
+ else:
+ return read_u32(buffer[4:8]) + (read_u32(buffer[0:4]) << 32)
--
1.7.3.4
Add the internal helper get_thread_info that calculated the thread_info
from a given task variable. Also export this service as a convenience
function.
Note: ia64 version is untested.
CC: Tony Luck <[email protected]>
CC: Fenghua Yu <[email protected]>
CC: [email protected]
Signed-off-by: Jan Kiszka <[email protected]>
---
scripts/gdb/task.py | 39 +++++++++++++++++++++++++++++++++++++++
1 files changed, 39 insertions(+), 0 deletions(-)
diff --git a/scripts/gdb/task.py b/scripts/gdb/task.py
index 41d85cc..523e495 100644
--- a/scripts/gdb/task.py
+++ b/scripts/gdb/task.py
@@ -67,3 +67,42 @@ class LxTaskByPidFunc(gdb.Function):
raise gdb.GdbError("No task of PID " + str(pid))
LxTaskByPidFunc()
+
+
+thread_info_ptr_type = None
+
+def get_thread_info_ptr_type():
+ global thread_info_ptr_type
+ if thread_info_ptr_type == None:
+ thread_info_ptr_type = get_type('struct thread_info').pointer()
+ return thread_info_ptr_type
+
+ia64_task_size = None
+
+def get_thread_info(task):
+ if is_target_arch("ia64"):
+ global ia64_task_size
+ if ia64_task_size == None:
+ ia64_task_size = gdb.parse_and_eval(
+ "sizeof(struct task_struct)")
+ thread_info_addr = task.address + ia64_task_size
+ thread_info = thread_info_addr.cast(get_thread_info_ptr_type())
+ else:
+ thread_info = task['stack'].cast(get_thread_info_ptr_type())
+ return thread_info.dereference()
+
+
+class LxThreadInfoFunc (gdb.Function):
+ # Calculate Linux thread_info from task variable.
+ __doc__ = "Calculate Linux thread_info from task variable.\n" \
+ "\n" \
+ "$lx_thread_info(TASK): Given TASK, return the corresponding thread_info\n" \
+ "variable.\n"
+
+ def __init__(self):
+ super(LxThreadInfoFunc, self).__init__("lx_thread_info")
+
+ def invoke(self, task):
+ return get_thread_info(task)
+
+LxThreadInfoFunc()
--
1.7.3.4