使用bochs调试简单的Image
使用bochs模拟器来调试
Deepin15.4.1 安装 bochs-2.6.9 跳坑记录
总体是参照这个教程来的:
http://www.jianshu.com/p/db3a62564b47
但是这个教程是ubuntu的,对deepin来说还是有些坑需要跳,这里记录一下:
第一次make出现问题:
x.cc:42:35: fatal error: X11/extensions/Xrandr.h: No such file or directory
#include <X11/extensions/Xrandr.h>
^
compilation terminated.
Makefile:114: recipe for target 'x.o' failed
make[1]: *** [x.o] Error 1
make[1]: Leaving directory '/home/vccxx1337/Documents/os/bochs-2.6.9/gui'
Makefile:360: recipe for target 'gui/libgui.a' failed
make: *** [gui/libgui.a] Error 2
这个虽然解决方法是:
sudo apt install xorg-dev
之后再
sudo make
就可以继续按照教程走了。
拓展:
在教程开始时有提及:Bochs 需要在 X11 环境下运行,因此你的 Linux 系统必须已经安装了X Window 系统才能使用Bochs
但是教程中没有提及安装x11环境的命令,但是这个x11是啥。。。于是google了一下;
The X Window System (X11, or shortened to simply X) is a windowing system for bitmap displays, common on UNIX-like computer operating systems.
X provides the basic framework for a GUI environment: drawing and moving windows on the display device and interacting with a mouse and keyboard. X does not mandate the user interface – this is handled by individual programs. As such, the visual styling of X-based environments varies greatly; different programs may present radically different interfaces.
X is an architecture-independent system for remote graphical user interfaces and input device capabilities. Each person using a networked terminal has the ability to interact with the display with any type of user input device.
看的不是很懂,应该是类unix操作系统用来实现图形化用户接口的东西,于是只需要安装xorg这个x11驱动即可。
运行时报错:
在
sudo make install
之后,运行bochs报错:
bochs
========================================================================
Bochs x86 Emulator 2.6.9
Built from SVN snapshot on April 9, 2017
Compiled on Oct 25 2017 at 23:09:25
========================================================================
00000000000i[ ] BXSHARE not set. using compile time default '/usr/local/share/bochs'
00000000000i[ ] reading configuration from .bochsrc
00000000000e[ ] .bochsrc:187: wrong value for parameter 'model'
00000000000p[ ] >>PANIC<< .bochsrc:187: cpu directive malformed.
00000000000e[SIM ] notify called, but no bxevent_callback function is registered
00000000000e[SIM ] notify called, but no bxevent_callback function is registered
========================================================================
Bochs is exiting with the following message:
[ ] .bochsrc:187: cpu directive malformed.
========================================================================
00000000000i[CPU0 ] CPU is in real mode (active)
00000000000i[CPU0 ] CS.mode = 16 bit
00000000000i[CPU0 ] SS.mode = 16 bit
00000000000i[CPU0 ] EFER = 0x00000000
00000000000i[CPU0 ] | EAX=00000000 EBX=00000000 ECX=00000000 EDX=00000000
00000000000i[CPU0 ] | ESP=00000000 EBP=00000000 ESI=00000000 EDI=00000000
00000000000i[CPU0 ] | IOPL=0 id vip vif ac vm rf nt of df if tf sf ZF af PF cf
00000000000i[CPU0 ] | SEG sltr(index|ti|rpl) base limit G D
00000000000i[CPU0 ] | CS:0000( 0000| 0| 0) 00000000 00000000 0 0
00000000000i[CPU0 ] | DS:0000( 0000| 0| 0) 00000000 00000000 0 0
00000000000i[CPU0 ] | SS:0000( 0000| 0| 0) 00000000 00000000 0 0
00000000000i[CPU0 ] | ES:0000( 0000| 0| 0) 00000000 00000000 0 0
00000000000i[CPU0 ] | FS:0000( 0000| 0| 0) 00000000 00000000 0 0
00000000000i[CPU0 ] | GS:0000( 0000| 0| 0) 00000000 00000000 0 0
00000000000i[CPU0 ] | EIP=00000000 (00000000)
00000000000i[CPU0 ] | CR0=0x00000000 CR2=0x00000000
00000000000i[CPU0 ] | CR3=0x00000000 CR4=0x00000000
bx_dbg_read_linear: physical memory read error (phy=0x000000000000, lin=0x00000000)
00000000000i[SIM ] quit_sim called with exit code 1
在sourceforge上找到了同病相怜的外国兄弟:
https://sourceforge.net/p/bochs/discussion/39592/thread/bdfba64c/
这个兄弟解决了这个问题,原因处在bochs的配置文件上:
You didn't compile it properly. Penryn is x86-64 capable core and you didn't enable x86-64 support during configure - so this model is unavailable for you.
并且给出两种处理方法:
-
You could run Bochs with '-help cpu' and see which cpu models available in your build.
-
You also recommended to use standard configure scripts which we use for Bochs release. They are included in the source packages. .conf.linux for example.
于是我进到解压后的bochs文件夹下,执行命令:
sudo ./configure
之后再在命令行中执行bochs命令即可。
这里注意到教程中运行过cnfigure这个文件,但是是带参数的;
sudo ./configure --enable-debugger --enable-disasm
但是看这个参数似乎没什么影响,最后猜想可能是在按教程执行configure时,还没有安装x11环境,于是在配置时没有检测到x86的支持(也就是那个外国兄弟提到的:
enryn is x86-64 capable core and you didn't enable x86-64 support during configure
于是在安装了这个x11环境之后再次进行配置就可以正常配置运行了。
编译不同版本的bochs
之后的使用中发现,可以使用不同的参数编译bochs的源码,比如上面的
sudo ./configure --enable-debugger --enable-disasm
之后再make;make install将会编译出允许调试和反汇编的bochs;
如果使用其他参数
sudo ./configure --enable-debugger --enable-disasm --enable-gdb-stub
可以编译出能够等待gdb连接的bochs
具体需要什么参数可以通过google按需获取。
使用bochs调试过程
bochs每次运行需要用-f参数读取配置文件,主要配置程序位数读取的Image名以及一些调试参数,具体我也没研究过,这是这次使用的用的配置文件:
#=======================================================================
# ROMIMAGE:
# You now need to load a ROM BIOS into F0000-FFFFF. I've wiped
# out most of the BIOS hooks, and replace them with real BIOS
# support. Normally, you can use a precompiled BIOS in the bios/
# directory, named BIOS-bochs-yymmdd. Use the latest one in there.
#=======================================================================
romimage: file=$BXSHARE/BIOS-bochs-latest
#, address=0xf0000
#romimage: file=$BXSHARE/BIOS-bochs-2-processors, address=0xf0000
#=======================================================================
# MEGS
# set this to the default number of Megabytes of memory you want
# to emulate. You may also pass the '-megs xyz' option to bochs
# The default is 32MB, most OS's won't need more than that.
#=======================================================================
#megs: 64
megs: 16
#gdbstub: enabled=1, port=1234
#=======================================================================
# VGAROMIMAGE
# You now need to load a VGA ROM BIOS into C0000.
#=======================================================================
vgaromimage: file=$BXSHARE/VGABIOS-lgpl-latest
#vgaromimage: $BXSHARE\VGABIOS-elpin-2.40
#=======================================================================
# FLOPPYA:
# Point this to pathname of floppy image file or device
# This should be of a bootable floppy(image/device) if you're
# booting from 'a'.
#
# You can set the initial status of the media to 'ejected' or 'inserted'.
# floppya: 2_88=path, status=ejected (2.88M 3.5" floppy)
# floppya: 1_44=path, status=inserted (1.44M 3.5" floppy)
# floppya: 1_2=path, status=ejected (1.2M 5.25" floppy)
# floppya: 720k=path, status=inserted (720K 3.5" floppy)
#
# The path should be the name of a disk image file. On unix, you can use
# a raw device name such as /dev/fd0 on Linux. On WinNT and Win2k, use
# drive letters such as a: or b: as the path. Raw floppy access is not
# supported on Windows 95 and 98.
#=======================================================================
#floppya: 1_44=/dev/fd0, status=inserted
#floppya: 1_44=a:, status=inserted # for win32
floppya: 1_44="Image", status=inserted
#=======================================================================
# BOOT:
# This defines your boot drive.
# You can either boot from 'a', 'c' or 'cdrom'
# Examples:
# boot: c
# boot: a
# boot: cdrom
#=======================================================================
boot: a
#boot: c
#=======================================================================
# LOG:
# Give the path of the log file you'd like Bochs debug and misc. verbage
# to be written to. If you really don't want it, make it /dev/null. :^(
#
# Examples:
# log: ./bochs.out
# log: /dev/tty
#=======================================================================
#log: /dev/null
log: bochsout.txt
#=======================================================================
# VGA_UPDATE_INTERVAL:
# Video memory is scanned for updates and screen updated every so many
# virtual seconds. The default is 300000, about 3Hz. This is generally
# plenty. Keep in mind that you must tweak the 'ips:' directive
# to be as close to the number of emulated instructions-per-second
# your workstation can do, for this to be accurate.
#
# Examples:
# vga_update_interval: 250000
#=======================================================================
#vga_update_interval: 300000
#=======================================================================
# KEYBOARD_SERIAL_DELAY:
# Approximate time in microseconds that it takes one character to
# be transfered from the keyboard to controller over the serial path.
# Examples:
# keyboard_serial_delay: 200
#=======================================================================
#keyboard_serial_delay: 200
#=======================================================================
# KEYBOARD_PASTE_DELAY:
# Approximate time in microseconds between attempts to paste
# characters to the keyboard controller. This leaves time for the
# guest os to deal with the flow of characters. The ideal setting
# depends on how your operating system processes characters. The
# default of 100000 usec (.1 seconds) was chosen because it works
# consistently in Windows.
#=======================================================================
#keyboard_paste_delay: 100000
#=======================================================================
# FLOPPY_COMMAND_DELAY:
# Time in microseconds to wait before completing some floppy commands
# such as read/write/seek/etc, which normally have a delay associated.
# I had this hardwired to 50,000 before.
#=======================================================================
#floppy_command_delay: 50000
#=======================================================================
# IPS:
# Emulated Instructions Per Second. This is the number of IPS that bochs
# is capable of running on your machine. Read the note in config.h
# on how to find this. Make sure to recompile after.
#
# IPS is used to calibrate many time-dependent events within the bochs
# simulation. For example, changing IPS affects the frequency of VGA
# updates, the duration of time before a key starts to autorepeat, and
# the measurement of BogoMips and other benchmarks.
#
# Examples:
# Machine Mips
# ________________________________________________________________
# 650Mhz Athlon K-7 with Linux 2.4.4/egcs-2.91.66 2 to 2.5 Mips
# 400Mhz Pentium II with Linux 2.0.36/egcs-1.0.3 1 to 1.8 Mips
# 166Mhz 64bit Sparc with Solaris 2.x approx 0.75 Mips
# 200Mhz Pentium with Linux 2.x approx 0.5 Mips
#
#=======================================================================
#ips: 4000000
进入之后可以google一些bochs的调试命令来调试程序,基本操作是在0x7c00处下个断点然后c执行到断点然后n/s开始单步调试。