Skip to contentSkip to conversation

scripts/qemu-gdb: Split into modules

PATCH In review Peter Maydell 2 participants 7 messages Aug 14, 2015 – Sep 11, 2015

lore
Share & tools
lore
#0 lore raw

v2 cover scripts/qemu-gdb: Split into modules

This patch series splits scripts/qemu-gdb into separate files for each command, to make it easier to add new commands in future without them all going into a single huge file.

Patches 1 and 2 do that split, and have been on the list before (and reviewed by Stefan).

Patch 3 makes the script tell gdb not to stop on SIGUSR1, since that's our SIGIPI and happens all the time.

Patch 4 adds a brief comment explaining how to source the script. NB that I haven't attempted to make the script work with automatic-sourcing based on the executable being debugged. Somebody who uses the script that way can do that :-)

I've dropped the script which added support for setting breakpoints on QEMU tracepoints for the moment (pending investigating static probe point support which might render it unnecessary). Mostly I'd like to get these into master so that future debug commands can go into the right place -- IIRC David Gilbert had a patchset which added another command.

thanks
-- PMM

Peter Maydell (4):
scripts/qemu-gdb: Split MtreeCommand into its own module scripts/qemu-gdb: Split CoroutineCommand into its own file scripts/qemu-gdb: Silently pass through SIGUSR1 scripts/qemu-gdb: Add brief comment describing usage

 scripts/qemu-gdb.py          | 146 ++++---------------------------------------
 scripts/qemugdb/__init__.py  |  28 +++++++++
 scripts/qemugdb/coroutine.py |  91 +++++++++++++++++++++++++++
 scripts/qemugdb/mtree.py     |  82 ++++++++++++++++++++++++
 4 files changed, 212 insertions(+), 135 deletions(-)
 create mode 100644 scripts/qemugdb/__init__.py
 create mode 100644 scripts/qemugdb/coroutine.py
 create mode 100644 scripts/qemugdb/mtree.py
Signature · 1 line
1.9.1
#1 lore raw

PATCH v2 1/4 scripts/qemu-gdb: Split MtreeCommand into its own module

As we add more commands to our Python gdb debugging support, it's going to get unwieldy to have everything in a single file. Split the implementation of the 'mtree' command from qemu-gdb.py into its own module.

---
 scripts/qemu-gdb.py         | 70 +++++---------------------------------
 scripts/qemugdb/__init__.py | 28 ++++++++++++++++
 scripts/qemugdb/mtree.py    | 82 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 118 insertions(+), 62 deletions(-)
 create mode 100644 scripts/qemugdb/__init__.py
 create mode 100644 scripts/qemugdb/mtree.py
scripts/qemu-gdb.py +8 -62
index 6c7f4fb..7b072ad 100644
@@ -16,11 +16,14 @@
 
 import gdb
 
-def isnull(ptr):
-    return ptr == gdb.Value(0).cast(ptr.type)
+import os, sys
 
-def int128(p):
-    return long(p['lo']) + (long(p['hi']) << 64)
+# Annoyingly, gdb doesn't put the directory of scripts onto the
+# module search path. Do it manually.
+
+sys.path.append(os.path.dirname(__file__))
+
+from qemugdb import mtree
 
 def get_fs_base():
     '''Fetch %fs base value using arch_prctl(ARCH_GET_FS)'''
@@ -102,63 +105,6 @@ class CoroutineCommand(gdb.Command):
         coroutine_pointer = gdb.parse_and_eval(argv[0]).cast(gdb.lookup_type('CoroutineUContext').pointer())
         bt_jmpbuf(coroutine_pointer['env']['__jmpbuf'])
 
-class MtreeCommand(gdb.Command):
-    '''Display the memory tree hierarchy'''
-    def __init__(self):
-        gdb.Command.__init__(self, 'qemu mtree', gdb.COMMAND_DATA,
-                             gdb.COMPLETE_NONE)
-        self.queue = []
-    def invoke(self, arg, from_tty):
-        self.seen = set()
-        self.queue_root('address_space_memory')
-        self.queue_root('address_space_io')
-        self.process_queue()
-    def queue_root(self, varname):
-        ptr = gdb.parse_and_eval(varname)['root']
-        self.queue.append(ptr)
-    def process_queue(self):
-        while self.queue:
-            ptr = self.queue.pop(0)
-            if long(ptr) in self.seen:
-                continue
-            self.print_item(ptr)
-    def print_item(self, ptr, offset = gdb.Value(0), level = 0):
-        self.seen.add(long(ptr))
-        addr = ptr['addr']
-        addr += offset
-        size = int128(ptr['size'])
-        alias = ptr['alias']
-        klass = ''
-        if not isnull(alias):
-            klass = ' (alias)'
-        elif not isnull(ptr['ops']):
-            klass = ' (I/O)'
-        elif bool(ptr['ram']):
-            klass = ' (RAM)'
-        gdb.write('%s%016x-%016x %s%s (@ %s)\n'
-                  % ('  ' * level,
-                     long(addr),
-                     long(addr + (size - 1)),
-                     ptr['name'].string(),
-                     klass,
-                     ptr,
-                     ),
-                  gdb.STDOUT)
-        if not isnull(alias):
-            gdb.write('%s    alias: %s@%016x (@ %s)\n' %
-                      ('  ' * level,
-                       alias['name'].string(),
-                       ptr['alias_offset'],
-                       alias,
-                       ),
-                      gdb.STDOUT)
-            self.queue.append(alias)
-        subregion = ptr['subregions']['tqh_first']
-        level += 1
-        while not isnull(subregion):
-            self.print_item(subregion, addr, level)
-            subregion = subregion['subregions_link']['tqe_next']
-
 QemuCommand()
 CoroutineCommand()
-MtreeCommand()
+mtree.MtreeCommand()
scripts/qemugdb/__init__.py new file +28 -0
new file mode 100644
index 0000000..969f552
@@ -0,0 +1,28 @@
+#!/usr/bin/python
+
+# GDB debugging support
+#
+# Copyright (c) 2015 Linaro Ltd
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, see
+# <http://www.gnu.org/licenses/gpl-2.0.html>
+#
+
+# We don't need to do anything in our init file currently.
+
+"""
+Support routines for debugging QEMU under GDB
+"""
+
+__license__    = "GPL version 2 or (at your option) any later version"
scripts/qemugdb/mtree.py new file +82 -0

82 lines not on this page · raw message

Signature · 1 line
1.9.1
#2 lore raw

PATCH v2 2/4 scripts/qemu-gdb: Split CoroutineCommand into its own file

In reply to Peter Maydell

Split the implementation of CoroutineCommand into its own file.

---
 scripts/qemu-gdb.py          | 78 +------------------------------------
 scripts/qemugdb/coroutine.py | 91 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+), 76 deletions(-)
 create mode 100644 scripts/qemugdb/coroutine.py
scripts/qemu-gdb.py +2 -76
index 7b072ad..1c94b2a 100644
@@ -23,66 +23,7 @@ import os, sys
 
 sys.path.append(os.path.dirname(__file__))
 
-from qemugdb import mtree
-
-def get_fs_base():
-    '''Fetch %fs base value using arch_prctl(ARCH_GET_FS)'''
-    # %rsp - 120 is scratch space according to the SystemV ABI
-    old = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)')
-    gdb.execute('call arch_prctl(0x1003, $rsp - 120)', False, True)
-    fs_base = gdb.parse_and_eval('*(uint64_t*)($rsp - 120)')
-    gdb.execute('set *(uint64_t*)($rsp - 120) = %s' % old, False, True)
-    return fs_base
-
-def get_glibc_pointer_guard():
-    '''Fetch glibc pointer guard value'''
-    fs_base = get_fs_base()
-    return gdb.parse_and_eval('*(uint64_t*)((uint64_t)%s + 0x30)' % fs_base)
-
-def glibc_ptr_demangle(val, pointer_guard):
-    '''Undo effect of glibc's PTR_MANGLE()'''
-    return gdb.parse_and_eval('(((uint64_t)%s >> 0x11) | ((uint64_t)%s << (64 - 0x11))) ^ (uint64_t)%s' % (val, val, pointer_guard))
-
-def bt_jmpbuf(jmpbuf):
-    '''Backtrace a jmpbuf'''
-    JB_RBX  = 0
-    JB_RBP  = 1
-    JB_R12  = 2
-    JB_R13  = 3
-    JB_R14  = 4
-    JB_R15  = 5
-    JB_RSP  = 6
-    JB_PC   = 7
-
-    old_rbx = gdb.parse_and_eval('(uint64_t)$rbx')
-    old_rbp = gdb.parse_and_eval('(uint64_t)$rbp')
-    old_rsp = gdb.parse_and_eval('(uint64_t)$rsp')
-    old_r12 = gdb.parse_and_eval('(uint64_t)$r12')
-    old_r13 = gdb.parse_and_eval('(uint64_t)$r13')
-    old_r14 = gdb.parse_and_eval('(uint64_t)$r14')
-    old_r15 = gdb.parse_and_eval('(uint64_t)$r15')
-    old_rip = gdb.parse_and_eval('(uint64_t)$rip')
-
-    pointer_guard = get_glibc_pointer_guard()
-    gdb.execute('set $rbx = %s' % jmpbuf[JB_RBX])
-    gdb.execute('set $rbp = %s' % glibc_ptr_demangle(jmpbuf[JB_RBP], pointer_guard))
-    gdb.execute('set $rsp = %s' % glibc_ptr_demangle(jmpbuf[JB_RSP], pointer_guard))
-    gdb.execute('set $r12 = %s' % jmpbuf[JB_R12])
-    gdb.execute('set $r13 = %s' % jmpbuf[JB_R13])
-    gdb.execute('set $r14 = %s' % jmpbuf[JB_R14])
-    gdb.execute('set $r15 = %s' % jmpbuf[JB_R15])
-    gdb.execute('set $rip = %s' % glibc_ptr_demangle(jmpbuf[JB_PC], pointer_guard))
-
-    gdb.execute('bt')
-
-    gdb.execute('set $rbx = %s' % old_rbx)
-    gdb.execute('set $rbp = %s' % old_rbp)
-    gdb.execute('set $rsp = %s' % old_rsp)
-    gdb.execute('set $r12 = %s' % old_r12)
-    gdb.execute('set $r13 = %s' % old_r13)
-    gdb.execute('set $r14 = %s' % old_r14)
-    gdb.execute('set $r15 = %s' % old_r15)
-    gdb.execute('set $rip = %s' % old_rip)
+from qemugdb import mtree, coroutine
 
 class QemuCommand(gdb.Command):
     '''Prefix for QEMU debug support commands'''
@@ -90,21 +31,6 @@ class QemuCommand(gdb.Command):
         gdb.Command.__init__(self, 'qemu', gdb.COMMAND_DATA,
                              gdb.COMPLETE_NONE, True)
 
-class CoroutineCommand(gdb.Command):
-    '''Display coroutine backtrace'''
-    def __init__(self):
-        gdb.Command.__init__(self, 'qemu coroutine', gdb.COMMAND_DATA,
-                             gdb.COMPLETE_NONE)
-
-    def invoke(self, arg, from_tty):
-        argv = gdb.string_to_argv(arg)
-        if len(argv) != 1:
-            gdb.write('usage: qemu coroutine <coroutine-pointer>\n')
-            return
-
-        coroutine_pointer = gdb.parse_and_eval(argv[0]).cast(gdb.lookup_type('CoroutineUContext').pointer())
-        bt_jmpbuf(coroutine_pointer['env']['__jmpbuf'])
-
 QemuCommand()
-CoroutineCommand()
+coroutine.CoroutineCommand()
 mtree.MtreeCommand()
scripts/qemugdb/coroutine.py new file +91 -0

91 lines not on this page · raw message

Signature · 1 line
1.9.1
#3 lore raw

PATCH v2 3/4 scripts/qemu-gdb: Silently pass through SIGUSR1

In reply to Peter Maydell

SIGUSR1 is QEMU's IPI signal, and it gets sent a lot, so is best silently passed through to the guest without stopping. Make qemu-gdb.py do this bit of configuration for the user.

---
 scripts/qemu-gdb.py | 4 ++++
 1 file changed, 4 insertions(+)
scripts/qemu-gdb.py +4 -0
index 1c94b2a..f98cd21 100644
@@ -34,3 +34,7 @@ class QemuCommand(gdb.Command):
 QemuCommand()
 coroutine.CoroutineCommand()
 mtree.MtreeCommand()
+
+# Default to silently passing through SIGUSR1, because QEMU sends it
+# to itself a lot.
+gdb.execute('handle SIGUSR1 pass noprint nostop')
Signature · 1 line
1.9.1
#4 lore raw

PATCH v2 4/4 scripts/qemu-gdb: Add brief comment describing usage

In reply to Peter Maydell

Add a brief comment describing how to use the debug support from GDB.

---
 scripts/qemu-gdb.py | 3 +++
 1 file changed, 3 insertions(+)
scripts/qemu-gdb.py +3 -0
index f98cd21..d6f2e5a 100644
@@ -13,6 +13,9 @@
 # Contributions after 2012-01-13 are licensed under the terms of the
 # GNU GPL, version 2 or (at your option) any later version.
 
+# Usage:
+# At the (gdb) prompt, type "source scripts/qemu-gdb.py".
+# "help qemu" should then list the supported QEMU debug support commands.
 
 import gdb
 
Signature · 1 line
1.9.1
Reviewed-by on the v2 series
#5 lore raw
In reply to Peter Maydell
Quoted 34 lines · Peter Maydell

This patch series splits scripts/qemu-gdb into separate files for each command, to make it easier to add new commands in future without them all going into a single huge file.

Patches 1 and 2 do that split, and have been on the list before (and reviewed by Stefan).

Patch 3 makes the script tell gdb not to stop on SIGUSR1, since that's our SIGIPI and happens all the time.

Patch 4 adds a brief comment explaining how to source the script. NB that I haven't attempted to make the script work with automatic-sourcing based on the executable being debugged. Somebody who uses the script that way can do that :-)

I've dropped the script which added support for setting breakpoints on QEMU tracepoints for the moment (pending investigating static probe point support which might render it unnecessary). Mostly I'd like to get these into master so that future debug commands can go into the right place -- IIRC David Gilbert had a patchset which added another command.

thanks
-- PMM

Peter Maydell (4):
scripts/qemu-gdb: Split MtreeCommand into its own module scripts/qemu-gdb: Split CoroutineCommand into its own file scripts/qemu-gdb: Silently pass through SIGUSR1 scripts/qemu-gdb: Add brief comment describing usage

Quoted 12 lines
 scripts/qemu-gdb.py          | 146 ++++---------------------------------------
 scripts/qemugdb/__init__.py  |  28 +++++++++
 scripts/qemugdb/coroutine.py |  91 +++++++++++++++++++++++++++
 scripts/qemugdb/mtree.py     |  82 ++++++++++++++++++++++++
 4 files changed, 212 insertions(+), 135 deletions(-)
 create mode 100644 scripts/qemugdb/__init__.py
 create mode 100644 scripts/qemugdb/coroutine.py
 create mode 100644 scripts/qemugdb/mtree.py
Signature · 1 line
1.9.1
author Applied
#6 lore raw
Quoted 44 lines · Stefan Hajnoczi
Quoted 43 lines · Peter Maydell

This patch series splits scripts/qemu-gdb into separate files for each command, to make it easier to add new commands in future without them all going into a single huge file.

Patches 1 and 2 do that split, and have been on the list before (and reviewed by Stefan).

Patch 3 makes the script tell gdb not to stop on SIGUSR1, since that's our SIGIPI and happens all the time.

Patch 4 adds a brief comment explaining how to source the script. NB that I haven't attempted to make the script work with automatic-sourcing based on the executable being debugged. Somebody who uses the script that way can do that :-)

I've dropped the script which added support for setting breakpoints on QEMU tracepoints for the moment (pending investigating static probe point support which might render it unnecessary). Mostly I'd like to get these into master so that future debug commands can go into the right place -- IIRC David Gilbert had a patchset which added another command.

thanks
-- PMM

Peter Maydell (4):
scripts/qemu-gdb: Split MtreeCommand into its own module scripts/qemu-gdb: Split CoroutineCommand into its own file scripts/qemu-gdb: Silently pass through SIGUSR1 scripts/qemu-gdb: Add brief comment describing usage

 scripts/qemu-gdb.py          | 146 ++++---------------------------------------
 scripts/qemugdb/__init__.py  |  28 +++++++++
 scripts/qemugdb/coroutine.py |  91 +++++++++++++++++++++++++++
 scripts/qemugdb/mtree.py     |  82 ++++++++++++++++++++++++
 4 files changed, 212 insertions(+), 135 deletions(-)
 create mode 100644 scripts/qemugdb/__init__.py
 create mode 100644 scripts/qemugdb/coroutine.py
 create mode 100644 scripts/qemugdb/mtree.py
Quoted 5 lines
Quoted 3 lines

--
1.9.1

Applied, thanks.

-- PMM