跟我一起玩《linux内核设计的艺术》第1章(三)——在运行态下,用gdb调试汇编bootsect.s

坑一:学内核肯定少不了调试内核,如何搭建这么爽的调试环境?额……这个该怎么说呢?

先下载linux内核源码,我的网盘里有:

链接:https://pan.baidu.com/s/199YYzz3mTtSVZFzWee3hxw 
提取码:wqr5 

我的建议是:用虚拟机安装最新的ubuntu桌面版(目前我用的是ubuntu20.04)。最新的安装包去度娘那搜索下载,如果下载的是.iso,还要安装虚拟光盘才能加载。具体虚拟机怎么安装ubuntu不想多说了。具体可以参考:https://freesion.com/article/6543744373/

为了顺利运行,最好在ubuntu下检查以下插件是否安装到位(什么你竟然直接复制进去……这么懒)
sudo apt install vim

sudo apt install cscope

sudo apt install ctags

sudo apt install git

sudo apt install gcc-3.4

sudo apt install build-essential cmake python-dev python3-dev
sudo apt install bin86
sudo apt install libc6-dev-i386
sudo apt install libsm6:i386
sudo apt install libx11-6:i386
sudo apt install libxpm4:i386

sudo apt-get install libncurses5:i386
sudo apt-get install libexpat1-dev:i386

sudo apt-get install openbsd-inetd

sudo apt-get install telnetd

sudo apt-get install net-tools

好了,把linux 0.11内核弄进ubuntu某目录下,按照https://freesion.com/article/6543744373/ 的步骤启动成功linux 0.11到命令行状态。然后呢?当然是调试了。怎么调试?难道是用./rungdb?结果:

This GDB was configured as "i686-pc-linux-gnu"...
Breakpoint 1 at 0x66d1: file init/main.c, line 110.
/home/spomu/linux-0.11/oslab/gdb-cmd.txt:2: Error in sourced command file:
localhost:1234: Connection refused.

这时候呢,因为之前安装了telnet和netstat工具,确实可以检查下netstat -a |grep localhost:

tcp        0      0 localhost:ipp           0.0.0.0:*               LISTEN     
tcp        0      0 localhost:domain        0.0.0.0:*               LISTEN     
tcp        0      0 localhost:telnet        localhost:43728         TIME_WAIT  
tcp6       0      0 ip6-localhost:ipp       [::]:*                  LISTEN     
udp        0      0 localhost:domain        0.0.0.0:* 

我们发现,locahost状态正常,只是没有localhost:1234这个端口服务。当然,如果你够牛,可以vi rungdb里面到底需要哪些服务,反正我是没耐心了,一不小心新试了下./dbg -c,结果哈哈哈:

原来1234就是gdb调试时的服务端口,不过哈哈完才发现,什么都干不了。这时,只要另外开个终端,然后运行./rungdb,见证奇迹的时候:

原来dbg-c就是用来启动1234服务端口的,哇塞,直接停在了main函数,可以愉快的调试了o(* ̄︶ ̄*)o……等等,怎么就main函数了?臣妾才学到bootsect.s囧,怎么调试呢?别急,汇编调试只需要输入./dbg-asm这一个工具就可以:

坑二:哈哈,可以调试汇编了哦耶……我们怎么设置break断点呢?汇编不能像C那样用行号做断点也就罢了,竟然连go:这种标签也不能做断点,要不要这么欺负人……事实上,汇编只能用运行时的内存地址,也就是CS:IP的值来做断点。我们确实可以用b 0x7c00这个bootsect起始地址做断点,但怎么才能获得其他行汇编的CS:IP值呢?我怎么知道mov ds,ax这句的偏移刚好是3呢?那当然是,反~汇~编~bootsect了!我们在linux-0.11/boot中找到了bootsect.s和可执行程序bootsect。熟悉反汇编的同学迫不及待的输入:

spomu@ubuntu:~/linux-0.11/oslab/linux-0.11/boot$ objdump -D bootsect
objdump: bootsect: file format not recognized

纳尼???文件格式不能识别???没事我们换bootsect.o,结果一样……基础好的同学可能马上用hexdump尝试:

spomu@ubuntu:~/linux-0.11/oslab/linux-0.11/boot$ hexdump -C bootsect
000

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值