[Qemu-devel] [PATCH] qemu-gdb: add a QObject pretty printer

Marc-André Lureau posted 1 patch 6 years, 9 months ago
Test docker-mingw@fedora passed
Test docker-clang@ubuntu failed
Test asan passed
Test checkpatch passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20190118134843.28916-1-marcandre.lureau@redhat.com
Maintainers: Eduardo Habkost <ehabkost@redhat.com>, Cleber Rosa <crosa@redhat.com>
There is a newer version of this series
scripts/qemu-gdb.py | 58 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 58 insertions(+)
mode change 100644 => 100755 scripts/qemu-gdb.py
[Qemu-devel] [PATCH] qemu-gdb: add a QObject pretty printer
Posted by Marc-André Lureau 6 years, 9 months ago
Inspired by GObject/GType pretty printer.

Example:
machine_set_accel (obj=0x555556807550 [pc-i440fx-4.0-machine],...

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 scripts/qemu-gdb.py | 58 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)
 mode change 100644 => 100755 scripts/qemu-gdb.py

diff --git a/scripts/qemu-gdb.py b/scripts/qemu-gdb.py
old mode 100644
new mode 100755
index 690827e6fc6..90c6fd13b93
--- a/scripts/qemu-gdb.py
+++ b/scripts/qemu-gdb.py
@@ -21,6 +21,9 @@ import gdb
 
 import os, sys
 
+if sys.version_info[0] >= 3:
+    long = int
+
 # Annoyingly, gdb doesn't put the directory of scripts onto the
 # module search path. Do it manually.
 
@@ -47,3 +50,58 @@ coroutine.CoroutinePCFunction()
 # Default to silently passing through SIGUSR1, because QEMU sends it
 # to itself a lot.
 gdb.execute('handle SIGUSR1 pass noprint nostop')
+
+
+def is_object(val):
+    def is_object_helper(type):
+        if str(type) == "Object":
+            return True
+
+        while type.code == gdb.TYPE_CODE_TYPEDEF:
+            type = type.target()
+
+        if type.code != gdb.TYPE_CODE_STRUCT:
+            return False
+
+        fields = type.fields()
+        if len (fields) < 1:
+            return False
+
+        first_field = fields[0]
+        return is_object_helper(first_field.type)
+
+    type = val.type
+    if type.code != gdb.TYPE_CODE_PTR:
+        return False
+    type = type.target()
+    return is_object_helper (type)
+
+
+def object_class_name(instance):
+    try:
+        inst = instance.cast(gdb.lookup_type("Object").pointer())
+        klass = inst["class"]
+        typ = klass["type"]
+        return typ["name"].string()
+    except RuntimeError:
+        pass
+
+
+class QObjectPrinter:
+    def __init__(self, val):
+        self.val = val
+
+    def to_string(self):
+        name = object_class_name(self.val)
+        if name:
+            return ("0x%x [%s]")% (long(self.val), name)
+        return  ("0x%x") % (long(self.val))
+
+
+def lookup_type(val):
+    if is_object(val):
+        return QObjectPrinter(val)
+    return None
+
+
+gdb.pretty_printers.append(lookup_type)
-- 
2.20.1.98.gecbdaf0899


Re: [Qemu-devel] [PATCH] qemu-gdb: add a QObject pretty printer
Posted by Eduardo Habkost 6 years, 9 months ago
On Fri, Jan 18, 2019 at 05:48:43PM +0400, Marc-André Lureau wrote:
> Inspired by GObject/GType pretty printer.
> 
> Example:
> machine_set_accel (obj=0x555556807550 [pc-i440fx-4.0-machine],...
> 
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  scripts/qemu-gdb.py | 58 +++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 58 insertions(+)
>  mode change 100644 => 100755 scripts/qemu-gdb.py
> 
> diff --git a/scripts/qemu-gdb.py b/scripts/qemu-gdb.py
> old mode 100644
> new mode 100755
> index 690827e6fc6..90c6fd13b93
> --- a/scripts/qemu-gdb.py
> +++ b/scripts/qemu-gdb.py
> @@ -21,6 +21,9 @@ import gdb
>  
>  import os, sys
>  
> +if sys.version_info[0] >= 3:
> +    long = int
[...]
> +class QObjectPrinter:
> +    def __init__(self, val):
> +        self.val = val
> +
> +    def to_string(self):
> +        name = object_class_name(self.val)
> +        if name:
> +            return ("0x%x [%s]")% (long(self.val), name)
> +        return  ("0x%x") % (long(self.val))

I took a while to find out that int(v) doesn't work if v is a
pointer gdb value.  This is surprising, because int(long_number)
(e.g. int(2**65)) works on Python 2.

Also, this works on Python 2:

  int(self.val.cast(gdb.lookup_type("unsigned long long")))

But it's so ugly that a sys.version_info check still sounds
better.

I would prefer this, though:

  if sys.version_info[0] < 3:
      int = long
  ...
      def to_string(self):
          ...
          return  ("0x%x") % (int(self.val))

Because it makes the python2-specific code easier to remove in
the future.

> +
> +
> +def lookup_type(val):
> +    if is_object(val):
> +        return QObjectPrinter(val)
> +    return None
> +
> +
> +gdb.pretty_printers.append(lookup_type)
> -- 
> 2.20.1.98.gecbdaf0899
> 

-- 
Eduardo