QEMU and GDB in long mode: Difference between revisions

Jump to navigation Jump to search
reworked the article
[unchecked revision][unchecked revision]
m (added categories)
(reworked the article)
Line 1:
If you're running in [[Long Mode]] and using [[QEMU]] as your emulator you'll likely encounter the following error trying to use [[QEMU#GDB-stub|QEMU's gdb-stub]]:
If you encounter "Remote 'g' packet reply is too long: ..." error when the QEMU has switched to long mode, you have two possible solutions. You can either disconnect and reconnect to force GDB to use the x86_64 architecture, or you can patch GDB to switch the architecture on the fly.
<pre>Remote 'g' packet reply is too long: ...</pre>
IfThis you encounter "Remote 'g' packet reply is too long: ..."happens error when the QEMU has switched to long mode,. youThere haveare two possibleworkarounds solutions.for Youthis: you can either disconnect and reconnect to force GDB to use the x86_64 architecture, or you can patch GDB to switch the architecture on the fly.
 
==Workaround Reconnecting1: Reconnecting==
 
When you start GDB, you connect to the QEMU as follows:
 
When you start GDB, you connect to theQEMU QEMUwith asthese followscommands:
<source lang="text">
>$ gdb
(gdb) set arch i386:x86-64:intel
(gdb) target remote localhost:1234
(gdb) symbol-file YOUR_KERNEL_FILE
(gdb) break SOME_FUNCTION_IN_LONG_MODE
(gdb) continue
</source>
 
When the break point is hit and the "packet reply is too long" happens, you just run the following:
 
<source lang="text">
(gdb) disconnect
(gdb) set arch i386:x86-64
(gdb) target remote localhost:1234
</source>
 
Note that you must use different architecture names in those two "set arch" commands.
 
==Workaround 2: Patching GDB ==
 
This is a slightly modified version from http://forum.osdev.org/viewtopic.php?f=1&t=28262.
I added a warning to see when the package error happens.
 
This is a slightly modified version of the patch posted in [[topic:28262|this thread]]. As a bonus, this patch produces a warning when it suspects that the mode switch happened.
<source lang="text">
tar xzf gdb-7.11.tar.gz
cd gdb-7.11
patch -p0 <<EOF
--- gdb/remote.c 2016-04-14 11:13:49.962628700 +0300
+++ gdb/remote.c 2016-04-14 11:15:38.257783400 +0300
Line 65 ⟶ 60:
/* Save the size of the packet sent to us by the target. It is used
as a heuristic when determining the max size of packets that the
 
EOF
cd ..
mkdir build-gdb
cd build-gdb
../gdb-7.11/configure --prefix$HOME/opt/cross --disable-docs
make && make install
</source>
 
The patched GDB emits the error as a warning but continues to function across the mode change. This patch has been tested with gdb 7.11 and 7.12.
 
Alternatively, you can patch QEMU so that you can use gdb unpatched. Be warned, this probably breaks support for debugging 32-bit code (also, compiling QEMU is hour-long task). In order to do this, replace each occurence of <code>if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) {</code> in the file gdbstub.c with <code>if (TARGET_LONG_BITS == 64) {</code>. However, this method originates from [[topic:21836|a thread back in 2011]], and is untested since.
 
==Other issues==
Some have experienced [[topic:26224|issues with breakpoints]] (using the <code>break</code> command) not firing. Use <code>hbreak</code> instead.
 
==See also==
Line 80 ⟶ 75:
* [[topic:30285|Debugging long mode kernel (gdb + qemu)?]] - workaround without patching gdb
* [[topic:21836|GDB and QEMU on Win64]] - patches for either gdb or qemu
* [[topic:26224|GDB not breaking at my breakpoints]] - dealing with 'skipped' breakpoints
 
[[Category:Troubleshooting]]
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu