[PATCH v3] qemu-gdb: add an Object pretty printer

Marc-André Lureau posted 1 patch 4 years, 5 months ago
Test asan passed
Test checkpatch passed
Test FreeBSD passed
Test docker-mingw@fedora passed
Test docker-clang@ubuntu passed
Test docker-quick@centos7 passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20191114144725.2310-1-marcandre.lureau@redhat.com
Maintainers: Eduardo Habkost <ehabkost@redhat.com>, Cleber Rosa <crosa@redhat.com>
scripts/qemu-gdb.py | 58 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 58 insertions(+)
mode change 100644 => 100755 scripts/qemu-gdb.py
[PATCH v3] qemu-gdb: add an Object pretty printer
Posted by Marc-André Lureau 4 years, 5 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 f2a305c42e..b9fc1d10d4
--- a/scripts/qemu-gdb.py
+++ b/scripts/qemu-gdb.py
@@ -18,6 +18,9 @@ import gdb
 
 import os, sys
 
+if sys.version_info[0] < 3:
+    int = long
+
 # Annoyingly, gdb doesn't put the directory of scripts onto the
 # module search path. Do it manually.
 
@@ -44,3 +47,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 ObjectPrinter:
+    def __init__(self, val):
+        self.val = val
+
+    def to_string(self):
+        name = object_class_name(self.val)
+        if name:
+            return ("0x%x [%s]")% (int(self.val), name)
+        return  ("0x%x") % (int(self.val))
+
+
+def lookup_type(val):
+    if is_object(val):
+        return ObjectPrinter(val)
+    return None
+
+
+gdb.pretty_printers.append(lookup_type)
-- 
2.24.0


Re: [PATCH v3] qemu-gdb: add an Object pretty printer
Posted by Marc-André Lureau 4 years, 2 months ago
On Thu, Nov 14, 2019 at 3:50 PM Marc-André Lureau
<marcandre.lureau@redhat.com> 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>

ping

> ---
>  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 f2a305c42e..b9fc1d10d4
> --- a/scripts/qemu-gdb.py
> +++ b/scripts/qemu-gdb.py
> @@ -18,6 +18,9 @@ import gdb
>
>  import os, sys
>
> +if sys.version_info[0] < 3:
> +    int = long
> +
>  # Annoyingly, gdb doesn't put the directory of scripts onto the
>  # module search path. Do it manually.
>
> @@ -44,3 +47,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 ObjectPrinter:
> +    def __init__(self, val):
> +        self.val = val
> +
> +    def to_string(self):
> +        name = object_class_name(self.val)
> +        if name:
> +            return ("0x%x [%s]")% (int(self.val), name)
> +        return  ("0x%x") % (int(self.val))
> +
> +
> +def lookup_type(val):
> +    if is_object(val):
> +        return ObjectPrinter(val)
> +    return None
> +
> +
> +gdb.pretty_printers.append(lookup_type)
> --
> 2.24.0
>
>


-- 
Marc-André Lureau