title debug ring0 code with bochs/gdb user omin0us ip 24.119.76.153 vol 1 lock ******** This small tutorial will describe the process for debugging kernel code using bochs x86 emulator. if you have any questions contact me at anthony dot lineberry at gmail dot com Requirments: As far as i know, in my example, you must be booting with GRUB using an elf kernel. gdb must obviously be installed as well. first download the bochs source code from http://sourceforge.net/project/showfiles.php?group_id=12580 (current release at the time of writing this is 2.3, but I will be using 2.2.6 in this example. unpack the sourcecode and configure it then compile and install like this: / /tt ./configure --enable-gdb-stub / /tt make / /tt make install At this point you will have bochs installed. Now you will need to edit your / bochsrc and add this line: / /tt gdbstub: enabled=1, port=1234, text_base=0, data_base=0, bss_base=0 at this point when you start up bochs, you will see this: / /tt Waiting for gdb connection on localhost:1234 open another terminal and open your kernel with gdb. My kernel's filename is kernel32 in this example. /tt anthony@rupert:~/code/os/ominos$ gdb -q kernel32 / /tt Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1". / /tt (gdb) at the gdb prompt enter the command: target remote localhost:1234 /tt (gdb) target remote localhost:1234 / /tt Remote debugging using localhost:1234 / /tt 0x0000fff0 in ?? () / /tt warning: shared library handler failed to enable breakpoint / /tt Reply contains invalid hex digit 78 / /tt (gdb) I always get that warning. but it has never affected anything. at this point you can set break points. I will set a breakpoint on vfsMount. /tt (gdb) break vfsMount / /tt Breakpoint 1 at 0xc0208078: file vfs.c, line 324. / /tt (gdb) Then run the continue command at the gdb prompt to start bochs. /tt (gdb) continue / /tt Continuing. / /tt Breakpoint 1, vfsMount (device=0x0, mountpoint=0x2c9c0 "�a", fstype=0x0) / /tt at vfs.c:324 / /tt 324 { / /tt (gdb) I will run through a few lines of code printing variables and whatnot to show that you can debug your kernel this way /tt (gdb) continue / /tt Continuing. /tt Breakpoint 1, vfsMount (device=0x0, mountpoint=0x2c9c0 "�a", fstype=0x0) / /tt at vfs.c:324 / /tt 324 { / /tt (gdb) next / /tt 330 kprintf("%s: mounting '%s' on '%s', fstype='%s'\n", __FUNCTION__, mountpoint, device?device:"NULL", fstype); / /tt (gdb) next / /tt 331 mount = kmalloc(sizeof(mount_t)); / /tt (gdb) print fstype / /tt $1 = 0xc020c434 "rootfs" / /tt (gdb) next / /tt 332 if(mount == NULL) / /tt (gdb) print/x mount / /tt $2 = 0xd00020fa / /tt (gdb) next / /tt 335 memset(mount, 0, sizeof(mount_t)); / /tt (gdb) next / /tt 337 mount->fs = getFileSystem(fstype); / /tt (gdb) next / /tt 338 if(mount->fs == NULL) / /tt (gdb) print *mount->fs / /tt $3 = {name = 0xd00020ac "rootfs", blocksize = 0, blockCount = 0, / /tt fscalls = 0xc0209a14, fsInfo = 0x0, next = 0xd00020c2} / /tt (gdb) next / /tt 345 if(device != NULL) / /tt (gdb) next / /tt 354 mount->mountpoint = kstrdup(mountpoint); / /tt (gdb) step / /tt kstrdup (s=0xc020c43b "/") at kstrdup.c:6 / /tt 6 char *ret = (char *)kmalloc(strlen(s)+1); / /tt (gdb) next / /tt 7 if(ret == NULL) / /tt (gdb) next / /tt 9 memset(ret, 0, strlen(s)+1); / /tt (gdb) next / /tt 10 strncpy(ret, s, strlen(s)); / /tt (gdb) next / /tt 11 return ret; / /tt (gdb) next / /tt 12 } / /tt (gdb) next / /tt vfsMount (device=0x0, mountpoint=0xc020c43b "/", fstype=0xc020c434 "rootfs") / /tt at vfs.c:355 / /tt 355 if(mount->mountpoint == NULL) / /tt (gdb) print mount->mountpoint / /tt $4 = 0xd0002122 "/" / /tt (gdb) print *mount / /tt $5 = {device = 0, fs = 0xd0002088, mountpoint = 0xd0002122 "/", / /tt rootVnode = 0x0, mountCover = 0x0, vTable = 0x0, next = 0x0} / /tt (gdb) There you go. That is all there is to debugging with gdb and bochs. Look on the internet for more indepth tutorials on actually using gdb and all its available co