Debugging NetBSD kernel with qemu[zt]
http://koowaldah.org/people/ash/netbsd/debugging-with-qemu.htmlAlexander Shishkin
<alexander.shishkin@teleca.com>
version 0.2,September 2008IntroductionThis document describes the process of debugging netbsd kernel using gdbwhile running said kernel in qemu. It also talks about how a disk imagewith a suitable file system can be created on a linux host. Most of theseinstructions stand for other guest operating systems; some of theseinstructions (although yet untested) will as well apply for non-linuxhost.Any mentions of NetBSD hereafter shall denote for the CURRENT branch ofNetBSD codebase, for convenience of the reader (and writer).Note: for the time being, the only described architecture is i386. Moreto come in future, look out for updates.
NetBSD kernel and userlandFirst off, you'll need to compile netbsd kernel and, if you aim at debuggingkernel-userspace interaction, you'll also need to compile netbsd userland.Most of this is described in detail elsewhere, so I will say only a few wordshere. This chapter is NetBSD specific.
Getting sourcesYou can checkout the source tree from netbsd cvs repository. This is guaranteedto provide you with the very latest version thereof.
Example: checking out sources from cvs
$ cvs -d anoncvs@anoncvs.netbsd.org:/cvsroot -z6 co src
Alternatively, you can clone a git repository of NetBSD. Note, however, that thisrepo is being hosted on my machine at home and is not guaranteed to always work.
Example: cloning a git repository
$ git clone git://shisha.kicks-ass.net/git/netbsd-current.git/
Building it allBuilding is fairly simple, done by build.sh script, which can be found in theroot of NetBSD source tree. The only thing that you'll have to make sure of, ishaving development packages for zlib and ncurses, without those NetBSD buildbreaks in all sorts of funny ways. In debian and ubuntu those are zlib1g-devand libncurses5-dev, respectively.
First, build the toolchain.
Example: building the toolchain
$ ./build.sh -T $PWD/../nbtools -O $PWD/../nbobj -D $PWD/../nbdist -U -m i386 tools
Then, the kernel. You'll have to tweak its configuration a bit so that it[*]automatically starts with a correct root partition, by changing the root on ? type ?line;
[*]generates an image with debugging symbols (used with gdb later on);
[*]defaults the console to serial port, so that you won't have to use the vga consoleemulation window.
Example: kernel configuration excerpt 1: root fs device
# Kernel root file system and dump configuration.
config netbsdroot on wd0e type ext2fs
Example: kernel configuration excerpt 2: debugging symbols
makeoptions DEBUG="-g3" # compile full symbol table
Example: kernel configuration excerpt 3: serial console
# This option allows you to force a serial console at the specified
# I/O address. see console(4) for details.
options Ccom\"",CONADDR=0x3f8,CONSPEED=57600
Example: building the kernel
$ ./build.sh -T $PWD/../nbtools -O $PWD/../nbobj -D $PWD/../nbdist -U -m i386 kernel=GENERIC
And then, build userland.
Example: building the userland
$ ./build.sh -T $PWD/../nbtools -O $PWD/../nbobj -D $PWD/../nbdist -U -m i386 build
If all went well, by now you should have a kernel in ../nbobj/sys/arch/i386/compile/GENERIC/netbsdand userland installed into ../nbdist.
Disk imageThis chapter describes means of generating a disk image containing NetBSD userland.If you intend to go into the very beginning of the kernel bootup process, you might(although, not advised to) skip to the next chapter.
Relying on the fact that NetBSD does support ext2fs of linux (to a limited extent,though), we'll be creating a ext2fs image, which is a far more convenient option ona linux host.
All the trickery is done by a simple shell script, which can be found athttp://koowaldah.org/people/ash/netbsd/mkdisk.shWhat it does is creating a disk image with single partition which is formattedinto ext2fs, installs a grub (0.9x) bootloader and copies NetBSD userland over there.Prerequisites are that you should have grub 0.9x installed and available in your $PATHand, if you don't have sudo (or any other means of becoming root on your machine), e2fsprogs.
Note: at the moment, e2fsprogs are not able to create symbolic links (of which netbsduserland makes use), it is preferred that you choose the method involving root permissions.
Running the script as shown below will get you a disk image.
Example: running mkdisk.sh
$ sudo ./mkdisk.sh ../nbdist
Again, if all works out well, you should have a shiny new disk image called netbsd.img.
DebuggingFinally, you should have qemu (with all its dependencies) and gdb installedand you are ready and set up for debugging.
Starting guest NetBSD with qemu is simple.
Example: qemu invocation
$ qemu -hda ../netbsd.img -nographic -s -serial stdio
-hda option tells qemu to use ../netbsd.img as a hard disk image;-nographic is for not having a vga console emulation window;-s turns on qemu's internal gdbstub so that it starts listening on 1234 port;-serial is for using plain stdio for serial port emulation.Additionally, -S option might come in handy: it means that qemu will waitfor remote gdb to connect before running the guest. Otherwise, connection toqemu's gdbstub will stop guest execution until gdb explicitly asks to continue.See qemu manual for all the handy options.
Once you have this, you can start debugging with gdb. Use target remotecommand to attach to qemu's gdbstub.
Example: gdb invocation
$ gdb ../nbobj/sys/arch/i386/compile/GENERIC/netbsd.gdb
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
warning: A handler for the OS ABI "NetBSD ELF" is not built into this configuration
of GDB.Attempting to continue with the default i386 settings.
(gdb) target remote :1234
Remote debugging using :1234
0xc062b932 in wi_pci_attach ()
(gdb)
Example sessionHere's an example of a debugging session.
Example: start qemu
$ qemu -hda ../netbsd.img -nographic -s -S -serial stdio
Example: start gdb
$ gdb ../nbobj/sys/arch/i386/compile/GENERIC/netbsd.gdb
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
warning: A handler for the OS ABI "NetBSD ELF" is not built into this configuration
of GDB.Attempting to continue with the default i386 settings.
(gdb) target remote :1234
Remote debugging using :1234
0x0000fff0 in ?? ()
(gdb) b cpu_attach
Breakpoint 1 at 0xc0514850: file netbsd-current/sys/arch/x86/x86/cpu.c, line 264.
(gdb) c
Continuing.
Breakpoint 1, cpu_attach (parent=0xca64b5ec, self=0xca64b870, aux=0xc0c27b68)
at netbsd-current/sys/arch/x86/x86/cpu.c:264
264 {
(gdb) info registers
eax 0xc0a5b100 -1062883072
ecx 0x0 0
edx 0xca64b5ec -899369492
ebx 0xc0a48e80 -1062957440
esp 0xc0c27b1c 0xc0c27b1c
ebp 0xc0c27b48 0xc0c27b48
esi 0xc0a01a41 -1063249343
edi 0x0 0
eip 0xc0514850 0xc0514850 <cpu_attach>
eflags 0x246 [ PF ZF IF ]
cs 0x8 8
ss 0x10 16
ds 0x10 16
es 0x10 16
fs 0x30 48
gs 0x10 16
(gdb) list
259 }
260
261
262 void
263 cpu_attach(device_t parent, device_t self, void *aux)
264 {
265 struct cpu_softc *sc = device_private(self);
266 struct cpu_attach_args *caa = aux;
267 struct cpu_info *ci;
268 uintptr_t ptr;
(gdb) print parent
$1 = (device_t) 0xca64b5ec
(gdb)
Version 0.2
Last updated 2008-09-12 15:55:43 EEST
页:
[1]