免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 10601 | 回复: 29

[FreeBSD] 如何调试FreeBSD内核和内核模块? [复制链接]

论坛徽章:
0
发表于 2006-06-19 12:47 |显示全部楼层
哪位大侠可以指点一二?

论坛徽章:
0
发表于 2006-06-19 12:49 |显示全部楼层

论坛徽章:
0
发表于 2006-06-19 16:49 |显示全部楼层
10.7 Debugging Loadable Modules Using GDB

When debugging a panic that occurred within a module, or using remote GDB against a machine that uses dynamic modules, you need to tell GDB how to obtain symbol information for those modules.

First, you need to build the module(s) with debugging information:

  1. # cd /sys/modules/linux
  2. # make clean; make COPTS=-g
复制代码

If you are using remote GDB, you can run kldstat on the target machine to find out where the module was loaded:

  1. # kldstat
  2. Id Refs Address    Size     Name
  3. 1    4 0xc0100000 1c1678   kernel
  4. 2    1 0xc0a9e000 6000     linprocfs.ko
  5. 3    1 0xc0ad7000 2000     warp_saver.ko
  6. 4    1 0xc0adc000 11000    linux.ko
复制代码

If you are debugging a crash dump, you will need to walk the linker_files list, starting at linker_files->tqh_first and following the link.tqe_next pointers until you find the entry with the filename you are looking for. The address member of that entry is the load address of the module.

Next, you need to find out the offset of the text section within the module:

  1. # objdump --section-headers /sys/modules/linux/linux.ko | grep text
  2.   3 .rel.text     000016e0  000038e0  000038e0  000038e0  2**2
  3. 10 .text         00007f34  000062d0  000062d0  000062d0  2**2
复制代码

The one you want is the .text section, section 10 in the above example. The fourth hexadecimal field (sixth field overall) is the offset of the text section within the file. Add this offset to the load address of the module to obtain the relocation address for the module's code. In our example, we get 0xc0adc000 + 0x62d0 = 0xc0ae22d0. Use the add-symbol-file command in GDB to tell the debugger about the module:

  1. (kgdb) add-symbol-file /sys/modules/linux/linux.ko 0xc0ae22d0
  2. add symbol table from file "/sys/modules/linux/linux.ko" at text_addr = 0xc0ae22d0?
  3. (y or n) y
  4. Reading symbols from /sys/modules/linux/linux.ko...done.
  5. (kgdb)
复制代码

You should now have access to all the symbols in the module.

论坛徽章:
0
发表于 2006-06-19 17:18 |显示全部楼层
出现了一个错误:
make COPTS=-g

[root@shyfzx ~/hook]# kldstat
Id Refs Address    Size     Name
1    4 0xc0400000 6aacb4   kernel
2    1 0xc0aab000 58570    acpi.ko
3    1 0xc6253000 2000     hook.ko

[root@shyfzx ~/hook]# objdump --section-headers ./hook.ko | grep text
  4 .text         000000ac  00000450  00000450  00000450  2**2

(kgdb) add-symbol-file ./hook.ko 0xc6253450
add symbol table from file "./hook.ko" at
        .text_addr = 0xc6253450
(y or n) y
Reading symbols from ./hook.ko...(no debugging symbols found)...done.
(kgdb)

论坛徽章:
0
发表于 2006-06-19 17:19 |显示全部楼层
是我makefile写的不对?
SRCS=hook.c
KMOD=hook

.include <bsd.kmod.mk>

论坛徽章:
0
发表于 2006-06-19 17:21 |显示全部楼层
原帖由 ktrudger 于 2006-6-19 17:18 发表
出现了一个错误:
make COPTS=-g

[root@shyfzx ~/hook]# kldstat
Id Refs Address    Size     Name
1    4 0xc0400000 6aacb4   kernel
2    1 0xc0aab000 58570    acpi.ko
3    1 0xc6253000 2000 ...



看看你编译内核模块的时候有没有这句话:
objcopy --strip-debug xxxx.ko

论坛徽章:
0
发表于 2006-06-19 17:25 |显示全部楼层
最后一句就是这个:
[root@shyfzx ~/hook]# make COPTS=-g
Warning: Object directory not changed from original /root/hook
@ -> /usr/src/sys
machine -> /usr/src/sys/i386/include
cc -O2 -fno-strict-aliasing -pipe -Werror -D_KERNEL -DKLD_MODULE -nostdinc -I-   -I. -I@ -I@/contrib/altq -I@/../include -I/usr/include -finline-limit=8000 -fno-common  -mno-align-long-strings -mpreferred-stack-boundary=2  -mno-mmx -mno-3dnow -mno-sse -mno-sse2 -ffreestanding -Wall -Wredundant-decls -Wnested-externs -Wstrict-prototypes  -Wmissing-prototypes -Wpointer-arith -Winline -Wcast-qual  -fformat-extensions -std=c99 -c hook.c
ld  -d -warn-common -r -d -o hook.kld hook.o
touch export_syms
awk -f /sys/conf/kmod_syms.awk hook.kld  export_syms | xargs -J% objcopy % hook.kld
ld -Bshareable  -d -warn-common -o hook.ko hook.kld
objcopy --strip-debug hook.ko

论坛徽章:
0
发表于 2006-06-19 17:32 |显示全部楼层
其实,最后还是printf最可靠,虚拟机有时侯有bug很郁闷.前几天用qemu跑pistachio的时候就直接死掉了(也许与我对qemu的修改有关),还是bochs可靠一点,可惜慢的让人郁闷.如果是跟踪执行流程的话,qemu+GDB确实方便,能源代码级的跟踪.

论坛徽章:
0
发表于 2006-06-19 17:35 |显示全部楼层
我下载了一个0.81的qemu源码,configure的时候会错。报告少SDL。我已经对这些复杂的依赖关系深恶痛绝,所以也就懒得理它了。
ports目录下好像没有qemu的,呵呵。
ERROR: QEMU requires SDL or Cocoa for graphical output
To build QEMU without graphical output configure with --disable-gfx-check
Note that this will disable all output from the virtual graphics card.
[root@shyfzx ~/soft/qemu-0.8.1]#


[ 本帖最后由 ktrudger 于 2006-6-19 17:40 编辑 ]

论坛徽章:
0
发表于 2006-06-19 17:40 |显示全部楼层
忘了加这个试试“--disable-gfx-check”
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

SACC2019中国系统架构师大会

【数字转型 架构演进】SACC2019中国系统架构师大会,8.5折限时优惠重磅来袭!
2019年10月31日~11月2日第11届中国系统架构师大会(SACC2019)将在北京隆重召开。四大主线并行的演讲模式,1个主会场、20个技术专场、超千人参与的会议规模,100+来自互联网、金融、制造业、电商等领域的嘉宾阵容,将为广大参会者提供一场最具价值的技术交流盛会。

限时8.5折扣期:2019年9月30日前


----------------------------------------

大会官网>>
  

北京盛拓优讯信息技术有限公司. 版权所有 16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122
中国互联网协会会员  联系我们:huangweiwei@it168.com
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP