Debugging long mode kernel (gdb + qemu)?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Hellbender
Member
Member
Posts: 63
Joined: Fri May 01, 2015 2:23 am
Libera.chat IRC: Hellbender

Debugging long mode kernel (gdb + qemu)?

Post by Hellbender »

Hi.

I noticed that gdb has trouble when qemu switches from protected mode to long mode.
I found 'kind of a solution' in http://forum.osdev.org/viewtopic.php?f=1&t=28262
so I can gt around this in an ugly way: either disconnect+reconnect every time or
recompile gdb with this hack. So far I've just reconnected every time..

So the question is, do you know if there has been any official improvements on
this, either in gdb or qemu, or some cleaner solution to get around the issue?
Hellbender OS at github.
User avatar
Rusky
Member
Member
Posts: 792
Joined: Wed Jan 06, 2010 7:07 pm

Re: Debugging long mode kernel (gdb + qemu)?

Post by Rusky »

You should be able to switch GDB's architecture without reconnecting using something like "set architecture i386:x86-64" (with "set architecture i386" to go back, and "show architecture" to see which one it's set to).
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Debugging long mode kernel (gdb + qemu)?

Post by iansjack »

I just break the program once it is in long mode (pressing control-C when at the command prompt in my shell is a good place) then "load" the 64-bit binary (or you can always just set a breakpoint at a suitable address). This switches the debugger into 64-bit mode without any further problems. It's not really a big deal.
Hellbender
Member
Member
Posts: 63
Joined: Fri May 01, 2015 2:23 am
Libera.chat IRC: Hellbender

Re: Debugging long mode kernel (gdb + qemu)?

Post by Hellbender »

EDIT: I noticed that Ubuntu14.04 includes pretty old QEMU (2.0.0 vs the latest 2.6.0). That could be the actual cause..
EDIT2: Compiled QEmu 2.6.0-rc1. No dice. I just stick with the patched GDB..

I have the following in my .gdbinit:

Code: Select all

set arch i386:x86-64:intel
target remote localhost:1234
symbol-file kernel/hellbender.kernel.sym
break boot_64
continue
When the break happens, I get "Remote 'g' packet reply is too long: ...".
The only way forward is to run:

Code: Select all

disconnect
set arch i386:x86-64
target remote localhost:1234
Note that the latter 'set arch' is done without the intel flag because otherwise
gdb didn't really do anything (most likely it detects that I set the arch twice).
It doesn't matter which way archs are, as long as the other has the flag and
the other does not..

Strange if it works for you.. Well, I just applied the patch mentioned in the original
post and compiled GDB. Now it works across the mode change.
I changed the hack to emit a warning when that packet length problem occurs,
so I know it still happens with the new GDB as well.

Original GDB was 7.7.1-0ubuntu5~14.04.2 and the new one is 7.11.

I don't know if it makes any difference but the 'boot_64' symbol address is 0xffff8000002000df.
Maybe that is why I cannot even break in my code without setting the arch first.
Last edited by Hellbender on Thu Apr 14, 2016 4:41 am, edited 1 time in total.
Hellbender OS at github.
Wallbraker
Posts: 2
Joined: Wed Mar 23, 2016 4:17 am
Libera.chat IRC: Prf_Jakob

Re: Debugging long mode kernel (gdb + qemu)?

Post by Wallbraker »

I had trouble getting gdb into 64bit mode after getting a bad packet
(triggered by the break) (I need to debug code that run faster then
I can ctrl-c, hence the breakpoint). So I solved this by running gdb
twice, the first one is non-interactive. I needed 'disconnect' to keep
qemu stopped.

Code: Select all

qemu-system-x86_64 -serial stdio -kernel $BIN_FILE -S -s &
gdb $ELF_FILE -batch -ex 'target remote localhost:1234' -ex 'b boot_main' -ex 'c' -ex 'disconnect'
gdb $ELF_FILE -ex 'set arch i386:x86-64' -ex 'target remote localhost:1234'
Post Reply