Skip to contentSkip to conversation

dump-guest-memory.py: fix python 2 support

PATCH In review Marc-André Lureau 3 participants 5 messages Jan 19, 2018

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 dump-guest-memory.py: fix python 2 support

Python GDB support may use Python 2 or 3.

Inferior.read_memory() may return a 'buffer' with Python 2 or a 'memoryview' with Python 3 (see also
https://sourceware.org/gdb/onlinedocs/gdb/Inferiors-In-Python.html)

The elf.add_vmcoreinfo_note() method expects a "bytes" object. Wrap the returned memory with bytes(), which works with both 'memoryview' and 'buffer'.

Fixes a regression introduced with commit d23bfa91b7789534d16ede6cb7d925bfac3f3c4c ("add vmcoreinfo").

---
 scripts/dump-guest-memory.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/dump-guest-memory.py +1 -1
index 09bec92b50..03fbf69f8a 100644
@@ -564,7 +564,7 @@ shape and this command should mostly work."""
 
         vmcoreinfo = self.phys_memory_read(addr, size)
         if vmcoreinfo:
-            self.elf.add_vmcoreinfo_note(vmcoreinfo.tobytes())
+            self.elf.add_vmcoreinfo_note(bytes(vmcoreinfo))
 
     def invoke(self, args, from_tty):
         """Handles command invocation from gdb."""
Signature · 1 line
2.16.0.rc1.1.gef27df75a1
#1 lore raw
Quoted 25 lines · Marc-André Lureau

Python GDB support may use Python 2 or 3.

Inferior.read_memory() may return a 'buffer' with Python 2 or a 'memoryview' with Python 3 (see also
https://sourceware.org/gdb/onlinedocs/gdb/Inferiors-In-Python.html)

The elf.add_vmcoreinfo_note() method expects a "bytes" object. Wrap the returned memory with bytes(), which works with both 'memoryview' and 'buffer'.

Fixes a regression introduced with commit d23bfa91b7789534d16ede6cb7d925bfac3f3c4c ("add vmcoreinfo").

---
 scripts/dump-guest-memory.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/dump-guest-memory.py +0 -0
index 09bec92b50..03fbf69f8a 100644

scripts/dump-guest-memory.py shape and this command should mostly work.""" · line 570

Quoted 8 lines
scripts/dump-guest-memory.py +1 -1
@@ -564,7 +564,7 @@ shape and this command should mostly work."""
 
         vmcoreinfo = self.phys_memory_read(addr, size)
         if vmcoreinfo:
-            self.elf.add_vmcoreinfo_note(vmcoreinfo.tobytes())
+            self.elf.add_vmcoreinfo_note(bytes(vmcoreinfo))
 
     def invoke(self, args, from_tty):
         """Handles command invocation from gdb."""

Ugh, can you please point me to the v1->v2 difference?

Thanks
Laszlo

#2 lore raw
Quoted 11 lines · Laszlo Ersek
Quoted 10 lines · Marc-André Lureau

Python GDB support may use Python 2 or 3.

Inferior.read_memory() may return a 'buffer' with Python 2 or a 'memoryview' with Python 3 (see also
https://sourceware.org/gdb/onlinedocs/gdb/Inferiors-In-Python.html)

The elf.add_vmcoreinfo_note() method expects a "bytes" object. Wrap the returned memory with bytes(), which works with both 'memoryview' and 'buffer'.

If I understand it, the issue stems from the fact that vmcoreinfo is a bytes buffer under python2, but a memoryview under python3. Let's compare the patches:

The V1 patch:
https://lists.gnu.org/archive/html/qemu-devel/2018-01/msg04010.html

Review

Quoted 6 lines

if vmcoreinfo:

(quoted diff) +3 -1
-            self.elf.add_vmcoreinfo_note(vmcoreinfo.tobytes())
+            # Python 2.7 returns a buffer
+            vmciview = memoryview(vmcoreinfo)
+            self.elf.add_vmcoreinfo_note(vmciview.tobytes())

did an unconditional memoryview(vmcoreinfo) followed by a .tobytes() call. Under python 2.7, vmcoreinfo is converted from a buffer to a memoryview, then back to bytes; under python 3, memoryview(vmcoreinfo) is a no-op (since it is already a memory view), then that is converted to bytes; the double conversion was necessary because bytes.tobytes() doesn't exist. But python 2.6 doesn't have the memoryview conversion, so we've excluded older python users.

Now looking at the V2 patch:

Quoted 6 lines

scripts/dump-guest-memory.py shape and this command should mostly work.""" · line 567

Quoted 6 lines
scripts/dump-guest-memory.py +1 -1
@@ -564,7 +564,7 @@ shape and this command should mostly work."""
 
         vmcoreinfo = self.phys_memory_read(addr, size)
         if vmcoreinfo:
-            self.elf.add_vmcoreinfo_note(vmcoreinfo.tobytes())
+            self.elf.add_vmcoreinfo_note(bytes(vmcoreinfo))

Under python2, and bytes(buffer) is still bytes; under python3, vmcoreinfo is a memoryview but bytes(buffer) is equivalent to buffer.to_bytes(). We've avoided the intermediate conversion through memoryview, and thus work regardless of python version.

Quoted 1 line

Ugh, can you please point me to the v1->v2 difference?

Hope that helped (and hope I got it right, I'm learning as well from this thread).

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Acked-by
#3 lore raw
Quoted 40 lines · Eric Blake
Quoted 11 lines · Laszlo Ersek
Quoted 10 lines · Marc-André Lureau

Python GDB support may use Python 2 or 3.

Inferior.read_memory() may return a 'buffer' with Python 2 or a 'memoryview' with Python 3 (see also
https://sourceware.org/gdb/onlinedocs/gdb/Inferiors-In-Python.html)

The elf.add_vmcoreinfo_note() method expects a "bytes" object. Wrap the returned memory with bytes(), which works with both 'memoryview' and 'buffer'.

If I understand it, the issue stems from the fact that vmcoreinfo is a bytes buffer under python2, but a memoryview under python3. Let's compare the patches:

The V1 patch:
https://lists.gnu.org/archive/html/qemu-devel/2018-01/msg04010.html

Review

Quoted 6 lines

if vmcoreinfo:

(quoted diff) +3 -1
-            self.elf.add_vmcoreinfo_note(vmcoreinfo.tobytes())
+            # Python 2.7 returns a buffer
+            vmciview = memoryview(vmcoreinfo)
+            self.elf.add_vmcoreinfo_note(vmciview.tobytes())

did an unconditional memoryview(vmcoreinfo) followed by a .tobytes() call. Under python 2.7, vmcoreinfo is converted from a buffer to a memoryview, then back to bytes; under python 3, memoryview(vmcoreinfo) is a no-op (since it is already a memory view), then that is converted to bytes; the double conversion was necessary because bytes.tobytes() doesn't exist. But python 2.6 doesn't have the memoryview conversion, so we've excluded older python users.

Now looking at the V2 patch:

Quoted 2 lines
Quoted 2 lines
(quoted diff) +0 -0
@@ -564,7 +564,7 @@ shape and this command should mostly work."""
 
Quoted 9 lines
Quoted 4 lines

scripts/dump-guest-memory.py shape and this command should mostly work.""" · line 567

Quoted 4 lines
scripts/dump-guest-memory.py +1 -1
         vmcoreinfo = self.phys_memory_read(addr, size)
         if vmcoreinfo:
-            self.elf.add_vmcoreinfo_note(vmcoreinfo.tobytes())
+            self.elf.add_vmcoreinfo_note(bytes(vmcoreinfo))

Under python2, and bytes(buffer) is still bytes; under python3, vmcoreinfo is a memoryview but bytes(buffer) is equivalent to buffer.to_bytes(). We've avoided the intermediate conversion through memoryview, and thus work regardless of python version.

Heh, "semantics" :)

Quoted 6 lines
Quoted 2 lines

Ugh, can you please point me to the v1->v2 difference?

Hope that helped (and hope I got it right, I'm learning as well from this thread).

It sure helped a lot, thank you very much! In fact, you've done all the review work, so technically, "R-b: Laszlo" would be misappropriation. Can you give your R-b?

I'll manage an "I agree" statement:

Thanks!
Laszlo

Reviewed-by on v2 1/1
#4 lore raw
In reply to Marc-André Lureau
Quoted 10 lines · Marc-André Lureau

Python GDB support may use Python 2 or 3.

Inferior.read_memory() may return a 'buffer' with Python 2 or a 'memoryview' with Python 3 (see also
https://sourceware.org/gdb/onlinedocs/gdb/Inferiors-In-Python.html)

The elf.add_vmcoreinfo_note() method expects a "bytes" object. Wrap the returned memory with bytes(), which works with both 'memoryview' and 'buffer'.

Quoted 8 lines

Fixes a regression introduced with commit d23bfa91b7789534d16ede6cb7d925bfac3f3c4c ("add vmcoreinfo").

---
 scripts/dump-guest-memory.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org