Debugging small OS kernels (part 2)

(Continued from part 1)

The QEMU instance should appear as a window that is completely black. The reason for this is that we started the OS instance in a “stopped” state. The “-S” option on the command line is what let’s us start QEMU in a “stopped” state. In a “stopped” state, QEMU is waiting for a connection from the GNU debugger. We’ll start the debugger as a subprocess of Emacs, the “IDE” we’ll be using to probe the running instance of the kernel.

So far, so good. Now we need to create a symbol file from the kernel we compiled. The following command line will do that for us:

objcopy --only-keep-debug ./visopsys ./visopsys.syms

It is easy to just copy the syms file to the kernel source directory, and start Emacs in that directory. It saves setting up search paths for the source, and GDB will automatically look there for the symbol file. As a regular user, start Emacs. In the application window, from the “tools” menu, select “GDB debugger.” A GDB frame will appear in Emacs, with the following line at the bottom of it. Note that the debugger found our visopsys.syms file. Pressing the enter key will load it. If GDB doesn’t find the syms file, then enter it at the end of the line. Check that the response in the window acknowledges that the symbols were loaded.

Run gdb (like this) gdb -i=mi visopsys.syms
or
Run gdb (like this): gdb --annotate=3 visopsys.syms

If for some reason the symbol file did not load, you can tell gdb to load it with:

add-symbol-file visopsys.syms

Now, we’ll connect to the debugging server in the running QEMU session. In the upper part of the GDB frame in Emacs, type the following to connect:

target remote localhost:1234

Set a breakpoint, such as:

b driverDetect

Now is when the magic starts. Type “c” into the GDB frame to make the instance of the OS in QEMU continue. It will start, and then shortly hit a breakpoint. A source window frame will appear, with the breakpoint marked on the left of the code. It’s off to the races! You can launch individual frames for things like “stack” or “local” displays from the “gud-frames” menu item in Emacs.

If you do not want to install Emacs, you can use vim or you can use the built-in “ide” of GDB, by executing gdb with the “-tui” option:

visop5-s

As you can see, Emacs is MUCH prettier.


Turning on Networking (work in progress) – use at your own risk!

The next launch of the OS on QEMU will be done in a way that allows networking.

qemu-system-i386 -hda ./vistest.img -cdrom ./visopsys-2014-12-13.iso -net nic,model=lance macaddr=52:54:00:52:05:00 -net tap,script=no,downscript=no,ifname=tap0 -boot d -s -serial stdio -S

By the way – there may be security issues involved with the use of this network setup and the tap interface! It is recommended to run this setup on an off-line computer! The “user mode networking” (-net user) is less complicated than the “-net tap” option, but alas is not available on all platforms I’ve used IIRC. Recent reading tells me that it might be the way to go if it’s available. I haven’t used it, but in any case – it’s “caveat emptor” when it comes to network setup, regardless of how accomplished!

Note: Visopsys is a project of, and owned by J. Andrew McLaughlin, at http://www.visopsys.org. These pages are not affiliated with that author or website. His project has a GPL2 license.

Go to section 3 of this article/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.