Skip to contentSkip to conversation

Handle gdb.MemoryError exception in dump-guest-memory.py

PATCH In review Kevin Buettner 3 participants 3 messages Feb 15, 2020 – Mar 5, 2020

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Handle gdb.MemoryError exception in dump-guest-memory.py

[Included a "Signed-off-by" line in this version.]

I recently investigated a bug in which the dump-guest-memory.py script sees a gdb.MemoryError exception while attempting to dump memory obtained from a QEMU core dump. (And, yes, dump-guest-core=on was specified in the -machine option of the QEMU invocation.)

It turns out that memory region in question is not being placed in the core dump and, after stepping through the kernel core dumping code responsible for making this decision, it looks reasonable to me to not include that region in the core dump. The region in question consists of all zeros and, according to the kernel's logic, has never been written to.

This commit makes a small change to the dump-guest-memory script to cause inaccessible memory to be dumped as zeroes. This avoids the exception and places the correct values in the guest memory dump.

---
 scripts/dump-guest-memory.py | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
scripts/dump-guest-memory.py +6 -1
index 4177261d33..fbdfba458b 100644
@@ -539,7 +539,12 @@ shape and this command should mostly work."""
 
             while left > 0:
                 chunk_size = min(TARGET_PAGE_SIZE, left)
-                chunk = qemu_core.read_memory(cur, chunk_size)
+                try:
+                    chunk = qemu_core.read_memory(cur, chunk_size)
+                except gdb.MemoryError:
+                    # Consider blocks of memory absent from a core file
+                    # as being zeroed.
+                    chunk = bytes(chunk_size)
                 vmcore.write(chunk)
                 cur += chunk_size
                 left -= chunk_size
Signature · 1 line
2.24.1
#1 lore raw

Hi

Quoted 29 lines · Kevin Buettner

[Included a "Signed-off-by" line in this version.]

I recently investigated a bug in which the dump-guest-memory.py script sees a gdb.MemoryError exception while attempting to dump memory obtained from a QEMU core dump. (And, yes, dump-guest-core=on was specified in the -machine option of the QEMU invocation.)

It turns out that memory region in question is not being placed in the core dump and, after stepping through the kernel core dumping code responsible for making this decision, it looks reasonable to me to not include that region in the core dump. The region in question consists of all zeros and, according to the kernel's logic, has never been written to.

This commit makes a small change to the dump-guest-memory script to cause inaccessible memory to be dumped as zeroes. This avoids the exception and places the correct values in the guest memory dump.

---
 scripts/dump-guest-memory.py | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
scripts/dump-guest-memory.py +0 -0
index 4177261d33..fbdfba458b 100644

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

Quoted 10 lines
scripts/dump-guest-memory.py +6 -1
@@ -539,7 +539,12 @@ shape and this command should mostly work."""
 
             while left > 0:
                 chunk_size = min(TARGET_PAGE_SIZE, left)
-                chunk = qemu_core.read_memory(cur, chunk_size)
+                try:
+                    chunk = qemu_core.read_memory(cur, chunk_size)
+                except gdb.MemoryError:
+                    # Consider blocks of memory absent from a core file
+                    # as being zeroed.
+                    chunk = bytes(chunk_size)

That seems reasonable, but it will silently ignore any other memory error.

Keith Seitz also looked at this bug, and he was wondering if BFD shouldn't treat the missing section differently:
https://bugzilla.redhat.com/show_bug.cgi?id=1777751#c6

Keith, what do you think?

thanks

#2 lore raw
In reply to Kevin Buettner

Hi

Quoted 15 lines · Kevin Buettner

[Included a "Signed-off-by" line in this version.]

I recently investigated a bug in which the dump-guest-memory.py script sees a gdb.MemoryError exception while attempting to dump memory obtained from a QEMU core dump. (And, yes, dump-guest-core=on was specified in the -machine option of the QEMU invocation.)

It turns out that memory region in question is not being placed in the core dump and, after stepping through the kernel core dumping code responsible for making this decision, it looks reasonable to me to not include that region in the core dump. The region in question consists of all zeros and, according to the kernel's logic, has never been written to.

Quoted 5 lines

This commit makes a small change to the dump-guest-memory script to cause inaccessible memory to be dumped as zeroes. This avoids the exception and places the correct values in the guest memory dump.

fwiw, Kevin fixed it in gdb:
https://sourceware.org/ml/gdb-patches/2020-03/msg00106.html

Quoted 26 lines
---
 scripts/dump-guest-memory.py | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
scripts/dump-guest-memory.py +6 -2
index 4177261d33..fbdfba458b 100644
@@ -539,7 +539,12 @@ shape and this command should mostly work."""
 
             while left > 0:
                 chunk_size = min(TARGET_PAGE_SIZE, left)
-                chunk = qemu_core.read_memory(cur, chunk_size)
+                try:
+                    chunk = qemu_core.read_memory(cur, chunk_size)
+                except gdb.MemoryError:
+                    # Consider blocks of memory absent from a core file
+                    # as being zeroed.
+                    chunk = bytes(chunk_size)
                 vmcore.write(chunk)
                 cur += chunk_size
                 left -= chunk_size
--

2.24.1

Signature · 1 line
Marc-André Lureau