Diff for /wikisrc/kernel_debugging_with_qemu.mdwn between versions 1.1 and 1.16

version 1.1, 2010/04/08 11:20:54 version 1.16, 2018/03/16 15:29:26
Line 1 Line 1
 # Introduction  # Introduction
   
 Virtual machines are a convenient way to test, debug or even audit different systems on one single host. This is particularly helpful when you need to set up a machine for which you do not necessarily have the hardware, or the access, in a very cheap way.  This HOWTO explains how to set up a test environment for symbolic
   debugging of the NetBSD kernel using a pair of QEMU virtual machines.
   
 This tutorial show the different steps required to set up a raw disk image like the one used by QEMU, to either try, test, audit (or even assess) modifications done to a particular NetBSD's environment, without having access to the real hardware, or alter your day-to-day system, with the risk of making it unusable.  ## Prerequisites
   
 It is also a lot faster to test modifications within a virtual machine.  You need a computer running an OS capable of cross-building NetBSD
   (the "host system").
   This can be NetBSD itself, Linux, or some other Unix-like OS.
   These instructions have been tested with NetBSD/amd64 6.1.4 and
   Debian 7 hosts.   There should be at least 20 gigabytes of available
   disk space.
   
 This tutorial deals with two different point of views:  If your host system is running NetBSD, install the following packages
 * the host, which is the machine and OS hosting the different VMs.  from pkgsrc:
 * the guest(s), representing the different systems emulated/hosted on the host, through QEMU.  
   
 # Setting up the environment  * emulators/qemu >= 2.0.0nb4
   * misc/py-anita >= 1.44
   
 To start our VM, we need some disk space to provide an emulated hard drive. For QEMU, by default, this is done through raw disk images. Therefore, the first step will be the creation of a disk image file. Here, we create a 2GB file, filled with zeros:  If your host system uses a package system other than pkgsrc,
   use that to install cvs, make, gcc, qemu, the Python pexpect
   library, and genisoimage or mkisofs.  Also download and 
   install the most recent anita package from
   <http://www.gson.org/netbsd/anita/download/>.
   
     dd if=/dev/null of=netbsd-guest.img bs=1m count=2000  ## Building the target system
   
 /!\ if you want to mount the file image from within the host later through [[!template id=man name="vnconfig" section="8"]], it is recommended to use [[!template id=man name="dd" section="1"]] and not the *qemu-img* tool, as [[!template id=man name="vnd" section="4"]] does not support sparse disk image yet.  Check out the NetBSD-current sources from CVS and build a full release
   of NetBSD-current/i386 with debug symbols using the build.sh script.
   The i386 port is preferred because these instructions have been 
   successfully tested with it.
   The amd64 port won't work because of [[PR 50128|http://gnats.NetBSD.org/50128]],
   and sparc has not been tested since [[qemu bug
   1399943|https://bugs.launchpad.net/qemu/+bug/1399943]] was fixed.
   
 Now that the disk image file is ready, we need to install our system inside.  If you do the build in a directory other than /usr/src, 
   use the -fdebug-prefix-map option to ensure that the source file names embedded
   in the debug symbols point to /usr/src, which is where the sources will be
   installed on the target system.  For example:
   
 => TODO  [[!template  id=programlisting text="""
    $ CVSROOT=anoncvs@anoncvs.NetBSD.org:/cvsroot cvs checkout -A -P src
    $ cd src
    $ ./build.sh -j 4 -V MKDEBUG=YES -V COPTS="-g -fdebug-prefix-map=$(pwd)=/usr/src" -O ../obj -m i386 -U release sourcesets
   """]]
   
   For best performance, change the number after "-j" to the number of CPU cores
   you have, or slightly more.
   
   ## Installing the target system
   
   Install the system in a virtual machine, including the debug symbols and source code:
   
   [[!template  id=programlisting text="""
    $ cd ..
    $ anita --workdir work --disk-size 8G --memory-size 256M \
        --sets kern-GENERIC,modules,base,etc,comp,debug,games,man,misc,tests,text,syssrc,src,sharesrc,gnusrc \
        install $(pwd)/obj/releasedir/i386/
   """]]
   
   ## Booting the VMs
   
   Next, start two qemu virtual machines, one to run the kernel being
   debugged (the "target VM") and another to run gdb (the "gdb VM").
   
   The two VMs could be run on separate physical machines, but in this
   example, they are run on the same physical machine and share the same
   hard disk image.  This sharing is made possible by the "-snapshot"
   option to qemu, which ensures that the disk image is not written to by
   either VM.
   
   First start the target VM, enabling qemu's built-in GDB target stub
   on TCP port 1234:
   
   [[!template  id=programlisting text="""
    $ qemu-system-i386 -nographic -snapshot -hda work/wd0.img -m 128 -gdb tcp::1234
   """]]
   
   If you don't want everyone on the Internet to be able to debug your
   target, make sure incoming connections on port 1234 are blocked in
   your firewall.
   
   In a second terminal window, start the gdb VM:
   
   [[!template  id=programlisting text="""
    $ qemu-system-i386 -nographic -snapshot -hda work/wd0.img -m 256
   """]]
   
   Log in to the gdb VM as root and set up the network:
   
   [[!template  id=programlisting text="""
    login: root
    # dhcpcd
   """]]
   
   Start gdb on the gdb VM and connect to the target:
   
   [[!template  id=programlisting text="""
    # gdb /netbsd
    (gdb) target remote my.host.name:1234
   """]]
   
   where my.host.name is the domain name or IP address of the
   host system.
   
   Now you should be able to get a stack trace and start debugging
   with full debug symbols and access to the source code:
   
   [[!template  id=programlisting text="""
    (gdb) where
    (gdb) list
   """]]
   
   If the stack trace prints very slowly (like 30 seconds per stack
   frame), it's likely because you are using a version of qemu where
   the user-mode networking code fails to disable the Nagle algorithm.
   This is fixed in the qemu in pkgsrc, but you may run into it if your
   qemu is not installed via pkgsrc.
   
   ## Qemu tips
   
   Here is a couple of useful qemu commands to know:
   
   * Ctrl-a b will send a break which will make the NetBSD VM enter the ddb kernel debugger.
   
   * Ctrl-a c will switch to the qemu monitor where you can enter commands like "quit" to exit qemu,
   or do things like saving/restoring the VM to/from a file.

Removed from v.1.1  
changed lines
  Added in v.1.16


CVSweb for NetBSD wikisrc <wikimaster@NetBSD.org> software: FreeBSD-CVSweb